01: // ========================================================================
02: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
03: // ------------------------------------------------------------------------
04: // Licensed under the Apache License, Version 2.0 (the "License");
05: // you may not use this file except in compliance with the License.
06: // You may obtain a copy of the License at
07: // http://www.apache.org/licenses/LICENSE-2.0
08: // Unless required by applicable law or agreed to in writing, software
09: // distributed under the License is distributed on an "AS IS" BASIS,
10: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11: // See the License for the specific language governing permissions and
12: // limitations under the License.
13: // ========================================================================
14:
15: package org.mortbay.util;
16:
17: import java.util.Locale;
18: import java.util.TimeZone;
19:
20: import junit.framework.TestSuite;
21:
22: /* ------------------------------------------------------------ */
23: /** Util meta Tests.
24: * @author Greg Wilkins (gregw)
25: */
26: public class DateCacheTest extends junit.framework.TestCase {
27: public DateCacheTest(String name) {
28: super (name);
29: }
30:
31: public static junit.framework.Test suite() {
32: TestSuite suite = new TestSuite(DateCacheTest.class);
33: return suite;
34: }
35:
36: /* ------------------------------------------------------------ */
37: /** main.
38: */
39: public static void main(String[] args) {
40: junit.textui.TestRunner.run(suite());
41: }
42:
43: /* ------------------------------------------------------------ */
44: public void testDateCache() throws Exception {
45: //@WAS: Test t = new Test("org.mortbay.util.DateCache");
46: // 012345678901234567890123456789
47: DateCache dc = new DateCache(
48: "EEE, dd MMM yyyy HH:mm:ss zzz ZZZ", Locale.US);
49: dc.setTimeZone(TimeZone.getTimeZone("GMT"));
50: String last = dc.format(System.currentTimeMillis());
51: boolean change = false;
52: for (int i = 0; i < 15; i++) {
53: Thread.sleep(100);
54: String date = dc.format(System.currentTimeMillis());
55:
56: assertEquals("Same Date", last.substring(0, 17), date
57: .substring(0, 17));
58:
59: if (last.substring(17).equals(date.substring(17)))
60: change = true;
61: else {
62: int lh = Integer.parseInt(last.substring(17, 19));
63: int dh = Integer.parseInt(date.substring(17, 19));
64: int lm = Integer.parseInt(last.substring(20, 22));
65: int dm = Integer.parseInt(date.substring(20, 22));
66: int ls = Integer.parseInt(last.substring(23, 25));
67: int ds = Integer.parseInt(date.substring(23, 25));
68:
69: // This won't work at midnight!
70: assertTrue("Time changed", ds == ls + 1 || ds == 0
71: && dm == lm + 1 || ds == 0 && dm == 0
72: && dh == lh + 1);
73: }
74: last = date;
75: }
76: assertTrue("time changed", change);
77:
78: // Test string is cached
79: dc = new DateCache();
80: String s1 = dc.format(System.currentTimeMillis());
81: dc.format(1);
82: String s2 = dc.format(System.currentTimeMillis());
83: dc.format(System.currentTimeMillis() + 10 * 60 * 60);
84: String s3 = dc.format(System.currentTimeMillis());
85: assertTrue(s1 == s2 || s2 == s3);
86: }
87:
88: }
|