You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by jc...@apache.org on 2006/12/14 18:25:22 UTC

svn commit: r487267 - /directory/sandbox/jconlon/osgi-services/logging-service-integration-test/src/test/java/org/apache/directory/osgi/services/logging/test/LogServiceBundleTest.java

Author: jconlon
Date: Thu Dec 14 09:25:21 2006
New Revision: 487267

URL: http://svn.apache.org/viewvc?view=rev&rev=487267
Log:
Added reflection tests

Modified:
    directory/sandbox/jconlon/osgi-services/logging-service-integration-test/src/test/java/org/apache/directory/osgi/services/logging/test/LogServiceBundleTest.java

Modified: directory/sandbox/jconlon/osgi-services/logging-service-integration-test/src/test/java/org/apache/directory/osgi/services/logging/test/LogServiceBundleTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/jconlon/osgi-services/logging-service-integration-test/src/test/java/org/apache/directory/osgi/services/logging/test/LogServiceBundleTest.java?view=diff&rev=487267&r1=487266&r2=487267
==============================================================================
--- directory/sandbox/jconlon/osgi-services/logging-service-integration-test/src/test/java/org/apache/directory/osgi/services/logging/test/LogServiceBundleTest.java (original)
+++ directory/sandbox/jconlon/osgi-services/logging-service-integration-test/src/test/java/org/apache/directory/osgi/services/logging/test/LogServiceBundleTest.java Thu Dec 14 09:25:21 2006
@@ -15,6 +15,10 @@
  */
 package org.apache.directory.osgi.services.logging.test;
 
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -24,9 +28,9 @@
 import org.springframework.osgi.test.ConfigurableBundleCreatorTests;
 
 
-
 /**
- * Starts up an OSGi environment (equinox, knopflerfish, or felix according to
+ * 
+ * LogServiceBundleTest starts up an OSGi environment (equinox, knopflerfish, or felix according to
  * the profile selected) and installs the simple service bundle and the bundles
  * it depends on. The test classes in this project will be turned into a virtual
  * bundle which is also installed and the tests are then run inside the OSGi
@@ -35,15 +39,18 @@
  * The tests have access to a BundleContext, which we use to test that the
  * LogService was indeed published as an OSGi service.
  * 
- * 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
  */
-public class LogServiceBundleTest extends
-        ConfigurableBundleCreatorTests {
-    /** the log for this class */
-    
+public class LogServiceBundleTest extends ConfigurableBundleCreatorTests
+{
+    private final static int LOGGER_OBJECT_IDX = 0;
+    private final static int LOGGER_METHOD_IDX = 1;
+
     LogService logService;
 
-    
+
     /**
      * The manifest to use for the "virtual bundle" created out of the test
      * classes and resources in this project
@@ -52,10 +59,12 @@
      * import-package added. We should provide a simpler customization point for
      * such use cases that doesn't require duplication of the entire manifest...
      */
-    protected String getManifestLocation() {
+    protected String getManifestLocation()
+    {
         return "classpath:org/apache/directory/osgi/services/logging/test/MANIFEST.MF";
     }
 
+
     /**
      * The location of the packaged OSGi bundles to be installed for this test.
      * Values are Spring resource paths. The bundles we want to use are part of
@@ -68,31 +77,34 @@
      * automatically included so do not need to be specified here.
      * localMavenArtifact("org.apache.directory.osgi.services","logging-service", "1.0.6-SNAPSHOT")
      */
-    protected String[] getBundleLocations() {
-        return new String[] {
-            localMavenArtifact("org.springframework.osgi", "aopalliance.osgi","1.0-SNAPSHOT"),
-            localMavenArtifact("org.springframework.osgi", "spring-context", "2.1-SNAPSHOT"),
-            localMavenArtifact("org.springframework.osgi", "spring-beans","2.1-SNAPSHOT"),
-            localMavenArtifact("org.springframework.osgi", "spring-osgi-core","1.0-SNAPSHOT"),
-            localMavenArtifact("org.springframework.osgi", "spring-aop","2.1-SNAPSHOT"),
-            localMavenArtifact("org.apache.directory.osgi.commons","jzlib-osgi", "1.0.7"),
-            localMavenArtifact("org.apache.felix", "javax.servlet", "0.8.0-SNAPSHOT"),
-            localMavenArtifact("org.apache.felix","org.osgi.compendium", "0.8.0-SNAPSHOT"),
-            localMavenArtifact("org.apache.directory.osgi.services","logging-service", "1.0.6-SNAPSHOT")
-        };
+    protected String[] getBundleLocations()
+    {
+        return new String[]
+            { localMavenArtifact( "org.springframework.osgi", "aopalliance.osgi", "1.0-SNAPSHOT" ),
+                localMavenArtifact( "org.springframework.osgi", "spring-context", "2.1-SNAPSHOT" ),
+                localMavenArtifact( "org.springframework.osgi", "spring-beans", "2.1-SNAPSHOT" ),
+                localMavenArtifact( "org.springframework.osgi", "spring-osgi-core", "1.0-SNAPSHOT" ),
+                localMavenArtifact( "org.springframework.osgi", "spring-aop", "2.1-SNAPSHOT" ),
+                localMavenArtifact( "org.apache.directory.osgi.commons", "jzlib-osgi", "1.0.7" ),
+                localMavenArtifact( "org.apache.felix", "javax.servlet", "0.8.0-SNAPSHOT" ),
+                localMavenArtifact( "org.apache.felix", "org.osgi.compendium", "0.8.0-SNAPSHOT" ),
+                localMavenArtifact( "org.apache.directory.osgi.services", "logging-service", "1.0.6-SNAPSHOT" ) };
     }
 
+
     /**
      * The superclass provides us access to the root bundle context via the
      * 'getBundleContext' operation
      */
-    public void testOSGiStartedOk() {
+    public void testOSGiStartedOk()
+    {
         BundleContext bundleContext = getBundleContext();
-        assertNotNull(bundleContext);
-        
-        assertEquals(17,bundleContext.getBundles().length);
+        assertNotNull( bundleContext );
+
+        assertEquals( 17, bundleContext.getBundles().length );
     }
 
+
     /**
      * The LogService should have been exported as an OSGi service, which we
      * can verify using the OSGi service APIs.
@@ -101,33 +113,104 @@
      * reference to a published service.
      * 
      */
-    public void testLogService() {
+    public void testLogService()
+    {
         BundleContext context = getBundleContext();
-        ServiceReference ref = context.getServiceReference(LogService.class
-                .getName());
-        assertNotNull("Service Reference is null", ref);
-        
-        try {
-            logService = (LogService) context.getService(ref);
-            assertNotNull("Cannot find the service", logService);
-            logService.log(LogService.LOG_WARNING,"Found LogService. ");
+        ServiceReference ref = context.getServiceReference( LogService.class.getName() );
+        assertNotNull( "Service Reference is null", ref );
+
+        try
+        {
+            logService = ( LogService ) context.getService( ref );
+            assertNotNull( "Cannot find the service", logService );
+            logService.log( LogService.LOG_WARNING, "Found LogService. " );
             Bundle[] bundles = context.getBundles();
-            logService.log(LogService.LOG_DEBUG, "Loaded bundles:");
-            for (Bundle bundle : bundles) {
-                logService.log(LogService.LOG_DEBUG, bundle.getSymbolicName());
+            logService.log( LogService.LOG_DEBUG, "Loaded bundles:" );
+            for ( Bundle bundle : bundles )
+            {
+                logService.log( LogService.LOG_DEBUG, bundle.getSymbolicName() );
             }
-        } finally {
-            context.ungetService(ref);
         }
-       Logger log = LoggerFactory
-       .getLogger(LogServiceBundleTest.class);;
-      
-      log.info("A slf4j logger works too.");
-  }
+        finally
+        {
+            context.ungetService( ref );
+        }
+        Logger log = LoggerFactory.getLogger( LogServiceBundleTest.class );;
+
+        log.info( "A slf4j logger works too." );
+    }
     
+    public void testLogServiceReflection()
+    {
+        BundleContext context = getBundleContext();
+        ServiceReference ref = context.getServiceReference( LogService.class.getName() );
+        assertNotNull( "Service Reference is null", ref );
+
+        try
+        {
+           Object logObj = context.getService( ref );
+            assertNotNull( "Cannot find the service", logObj );
+            
+            Class[] formalParams = {
+                ServiceReference.class,
+                Integer.TYPE,
+                String.class,
+                Throwable.class
+            };
+            
+            Method logMethod = null;
+            try
+            {
+                logMethod = logObj.getClass().getMethod("log", formalParams);
+            }
+            catch ( SecurityException e )
+            {
+                fail(e.toString());
+            }
+            catch ( NoSuchMethodException e )
+            {
+                fail(e.toString());
+            }
+            assertNotNull(logMethod);
+            logMethod.setAccessible(true);
+            Object[] m_logger = new Object[] { logObj, logMethod };
+            
+            _logReflectively(m_logger, ref, 2, "Some message without nulls", new Exception("Something easy to fix"));
+            
+            _logReflectively(m_logger, null, 2, "Some message with null service reference", new Exception("Something easy to fix"));
+            
+            _logReflectively(m_logger, null, 2, "Some message with null service reference and null exception", null);
+        }
+        finally
+        {
+            context.ungetService( ref );
+        }
+        
+    }
     
-  
- 
-  
- 
+    private void _logReflectively(
+        Object[] logger, ServiceReference sr, int level, String msg, Throwable throwable)
+    {
+        if (logger != null)
+        {
+            Object[] params = {
+                sr, new Integer(level), msg, throwable
+            };
+            try
+            {
+                ((Method) logger[LOGGER_METHOD_IDX]).invoke(logger[LOGGER_OBJECT_IDX], params);
+             
+                
+            }
+            catch (InvocationTargetException ex)
+            {
+                System.err.println("Logger: " + ex);
+            }
+            catch (IllegalAccessException ex)
+            {
+                System.err.println("Logger: " + ex);
+            }
+        }
+    }
+
 }