[logback-dev] svn commit: r2365 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/spi test/java/ch/qos/logback/classic/spi

noreply.ceki at qos.ch noreply.ceki at qos.ch
Wed Jul 22 19:53:23 CEST 2009


Author: ceki
Date: Wed Jul 22 19:53:23 2009
New Revision: 2365

Added:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/BogusClassLoader.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LocalFirstClassLoader.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PackagingDataCalculatorTest.java

Log:
- Fix LBCLASSIC-125
 
  It was surprisingly difficult to write a test case where a NoClassDefFoundError was thrown

  See also http://jira.qos.ch/browse/LBCLASSIC-125

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java	Wed Jul 22 19:53:23 2009
@@ -50,10 +50,10 @@
   }
 
   public void calculate(IThrowableProxy tp) {
-    while(tp != null) {
+    while (tp != null) {
       populateFrames(tp.getStackTraceElementProxyArray());
       tp = tp.getCause();
-    } 
+    }
   }
 
   void populateFrames(StackTraceElementProxy[] stepArray) {
@@ -134,7 +134,7 @@
   }
 
   String getImplementationVersion(Class type) {
-    if(type == null) {
+    if (type == null) {
       return "na";
     }
     Package aPackage = type.getPackage();
@@ -194,6 +194,8 @@
       return cl.loadClass(className);
     } catch (ClassNotFoundException e1) {
       return null;
+    } catch (NoClassDefFoundError e1) {
+      return null;
     } catch (Exception e) {
       e.printStackTrace(); // this is unexpected
       return null;
@@ -203,8 +205,9 @@
 
   /**
    * 
-   * @param lastGuaranteedClassLoader may be null
-   *                
+   * @param lastGuaranteedClassLoader
+   *                may be null
+   * 
    * @param className
    * @return
    */
@@ -226,6 +229,8 @@
       return Class.forName(className);
     } catch (ClassNotFoundException e1) {
       return null;
+    } catch (NoClassDefFoundError e1) {
+      return null;
     } catch (Exception e) {
       e.printStackTrace(); // this is unexpected
       return null;

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/BogusClassLoader.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/BogusClassLoader.java	Wed Jul 22 19:53:23 2009
@@ -0,0 +1,48 @@
+/**
+ * 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.classic.spi;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+/**
+ * A trivial class loader which throws a NoClassDefFoundError if the requested
+ * class name contains the string "Bogus".
+ * 
+ * @author Ceki Gülcü
+ */
+public class BogusClassLoader extends URLClassLoader {
+
+  public BogusClassLoader(URL[] urls) {
+    super(urls);
+  }
+
+  public BogusClassLoader(URL[] urls, ClassLoader parent) {
+    super(urls, parent);
+  }
+
+  public Class<?> loadClass(String name) throws ClassNotFoundException {
+    return loadClass(name, false);
+  }
+
+  /**
+   * Throw NoClassDefFoundError if the requested class contains the string
+   * "Bogus". Otherwise, delegate to super-class.
+   */
+  protected Class<?> loadClass(String name, boolean resolve)
+      throws ClassNotFoundException {
+
+    if (name.contains("Bogus")) {
+      throw new NoClassDefFoundError();
+    }
+
+    return super.loadClass(name, resolve);
+  }
+}

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LocalFirstClassLoader.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LocalFirstClassLoader.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LocalFirstClassLoader.java	Wed Jul 22 19:53:23 2009
@@ -1,3 +1,12 @@
+/**
+ * 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.classic.spi;
 
 import java.net.URL;

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PackagingDataCalculatorTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PackagingDataCalculatorTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PackagingDataCalculatorTest.java	Wed Jul 22 19:53:23 2009
@@ -13,6 +13,10 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
 import org.junit.Test;
 
 import ch.qos.logback.classic.util.TeztHelper;
@@ -92,26 +96,29 @@
 
   }
 
+  private ClassLoader makeBogusClassLoader() throws MalformedURLException {
+    ClassLoader currentClassLoader = this.getClass().getClassLoader();
+    BogusClassLoader bcl = new BogusClassLoader(new URL[] {},
+        currentClassLoader);
+    return bcl;
+  }
+
   @Test
   // Test http://jira.qos.ch/browse/LBCLASSIC-125
-  public void noClassDefFoundError() {
+  public void noClassDefFoundError_LBCLASSIC_125Test()
+      throws MalformedURLException {
+    ClassLoader cl = (URLClassLoader) makeBogusClassLoader();
+    Thread.currentThread().setContextClassLoader(cl);
     Throwable t = new Throwable("x");
     ThrowableProxy tp = new ThrowableProxy(t);
     StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
-    StackTraceElement bogusSTE = new StackTraceElement(MyBogus.class.getName(),
-        "myMethod", "myFile", 12);
-    StackTraceElementProxy bogusSTEP;// = new StackTraceElementProxy(bogusSTE);
-    System.out.println(stepArray.length);
-    for (int i = 0; i < stepArray.length; i++) {
-      System.out.println(i);
-      
-      stepArray[i] = new StackTraceElementProxy(bogusSTE);
-    }
+    StackTraceElement bogusSTE = new StackTraceElement("com.Bogus", "myMethod",
+        "myFile", 12);
+    stepArray[0] = new StackTraceElementProxy(bogusSTE);
     PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
+    // NoClassDefFoundError should be caught
     pdc.calculate(tp);
-    System.out.println(ThrowableProxyUtil.asString(tp));
-    
-    System.out.println(new MyBogus());
-    
+
   }
+
 }


More information about the logback-dev mailing list