details:   https://code.openbravo.com/erp/devel/pi/rev/06cd9c11c84e
changeset: 28617:06cd9c11c84e
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Thu Feb 18 14:54:19 2016 +0100
summary:   fixed issue 32275: jUnit: add capability to assert on logs

  TestLogAppender can be used in test cases extending OBBaseTest to perform
  assertions on what is logged.

diffstat:

 src-test/src/org/openbravo/test/base/OBBaseTest.java      |  40 ++++++-
 src-test/src/org/openbravo/test/base/TestLogAppender.java |  86 +++++++++++++++
 2 files changed, 124 insertions(+), 2 deletions(-)

diffs (177 lines):

diff -r 3e358d0a050a -r 06cd9c11c84e 
src-test/src/org/openbravo/test/base/OBBaseTest.java
--- a/src-test/src/org/openbravo/test/base/OBBaseTest.java      Wed Feb 10 
22:16:11 2016 -0500
+++ b/src-test/src/org/openbravo/test/base/OBBaseTest.java      Thu Feb 18 
14:54:19 2016 +0100
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2014-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2014-2016 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -26,9 +26,11 @@
 import java.util.Map;
 import java.util.Random;
 
+import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;
 import org.hibernate.criterion.Restrictions;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Rule;
@@ -167,6 +169,8 @@
    */
   protected static Map<String, String[]> TEST_ORG_TREE = new HashMap<String, 
String[]>();
 
+  private static TestLogAppender testLogAppender;
+
   static {
 
     // "F&B International Group"
@@ -206,13 +210,20 @@
   protected static final String TEST_LOCATION_ID = 
"A21EF1AB822149BEB65D055CD91F261B";
 
   /**
-   * Overridden to initialize the Dal layer
+   * Initializes DAL, it also craetes a log appender that can be used to 
assert on logs. This log
+   * appender is disabled by default, to activate it set the level with
+   * {@link OBBaseTest#setTestLogAppenderLevel(Level)}
    * 
+   * @see TestLogAppender
    */
   @BeforeClass
   public static void setDalUp() throws Exception {
     if (OBBaseTest.class.getResource("/log4j.lcf") != null) {
       
PropertyConfigurator.configure(OBBaseTest.class.getResource("/log4j.lcf"));
+      testLogAppender = new TestLogAppender();
+
+      testLogAppender.setThreshold(Level.OFF);
+      Logger.getRootLogger().addAppender(testLogAppender);
     }
     staticInitializeDalLayer();
   }
@@ -227,6 +238,31 @@
     errorOccured = false;
   }
 
+  /** Test log appender is reset and switched off */
+  @After
+  public void testDone() {
+    if (testLogAppender != null) {
+      testLogAppender.reset();
+      setTestLogAppenderLevel(Level.OFF);
+    }
+  }
+
+  /**
+   * Defines the threshold {@link Level} that will make messages to be tracked 
by
+   * {@link TestLogAppender}. Note after test completion appender is reset and 
its level is set back
+   * to Level.OFF disabling in this manner subsequent logging track.
+   */
+  protected void setTestLogAppenderLevel(Level level) {
+    if (testLogAppender != null) {
+      testLogAppender.setThreshold(level);
+    }
+  }
+
+  /** Returns log appender in order to be possible to do assertions on it */
+  protected TestLogAppender getTestLogAppender() {
+    return testLogAppender;
+  }
+
   /**
    * Initializes the DALLayer, can be overridden to add specific 
initialization behavior.
    * 
diff -r 3e358d0a050a -r 06cd9c11c84e 
src-test/src/org/openbravo/test/base/TestLogAppender.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/base/TestLogAppender.java Thu Feb 18 
14:54:19 2016 +0100
@@ -0,0 +1,86 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2016 Openbravo SLU 
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.test.base;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.Level;
+import org.apache.log4j.spi.LoggingEvent;
+
+/**
+ * Used in {@link OBBaseTest}, keeps track of all messages written in log in 
order to make possible
+ * to later do assertions on them.
+ *
+ * @author alostale
+ *
+ */
+public class TestLogAppender extends AppenderSkeleton {
+  private Map<Level, List<String>> messages = new HashMap<Level, 
List<String>>();
+
+  @Override
+  protected void append(LoggingEvent event) {
+    List<String> levelMsgs = messages.get(event.getLevel());
+    if (levelMsgs == null) {
+      levelMsgs = new ArrayList<String>();
+      messages.put(event.getLevel(), levelMsgs);
+    }
+    levelMsgs.add(event.getMessage().toString());
+  }
+
+  @Override
+  public void close() {
+  }
+
+  @Override
+  public boolean requiresLayout() {
+    return false;
+  }
+
+  /** Removes all the messages tracked so far */
+  public void reset() {
+    messages = new HashMap<Level, List<String>>();
+  }
+
+  /**
+   * Returns a list with all messaged currently tracked. If none is tracked, 
an empty list is
+   * returned.
+   */
+  public List<String> getAllMessages() {
+    List<String> allMessages = new ArrayList<String>();
+    for (Entry<Level, List<String>> msgLvl : messages.entrySet()) {
+      allMessages.addAll(msgLvl.getValue());
+    }
+    return allMessages;
+  }
+
+  /** Returns a list of tracked message for a given Level, or an empty if none 
is tracked */
+  public List<String> getMessages(Level level) {
+    List<String> msgs = messages.get(level);
+    if (msgs == null) {
+      msgs = Collections.emptyList();
+    }
+    return msgs;
+  }
+}

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to