View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.spi;
15  
16  import static org.junit.Assert.assertNotNull;
17  import static org.junit.Assert.assertTrue;
18  
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.net.URLClassLoader;
22  
23  import org.junit.Ignore;
24  import org.junit.Test;
25  
26  import ch.qos.logback.classic.util.TestHelper;
27  import ch.qos.logback.core.util.SystemInfo;
28  
29  public class PackagingDataCalculatorTest {
30  
31      public void verify(ThrowableProxy tp) {
32          for (StackTraceElementProxy step : tp.getStackTraceElementProxyArray()) {
33              if (step != null) {
34                  assertNotNull(step.getClassPackagingData());
35              }
36          }
37      }
38  
39      @Test
40      public void smoke() throws Exception {
41          Throwable t = new Throwable("x");
42          ThrowableProxy tp = new ThrowableProxy(t);
43          PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
44          pdc.calculate(tp);
45          verify(tp);
46          tp.fullDump();
47      }
48  
49      @Test
50      public void nested() throws Exception {
51          Throwable t = TestHelper.makeNestedException(3);
52          ThrowableProxy tp = new ThrowableProxy(t);
53          PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
54          pdc.calculate(tp);
55          verify(tp);
56      }
57  
58      public void doCalculateClassPackagingData(boolean withClassPackagingCalculation) {
59          try {
60              throw new Exception("testing");
61          } catch (Throwable e) {
62              ThrowableProxy tp = new ThrowableProxy(e);
63              if (withClassPackagingCalculation) {
64                  PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
65                  pdc.calculate(tp);
66              }
67          }
68      }
69  
70      double loop(int len, boolean withClassPackagingCalculation) {
71          long start = System.nanoTime();
72          for (int i = 0; i < len; i++) {
73              doCalculateClassPackagingData(withClassPackagingCalculation);
74          }
75          return (1.0 * System.nanoTime() - start) / len / 1000;
76      }
77  
78      @Ignore
79      @Test
80      public void perfTest() {
81          int len = 1000;
82          loop(len, false);
83          loop(len, true);
84  
85          double d0 = loop(len, false);
86          System.out.println("without packaging info " + d0 + " microseconds");
87  
88          double d1 = loop(len, true);
89          System.out.println("with    packaging info " + d1 + " microseconds");
90  
91          int slackFactor = 8;
92          if (!SystemInfo.getJavaVendor().contains("Sun")) {
93              // be more lenient with other JDKs
94              slackFactor = 15;
95          }
96          assertTrue("computing class packaging data (" + d1 + ") should have been less than " + slackFactor
97                          + " times the time it takes to process an exception " + (d0 * slackFactor), d0 * slackFactor > d1);
98  
99      }
100 
101     private ClassLoader makeBogusClassLoader() throws MalformedURLException {
102         ClassLoader currentClassLoader = this.getClass().getClassLoader();
103         return new BogusClassLoader(new URL[] {}, currentClassLoader);
104     }
105 
106     @Test
107     // Test http://jira.qos.ch/browse/LBCLASSIC-125
108     public void noClassDefFoundError_LBCLASSIC_125Test() throws MalformedURLException {
109         ClassLoader cl = (URLClassLoader) makeBogusClassLoader();
110         Thread.currentThread().setContextClassLoader(cl);
111         Throwable t = new Throwable("x");
112         ThrowableProxy tp = new ThrowableProxy(t);
113         StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
114         StackTraceElement bogusSTE = new StackTraceElement("com.Bogus", "myMethod", "myFile", 12);
115         stepArray[0] = new StackTraceElementProxy(bogusSTE);
116         PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
117         // NoClassDefFoundError should be caught
118         pdc.calculate(tp);
119 
120     }
121 
122 }