carnold 2004/12/22 12:29:01
Modified: src/java/org/apache/log4j/helpers CachedDateFormat.java
tests/src/java/org/apache/log4j/helpers
CachedDateFormatTestCase.java
tests build.xml
Log:
BUG 32064: Improved CachedDateFormat
Revision Changes Path
1.2 +7 -22
logging-log4j/src/java/org/apache/log4j/helpers/CachedDateFormat.java
Index: CachedDateFormat.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/helpers/CachedDateFormat.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CachedDateFormat.java 21 Dec 2004 15:21:56 -0000 1.1
+++ CachedDateFormat.java 22 Dec 2004 20:29:01 -0000 1.2
@@ -135,6 +135,7 @@
cache.insert(millisecondStart, "0");
}
}
+ sbuf.append(cache);
} else {
previousTime = (now / 1000L) * 1000L;
//
@@ -143,25 +144,14 @@
if (now - previousTime < 0) {
previousTime -= 1000;
}
- int prevLength = cache.length();
cache.setLength(0);
- formatter.format(date, cache, fieldPosition);
+ formatter.format(new Date(previousTime), cache, fieldPosition);
+ millisecondStart = findMillisecondStart(previousTime,
cache.toString(), formatter);
//
- // if the length changed then
- // recalculate the millisecond position
- if (cache.length() != prevLength) {
- //
- // format the previous integral second
- StringBuffer tempBuffer = new StringBuffer(cache.length());
- formatter.format(new Date(previousTime), tempBuffer, fieldPosition);
- //
- // detect the start of the millisecond field
- millisecondStart = findMillisecondStart(previousTime,
- tempBuffer.toString(),
- formatter);
- }
+ // calling ourself should be safe and faster
+ // but why risk it
+ formatter.format(date, sbuf, fieldPosition);
}
- sbuf.append(cache);
return sbuf;
}
@@ -178,14 +168,9 @@
int prevLength = cache.length();
cache.setLength(0);
cache.append(formatter.format(new Date(previousTime)));
- //
- // if the length changed then
- // recalculate the millisecond position
- if (cache.length() != prevLength) {
- millisecondStart = findMillisecondStart(previousTime,
+ millisecondStart = findMillisecondStart(previousTime,
cache.toString(),
formatter);
- }
}
/**
1.2 +28 -0
logging-log4j/tests/src/java/org/apache/log4j/helpers/CachedDateFormatTestCase.java
Index: CachedDateFormatTestCase.java
===================================================================
RCS file:
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/helpers/CachedDateFormatTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CachedDateFormatTestCase.java 21 Dec 2004 15:21:56 -0000 1.1
+++ CachedDateFormatTestCase.java 22 Dec 2004 20:29:01 -0000 1.2
@@ -236,4 +236,32 @@
}
+ /**
+ * Test when millisecond position moves but length remains constant.
+ */
+ public void test10() {
+ DateFormat baseFormat = new SimpleDateFormat("MMMM SSS EEEEEE",
Locale.US);
+ DateFormat cachedFormat = new CachedDateFormat(baseFormat);
+ TimeZone cet = TimeZone.getTimeZone("GMT+1");
+ cachedFormat.setTimeZone(cet);
+
+ Calendar c = Calendar.getInstance();
+ c.set(2004, Calendar.OCTOBER, 5, 20, 0);
+ c.set(Calendar.SECOND, 37);
+ c.set(Calendar.MILLISECOND, 23);
+ c.setTimeZone(cet);
+
+ String s = cachedFormat.format(c.getTime());
+ assertEquals("October 023 Tuesday", s);
+
+ c.set(2004, Calendar.NOVEMBER, 1, 0, 0);
+ c.set(Calendar.MILLISECOND, 23);
+ s = cachedFormat.format(c.getTime());
+ assertEquals("November 023 Monday", s);
+
+
+ c.set(Calendar.MILLISECOND, 984);
+ s = cachedFormat.format(c.getTime());
+ assertEquals("November 984 Monday", s);
+ }
}
1.78 +10 -1 logging-log4j/tests/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/logging-log4j/tests/build.xml,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -r1.77 -r1.78
--- build.xml 21 Dec 2004 02:53:37 -0000 1.77
+++ build.xml 22 Dec 2004 20:29:01 -0000 1.78
@@ -192,7 +192,8 @@
ReaderWriterLock,
NDC,
Plugins,
-
MessageFormatter"/>
+
MessageFormatter,
+ DateFormat"/>
@@ -619,6 +620,14 @@
<classpath refid="tests.classpath"/>
<formatter type="plain" usefile="false"/>
<test name="org.apache.log4j.helpers.MessageFormatterTest" />
+ </junit>
+ </target>
+
+ <target name="DateFormat" depends="check, build, cleanOutputDir">
+ <junit printsummary="yes" fork="yes" haltonfailure="yes">
+ <classpath refid="tests.classpath"/>
+ <formatter type="plain" usefile="false"/>
+ <test name="org.apache.log4j.helpers.CachedDateFormatTestCase" />
</junit>
</target>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]