You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2015/12/22 12:58:12 UTC

svn commit: r1721374 - /sling/trunk/bundles/commons/logservice/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java

Author: rombert
Date: Tue Dec 22 11:58:12 2015
New Revision: 1721374

URL: http://svn.apache.org/viewvc?rev=1721374&view=rev
Log:
SLING-5263 - implement some junit test cases for LogSupport.java

Applied patch submitted by Tien Nguyen, with some refactoring to reduce
code duplication.

This closes #111.

Modified:
    sling/trunk/bundles/commons/logservice/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java

Modified: sling/trunk/bundles/commons/logservice/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/logservice/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java?rev=1721374&r1=1721373&r2=1721374&view=diff
==============================================================================
--- sling/trunk/bundles/commons/logservice/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java (original)
+++ sling/trunk/bundles/commons/logservice/src/test/java/org/apache/sling/commons/logservice/internal/LogSupportTest.java Tue Dec 22 11:58:12 2015
@@ -22,12 +22,16 @@ import java.lang.reflect.Field;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.log.LogEntry;
@@ -36,24 +40,39 @@ import org.osgi.service.startlevel.Start
 import org.slf4j.Logger;
 
 public class LogSupportTest {
-    @Test @SuppressWarnings("unchecked")
-    public void testServiceEvent() throws Exception {
+    
+    private Bundle bundle;
+    private LogSupport logSupport;
+    private Logger testLogger;
+
+    @Before
+    @SuppressWarnings("unchecked")
+    public void prepare() throws Exception {
+        
+        bundle = Mockito.mock(Bundle.class);
+        Mockito.when(bundle.getSymbolicName()).thenReturn("foo.bundle");
+        Mockito.when(bundle.getBundleId()).thenReturn(42L);
+        
         StartLevel startLevel = Mockito.mock(StartLevel.class);
-        LogSupport ls = new LogSupport(startLevel);
-        Field lf = LogSupport.class.getDeclaredField("loggers");
-        lf.setAccessible(true);
-        Map<Long, Logger> loggers = (Map<Long, Logger>) lf.get(ls);
-
-        Bundle b = Mockito.mock(Bundle.class);
-        Mockito.when(b.getSymbolicName()).thenReturn("foo.bundle");
-        Mockito.when(b.getBundleId()).thenReturn(42L);
+        logSupport = new LogSupport(startLevel);
+        Field loggerField = LogSupport.class.getDeclaredField("loggers");
+        loggerField.setAccessible(true);
+        Map<Long, Logger> loggers = (Map<Long, Logger>) loggerField.get(logSupport);
+        
+        testLogger = getMockInfoLogger();
+        loggers.put(bundle.getBundleId(), testLogger);
+    }
+    
+    @Test
+    public void testServiceEvent() throws Exception {
+
 
         final Map<String, Object> props = new HashMap<String, Object>();
         props.put(Constants.OBJECTCLASS, new String [] {"some.class.Name"});
         props.put(Constants.SERVICE_ID, 999L);
 
         ServiceReference sr = Mockito.mock(ServiceReference.class);
-        Mockito.when(sr.getBundle()).thenReturn(b);
+        Mockito.when(sr.getBundle()).thenReturn(bundle);
         Mockito.when(sr.getProperty(Mockito.anyString())).then(new Answer<Object>() {
             public Object answer(InvocationOnMock invocation) throws Throwable {
                 return props.get(invocation.getArguments()[0]);
@@ -62,33 +81,18 @@ public class LogSupportTest {
         Mockito.when(sr.getPropertyKeys()).thenReturn(props.keySet().toArray(new String[] {}));
         ServiceEvent se = new ServiceEvent(ServiceEvent.REGISTERED, sr);
 
-        Logger testLogger = getMockInfoLogger();
-        loggers.put(42L, testLogger);
-
-        ls.serviceChanged(se);
+        logSupport.serviceChanged(se);
 
         Mockito.verify(testLogger).info("Service [999, [some.class.Name]] ServiceEvent REGISTERED", (Throwable) null);
     }
 
-    @Test @SuppressWarnings("unchecked")
+    @Test
     public void testEarlyExit() throws Exception {
-        Bundle b = Mockito.mock(Bundle.class);
-        Mockito.when(b.getSymbolicName()).thenReturn("bar.bundle");
-        Mockito.when(b.getBundleId()).thenReturn(1L);
-
+        
         ServiceReference sr = Mockito.mock(ServiceReference.class);
-        LogEntry le = new LogEntryImpl(b, sr, LogService.LOG_DEBUG, "test", null);
-
-        StartLevel startLevel = Mockito.mock(StartLevel.class);
-        LogSupport ls = new LogSupport(startLevel);
-        Field lf = LogSupport.class.getDeclaredField("loggers");
-        lf.setAccessible(true);
-        Map<Long, Logger> loggers = (Map<Long, Logger>) lf.get(ls);
+        LogEntry le = new LogEntryImpl(bundle, sr, LogService.LOG_DEBUG, "test", null);
 
-        Logger testLogger = getMockInfoLogger();
-        loggers.put(1L, testLogger);
-
-        ls.fireLogEvent(le);
+        logSupport.fireLogEvent(le);
 
         // The log message is on DEBUG level while the logger is set to INFO level
         // we don't want the actual log.info() call to be made, neither do we want
@@ -101,29 +105,67 @@ public class LogSupportTest {
         Mockito.verifyZeroInteractions(sr);
     }
 
-    @Test @SuppressWarnings("unchecked")
+    @Test
     public void testErrorLogger() throws Exception {
-        Bundle b = Mockito.mock(Bundle.class);
-        Mockito.when(b.getSymbolicName()).thenReturn("bar.bundle");
-        Mockito.when(b.getBundleId()).thenReturn(1L);
-
+        
         Exception e = new Exception();
-        LogEntry le = new LogEntryImpl(b, null, LogService.LOG_ERROR, "my-error-msg", e);
-
-        StartLevel startLevel = Mockito.mock(StartLevel.class);
-        LogSupport ls = new LogSupport(startLevel);
-        Field lf = LogSupport.class.getDeclaredField("loggers");
-        lf.setAccessible(true);
-        Map<Long, Logger> loggers = (Map<Long, Logger>) lf.get(ls);
-
-        Logger testLogger = getMockInfoLogger();
-        loggers.put(1L, testLogger);
-
-        ls.fireLogEvent(le);
+        LogEntry le = new LogEntryImpl(bundle, null, LogService.LOG_ERROR, "my-error-msg", e);
 
+        logSupport.fireLogEvent(le);
+        
         Mockito.verify(testLogger).error("my-error-msg (java.lang.Exception)", e);
     }
+    
+	@Test
+    public void testWarningLogger() throws Exception {
+	    
+        Exception e = new Exception();
+        LogEntry le = new LogEntryImpl(bundle, null, LogService.LOG_WARNING, "my-warning-message", e);
 
+        logSupport.fireLogEvent(le);
+        
+        Mockito.verify(testLogger).warn("my-warning-message (java.lang.Exception)", e);
+    }
+	
+	@Test
+	public void testInfoLogger() throws Exception {
+	    
+        LogEntry le = new LogEntryImpl(bundle, null, LogService.LOG_INFO, "my-info-message", null);
+
+        logSupport.fireLogEvent(le);
+        
+        Mockito.verify(testLogger).info("my-info-message", (Throwable) null);
+	}
+
+	@Test
+	public void testBundleChanges() throws Exception {
+
+        logSupport.bundleChanged(new BundleEvent(BundleEvent.INSTALLED, bundle));
+
+        Mockito.verify(testLogger).info("BundleEvent INSTALLED", (Throwable) null);
+	}
+	
+	@Test
+	public void testFrameworkEventStarted() throws Exception {
+        
+        FrameworkEvent frameworkEvent = new FrameworkEvent(FrameworkEvent.STARTED, bundle, null);
+        
+        logSupport.frameworkEvent(frameworkEvent);
+        
+        Mockito.verify(testLogger).info("FrameworkEvent STARTED", (Throwable) null);
+	}
+	
+	@Test
+	public void testFrameworkEventError() throws Exception {
+
+	    BundleException bundleException = new BundleException("my bundle exception", BundleException.ACTIVATOR_ERROR);
+        FrameworkEvent frameworkEvent = new FrameworkEvent(FrameworkEvent.ERROR, bundle, bundleException);
+        
+        logSupport.frameworkEvent(frameworkEvent);
+        
+        Mockito.verify(testLogger).error("FrameworkEvent ERROR (org.osgi.framework.BundleException: my bundle exception)", bundleException);
+	}
+	
     @Test
     public void testGetLevels() {
         Logger traceLogger = Mockito.mock(Logger.class);