Skip to content

Commit 1033200

Browse files
Noa Resarechipchilders
authored andcommitted
CLOUDSTACK-933: CglibThrowableRendererTest writing stack traces...
Improve CglibThrowableRenderer test case Log to a separate Logger instead of the default one to avoid spurious stack traces in test run output. Actually verify that registering CglibThrowableRenderer with the alternative log hierarchy actually removes call trace lines that contains the string <generated>
1 parent d8ebd5e commit 1033200

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

utils/test/com/cloud/utils/log/CglibThrowableRendererTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@
1818

1919
import junit.framework.TestCase;
2020

21-
import org.apache.log4j.Logger;
21+
import org.apache.log4j.*;
2222

2323
import com.cloud.utils.component.ComponentLocator;
2424
import com.cloud.utils.db.DB;
2525
import com.cloud.utils.exception.CloudRuntimeException;
26+
import org.apache.log4j.spi.RootLogger;
27+
import org.apache.log4j.spi.ThrowableRenderer;
28+
29+
import java.io.CharArrayWriter;
30+
import java.io.Writer;
2631

2732

2833
public class CglibThrowableRendererTest extends TestCase {
34+
static Logger another = Logger.getLogger("TEST");
35+
2936
private final static Logger s_logger = Logger.getLogger(CglibThrowableRendererTest.class);
3037
public static class Test {
3138
@DB
@@ -48,13 +55,40 @@ public void exception() {
4855
}
4956
}
5057
}
58+
59+
private Logger getAlternateLogger(Writer writer, ThrowableRenderer renderer) {
60+
Hierarchy hierarchy = new Hierarchy(new RootLogger(Level.INFO));
61+
if (renderer != null) {
62+
hierarchy.setThrowableRenderer(renderer);
63+
}
64+
Logger alternateRoot = hierarchy.getRootLogger();
65+
alternateRoot.addAppender(new WriterAppender(new SimpleLayout(), writer));
66+
return alternateRoot;
67+
}
5168

5269
public void testException() {
70+
Writer w = new CharArrayWriter();
71+
Logger alt = getAlternateLogger(w, null);
72+
5373
Test test = ComponentLocator.inject(Test.class);
5474
try {
5575
test.exception();
5676
} catch (Exception e) {
57-
s_logger.warn("exception caught", e);
77+
alt.warn("exception caught", e);
5878
}
79+
// first check that we actually have some call traces containing "<generated>"
80+
assertTrue(w.toString().contains("<generated>"));
81+
82+
w = new CharArrayWriter();
83+
alt = getAlternateLogger(w, new CglibThrowableRenderer());
84+
85+
try {
86+
test.exception();
87+
} catch (Exception e) {
88+
alt.warn("exception caught", e);
89+
}
90+
// then we check that CglibThrowableRenderer indeed remove those occurrences
91+
assertFalse(w.toString().contains("<generated>"));
92+
5993
}
6094
}

0 commit comments

Comments
 (0)