incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@apache.org
Subject [30/50] git commit: CLOUDSTACK-933: CglibThrowableRendererTest writing stack traces...
Date Mon, 14 Jan 2013 16:15:58 GMT
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>


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/1033200b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/1033200b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/1033200b

Branch: refs/heads/cloud-agent-with-openvswitch
Commit: 1033200b0b5943cc21d5397d12d3b54b69aa2273
Parents: d8ebd5e
Author: Noa Resare <noa@spotify.com>
Authored: Wed Jan 9 16:39:10 2013 +0100
Committer: Chip Childers <chip.childers@gmail.com>
Committed: Fri Jan 11 17:09:44 2013 -0500

----------------------------------------------------------------------
 .../utils/log/CglibThrowableRendererTest.java      |   38 ++++++++++++++-
 1 files changed, 36 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/1033200b/utils/test/com/cloud/utils/log/CglibThrowableRendererTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/log/CglibThrowableRendererTest.java b/utils/test/com/cloud/utils/log/CglibThrowableRendererTest.java
index 5a9501d..c1cd81e 100644
--- a/utils/test/com/cloud/utils/log/CglibThrowableRendererTest.java
+++ b/utils/test/com/cloud/utils/log/CglibThrowableRendererTest.java
@@ -18,14 +18,21 @@ package com.cloud.utils.log;
 
 import junit.framework.TestCase;
 
-import org.apache.log4j.Logger;
+import org.apache.log4j.*;
 
 import com.cloud.utils.component.ComponentLocator;
 import com.cloud.utils.db.DB;
 import com.cloud.utils.exception.CloudRuntimeException;
+import org.apache.log4j.spi.RootLogger;
+import org.apache.log4j.spi.ThrowableRenderer;
+
+import java.io.CharArrayWriter;
+import java.io.Writer;
 
 
 public class CglibThrowableRendererTest extends TestCase {
+    static Logger another = Logger.getLogger("TEST");
+
     private final static Logger s_logger = Logger.getLogger(CglibThrowableRendererTest.class);
     public static class Test {
         @DB
@@ -48,13 +55,40 @@ public class CglibThrowableRendererTest extends TestCase {
             }
         }
     }
+
+    private Logger getAlternateLogger(Writer writer, ThrowableRenderer renderer) {
+        Hierarchy hierarchy = new Hierarchy(new RootLogger(Level.INFO));
+        if (renderer != null) {
+            hierarchy.setThrowableRenderer(renderer);
+        }
+        Logger alternateRoot = hierarchy.getRootLogger();
+        alternateRoot.addAppender(new WriterAppender(new SimpleLayout(), writer));
+        return alternateRoot;
+    }
     
     public void testException() {
+        Writer w = new CharArrayWriter();
+        Logger alt = getAlternateLogger(w, null);
+
         Test test = ComponentLocator.inject(Test.class);
         try {
             test.exception();
         } catch (Exception e) {
-            s_logger.warn("exception caught", e);
+            alt.warn("exception caught", e);
         }
+        // first check that we actually have some call traces containing "<generated>"
+        assertTrue(w.toString().contains("<generated>"));
+
+        w = new CharArrayWriter();
+        alt = getAlternateLogger(w, new CglibThrowableRenderer());
+
+        try {
+            test.exception();
+        } catch (Exception e) {
+            alt.warn("exception caught", e);
+        }
+        // then we check that CglibThrowableRenderer indeed remove those occurrences
+        assertFalse(w.toString().contains("<generated>"));
+
     }
 }


Mime
View raw message