001: package org.apache.turbine.util.parser;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.text.SimpleDateFormat;
023: import java.util.Calendar;
024: import java.util.Date;
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027: import org.apache.cactus.ServletTestCase;
028: import org.apache.turbine.Turbine;
029: import org.apache.turbine.util.TimeSelector;
030:
031: /**
032: * Used to test how BaseValueParser works with TimeSelector fields.
033: *
034: * @author <a href="mailto:brekke@apache.org">Jeffrey D. Brekke</a>
035: * @version $Id: BaseValueParserCactusTest.java 534527 2007-05-02 16:10:59Z tv $
036: */
037: public class BaseValueParserCactusTest extends ServletTestCase {
038: Turbine turbine = null;
039: BaseValueParser theBaseValueParser = null;
040: SimpleDateFormat stf = null;
041:
042: /**
043: * Creates a new <code>BaseValueParserTest</code> instance.
044: *
045: * @param name a <code>String</code> value
046: */
047: public BaseValueParserCactusTest(String name) {
048: super (name);
049: }
050:
051: /**
052: * This setup will be running server side. We startup Turbine and
053: * get our test port from the properties. This gets run before
054: * each testXXX test.
055: * @exception Exception if an error occurs
056: */
057: protected void setUp() throws Exception {
058: super .setUp();
059: /* Note: we are using the properties file from the cache test
060: * since we don't really need any specific property at this
061: * time. Future tests may require a test case specific
062: * properties file to be used.:
063: */
064: config.setInitParameter("properties",
065: "/WEB-INF/conf/TurbineComplete.properties");
066: turbine = new Turbine();
067: turbine.init(config);
068: theBaseValueParser = new BaseValueParser();
069: stf = new SimpleDateFormat("hh:mm:ss a");
070: }
071:
072: /**
073: * Shut down our turbine servlet and let our parents clean up also.
074: *
075: * @exception Exception if an error occurs
076: */
077: protected void tearDown() throws Exception {
078: turbine.destroy();
079: super .tearDown();
080: }
081:
082: /**
083: * Return a test suite of all our tests.
084: *
085: * @return a <code>Test</code> value
086: */
087: public static Test suite() {
088: return new TestSuite(BaseValueParserTest.class);
089: }
090:
091: /**
092: * Test that a current time
093: */
094: public void testCurrentTime() {
095: Calendar now = Calendar.getInstance();
096: checkTime(now.get(Calendar.HOUR), now.get(Calendar.MINUTE), now
097: .get(Calendar.SECOND), now.get(Calendar.AM_PM), stf
098: .format(now.getTime()));
099: }
100:
101: /**
102: * Test a time in the morning.
103: */
104: public void testMorning() {
105: checkTime(10, 5, 30, Calendar.AM, "10:05:30 AM");
106: }
107:
108: /**
109: * Test a time in the afternoon.
110: */
111: public void testAfternoon() {
112: checkTime(5, 32, 6, Calendar.PM, "05:32:06 PM");
113: }
114:
115: /**
116: * Test that an invalid time returns null.
117: *
118: */
119: public void testInvalidTime() {
120: theBaseValueParser.add(TimeSelector.HOUR_SUFFIX, 1);
121: theBaseValueParser.add(TimeSelector.MINUTE_SUFFIX, 100);
122: theBaseValueParser.add(TimeSelector.SECOND_SUFFIX, 0);
123: theBaseValueParser.add(TimeSelector.AMPM_SUFFIX, Calendar.AM);
124:
125: assertNull("Should not have received a date object.",
126: theBaseValueParser.getDate(""));
127: }
128:
129: /**
130: * Test the midnight special case.
131: */
132: public void testMidnight() {
133: checkTime(12, 0, 0, Calendar.AM, "12:00:00 AM");
134: }
135:
136: /**
137: * Test the noon special case.
138: */
139: public void testNoon() {
140: checkTime(12, 0, 0, Calendar.PM, "12:00:00 PM");
141: }
142:
143: /**
144: * Helper method which sets up the parser and gets the date.
145: *
146: * @param hour an <code>int</code> value
147: * @param min an <code>int</code> value
148: * @param sec an <code>int</code> value
149: * @param ampm an <code>int</code> value
150: * @param results a <code>String</code> value
151: */
152: private void checkTime(int hour, int min, int sec, int ampm,
153: String results) {
154: theBaseValueParser.add(TimeSelector.HOUR_SUFFIX, hour);
155: theBaseValueParser.add(TimeSelector.MINUTE_SUFFIX, min);
156: theBaseValueParser.add(TimeSelector.SECOND_SUFFIX, sec);
157: theBaseValueParser.add(TimeSelector.AMPM_SUFFIX, ampm);
158:
159: Date newDate = theBaseValueParser.getDate("");
160: assertNotNull("Could not create date for " + results, newDate);
161:
162: assertEquals(results, stf.format(newDate));
163: }
164: }
|