KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > tests > DateTimeValueURIHandlerTest


1 /**
2  * $Id: DateTimeValueURIHandlerTest.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.valueuri.tests;
30
31 import java.text.Format JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import junit.framework.TestSuite;
37
38 import com.idaremedia.antx.AntXFixture;
39 import com.idaremedia.antx.ValueURIHandler;
40 import com.idaremedia.antx.valueuri.datetime.DateTimeValueURIHandler;
41
42 /**
43  * Testsuite for {@linkplain DateTimeValueURIHandler}.
44  *
45  * @since JWare/AntX 0.5
46  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
47  * @version 0.5
48  * @.safety single
49  * @.group impl,test
50  **/

51
52 public final class DateTimeValueURIHandlerTest extends ValueURIHandlerTestSkeleton
53 {
54     private static final Map JavaDoc MAPPINGS = DateTimeValueURIHandler.copyOfMappings();
55  
56     /* 'Sun Oct 01 12:35:30 EDT 2000' = 970418130646L */
57     private static final String JavaDoc OCT012000 = "970418130646";
58     private static final Date JavaDoc OCT012000DATE = new Date JavaDoc(970418130646L);
59     private static final Map JavaDoc OCT012000STRINGS = AntXFixture.newMap();
60     static {
61         Map JavaDoc m = OCT012000STRINGS;
62         m.put("$gmtdate:","01-Oct-2000 GMT");
63         m.put("$longtime:","12:35:30 PM");
64         m.put("$cvsdate:","01 Oct 2000");
65         m.put("$date:","01-Oct-2000");
66         m.put("$shortdate:","01-Oct");
67         m.put("$svndate:","01 Oct 2000");
68         m.put("$datetime:","12:35:30 PM 01-Oct-2000");
69         m.put("$longdate:","01-Oct-2000");
70         m.put("$time:","12:35:30 PM");
71         m.put("$longdatetime:","12:35:30 PM 01-Oct-2000");
72         m.put("$shorttime:","12:35 PM");
73         m.put("$shortdatetime:","01-Oct 12:35 PM");
74         m.put("$gmtdatetime:","16:35:30 01-Oct-2000 GMT");
75         m.put("$gmttime:","16:35:30 GMT");
76         m.put("$changelogdate:","01 Oct 2000");
77         m.put("$duration:","309902hr.12min.10sec.902ms");
78         m.put("$query1:","001001123530-0400");
79         m.put("$query2:","02000.October.01 AD 12:35 PM");
80     }
81
82
83
84     /**
85      * Initializes a new test case for named method.
86      * @param methodname test case method's name (non-null)
87      **/

88     public DateTimeValueURIHandlerTest(String JavaDoc methodname)
89     {
90         super("DateTimeValueURIHandler::",methodname);
91     }
92
93
94     /**
95      * Returns full test suite for DateTimeValueURIHandler.
96      **/

97     public static TestSuite suite()
98     {
99         return new TestSuite(DateTimeValueURIHandlerTest.class);
100     }
101
102 // ---------------------------------------------------------------------------------------------------------
103
// --------------------------------------- [ Misc Factory Methods ] ----------------------------------------
104
// ---------------------------------------------------------------------------------------------------------
105

106     protected ValueURIHandler newOUT()
107     {
108         return new DateTimeValueURIHandler();
109     }
110     
111
112     private long defineNow(ValueURIHandler out, long nowTS)
113     {
114         if (nowTS<0L) {
115             nowTS = System.currentTimeMillis();
116         }
117         ((DateTimeValueURIHandler)out).setNow(nowTS);
118         getProject().setProperty("now",String.valueOf(nowTS));
119         return nowTS;
120
121     }
122
123 // ---------------------------------------------------------------------------------------------------------
124
// ------------------------------------------- [ The Test Cases ] ------------------------------------------
125
// ---------------------------------------------------------------------------------------------------------
126

127     /**
128      * Verify there is at least one predefined datetime uri scheme.
129      * @since JWare/AntX 0.5
130      **/

131     public void testBaseline_AntX05()
132     {
133         checkBaseline();
134
135         assertNotNil(MAPPINGS,"Default Mappings");
136         assertFalse(MAPPINGS.isEmpty(),"Default Mappings IsEmpty");
137     }
138
139
140     /**
141      * Verifies that we can pass the symbolic timestamp string "now"
142      * to all formatters and now have anything blow up.
143      * @since JWare/AntX 0.5
144      **/

145     public final void testAllPredefinedFormatsWithNowTimestamp_AntX05()
146     {
147         ValueURIHandler out = newOUT();
148         println(" NOW = "+defineNow(out,-1L));
149
150         String JavaDoc result0,result1;
151         Iterator JavaDoc itr = MAPPINGS.entrySet().iterator();
152         
153         while (itr.hasNext()) {
154             Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
155             String JavaDoc scheme = e.getKey().toString();
156             result0 = out.valueFrom("now",scheme+"now",m_rqlink);
157             println(scheme+"now"+" = "+result0);
158             assertNotNil(result0,scheme+"now");
159             result1 = out.valueFrom("",scheme,m_rqlink);
160             assertEqual(result1,result0,scheme);
161             result1 = out.valueFrom("@(now)?",scheme+"@(now)?",m_rqlink);
162             assertEqual(result1,result0,scheme+"@(now)?");
163         }
164     }
165
166
167
168     /**
169      * Verifies that we can pass a timestamp string to all formatters
170      * and not have anything blow up (duration). All formatters
171      * should return expected strings.
172      * @since JWare/AntX 0.5
173      **/

174     public final void testTimestampFormatsWithFixedTimestamp_AntX05()
175     {
176         ValueURIHandler out = newOUT();
177         Iterator JavaDoc itr = MAPPINGS.entrySet().iterator();
178         String JavaDoc result;
179         
180         while (itr.hasNext()) {
181             Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
182             String JavaDoc scheme = e.getKey().toString();
183             Format JavaDoc dfmt = (Format JavaDoc)e.getValue();
184             Boolean JavaDoc known = DateTimeValueURIHandler.isTimestampScheme(scheme);
185             assertNotNil(known,"category of default scheme");
186             if (known==Boolean.TRUE) {
187                 result = out.valueFrom(OCT012000,scheme+OCT012000,m_rqlink);
188                 assertEqual(result,OCT012000STRINGS.get(scheme),scheme+OCT012000+"(expected)");
189                 assertEqual(result,dfmt.format(OCT012000DATE),scheme+OCT012000);
190             }
191         }
192     }
193
194
195
196     /**
197      * Verifies that we can pass a timestamp of zero and one to all
198      * duration formatters without barfage.
199      * @since JWare/AntX 0.5
200      **/

201     public final void testDurationFormatsHandleOneAndZero_AntX05()
202     {
203         ValueURIHandler out = newOUT();
204         Iterator JavaDoc itr = MAPPINGS.entrySet().iterator();
205         String JavaDoc result;
206         
207         while (itr.hasNext()) {
208             Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
209             String JavaDoc scheme = e.getKey().toString();
210             if (DateTimeValueURIHandler.isTimestampScheme(scheme)==Boolean.FALSE) {
211                 result = out.valueFrom("0",scheme+"0",m_rqlink);
212                 assertNil(result,scheme+"0");
213                 Format JavaDoc dfmt = (Format JavaDoc)e.getValue();
214                 result = out.valueFrom("1",scheme+"1",m_rqlink);
215                 assertEqual(result,dfmt.format(new Date JavaDoc(1)),scheme+"1");
216             }
217         }
218     }
219
220
221
222     /**
223      * Verifies that for known schemes, the output's format is fixed
224      * regardless of what the query string is set to.
225      * @since JWare/AntX 0.5
226      **/

227     public void testPredefinedNameForcesFixedFormat_AntX05()
228     {
229         ValueURIHandler out = newOUT();
230         defineNow(out,-1L);
231
232         final String JavaDoc QUERY0 = "now?hh 'o''clock' a, zzzz";
233         final String JavaDoc QUERY1 = "@(now)?yyMMddHHmmssZ";
234         String JavaDoc customresult = out.valueFrom(QUERY1,"$blah:"+QUERY1,m_rqlink);
235         println(" CUSTOM RESULT: "+customresult);
236
237         String JavaDoc result0,result1,result2;
238         Iterator JavaDoc itr = MAPPINGS.entrySet().iterator();
239         while (itr.hasNext()) {
240             Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
241             String JavaDoc scheme = e.getKey().toString();
242             result0 = out.valueFrom("",scheme,m_rqlink);
243             assertNotNil(result0,scheme);
244             result1 = out.valueFrom(QUERY0,scheme+QUERY0,m_rqlink);
245             assertEqual(result1,result0,scheme+QUERY0);
246             result2 = out.valueFrom(QUERY1,scheme+QUERY1,m_rqlink);
247             assertEqual(result2,result1,scheme+QUERY1);
248             assertNotEqual(result1,customresult,scheme+QUERY1);
249         }
250     }
251     
252     
253     /**
254      * Verifies that for unknown schemes, a custom format string is
255      * necessary.
256      * @since JWare/AntX 0.5
257      **/

258     public void testCustomSchemeNeedsCustomFormat_AntX05()
259     {
260         ValueURIHandler out = newOUT();
261         defineNow(out,OCT012000DATE.getTime());
262         
263         String JavaDoc result = out.valueFrom("now","$query1:now",m_rqlink);
264         assertNil(result,"$query1:now");
265         result = out.valueFrom("","$query2:",m_rqlink);
266         assertNil(result,"$query2:");
267
268         final String JavaDoc QUERY1 = "@(now)?yyMMddHHmmssZ";
269         final String JavaDoc QUERY2 = "?yyyyy.MMMMM.dd GGG hh:mm aaa";
270         
271         result = out.valueFrom(QUERY1,"$query1:"+QUERY1,m_rqlink);
272         assertEqual(result,OCT012000STRINGS.get("$query1:"),"$query1:"+QUERY1);
273         result = out.valueFrom(QUERY2,"$query2:"+QUERY2,m_rqlink);
274         assertEqual(result,OCT012000STRINGS.get("$query2:"),"$query2:"+QUERY2);
275     }
276
277
278
279     /**
280      * Verifies that bogus inputs to the handler are managed
281      * gracefully. Always returns <i>null</i>.
282      * @since JWare/AntX 0.5
283      **/

284     public void testInvalidTimestampsHandledGracefully_AntX05()
285     {
286         ValueURIHandler out = newOUT();
287         String JavaDoc result;
288         
289         Iterator JavaDoc itr = MAPPINGS.entrySet().iterator();
290         while (itr.hasNext()) {
291             Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
292             String JavaDoc scheme = e.getKey().toString();
293             result = out.valueFrom("-111",scheme+"-111",m_rqlink);
294             assertNil(result,scheme+"-111");
295             result = out.valueFrom("NaN",scheme+"NaN",m_rqlink);
296             assertNil(result,scheme+"NaN");
297         }
298
299         result = out.valueFrom("-111","$query1:-111?yyyyMMddd",m_rqlink);
300         assertNil(result,"$query1:-111?yyyyMMddd");
301         result = out.valueFrom("-111","$query1:NaN?yyyyMMddd",m_rqlink);
302         assertNil(result,"$query1:NaN?yyyyMMddd");
303     }
304
305
306
307     /**
308      * Verifies that our default fallback API only groks the "now"
309      * timestamp.
310      * @since JWare/AntX 0.5
311      **/

312     public void testOnlyNowHandledByDefaultFallback_AntX05()
313     {
314         ValueURIHandler out = newOUT();
315         defineNow(out,OCT012000DATE.getTime());
316         String JavaDoc result;
317
318         result = out.getDefaultValue("$datetime:now",m_rqlink);
319         assertEqual(result,out.valueFrom("","$datetime:",m_rqlink),"default $datetime:");
320         result = out.getDefaultValue("$tstamp:now?yyyyMMddd",m_rqlink);
321         assertEqual(result,out.valueFrom("now?yyyyMMddd","$tstamp:now?yyyyMMddd",m_rqlink),
322                     "default $tstamp:now?yyyyMMddd");
323
324         assertNil(out.getDefaultValue("$datetime:"+OCT012000,m_rqlink),
325                   "default $datetime:"+OCT012000);
326         assertNil(out.getDefaultValue("$tstamp:"+OCT012000+"?yyyyMMddd",m_rqlink),
327                   "default $tstamp:"+OCT012000+"?yyyyMMddd");
328     }
329 }
330
331 /* end-of-DateTimeValueURIHandlerTest.java */
Popular Tags