[logback-dev] svn commit: r1287 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/selector test/java/ch/qos/logback/classic/selector

noreply.seb at qos.ch noreply.seb at qos.ch
Mon Jan 29 14:45:15 CET 2007


Author: seb
Date: Mon Jan 29 14:45:15 2007
New Revision: 1287

Added:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextDetachingSCLTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextJNDISelectorTest.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java

Log:
added two testcases for the ContextDetachingSCL and ContextJNDISelector classes

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java	Mon Jan 29 14:45:15 2007
@@ -108,5 +108,15 @@
           + " does not lead to a valid file");
     }
   }
+  
+  /**
+   * Returns the number of managed contexts
+   * Used for testing purposes
+   * 
+   * @return the number of managed contexts
+   */
+  public int getCount() {
+    return contextMap.size();
+  }
 
 }

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextDetachingSCLTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextDetachingSCLTest.java	Mon Jan 29 14:45:15 2007
@@ -0,0 +1,60 @@
+package ch.qos.logback.classic.selector;
+
+import junit.framework.TestCase;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.ClassicGlobal;
+import ch.qos.logback.classic.net.mock.MockInitialContext;
+import ch.qos.logback.classic.net.mock.MockInitialContextFactory;
+import ch.qos.logback.classic.selector.servlet.ContextDetachingSCL;
+
+public class ContextDetachingSCLTest extends TestCase {
+  
+  static String INITIAL_CONTEXT_KEY = "java.naming.factory.initial";
+
+  ContextDetachingSCL listener;
+  
+  @Override
+  protected void setUp() throws Exception {
+    System.setProperty(ClassicGlobal.LOGBACK_CONTEXT_SELECTOR, "JNDI");
+    listener = new ContextDetachingSCL();
+    
+    MockInitialContext mic = MockInitialContextFactory.getContext();
+    mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "toto");
+    
+    //The property must be set after we setup the Mock
+    System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
+    
+    //this call will create the context "toto"
+    @SuppressWarnings("unused")
+    Logger logger = LoggerFactory.getLogger(ContextDetachingSCLTest.class);
+
+    super.setUp();
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    System.clearProperty(INITIAL_CONTEXT_KEY);
+    super.tearDown();
+  }
+  
+  public void testDetach() {
+    ContextJNDISelector selector = (ContextJNDISelector) LoggerFactory.getContextSelector();
+    listener.contextDestroyed(null);
+    
+    assertEquals(0, selector.getCount());
+  }
+  
+  public void testDetachWithMissingContext() {
+    MockInitialContext mic = MockInitialContextFactory.getContext();
+    mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "tata");
+    
+    ContextJNDISelector selector = (ContextJNDISelector) LoggerFactory.getContextSelector();
+    listener.contextDestroyed(null);
+    
+    assertEquals(1, selector.getCount());
+  }
+  
+}

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextJNDISelectorTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextJNDISelectorTest.java	Mon Jan 29 14:45:15 2007
@@ -0,0 +1,63 @@
+package ch.qos.logback.classic.selector;
+
+import junit.framework.TestCase;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.ClassicGlobal;
+import ch.qos.logback.classic.net.mock.MockInitialContext;
+import ch.qos.logback.classic.net.mock.MockInitialContextFactory;
+import ch.qos.logback.classic.selector.servlet.ContextDetachingSCL;
+import ch.qos.logback.core.Context;
+
+public class ContextJNDISelectorTest extends TestCase {
+  
+  static String INITIAL_CONTEXT_KEY = "java.naming.factory.initial";
+
+  ContextDetachingSCL listener;
+  
+  @Override
+  protected void setUp() throws Exception {
+    System.setProperty(ClassicGlobal.LOGBACK_CONTEXT_SELECTOR, "JNDI");
+    listener = new ContextDetachingSCL();
+    
+    MockInitialContext mic = MockInitialContextFactory.getContext();
+    mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "toto");
+    
+    //The property must be set after we setup the Mock
+    System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
+    
+    //this call will create the context "toto"
+    @SuppressWarnings("unused")
+    Logger logger = LoggerFactory.getLogger(ContextDetachingSCLTest.class);
+
+    super.setUp();
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    System.clearProperty(INITIAL_CONTEXT_KEY);
+    super.tearDown();
+  }
+
+  public void testGetExistingContext() {
+    ContextSelector selector = LoggerFactory.getContextSelector();
+    Context context = selector.getLoggerContext();
+    assertEquals("toto", context.getName());
+  }
+  
+  public void testCreateContext() {
+    MockInitialContext mic = MockInitialContextFactory.getContext();
+    mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "tata");
+    
+    @SuppressWarnings("unused")
+    Logger logger = LoggerFactory.getLogger(ContextDetachingSCLTest.class);
+    
+    ContextJNDISelector selector = (ContextJNDISelector)LoggerFactory.getContextSelector();
+    Context context = selector.getLoggerContext();
+    assertEquals("tata", context.getName());
+    assertEquals(2, selector.getCount());
+  }
+  
+}



More information about the logback-dev mailing list