[logback-dev] svn commit: r2407 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/rolling/helper test/java/ch/qos/logback/core/rolling/helper

noreply.ceki at qos.ch noreply.ceki at qos.ch
Mon Aug 3 22:18:56 CEST 2009


Author: ceki
Date: Mon Aug  3 22:18:56 2009
New Revision: 2407

Added:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/DatePatternToRegexTest.java
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DatePatternToRegex.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SDFToken.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/PackageTest.java

Log:
initial tests for DatePatternToRegex

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DatePatternToRegex.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DatePatternToRegex.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DatePatternToRegex.java	Mon Aug  3 22:18:56 2009
@@ -1,3 +1,13 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2009, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
 package ch.qos.logback.core.rolling.helper;
 
 import java.util.ArrayList;
@@ -13,19 +23,16 @@
     length = datePattern.length();
   }
 
-  public String toRegex(String datePattern) {
+  String toRegex() {
     List<SDFToken> tokenList = tokenize();
+    StringBuilder sb = new StringBuilder();
     for(SDFToken token: tokenList) {
-      
-    }
-    char c = 0;
-    for (int i = 0; i < length; i++) {
-      char t = 1;
+      sb.append(token.toRegex());
     }
-    return null;
+    return sb.toString();
   }
 
-  List<SDFToken> tokenize() {
+  private List<SDFToken> tokenize() {
     List<SDFToken> tokenList = new ArrayList<SDFToken>();
     SDFToken token = null;
     for (int i = 0; i < length; i++) {

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java	Mon Aug  3 22:18:56 2009
@@ -1,7 +1,7 @@
 /**
- * LOGBack: the reliable, fast and flexible logging library for Java.
+ * Logback: the generic, reliable, fast and flexible logging framework.
  * 
- * Copyright (C) 1999-2006, QOS.ch
+ * Copyright (C) 2000-2009, QOS.ch
  * 
  * This library is free software, you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
@@ -16,6 +16,7 @@
 import ch.qos.logback.core.pattern.DynamicConverter;
 
 /**
+ * Returns a date formatted by SimpleDateFormatter.
  * 
  * @author Ceki G&uuml;c&uuml;
  */
@@ -26,8 +27,8 @@
    */
   public final static String CONVERTER_KEY = "d";
 
-  String datePattern;
-  SimpleDateFormat sdf;
+  private String datePattern;
+  private SimpleDateFormat sdf;
 
   public DateTokenConverter() {
   }
@@ -66,9 +67,11 @@
     return (o instanceof Date);
   }
 
-  String toRegex() {
-    return null;
+  public String toRegex() {
+    DatePatternToRegex toRegex = new DatePatternToRegex(datePattern);
+    return toRegex.toRegex();
   }
+  
   /**
    * Set the date pattern.
    */

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SDFToken.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SDFToken.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SDFToken.java	Mon Aug  3 22:18:56 2009
@@ -21,11 +21,57 @@
   void inc() {
     occurrences++;
   }
-  
+
   String toRegex() {
-    switch(c) {
+    switch (c) {
     case 'G':
-      return null;
+      return ".*";
+    case 'M':
+      if (occurrences >= 3) {
+        return ".*";
+      } else {
+        return number(occurrences);
+      }
+    case 'y':
+    case 'w':
+    case 'W':
+    case 'D':
+    case 'd':
+    case 'F':
+    case 'H':
+    case 'k':
+    case 'K':
+    case 'h':
+    case 'm':
+    case 's':
+    case 'S':
+      return number(occurrences);
+    case 'E':
+      if (occurrences >= 4) {
+        return ".{3,10}";
+      } else {
+        return number(occurrences);
+      }
+    case 'a':
+      return ".{2}";
+    case 'z':
+    case 'Z':
+      return ".*";
+    default:
+      if (occurrences == 1) {
+        return "" + c;
+      } else {
+        return c + "{" + occurrences + "}";
+      }
     }
   }
+
+  @Override
+  public String toString() {
+    return c+"("+occurrences+")";
+  }
+  
+  private String number(int occurences) {
+    return "\\d{" + occurrences + "}";
+  }
 }
\ No newline at end of file

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/DatePatternToRegexTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/DatePatternToRegexTest.java	Mon Aug  3 22:18:56 2009
@@ -0,0 +1,60 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2009, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package ch.qos.logback.core.rolling.helper;
+
+import static org.junit.Assert.assertTrue;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import ch.qos.logback.core.CoreConstants;
+
+public class DatePatternToRegexTest {
+
+  @Before
+  public void setUp() throws Exception {
+    // set(int year, int month, int date, int hourOfDay, int minute, int
+    // second);
+    // calendar.set(2009, 8, 3, 21, 57, 16);
+  }
+
+  @Test
+  public void ISO8601() {
+    SimpleDateFormat sdf = new SimpleDateFormat(CoreConstants.ISO8601_PATTERN);
+    Calendar calendar = Calendar.getInstance();
+    calendar.set(2009, 8, 3, 21, 57, 16);
+    calendar.set(Calendar.MILLISECOND, 333);
+    // 2009-09-03 21:57:16,333
+    DateTokenConverter dtc = makeDTC(CoreConstants.ISO8601_PATTERN);
+    verify(sdf, calendar, dtc);
+  }
+
+  void verify(SimpleDateFormat sdf, Calendar calendar, DateTokenConverter dtc) {
+    String expected = sdf.format(calendar.getTime());
+    String regex = dtc.toRegex();
+    assertTrue("[" + expected + "] does not match regex [" + regex + "]",
+        expected.matches(regex));
+  }
+
+  private DateTokenConverter makeDTC(String datePattern) {
+    DateTokenConverter dtc = new DateTokenConverter();
+    List<String> optionList = new ArrayList<String>();
+    optionList.add(datePattern);
+    dtc.setOptionList(optionList);
+    dtc.start();
+    return dtc;
+  }
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/PackageTest.java	Mon Aug  3 22:18:56 2009
@@ -17,7 +17,7 @@
 
 @RunWith(Suite.class)
 @Suite.SuiteClasses( { CompressTest.class, FileNamePatternTest.class,
-    RollingCalendarTest.class })
+    RollingCalendarTest.class, DatePatternToRegexTest.class })
 public class PackageTest extends TestCase {
 
 }


More information about the logback-dev mailing list