PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/apache-log4j-1.2.17/tests/src/java/org/apache/log4j/pattern/CachedDateFormatTest.java

#
Java | 393 lines | 232 code | 54 blank | 107 comment | 0 complexity | ddb5ced234fa89b3d6d4828a89b1f2ff MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.log4j.pattern;
  18. import junit.framework.Test;
  19. import junit.framework.TestCase;
  20. import junit.framework.TestSuite;
  21. import org.apache.log4j.pattern.CachedDateFormat;
  22. import java.text.DateFormat;
  23. import java.util.TimeZone;
  24. import java.util.Date;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Locale;
  27. import java.util.Calendar;
  28. /**
  29. Unit test {@link AbsoluteTimeDateFormat}.
  30. @author Curt Arnold
  31. */
  32. public final class CachedDateFormatTest
  33. extends TestCase {
  34. /**
  35. * Test constructor
  36. * @param name String test name
  37. */
  38. public CachedDateFormatTest(String name) {
  39. super(name);
  40. }
  41. private static DateFormat createAbsoluteTimeDateFormat(TimeZone timeZone) {
  42. DateFormat df = new SimpleDateFormat("HH:mm:ss,SSS");
  43. df.setTimeZone(timeZone);
  44. return df;
  45. }
  46. /**
  47. * Timezone representing GMT.
  48. */
  49. private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
  50. /**
  51. * Timezone for Chicago, Ill. USA.
  52. */
  53. private static final TimeZone CHICAGO = TimeZone.getTimeZone(
  54. "America/Chicago");
  55. /**
  56. * Test multiple calls in close intervals.
  57. */
  58. public void test1() {
  59. // subsequent calls within one minute
  60. // are optimized to reuse previous formatted value
  61. // make a couple of nearly spaced calls
  62. DateFormat gmtFormat = new CachedDateFormat(createAbsoluteTimeDateFormat(GMT), 1000);
  63. long ticks = 12601L * 86400000L;
  64. Date jul1 = new Date(ticks);
  65. assertEquals("00:00:00,000", gmtFormat.format(jul1));
  66. Date plus8ms = new Date(ticks + 8);
  67. assertEquals("00:00:00,008", gmtFormat.format(plus8ms));
  68. Date plus17ms = new Date(ticks + 17);
  69. assertEquals("00:00:00,017", gmtFormat.format(plus17ms));
  70. Date plus237ms = new Date(ticks + 237);
  71. assertEquals("00:00:00,237", gmtFormat.format(plus237ms));
  72. Date plus1415ms = new Date(ticks + 1415);
  73. assertEquals("00:00:01,415", gmtFormat.format(plus1415ms));
  74. }
  75. /**
  76. * Check for interaction between caches.
  77. */
  78. public void test2() {
  79. Date jul2 = new Date(12602L * 86400000L);
  80. DateFormat gmtFormat = new CachedDateFormat(createAbsoluteTimeDateFormat(GMT), 1000);
  81. DateFormat chicagoFormat = new CachedDateFormat(createAbsoluteTimeDateFormat(CHICAGO), 1000);
  82. assertEquals("00:00:00,000", gmtFormat.format(jul2));
  83. assertEquals("19:00:00,000", chicagoFormat.format(jul2));
  84. assertEquals("00:00:00,000", gmtFormat.format(jul2));
  85. }
  86. /**
  87. * Test multiple calls in close intervals prior to 1 Jan 1970.
  88. */
  89. public void test3() {
  90. // subsequent calls within one minute
  91. // are optimized to reuse previous formatted value
  92. // make a couple of nearly spaced calls
  93. DateFormat gmtFormat = new CachedDateFormat(
  94. createAbsoluteTimeDateFormat(GMT), 1000);
  95. //
  96. // if the first call was exactly on an integral
  97. // second, it would not test the round toward zero compensation
  98. long ticks = -7L * 86400000L;
  99. Date jul1 = new Date(ticks + 8);
  100. assertEquals("00:00:00,008", gmtFormat.format(jul1));
  101. Date plus8ms = new Date(ticks + 16);
  102. assertEquals("00:00:00,016", gmtFormat.format(plus8ms));
  103. Date plus17ms = new Date(ticks + 23);
  104. assertEquals("00:00:00,023", gmtFormat.format(plus17ms));
  105. Date plus237ms = new Date(ticks + 245);
  106. assertEquals("00:00:00,245", gmtFormat.format(plus237ms));
  107. Date plus1415ms = new Date(ticks + 1423);
  108. assertEquals("00:00:01,423", gmtFormat.format(plus1415ms));
  109. }
  110. public void test4() {
  111. // subsequent calls within one minute are optimized to reuse previous
  112. // formatted value. make a couple of nearly spaced calls
  113. // (Note: 'Z' is JDK 1.4, using 'z' instead.)
  114. SimpleDateFormat baseFormat =
  115. new SimpleDateFormat("EEE, MMM dd, HH:mm:ss.SSS z", Locale.ENGLISH);
  116. DateFormat cachedFormat = new CachedDateFormat(baseFormat, 1000);
  117. //
  118. // use a date in 2000 to attempt to confuse the millisecond locator
  119. long ticks = 11141L * 86400000L;
  120. Date jul1 = new Date(ticks);
  121. assertEquals(baseFormat.format(jul1), cachedFormat.format(jul1));
  122. Date plus8ms = new Date(ticks + 8);
  123. baseFormat.format(plus8ms);
  124. cachedFormat.format(plus8ms);
  125. assertEquals(baseFormat.format(plus8ms), cachedFormat.format(plus8ms));
  126. Date plus17ms = new Date(ticks + 17);
  127. assertEquals(baseFormat.format(plus17ms), cachedFormat.format(plus17ms));
  128. Date plus237ms = new Date(ticks + 237);
  129. assertEquals(baseFormat.format(plus237ms), cachedFormat.format(plus237ms));
  130. Date plus1415ms = new Date(ticks + 1415);
  131. assertEquals(baseFormat.format(plus1415ms), cachedFormat.format(plus1415ms));
  132. }
  133. public void test5() {
  134. // subsequent calls within one minute
  135. // are optimized to reuse previous formatted value
  136. // make a couple of nearly spaced calls
  137. // (Note: 'Z' is JDK 1.4, using 'z' instead.)
  138. Locale thai = new Locale("th", "TH");
  139. SimpleDateFormat baseFormat =
  140. new SimpleDateFormat("EEE, MMM dd, HH:mm:ss.SSS z", thai);
  141. DateFormat cachedFormat = new CachedDateFormat(baseFormat, 1000);
  142. //
  143. // use a date in the year 2000 CE to attempt to confuse the millisecond locator
  144. long ticks = 11141L * 86400000L;
  145. String sx;
  146. Date jul1 = new Date(ticks);
  147. sx = cachedFormat.format(jul1);
  148. System.out.println(baseFormat.format(jul1));
  149. System.out.println(sx);
  150. assertEquals(baseFormat.format(jul1), sx);
  151. sx = cachedFormat.format(jul1);
  152. System.out.println(baseFormat.format(jul1));
  153. System.out.println(sx);
  154. assertEquals(baseFormat.format(jul1), sx);
  155. Date plus8ms = new Date(ticks + 8);
  156. sx = cachedFormat.format(plus8ms);
  157. System.out.println(baseFormat.format(plus8ms));
  158. System.out.println(sx);
  159. assertEquals(baseFormat.format(plus8ms), sx);
  160. Date plus17ms = new Date(ticks + 17);
  161. assertEquals(baseFormat.format(plus17ms), cachedFormat.format(plus17ms));
  162. Date plus237ms = new Date(ticks + 237);
  163. assertEquals(baseFormat.format(plus237ms), cachedFormat.format(plus237ms));
  164. Date plus1415ms = new Date(ticks + 1415);
  165. assertEquals(baseFormat.format(plus1415ms), cachedFormat.format(plus1415ms));
  166. }
  167. /**
  168. * Checks that getNumberFormat does not return null.
  169. */
  170. public void test6() {
  171. assertNotNull(new CachedDateFormat(new SimpleDateFormat(), 1000).getNumberFormat());
  172. }
  173. /**
  174. * Set time zone on cached and check that it is effective.
  175. */
  176. public void test8() {
  177. DateFormat baseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
  178. baseFormat.setTimeZone(GMT);
  179. DateFormat cachedFormat = new CachedDateFormat(baseFormat, 1000);
  180. Date jul4 = new Date(12603L * 86400000L);
  181. assertEquals("2004-07-04 00:00:00,000", cachedFormat.format(jul4));
  182. cachedFormat.setTimeZone(TimeZone.getTimeZone("GMT-6"));
  183. assertEquals("2004-07-03 18:00:00,000", cachedFormat.format(jul4));
  184. }
  185. /**
  186. * Test of caching when less than three millisecond digits are specified.
  187. */
  188. public void test9() {
  189. // (Note: 'Z' is JDK 1.4, using 'z' instead.)
  190. DateFormat baseFormat = new SimpleDateFormat("yyyy-MMMM-dd HH:mm:ss,SS z", Locale.US);
  191. DateFormat cachedFormat = new CachedDateFormat(baseFormat, 1000);
  192. TimeZone cet = TimeZone.getTimeZone("GMT+1");
  193. cachedFormat.setTimeZone(cet);
  194. Calendar c = Calendar.getInstance();
  195. c.set(2004, Calendar.DECEMBER, 12, 20, 0);
  196. c.set(Calendar.SECOND, 37);
  197. c.set(Calendar.MILLISECOND, 23);
  198. c.setTimeZone(cet);
  199. String expected = baseFormat.format(c.getTime());
  200. String s = cachedFormat.format(c.getTime());
  201. assertEquals(expected, s);
  202. c.set(2005, Calendar.JANUARY, 1, 0, 0);
  203. c.set(Calendar.SECOND, 13);
  204. c.set(Calendar.MILLISECOND, 905);
  205. expected = baseFormat.format(c.getTime());
  206. s = cachedFormat.format(c.getTime());
  207. assertEquals(expected, s);
  208. }
  209. /**
  210. * Test when millisecond position moves but length remains constant.
  211. */
  212. public void test10() {
  213. DateFormat baseFormat = new SimpleDateFormat("MMMM SSS EEEEEE", Locale.US);
  214. DateFormat cachedFormat = new CachedDateFormat(baseFormat, 1000);
  215. TimeZone cet = TimeZone.getTimeZone("GMT+1");
  216. cachedFormat.setTimeZone(cet);
  217. Calendar c = Calendar.getInstance();
  218. c.set(2004, Calendar.OCTOBER, 5, 20, 0);
  219. c.set(Calendar.SECOND, 37);
  220. c.set(Calendar.MILLISECOND, 23);
  221. c.setTimeZone(cet);
  222. String expected = baseFormat.format(c.getTime());
  223. String s = cachedFormat.format(c.getTime());
  224. assertEquals(expected, s);
  225. c.set(2004, Calendar.NOVEMBER, 1, 0, 0);
  226. c.set(Calendar.MILLISECOND, 23);
  227. expected = baseFormat.format(c.getTime());
  228. s = cachedFormat.format(c.getTime());
  229. assertEquals(expected, s);
  230. c.set(Calendar.MILLISECOND, 984);
  231. expected = baseFormat.format(c.getTime());
  232. s = cachedFormat.format(c.getTime());
  233. assertEquals(expected, s);
  234. }
  235. /**
  236. * Test that tests if caching is skipped if only "SS"
  237. * is specified.
  238. */
  239. public void test11() {
  240. //
  241. // Earlier versions could be tricked by "SS0" patterns.
  242. //
  243. String badPattern = "ss,SS0";
  244. SimpleDateFormat simpleFormat = new SimpleDateFormat(badPattern);
  245. SimpleDateFormat baseFormat = new SimpleDateFormat(badPattern);
  246. DateFormat gmtFormat = new CachedDateFormat(simpleFormat, 1000);
  247. gmtFormat.setTimeZone(GMT);
  248. baseFormat.setTimeZone(GMT);
  249. //
  250. // The first request has to 100 ms after an ordinal second
  251. // to push the literal zero out of the pattern check
  252. long ticks = 11142L * 86400000L;
  253. Date jul2 = new Date(ticks + 120);
  254. String expected = baseFormat.format(jul2);
  255. assertEquals(expected, gmtFormat.format(jul2));
  256. jul2.setTime(ticks + 87);
  257. //
  258. // Cache gives 00,087
  259. expected = baseFormat.format(jul2);
  260. assertEquals(expected, gmtFormat.format(jul2));
  261. }
  262. /**
  263. * Check pattern location for ISO8601
  264. */
  265. public void test12() {
  266. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
  267. long ticks = 11142L * 86400000L;
  268. String formatted = df.format(new Date(ticks));
  269. int millisecondStart = CachedDateFormat.findMillisecondStart(ticks, formatted, df);
  270. assertEquals(20, millisecondStart);
  271. }
  272. /**
  273. * Check pattern location for DATE
  274. */
  275. public void test13() {
  276. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  277. long ticks = 11142L * 86400000L;
  278. String formatted = df.format(new Date(ticks));
  279. int millisecondStart = CachedDateFormat.findMillisecondStart(ticks, formatted, df);
  280. assertEquals(CachedDateFormat.NO_MILLISECONDS, millisecondStart);
  281. }
  282. /**
  283. * Check pattern location for ABSOLUTE
  284. */
  285. public void test14() {
  286. SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss,SSS");
  287. long ticks = 11142L * 86400000L;
  288. String formatted = df.format(new Date(ticks));
  289. int millisecondStart = CachedDateFormat.findMillisecondStart(ticks, formatted, df);
  290. assertEquals(9, millisecondStart);
  291. }
  292. /**
  293. * Check pattern location for single S
  294. */
  295. public void test15() {
  296. SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss,S");
  297. long ticks = 11142L * 86400000L;
  298. String formatted = df.format(new Date(ticks));
  299. int millisecondStart = CachedDateFormat.findMillisecondStart(ticks, formatted, df);
  300. assertEquals(CachedDateFormat.UNRECOGNIZED_MILLISECONDS, millisecondStart);
  301. }
  302. /**
  303. * Check pattern location for single SS
  304. */
  305. public void test16() {
  306. SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss,SS");
  307. long ticks = 11142L * 86400000L;
  308. String formatted = df.format(new Date(ticks));
  309. int millisecondStart = CachedDateFormat.findMillisecondStart(ticks, formatted, df);
  310. assertEquals(CachedDateFormat.UNRECOGNIZED_MILLISECONDS, millisecondStart);
  311. }
  312. /**
  313. * Check caching when multiple SSS appear in pattern
  314. */
  315. public void test17() {
  316. Date jul2 = new Date(12602L * 86400000L);
  317. String badPattern = "HH:mm:ss,SSS HH:mm:ss,SSS";
  318. SimpleDateFormat simpleFormat = new SimpleDateFormat(badPattern);
  319. simpleFormat.setTimeZone(GMT);
  320. DateFormat cachedFormat = new CachedDateFormat(simpleFormat, 1000);
  321. String s = cachedFormat.format(jul2);
  322. assertEquals("00:00:00,000 00:00:00,000", s);
  323. jul2.setTime(jul2.getTime() + 120);
  324. assertEquals("00:00:00,120 00:00:00,120", simpleFormat.format(jul2));
  325. s = cachedFormat.format(jul2);
  326. //
  327. // TODO: why is this returning ,120 ... , 120
  328. //
  329. //assertEquals("00:00:00,120 00:00:00,000", s) ;
  330. int maxValid = CachedDateFormat.getMaximumCacheValidity(badPattern);
  331. assertEquals(1, maxValid);
  332. }
  333. public static Test xsuite() {
  334. TestSuite suite = new TestSuite();
  335. suite.addTest(new CachedDateFormatTest("test5"));
  336. //suite.addTest(new CachedDateFormatTest("testS2"));
  337. return suite;
  338. }
  339. }