1   package ch.qos.logback.classic.spi;
2   
3   import static org.junit.Assert.assertNotNull;
4   import static org.junit.Assert.assertTrue;
5   
6   import org.junit.After;
7   import org.junit.Before;
8   import org.junit.Test;
9   
10  import ch.qos.logback.classic.util.TeztHelper;
11  import ch.qos.logback.core.util.SystemInfo;
12  
13  public class BasicCPDCTest {
14  
15    @Before
16    public void setUp() throws Exception {
17    }
18  
19    @After
20    public void tearDown() throws Exception {
21    }
22  
23    public void verify(ThrowableDataPoint[] tdpArray) {
24      for (ThrowableDataPoint tdp : tdpArray) {
25        StackTraceElementProxy step = tdp.getStackTraceElementProxy();
26        if (step != null) {
27          assertNotNull(step.getClassPackagingData());
28        }
29      }
30    }
31  
32    @Test
33    public void otherJD() {
34      System.out.println(SystemInfo.getJavaVendor());
35    }
36  
37  //  @Test
38  //  public void withGreenMail() {
39  //    try {
40  //      ServerSetup serverSetup = new ServerSetup(-1, "localhost",
41  //          ServerSetup.PROTOCOL_SMTP);
42  //      GreenMail greenMail = new GreenMail((ServerSetup) null);
43  //      // greenMail.start();
44  //    } catch (Throwable e) {
45  //      ThrowableProxy tp = new ThrowableProxy(e);
46  //      ClassPackagingDataCalculator cpdc = tp.getClassPackagingDataCalculator();
47  //      ThrowableDataPoint[] tdpArray = tp.getThrowableDataPointArray();
48  //      cpdc.calculate(tdpArray);
49  //      verify(tdpArray);
50  //      tp.fullDump();
51  //    }
52  //  }
53    
54   
55    @Test
56    public void integration() throws Exception {
57    
58    }
59    
60    @Test
61    public void smoke() throws Exception {
62      Throwable t = new Throwable("x");
63      ThrowableProxy tp = new ThrowableProxy(t);
64      PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
65      ThrowableDataPoint[] tdpArray = tp.getThrowableDataPointArray();
66      pdc.calculate(tdpArray);
67      verify(tdpArray);
68      tp.fullDump();
69    }
70  
71    @Test
72    public void nested() throws Exception {
73      Throwable t = TeztHelper.makeNestedException(3);
74      ThrowableProxy tp = new ThrowableProxy(t);
75      PackagingDataCalculator pdc = tp.getPackagingDataCalculator();
76      ThrowableDataPoint[] tdpArray = tp.getThrowableDataPointArray();
77      pdc.calculate(tdpArray);
78      verify(tdpArray);
79    }
80  
81    public void doCalculateClassPackagingData(
82        boolean withClassPackagingCalculation) {
83      try {
84        throw new Exception("testing");
85      } catch (Throwable e) {
86        ThrowableProxy tp = new ThrowableProxy(e);
87        if (withClassPackagingCalculation) {
88          PackagingDataCalculator pdc = tp
89              .getPackagingDataCalculator();
90          ThrowableDataPoint[] tdpArray = tp.getThrowableDataPointArray();
91          pdc.calculate(tdpArray);
92        }
93      }
94    }
95  
96    double loop(int len, boolean withClassPackagingCalculation) {
97      long start = System.nanoTime();
98      for (int i = 0; i < len; i++) {
99        doCalculateClassPackagingData(withClassPackagingCalculation);
100     }
101     return (1.0 * System.nanoTime() - start) / len / 1000;
102   }
103 
104   @Test
105   public void perfTest() {
106     int len = 1000;
107     loop(len, false);
108     loop(len, true);
109 
110     double d0 = loop(len, false);
111     System.out.println("without packaging info " + d0 + " microseconds");
112 
113     double d1 = loop(len, true);
114     System.out.println("with    packaging info " + d1 + " microseconds");
115 
116     int slackFactor = 8;  
117     if (!SystemInfo.getJavaVendor().contains("Sun")) {
118       // be more lenient with other JDKs
119       slackFactor = 10;
120     } 
121     assertTrue(
122         "computing class packaging data ("
123             + d1
124             + ") should have been less than "+slackFactor+" times the time it takes to process an exception "
125             + (d0 * slackFactor), d0 * slackFactor > d1);
126 
127   }
128 }