Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 89871 invoked from network); 14 Dec 2006 17:26:09 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 14 Dec 2006 17:26:09 -0000 Received: (qmail 34864 invoked by uid 500); 14 Dec 2006 17:26:17 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 34840 invoked by uid 500); 14 Dec 2006 17:26:17 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 34829 invoked by uid 99); 14 Dec 2006 17:26:17 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Dec 2006 09:26:17 -0800 X-ASF-Spam-Status: No, hits=-8.6 required=10.0 tests=ALL_TRUSTED,INFO_TLD,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Dec 2006 09:26:07 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 4D9B21A981A; Thu, 14 Dec 2006 09:25:22 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r487267 - /directory/sandbox/jconlon/osgi-services/logging-service-integration-test/src/test/java/org/apache/directory/osgi/services/logging/test/LogServiceBundleTest.java Date: Thu, 14 Dec 2006 17:25:22 -0000 To: commits@directory.apache.org From: jconlon@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061214172522.4D9B21A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 Apache Directory Project + * @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); + } + } + } + }