KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > parser > BaseValueParserTest


1 package org.apache.turbine.util.parser;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25 import org.apache.cactus.ServletTestCase;
26 import org.apache.turbine.Turbine;
27 import org.apache.turbine.util.TimeSelector;
28
29 /**
30  * Used to test how BaseValueParser works with TimeSelector fields.
31  *
32  * @author <a HREF="mailto:brekke@apache.org">Jeffrey D. Brekke</a>
33  * @version $Id: BaseValueParserTest.java,v 1.1.4.2 2004/05/20 03:30:07 seade Exp $
34  */

35 public class BaseValueParserTest extends ServletTestCase
36 {
37     Turbine turbine = null;
38     BaseValueParser theBaseValueParser = null;
39     SimpleDateFormat JavaDoc stf = null;
40
41     /**
42      * Creates a new <code>BaseValueParserTest</code> instance.
43      *
44      * @param name a <code>String</code> value
45      */

46     public BaseValueParserTest (String JavaDoc name)
47     {
48         super(name);
49     }
50                                              
51     /**
52      * This setup will be running server side. We startup Turbine and
53      * get our test port from the properties. This gets run before
54      * each testXXX test.
55      * @exception Exception if an error occurs
56      */

57     protected void setUp()
58         throws Exception JavaDoc
59     {
60         super.setUp();
61         /* Note: we are using the properties file from the cache test
62          * since we don't really need any specific property at this
63          * time. Future tests may require a test case specific
64          * properties file to be used.:
65          */

66         config.setInitParameter("properties",
67                 "/WEB-INF/conf/TurbineCacheTest.properties");
68         turbine = new Turbine();
69         turbine.init(config);
70         theBaseValueParser = new BaseValueParser();
71         stf = new SimpleDateFormat JavaDoc("hh:mm:ss a");
72     }
73                                              
74     /**
75      * Shut down our turbine servlet and let our parents clean up also.
76      *
77      * @exception Exception if an error occurs
78      */

79     protected void tearDown() throws Exception JavaDoc
80     {
81         turbine.destroy();
82         super.tearDown();
83     }
84                                              
85     /**
86      * Return a test suite of all our tests.
87      *
88      * @return a <code>Test</code> value
89      */

90     public static Test suite()
91     {
92         return new TestSuite(BaseValueParserTest.class);
93     }
94
95     /**
96      * Test that a current time
97      */

98     public void testCurrentTime()
99     {
100         Calendar JavaDoc now = Calendar.getInstance();
101         checkTime(now.get(Calendar.HOUR),
102                   now.get(Calendar.MINUTE),
103                   now.get(Calendar.SECOND),
104                   now.get(Calendar.AM_PM),
105                   stf.format(now.getTime()));
106     }
107     
108     /**
109      * Test a time in the morning.
110      */

111     public void testMorning()
112     {
113         checkTime(10, 5, 30, Calendar.AM, "10:05:30 AM");
114     }
115
116     /**
117      * Test a time in the afternoon.
118      */

119     public void testAfternoon()
120     {
121         checkTime(5, 32, 6, Calendar.PM, "05:32:06 PM");
122     }
123
124     /**
125      * Test that an invalid time returns null.
126      *
127      */

128     public void testInvalidTime()
129     {
130         theBaseValueParser.add(TimeSelector.HOUR_SUFFIX, 1);
131         theBaseValueParser.add(TimeSelector.MINUTE_SUFFIX, 100);
132         theBaseValueParser.add(TimeSelector.SECOND_SUFFIX, 0);
133         theBaseValueParser.add(TimeSelector.AMPM_SUFFIX, Calendar.AM);
134
135         assertNull("Should not have received a date object.",
136                    theBaseValueParser.getDate(""));
137     }
138
139     /**
140      * Test the midnight special case.
141      */

142     public void testMidnight()
143     {
144         checkTime(12, 0, 0, Calendar.AM, "12:00:00 AM");
145     }
146
147     /**
148      * Test the noon special case.
149      */

150     public void testNoon()
151     {
152         checkTime(12, 0, 0, Calendar.PM, "12:00:00 PM");
153     }
154
155     /**
156      * Helper method which sets up the parser and gets the date.
157      *
158      * @param hour an <code>int</code> value
159      * @param min an <code>int</code> value
160      * @param sec an <code>int</code> value
161      * @param ampm an <code>int</code> value
162      * @param results a <code>String</code> value
163      */

164     private void checkTime(int hour, int min, int sec, int ampm, String JavaDoc results)
165     {
166         theBaseValueParser.add(TimeSelector.HOUR_SUFFIX, hour);
167         theBaseValueParser.add(TimeSelector.MINUTE_SUFFIX, min);
168         theBaseValueParser.add(TimeSelector.SECOND_SUFFIX, sec);
169         theBaseValueParser.add(TimeSelector.AMPM_SUFFIX, ampm);
170
171         Date JavaDoc newDate = theBaseValueParser.getDate("");
172         assertNotNull("Could not create date for "+results, newDate);
173        
174         assertEquals(results, stf.format(newDate));
175     }
176 }
177
Popular Tags