1 package org.apache.turbine.util.parser; 2 3 18 19 import java.text.SimpleDateFormat ; 20 import java.util.Calendar ; 21 import java.util.Date ; 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 35 public class BaseValueParserTest extends ServletTestCase 36 { 37 Turbine turbine = null; 38 BaseValueParser theBaseValueParser = null; 39 SimpleDateFormat stf = null; 40 41 46 public BaseValueParserTest (String name) 47 { 48 super(name); 49 } 50 51 57 protected void setUp() 58 throws Exception 59 { 60 super.setUp(); 61 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 ("hh:mm:ss a"); 72 } 73 74 79 protected void tearDown() throws Exception 80 { 81 turbine.destroy(); 82 super.tearDown(); 83 } 84 85 90 public static Test suite() 91 { 92 return new TestSuite(BaseValueParserTest.class); 93 } 94 95 98 public void testCurrentTime() 99 { 100 Calendar 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 111 public void testMorning() 112 { 113 checkTime(10, 5, 30, Calendar.AM, "10:05:30 AM"); 114 } 115 116 119 public void testAfternoon() 120 { 121 checkTime(5, 32, 6, Calendar.PM, "05:32:06 PM"); 122 } 123 124 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 142 public void testMidnight() 143 { 144 checkTime(12, 0, 0, Calendar.AM, "12:00:00 AM"); 145 } 146 147 150 public void testNoon() 151 { 152 checkTime(12, 0, 0, Calendar.PM, "12:00:00 PM"); 153 } 154 155 164 private void checkTime(int hour, int min, int sec, int ampm, String 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 newDate = theBaseValueParser.getDate(""); 172 assertNotNull("Could not create date for "+results, newDate); 173 174 assertEquals(results, stf.format(newDate)); 175 } 176 } 177 | Popular Tags |