// Note that there is a similar test: // org.apache.catalina.valves.TestAccessLogValve.testBug54044()
@Test publicvoid testBug54044() throws Exception {
final String timeFormat = "dd-MMM-yyyy HH:mm:ss"; finalint cacheSize = 10;
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat, Locale.US);
sdf.setTimeZone(TimeZone.getDefault());
DateFormatCache dfc = new DateFormatCache(cacheSize, timeFormat, null);
// Get dfc.cache.cache field
Object dfcCache;
Field dfcCacheArray;
Field dfcCacheField = dfc.getClass().getDeclaredField("cache");
dfcCacheField.setAccessible(true);
dfcCache = dfcCacheField.get(dfc);
dfcCacheArray = dfcCache.getClass().getDeclaredField("cache");
dfcCacheArray.setAccessible(true);
// Create an array to hold the expected values
String[] expected = new String[cacheSize];
// Fill the cache & populate the expected values for (int secs = 0; secs < (cacheSize); secs++) {
dfc.getFormat(secs * 1000);
expected[secs] = generateExpected(sdf, secs);
} Assert.assertArrayEquals(expected,
(String[]) dfcCacheArray.get(dfcCache));
// Cause the cache to roll-around by one and then confirm
dfc.getFormat(cacheSize * 1000);
expected[0] = generateExpected(sdf, cacheSize); Assert.assertArrayEquals(expected,
(String[]) dfcCacheArray.get(dfcCache));
// Jump 2 ahead and then confirm (skipped value should be null)
dfc.getFormat((cacheSize + 2) * 1000);
expected[1] = null;
expected[2] = generateExpected(sdf, cacheSize + 2); Assert.assertArrayEquals(expected,
(String[]) dfcCacheArray.get(dfcCache));
// Back 1 to fill in the gap
dfc.getFormat((cacheSize + 1) * 1000);
expected[1] = generateExpected(sdf, cacheSize + 1); Assert.assertArrayEquals(expected,
(String[]) dfcCacheArray.get(dfcCache));
// Return to 1 and confirm skipped value is null
dfc.getFormat(1 * 1000);
expected[1] = generateExpected(sdf, 1);
expected[2] = null; Assert.assertArrayEquals(expected,
(String[]) dfcCacheArray.get(dfcCache));
// Go back one further
dfc.getFormat(0);
expected[0] = generateExpected(sdf, 0); Assert.assertArrayEquals(expected,
(String[]) dfcCacheArray.get(dfcCache));
// Jump ahead far enough that the entire cache will need to be cleared
dfc.getFormat(42 * 1000); for (int i = 0; i < cacheSize; i++) {
expected[i] = null;
}
expected[0] = generateExpected(sdf, 42); Assert.assertArrayEquals(expected,
(String[]) dfcCacheArray.get(dfcCache));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.