1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.spi;
15
16 import ch.qos.logback.classic.util.TestHelper;
17 import ch.qos.logback.core.util.SystemInfo;
18 import org.junit.jupiter.api.Disabled;
19 import org.junit.jupiter.api.Test;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.net.URLClassLoader;
24
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 public class PackagingDataCalculatorTest {
29
30 public void verify(ThrowableProxy tp) {
31 for (StackTraceElementProxy step : tp.getStackTraceElementProxyArray()) {
32 if (step != null) {
33 assertNotNull(step.getClassPackagingData());
34 }
35 }
36 }
37
38 @Test
39 public void smoke() throws Exception {
40 Throwable t = new Throwable("x");
41 ThrowableProxy tp = new ThrowableProxy(t);
42 PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
43 pdc.calculate(tp);
44 verify(tp);
45 tp.fullDump();
46 }
47
48 @Test
49 public void nested() throws Exception {
50 Throwable t = TestHelper.makeNestedException(3);
51 ThrowableProxy tp = new ThrowableProxy(t);
52 PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
53 pdc.calculate(tp);
54 verify(tp);
55 }
56
57 public void doCalculateClassPackagingData(boolean withClassPackagingCalculation) {
58 try {
59 throw new Exception("testing");
60 } catch (Throwable e) {
61 ThrowableProxy tp = new ThrowableProxy(e);
62 if (withClassPackagingCalculation) {
63 PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
64 pdc.calculate(tp);
65 }
66 }
67 }
68
69 double loop(int len, boolean withClassPackagingCalculation) {
70 long start = System.nanoTime();
71 for (int i = 0; i < len; i++) {
72 doCalculateClassPackagingData(withClassPackagingCalculation);
73 }
74 return (1.0 * System.nanoTime() - start) / len / 1000;
75 }
76
77 @Disabled
78 @Test
79 public void perfTest() {
80 int len = 1000;
81 loop(len, false);
82 loop(len, true);
83
84 double d0 = loop(len, false);
85 System.out.println("without packaging info " + d0 + " microseconds");
86
87 double d1 = loop(len, true);
88 System.out.println("with packaging info " + d1 + " microseconds");
89
90 int slackFactor = 8;
91 if (!SystemInfo.getJavaVendor().contains("Sun")) {
92
93 slackFactor = 15;
94 }
95 assertTrue(d0 * slackFactor > d1,
96 "computing class packaging data (" + d1 + ") should have been less than " + slackFactor
97 + " times the time it takes to process an exception " + (d0 * slackFactor));
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
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
118 pdc.calculate(tp);
119
120 }
121
122 }