carnold     2004/12/24 01:08:51

  Modified:    src/java/org/apache/log4j/pattern CachedDateFormat.java
                        DatePatternConverter.java
               tests/src/java/org/apache/log4j/pattern
                        CachedDateFormatTest.java
  Log:
  Bug 32064: Added pattern check and incomplete test for multiple SSS patterns
  
  Revision  Changes    Path
  1.14      +18 -0     
logging-log4j/src/java/org/apache/log4j/pattern/CachedDateFormat.java
  
  Index: CachedDateFormat.java
  ===================================================================
  RCS file: 
/home/cvs/logging-log4j/src/java/org/apache/log4j/pattern/CachedDateFormat.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- CachedDateFormat.java     24 Dec 2004 08:28:28 -0000      1.13
  +++ CachedDateFormat.java     24 Dec 2004 09:08:51 -0000      1.14
  @@ -339,4 +339,22 @@
       return formatter.getNumberFormat();
     }
     
  +  /**
  +   * Gets maximum cache validity for the specified SimpleDateTime
  +   *    conversion pattern.
  +   *  @param pattern conversion pattern, may not be null.
  +   *  @returns Duration in milliseconds from an integral second
  +   *      that the cache will return consistent results.
  +   */
  +  public static int getMaximumCacheValidity(final String pattern) {
  +     //
  +     //   If there are more "S" in the pattern than just one "SSS"
  +     //      prevent caching anything more than duplicate requests
  +     //
  +     int firstS = pattern.indexOf('S');
  +     if (firstS >= 0 && firstS != pattern.lastIndexOf("SSS")) {
  +         return 1;
  +     }
  +     return 1000;
  +  }
   }
  
  
  
  1.16      +3 -1      
logging-log4j/src/java/org/apache/log4j/pattern/DatePatternConverter.java
  
  Index: DatePatternConverter.java
  ===================================================================
  RCS file: 
/home/cvs/logging-log4j/src/java/org/apache/log4j/pattern/DatePatternConverter.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- DatePatternConverter.java 24 Dec 2004 08:28:28 -0000      1.15
  +++ DatePatternConverter.java 24 Dec 2004 09:08:51 -0000      1.16
  @@ -82,9 +82,11 @@
         pattern = patternOption;
       }
   
  +    int maximumCacheValidity = 1000;
       SimpleDateFormat simpleFormat = null;
       try {
         simpleFormat = new SimpleDateFormat(pattern);
  +      maximumCacheValidity = 
CachedDateFormat.getMaximumCacheValidity(pattern);
       } catch (IllegalArgumentException e) {
         logger.warn(
           "Could not instantiate SimpleDateFormat with pattern " + 
patternOption, e);
  @@ -99,7 +101,7 @@
         simpleFormat.setTimeZone(tz);
       }
   
  -    df = new CachedDateFormat(simpleFormat, 1000);
  +    df = new CachedDateFormat(simpleFormat, maximumCacheValidity);
     }
     
     public StringBuffer convert(LoggingEvent event) {
  
  
  
  1.9       +25 -1     
logging-log4j/tests/src/java/org/apache/log4j/pattern/CachedDateFormatTest.java
  
  Index: CachedDateFormatTest.java
  ===================================================================
  RCS file: 
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/pattern/CachedDateFormatTest.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CachedDateFormatTest.java 24 Dec 2004 08:28:03 -0000      1.8
  +++ CachedDateFormatTest.java 24 Dec 2004 09:08:51 -0000      1.9
  @@ -102,7 +102,6 @@
     /**
      *  Check for interaction between caches.
      */
  -
     public void test2() {
         Date jul2 = new Date(12602L * 86400000L);
         DateFormat gmtFormat = new 
CachedDateFormat(createAbsoluteTimeDateFormat(GMT), 1000);
  @@ -360,6 +359,31 @@
        int millisecondStart = CachedDateFormat.findMillisecondStart(ticks, 
formatted, df);
        assertEquals(CachedDateFormat.UNRECOGNIZED_MILLISECONDS, 
millisecondStart);     
     }
  +
  +
  +  /**
  +   * Check caching when multiple SSS appear in pattern
  +   */
  +  public void test17() {
  +      Date jul2 = new Date(12602L * 86400000L);
  +      String badPattern = "HH:mm:ss,SSS HH:mm:ss,SSS";
  +      SimpleDateFormat simpleFormat = new SimpleDateFormat(badPattern);
  +      simpleFormat.setTimeZone(GMT);
  +      DateFormat cachedFormat = new CachedDateFormat(simpleFormat, 1000);
  +      String s = cachedFormat.format(jul2);
  +      assertEquals("00:00:00,000 00:00:00,000", s);
  +      jul2.setTime(jul2.getTime() + 120);
  +      assertEquals("00:00:00,120 00:00:00,120", simpleFormat.format(jul2));
  +      s = cachedFormat.format(jul2);
  +      //
  +      //  TODO: why is this returning ,120 ... , 120
  +      //
  +      //assertEquals("00:00:00,120 00:00:00,000", s) ;
  +      
  +      int maxValid = CachedDateFormat.getMaximumCacheValidity(badPattern);
  +      assertEquals(1, maxValid);
  +  }
  +
     
     public static Test xsuite() {
       TestSuite suite = new TestSuite();
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to