001    /*
002    Copyright (c) 1996-2012, Damon Hart-Davis
003    All rights reserved.
004    
005    Redistribution and use in source and binary forms, with or without
006    modification, are permitted provided that the following conditions are
007    met:
008    
009      * Redistributions of source code must retain the above copyright
010        notice, this list of conditions and the following disclaimer.
011    
012      * Redistributions in binary form must reproduce the above copyright
013        notice, this list of conditions and the following disclaimer in the
014        documentation and/or other materials provided with the
015        distribution.
016    
017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
018    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
019    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
020    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
021    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028    */
029    
030    package org.hd.d.pg2k.test.dev;
031    
032    import java.util.Iterator;
033    
034    import junit.framework.TestCase;
035    
036    import org.hd.d.pg2k.svrCore.Rnd;
037    import org.hd.d.pg2k.svrCore.uploader.ExhibitHandlerBeanBase;
038    import org.hd.d.pg2k.webSvr.catalogue.SearchPageJavaBean;
039    import org.hd.d.pg2k.webSvr.util.LocaleBean;
040    
041    /**
042     * Created by IntelliJ IDEA.
043     * User: Damon Hart-Davis
044     * Date: 03-Jun-2003
045     * Time: 17:55:04
046     */
047    
048    /**Tests ExhibitHandlerBeanBase and SearchPageJavaBean.
049     * This bean can be used stand-alone (outside a JSP page) at least in part,
050     * so we can test the core algorithms.
051     */
052    public final class SearchPageJavaBeanTest extends TestCase
053        {
054        /**Tests getters and setters of SearchPageJavaBeanTest bean.
055         *
056         */
057        public static void testSPJBBasics()
058            {
059            final SearchPageJavaBean spjb = new SearchPageJavaBean();
060    
061            // Tests some initial state.
062            assertTrue("query should initially be empty and non-null", "".equals(spjb.getQ()));
063            assertTrue("recent-days value should default to zero", spjb.getRecentDaysFilter() == 0);
064            }
065    
066        /**Test recentDaysFilter.
067         * We had particular problems with this on the Web server,
068         * (2003/08/10, though it worked fine in development)
069         * so we give it a special grilling here.
070         */
071        public static void testRecentDaysFilter()
072            {
073            final SearchPageJavaBean spjb = new SearchPageJavaBean();
074    
075            // Tests initial state.
076            assertTrue("recent-days value should default to zero", spjb.getRecentDaysFilter() == 0);
077    
078            // Setting to any half-reasonable non-negative integer value should work.
079            // We test with some random values and some fixed values.
080            final int fixedValues[] = { 0, 1, 3, 6,7,8, 27,28,29,30,31,32, 364,365,366,367 };
081            for(int i = 99; --i >= 0; )
082                {
083                final int d1 = Rnd.fastRnd.nextInt(99999);
084                spjb.setRecentDaysFilter(String.valueOf(d1));
085                assertTrue("Should be able to set any non-negative recent-days value [1]: " + d1, d1 == spjb.getRecentDaysFilter());
086                final int d2 = fixedValues[i % fixedValues.length];
087                spjb.setRecentDaysFilter(String.valueOf(d2));
088                assertTrue("Should be able to set any non-negative recent-days value [2]: " + d2, d2 == spjb.getRecentDaysFilter());
089                }
090    
091            // Ensure that setting to any synonym for 'none'
092            // ("0" or "-" or null or "")
093            // resets to zero.
094            final String noneSyn[] = { "0", ExhibitHandlerBeanBase.SETTER_ALL, "", null };
095            for(int i = noneSyn.length; --i >= 0; )
096                {
097                spjb.setRecentDaysFilter("1");
098                assertTrue("setting to non-zero value did not work", 0 != spjb.getRecentDaysFilter());
099                spjb.setRecentDaysFilter(noneSyn[i]);
100                assertTrue("setting with " + noneSyn[i] + " should reset to zero", 0 == spjb.getRecentDaysFilter());
101                }
102    
103            // Check that setting to one of the symbolic values gives
104            // the right number of days,
105            // and that an appropriate HTML item is marked "selected".
106            for(final Iterator it = SearchPageJavaBean.symFilterDays.keySet().iterator(); it.hasNext(); )
107                {
108                final String symbolicName = (String) it.next();
109                final int d = (SearchPageJavaBean.symFilterDays.get(symbolicName)).intValue();
110                spjb.setRecentDaysFilter(symbolicName);
111                assertTrue("setting with " + symbolicName + " should set to " + d, d == spjb.getRecentDaysFilter());
112    
113                // We don't use locale in first instance, so we use the symbolic names.
114                final String selectBodySimple = spjb.makeRecentDaysFilterBody(null);
115                final String selectedOptionSimple = "<option selected>" + symbolicName + "</option>";
116    //System.out.println("RDF simple: " + symbolicName + " in " + selectBody);
117                assertTrue("appropriate option should be selected: " + selectedOptionSimple + " FROM " + selectBodySimple,
118                           selectBodySimple.indexOf(selectedOptionSimple) >= 0);
119    
120                // Now we use the default locale,
121                // for which some basic non-symbolic text must be present,
122                // else we won't get the form of option tag that we expect.
123                final String selectBody = spjb.makeRecentDaysFilterBody(new LocaleBean());
124                final String selectedOption = "<option value="+symbolicName+" selected>";
125    //System.out.println("RDF: " + symbolicName + " in " + selectBody);
126                assertTrue("appropriate option should be selected: " + selectedOptionSimple + " FROM " + selectBody,
127                           selectBody.indexOf(selectedOption) >= 0);
128                }
129            }
130        }