[slf4j-dev] svn commit: r971 - in slf4j/trunk/slf4j-converter/src: main/java/org/slf4j/converter main/java/org/slf4j/converter/helper main/java/org/slf4j/converter/internal test/java/org/slf4j/converter test/java/org/slf4j/converter/helper test/java/org/slf4j/converter/internal

ceki at slf4j.org ceki at slf4j.org
Wed Feb 20 18:58:40 CET 2008


Author: ceki
Date: Wed Feb 20 18:58:39 2008
New Revision: 971

Added:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/helper/Abbreviator.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/internal/
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/internal/ProgressLinstenerImpl.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/internal/ProgressListener.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/AbbreviatorTest.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/PackageTest.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/RandomHelper.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/internal/
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/internal/NopProgressListener.java
Modified:
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConverterFrame.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/FileSelector.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ProjectConverter.java
   slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/XSelector.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AllTest.java
   slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ProjectConverterTest.java

Log:
- Improving the visual aspects of the convertor
- The Abbreviator class has been tested according to the approach described
  by Alberto Savoia in "Beatiful Tests" in the book "Beatiful Code". 

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConverterFrame.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConverterFrame.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ConverterFrame.java	Wed Feb 20 18:58:39 2008
@@ -101,7 +101,7 @@
     if (res == JFileChooser.APPROVE_OPTION) {
       File folder = selector.getSelectedFile();
       ProjectConverter converter = new ProjectConverter(combo
-          .getSelectedIndex());
+          .getSelectedIndex(), null);
       showConfirmDialog(converter, folder);
     }
   }

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/FileSelector.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/FileSelector.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/FileSelector.java	Wed Feb 20 18:58:39 2008
@@ -4,10 +4,18 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.slf4j.converter.internal.ProgressListener;
+
 public class FileSelector {
 
   private List<File> javaFileList = new ArrayList<File>();
 
+  ProgressListener pl;
+  
+  FileSelector(ProgressListener pl) {
+    this.pl = pl;
+  }
+  
   public List<File> selectJavaFilesInFolder(File folder) {
     if(folder.isDirectory()) {
       selectFiles(folder);

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ProjectConverter.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ProjectConverter.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/ProjectConverter.java	Wed Feb 20 18:58:39 2008
@@ -30,6 +30,7 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.slf4j.converter.internal.ProgressListener;
 import org.slf4j.converter.line.RuleSet;
 
 public class ProjectConverter {
@@ -37,6 +38,8 @@
   private RuleSet ruleSet;
   private List<ConversionException> exception;
 
+  ProgressListener progressListener;
+  
   public static void main(String[] args) throws IOException {
 
     ConverterFrame frame = new ConverterFrame();
@@ -51,7 +54,8 @@
    * @return true if init operation complete
    * @throws IOException
    */
-  public ProjectConverter(int conversionType) {
+  public ProjectConverter(int conversionType, ProgressListener progressListener) {
+    this.progressListener = progressListener;
     ruleSet = RuleSetFactory.getMatcherImpl(conversionType);
     if (ruleSet == null) {
       addException(new ConversionException(ConversionException.NOT_IMPLEMENTED));
@@ -60,7 +64,7 @@
 
 
   public void convertProject(File folder) {
-    FileSelector fs = new FileSelector();
+    FileSelector fs = new FileSelector(progressListener);
     List<File> fileList = fs.selectJavaFilesInFolder(folder);
     convertFileList(fileList);
   }

Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/XSelector.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/XSelector.java	(original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/XSelector.java	Wed Feb 20 18:58:39 2008
@@ -29,17 +29,19 @@
   }
   
   private void selectFiles(File file) {
-    System.out.println(file);
+    //System.out.println(file);
     if (file.isDirectory()) {
+      jlabel.setText(file.getAbsolutePath());
       File[] files = file.listFiles();
       if (files != null) {
         for (int i = 0; i < files.length; i++) {
+        
           selectFiles(files[i]);
         }
       }
     } else {
       if (file.getName().endsWith(".java")) {
-        jlabel.setText(file.getAbsolutePath());
+        //jlabel.setText(file.getAbsolutePath());
         javaFileList.add(file);
       }
 

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/helper/Abbreviator.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/helper/Abbreviator.java	Wed Feb 20 18:58:39 2008
@@ -0,0 +1,66 @@
+package org.slf4j.converter.helper;
+
+public class Abbreviator {
+  static final String FILLER = "...";
+
+  final char folderSeparator;
+  final int invariantPrefixLength;
+  final int desiredLength;
+
+  public Abbreviator(int invariantPrefixLength, int desiredLength,
+      char folderSeparator) {
+    this.invariantPrefixLength = invariantPrefixLength;
+    this.desiredLength = desiredLength;
+    this.folderSeparator = folderSeparator;
+  }
+
+  public String abbreviate(String filename) {
+    if (filename.length() <= desiredLength) {
+      return filename;
+    } else {
+
+      int firstIndex = filename.indexOf(folderSeparator, invariantPrefixLength);
+      if (firstIndex == -1) {
+        // we cant't process this string
+        return filename;
+      }
+      StringBuffer buf = new StringBuffer(desiredLength);
+      buf.append(filename.substring(0, firstIndex + 1));
+      buf.append(FILLER);
+      int nextIndex = computeNextIndex(filename, firstIndex);
+      if (nextIndex != -1) {
+        buf.append(filename.substring(nextIndex));
+      } else {
+        // better long than wrong
+        return filename;
+      }
+
+      if (buf.length() < filename.length()) {
+        return buf.toString();
+      } else {
+        // we tried our best but we are still could not shorten the input
+        return filename;
+      }
+    }
+  }
+
+  int computeNextIndex(String filename, int firstIndex) {
+    int nextIndex = firstIndex + 1;
+    int hitCount = 0;
+    int minToRemove = filename.length() - desiredLength + FILLER.length();
+    while (nextIndex < firstIndex + minToRemove) {
+      int tmpIndex = filename.indexOf(folderSeparator, nextIndex + 1);
+      if (tmpIndex == -1) {
+        if (hitCount == 0) {
+          return -1;
+        } else {
+          return nextIndex;
+        }
+      } else {
+        hitCount++;
+        nextIndex = tmpIndex;
+      }
+    }
+    return nextIndex;
+  }
+}

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/internal/ProgressLinstenerImpl.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/internal/ProgressLinstenerImpl.java	Wed Feb 20 18:58:39 2008
@@ -0,0 +1,46 @@
+package org.slf4j.converter.internal;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.swing.JLabel;
+
+import org.slf4j.converter.helper.Abbreviator;
+
+public class ProgressLinstenerImpl implements ProgressListener {
+
+  int addFileCount = 0;
+  int conversionCount = 0;
+  final JLabel jLabel;
+  
+  Abbreviator abbr;
+  
+  public ProgressLinstenerImpl(File projectFolder, JLabel jLabel) {
+    this.jLabel = jLabel;
+    this.abbr = new Abbreviator((int) projectFolder.length(), 50, File.separatorChar);
+  }
+  
+  
+  public void onDirectory(File file) {
+    String abbreviatedName;
+    try {
+      abbreviatedName = abbr.abbreviate(file.getCanonicalPath());
+    } catch (IOException e) {
+      abbreviatedName = file.toString();
+    }
+    jLabel.setText("Searching folder ["+abbreviatedName+"]");
+  }
+
+  public void onDone() {
+  }
+
+  public void onFileAddition(File file) {
+    addFileCount++;
+  }
+
+  public void onFileConversion(File file) {
+    // TODO Auto-generated method stub
+
+  }
+
+}

Added: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/internal/ProgressListener.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/internal/ProgressListener.java	Wed Feb 20 18:58:39 2008
@@ -0,0 +1,13 @@
+package org.slf4j.converter.internal;
+
+import java.io.File;
+
+public interface ProgressListener {
+
+  public void onDirectory(File file);
+  public void onFileAddition(File file);
+  public void onFileConversion(File file);
+  public void onDone();
+
+}
+  

Modified: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AllTest.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AllTest.java	(original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/AllTest.java	Wed Feb 20 18:58:39 2008
@@ -34,6 +34,7 @@
     TestSuite suite = new TestSuite();
     suite.addTest(org.slf4j.converter.PackageTest.suite());
     suite.addTest(org.slf4j.converter.line.PackageTest.suite());
+    suite.addTest(org.slf4j.converter.helper.PackageTest.suite());
     
     return suite;
   }

Modified: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ProjectConverterTest.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ProjectConverterTest.java	(original)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/ProjectConverterTest.java	Wed Feb 20 18:58:39 2008
@@ -2,17 +2,18 @@
 
 import java.io.File;
 
+import org.slf4j.converter.internal.NopProgressListener;
+
 import junit.framework.TestCase;
 
 public class ProjectConverterTest extends TestCase {
 
-  
   public void test() {
   }
-  
-  
+
   public void XtestBarracuda() {
-    ProjectConverter pc = new ProjectConverter(Constant.LOG4J_TO_SLF4J);
+    ProjectConverter pc = new ProjectConverter(Constant.LOG4J_TO_SLF4J,
+        new NopProgressListener());
     File projectFolder = new File("c:/home/ceki//Varia/Barracuda");
     pc.convertProject(projectFolder);
   }

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/AbbreviatorTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/AbbreviatorTest.java	Wed Feb 20 18:58:39 2008
@@ -0,0 +1,139 @@
+package org.slf4j.converter.helper;
+
+import junit.framework.TestCase;
+
+public class AbbreviatorTest extends TestCase {
+
+  static final char FS = '/';
+  static final String INPUT_0 = "/abc/123456/ABC";
+  static final String INPUT_1 = "/abc/123456/xxxxx/ABC";
+
+  RandomHelper rh = new RandomHelper(FS);
+
+  public AbbreviatorTest(String arg0) {
+    super(arg0);
+  }
+
+  protected void setUp() throws Exception {
+    super.setUp();
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+  public void testSmoke() {
+    {
+      Abbreviator abb = new Abbreviator(2, 100, FS);
+      String r = abb.abbreviate(INPUT_0);
+      assertEquals(INPUT_0, r);
+    }
+
+    {
+      Abbreviator abb = new Abbreviator(3, 8, FS);
+      String r = abb.abbreviate(INPUT_0);
+      assertEquals("/abc/.../ABC", r);
+    }
+    {
+      Abbreviator abb = new Abbreviator(3, 8, FS);
+      String r = abb.abbreviate(INPUT_0);
+      assertEquals("/abc/.../ABC", r);
+    }
+  }
+
+  public void testImpossibleToAbbreviate() {
+    Abbreviator abb = new Abbreviator(2, 20, FS);
+    String in = "iczldqwivpgm/mgrmvbjdxrwmqgprdjusth";
+    String r = abb.abbreviate(in);
+    assertEquals(in, r);
+  }
+
+  public void testNoFS() {
+    Abbreviator abb = new Abbreviator(2, 100, FS);
+    String r = abb.abbreviate("hello");
+    assertEquals("hello", r);
+
+  }
+
+  public void testZeroPrefix() {
+    {
+      Abbreviator abb = new Abbreviator(0, 100, FS);
+      String r = abb.abbreviate(INPUT_0);
+      assertEquals(INPUT_0, r);
+    }
+  }
+
+  public void testTheories() {
+    int MAX_RANDOM_FIXED_LEN = 20;
+    int MAX_RANDOM_AVG_LEN = 20;
+    int MAX_RANDOM_MAX_LEN = 100;
+    for (int i = 0; i < 10000; i++) {
+
+      //System.out.println("Test number " + i);
+
+      // 0 <= fixedLen < MAX_RANDOM_FIXED_LEN
+      int fixedLen = rh.nextInt(MAX_RANDOM_FIXED_LEN);
+      // 5 <= averageLen < MAX_RANDOM_AVG_LEN
+      int averageLen = rh.nextInt(MAX_RANDOM_AVG_LEN) + 3;
+      // System.out.println("fixedLen="+fixedLen+", averageLen="+averageLen);
+
+      int maxLen = rh.nextInt(MAX_RANDOM_MAX_LEN) + fixedLen;
+      if (maxLen <= 1) {
+        continue;
+      }
+      // System.out.println("maxLen="+maxLen);
+      int targetLen = (maxLen / 2) + rh.nextInt(maxLen / 2) + 1;
+
+      if (targetLen > maxLen) {
+        targetLen = maxLen;
+      }
+      String filename = rh.buildRandomFileName(averageLen, maxLen);
+
+      Abbreviator abb = new Abbreviator(fixedLen, targetLen, FS);
+      String result = abb.abbreviate(filename);
+      assertTheory0(averageLen, filename, result, fixedLen, targetLen);
+      assertUsefulness(averageLen, filename, result, fixedLen, targetLen);
+      assertTheory1(filename, result, fixedLen, targetLen);
+      assertTheory2(filename, result, fixedLen, targetLen);
+    }
+  }
+
+  // result length is smaller than original length 
+  void assertTheory0(int averageLen, String filename, String result,
+      int fixedLen, int targetLength) {
+      assertTrue("filename=[" + filename + "] result=[" + result + "]", result
+        .length() <= filename.length());
+  }
+
+  // if conditions allow, result length should be to target length
+  void assertUsefulness(int averageLen, String filename, String result,
+      int fixedLen, int targetLength) {
+    int resLen = result.length();
+
+    int margin = averageLen * 4;
+    if (targetLength > fixedLen + margin) {
+      assertTrue("filename=[" + filename + "], result=[" + result
+          + "] resultLength=" + resLen + " fixedLength=" + fixedLen
+          + ", targetLength=" + targetLength + ", avgLen=" + averageLen, result
+          .length() <= targetLength + averageLen);
+    }
+  }
+
+  // result start with prefix found in filename
+  void assertTheory1(String filename, String result, int fixedLen,
+      int targetLength) {
+    String prefix = filename.substring(0, fixedLen);
+    assertTrue(result.startsWith(prefix));
+  }
+
+  // The string /.../ is found in the result once at a position higher
+  // than fixedLen
+  void assertTheory2(String filename, String result, int fixedLen,
+      int targetLength) {
+    if (filename == result) {
+      return;
+    }
+    int fillerIndex = result.indexOf(Abbreviator.FILLER);
+    assertTrue(fillerIndex >= fixedLen);
+  }
+}

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/PackageTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/PackageTest.java	Wed Feb 20 18:58:39 2008
@@ -0,0 +1,38 @@
+/* 
+ * Copyright (c) 2004-2007 QOS.ch
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free  of charge, to any person obtaining
+ * a  copy  of this  software  and  associated  documentation files  (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute,  sublicense, and/or sell  copies of  the Software,  and to
+ * permit persons to whom the Software  is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The  above  copyright  notice  and  this permission  notice  shall  be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+package org.slf4j.converter.helper;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class PackageTest extends TestCase {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(AbbreviatorTest.class);
+    return suite;
+  }
+}
\ No newline at end of file

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/RandomHelper.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/helper/RandomHelper.java	Wed Feb 20 18:58:39 2008
@@ -0,0 +1,46 @@
+package org.slf4j.converter.helper;
+
+import java.util.Random;
+
+public class RandomHelper {
+
+  private Random random = new Random(100);
+  final char folderSeparator;
+  
+  RandomHelper(char folderSeparator) {
+   this.folderSeparator = folderSeparator;
+  }
+  
+  private String randomString(int len) {
+    StringBuffer buf = new StringBuffer();
+    for (int i = 0; i < len; i++) {
+      int offset = random.nextInt(26);
+      char c = (char) ('a' + offset);
+      buf.append(c);
+    }
+    return buf.toString();
+  }
+
+  int nextInt(int n) {
+    return random.nextInt(n);
+  }
+  
+  String buildRandomFileName(int averageNodeLength, int totalLength) {
+    StringBuffer buf = new StringBuffer();
+    int MAX_NODE_LENGTH = averageNodeLength * 2;
+    while (buf.length() < totalLength) {
+      int remaining = totalLength - buf.length();
+      int currentNodeLength;
+      if (remaining > MAX_NODE_LENGTH) {
+        currentNodeLength = random.nextInt(MAX_NODE_LENGTH) + 1;
+        buf.append(randomString(currentNodeLength));
+        buf.append('/');
+      } else {
+        currentNodeLength = remaining;
+        buf.append(randomString(currentNodeLength));
+      }
+    }
+    return buf.toString();
+  }
+
+}

Added: slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/internal/NopProgressListener.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-converter/src/test/java/org/slf4j/converter/internal/NopProgressListener.java	Wed Feb 20 18:58:39 2008
@@ -0,0 +1,19 @@
+package org.slf4j.converter.internal;
+
+import java.io.File;
+
+public class NopProgressListener implements ProgressListener {
+
+  public void onDirectory(File file) {
+  }
+
+  public void onDone() {
+  }
+
+  public void onFileAddition(File file) {
+  }
+
+  public void onFileConversion(File file) {
+  }
+
+}



More information about the slf4j-dev mailing list