You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ga...@apache.org on 2009/10/07 04:48:18 UTC
svn commit: r822563 - in /incubator/aries/trunk: ./ jndi/ jndi/jndi-core/
jndi/jndi-core/src/test/java/org/apache/aries/jndi/ jndi/jndi-url/
jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/
Author: gawor
Date: Wed Oct 7 02:48:17 2009
New Revision: 822563
URL: http://svn.apache.org/viewvc?rev=822563&view=rev
Log:
enable disabled jndi tests
Modified:
incubator/aries/trunk/jndi/jndi-core/pom.xml
incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
incubator/aries/trunk/jndi/jndi-url/pom.xml
incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceHelperTest.java
incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java
incubator/aries/trunk/jndi/pom.xml
incubator/aries/trunk/pom.xml
Modified: incubator/aries/trunk/jndi/jndi-core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/pom.xml?rev=822563&r1=822562&r2=822563&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/pom.xml (original)
+++ incubator/aries/trunk/jndi/jndi-core/pom.xml Wed Oct 7 02:48:17 2009
@@ -37,6 +37,11 @@
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.aries.testsupport</groupId>
+ <artifactId>aries-testsupport-unit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
Modified: incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java?rev=822563&r1=822562&r2=822563&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java (original)
+++ incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java Wed Oct 7 02:48:17 2009
@@ -1,171 +1,170 @@
-///*
-// * Licensed to the Apache Software Foundation (ASF) under one
-// * or more contributor license agreements. See the NOTICE file
-// * distributed with this work for additional information
-// * regarding copyright ownership. The ASF licenses this file
-// * to you under the Apache License, Version 2.0 (the
-// * "License"); you may not use this file except in compliance
-// * with the License. You may obtain a copy of the License at
-// *
-// * http://www.apache.org/licenses/LICENSE-2.0
-// *
-// * Unless required by applicable law or agreed to in writing,
-// * software distributed under the License is distributed on an
-// * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
-// * KIND, either express or implied. See the License for the
-// * specific language governing permissions and limitations
-// * under the License.
-// */
-//package org.apache.aries.jndi;
-//
-//import static org.junit.Assert.assertEquals;
-//import static org.junit.Assert.assertSame;
-//
-//import java.lang.reflect.Field;
-//import java.util.Hashtable;
-//import java.util.Properties;
-//
-//import javax.naming.Context;
-//import javax.naming.Name;
-//import javax.naming.Reference;
-//import javax.naming.StringRefAddr;
-//import javax.naming.spi.NamingManager;
-//import javax.naming.spi.ObjectFactory;
-//
-//import mocks.BundleContextMock;
-//
-//import org.junit.After;
-//import org.junit.Before;
-//import org.junit.Test;
-//import org.osgi.framework.BundleContext;
-//
-//import com.ibm.aries.unittest.mocks.MethodCall;
-//import com.ibm.aries.unittest.mocks.Skeleton;
-//import org.apache.aries.jndi.ContextHelper;
-//import org.apache.aries.jndi.OSGiObjectFactoryBuilder;
-//import org.apache.aries.jndi.startup.Activator;
-//
-//public class ObjectFactoryTest
-//{
-// private BundleContext bc;
-//
-// /**
-// * This method does the setup .
-// * @throws NoSuchFieldException
-// * @throws SecurityException
-// * @throws IllegalAccessException
-// * @throws IllegalArgumentException
-// */
-// @Before
-// public void setup() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
-// {
-// bc = Skeleton.newMock(new BundleContextMock(), BundleContext.class);
-// new Activator().start(bc);
-//
-// Field f = ContextHelper.class.getDeclaredField("context");
-// f.setAccessible(true);
-// f.set(null, bc);
-// f = OSGiObjectFactoryBuilder.class.getDeclaredField("context");
-// f.setAccessible(true);
-// f.set(null, bc);
-//
-// }
-//
-// /**
-// * Make sure we clear the caches out before the next test.
-// */
-// @After
-// public void teardown()
-// {
-// new Activator().stop(bc);
-// BundleContextMock.clear();
-// }
-//
-// @Test
-// public void testURLReferenceWithNoMatchingHandler() throws Exception
-// {
-// Reference ref = new Reference(null);
-// ref.add(new StringRefAddr("URL", "wibble"));
-// Object obj = NamingManager.getObjectInstance(ref, null, null, null);
-//
-// assertSame("The naming manager should have returned the reference object", ref, obj);
-// }
-//
-// @Test
-// public void testURLReferenceWithMatchingHandler() throws Exception
-// {
-// String testObject = "Test object";
-// ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
-// Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
-//
-// Properties props = new Properties();
-// props.setProperty("osgi.jndi.urlScheme", "wibble");
-//
-// bc.registerService(ObjectFactory.class.getName(), factory, props);
-//
-// Reference ref = new Reference(null);
-// ref.add(new StringRefAddr("URL", "wibble"));
-// Object obj = NamingManager.getObjectInstance(ref, null, null, null);
-//
-// assertEquals("The naming manager should have returned the test object", testObject, obj);
-// }
-//
-// @Test
-// public void testReferenceWithNoClassName() throws Exception
-// {
-// String testObject = "Test object";
-// ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
-// Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
-//
-// bc.registerService(ObjectFactory.class.getName(), factory, null);
-//
-// Reference ref = new Reference(null);
-// Object obj = NamingManager.getObjectInstance(ref, null, null, null);
-//
-// assertEquals("The naming manager should have returned the test object", testObject, obj);
-// }
-//
-// @Test
-// public void testSpecifiedFactoryWithMatchingFactory() throws Exception
-// {
-// String testObject = "Test object";
-// ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
-// Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
-//
-// Reference ref = new Reference("dummy.class.name", factory.getClass().getName(), "");
-//
-// bc.registerService(new String[] {ObjectFactory.class.getName(), factory.getClass().getName()},
-// factory, null);
-//
-// Object obj = NamingManager.getObjectInstance(ref, null, null, null);
-//
-// assertEquals("The naming manager should have returned the test object", testObject, obj);
-// }
-//
-// @Test
-// public void testSpecifiedFactoryWithRegisteredButNotMatchingFactory() throws Exception
-// {
-// String testObject = "Test object";
-// ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
-// Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
-//
-// Reference ref = new Reference("dummy.class.name", "dummy.factory.class.name", "");
-//
-// bc.registerService(new String[] {ObjectFactory.class.getName(), factory.getClass().getName()},
-// factory, null);
-//
-// Object obj = NamingManager.getObjectInstance(ref, null, null, null);
-//
-// assertSame("The naming manager should have returned the reference object", ref, obj);
-// }
-//
-// @Test
-// public void testSpecifiedFactoryWithNoMatchingFactory() throws Exception
-// {
-// Reference ref = new Reference("dummy.class.name");
-//
-// Object obj = NamingManager.getObjectInstance(ref, null, null, null);
-//
-// assertSame("The naming manager should have returned the reference object", ref, obj);
-// }
-//}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.jndi;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+import java.lang.reflect.Field;
+import java.util.Hashtable;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
+import javax.naming.spi.NamingManager;
+import javax.naming.spi.ObjectFactory;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+
+import org.apache.aries.unittest.mocks.MethodCall;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.apache.aries.jndi.ContextHelper;
+import org.apache.aries.jndi.OSGiObjectFactoryBuilder;
+import org.apache.aries.jndi.startup.Activator;
+import org.apache.aries.mocks.BundleContextMock;
+
+public class ObjectFactoryTest
+{
+ private BundleContext bc;
+
+ /**
+ * This method does the setup .
+ * @throws NoSuchFieldException
+ * @throws SecurityException
+ * @throws IllegalAccessException
+ * @throws IllegalArgumentException
+ */
+ @Before
+ public void setup() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
+ {
+ bc = Skeleton.newMock(new BundleContextMock(), BundleContext.class);
+ new Activator().start(bc);
+
+ Field f = ContextHelper.class.getDeclaredField("context");
+ f.setAccessible(true);
+ f.set(null, bc);
+ f = OSGiObjectFactoryBuilder.class.getDeclaredField("context");
+ f.setAccessible(true);
+ f.set(null, bc);
+
+ }
+
+ /**
+ * Make sure we clear the caches out before the next test.
+ */
+ @After
+ public void teardown()
+ {
+ new Activator().stop(bc);
+ BundleContextMock.clear();
+ }
+
+ @Test
+ public void testURLReferenceWithNoMatchingHandler() throws Exception
+ {
+ Reference ref = new Reference(null);
+ ref.add(new StringRefAddr("URL", "wibble"));
+ Object obj = NamingManager.getObjectInstance(ref, null, null, null);
+
+ assertSame("The naming manager should have returned the reference object", ref, obj);
+ }
+
+ @Test
+ public void testURLReferenceWithMatchingHandler() throws Exception
+ {
+ String testObject = "Test object";
+ ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
+ Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
+
+ Properties props = new Properties();
+ props.setProperty("osgi.jndi.urlScheme", "wibble");
+
+ bc.registerService(ObjectFactory.class.getName(), factory, props);
+
+ Reference ref = new Reference(null);
+ ref.add(new StringRefAddr("URL", "wibble"));
+ Object obj = NamingManager.getObjectInstance(ref, null, null, null);
+
+ assertEquals("The naming manager should have returned the test object", testObject, obj);
+ }
+
+ @Test
+ public void testReferenceWithNoClassName() throws Exception
+ {
+ String testObject = "Test object";
+ ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
+ Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
+
+ bc.registerService(ObjectFactory.class.getName(), factory, null);
+
+ Reference ref = new Reference(null);
+ Object obj = NamingManager.getObjectInstance(ref, null, null, null);
+
+ assertEquals("The naming manager should have returned the test object", testObject, obj);
+ }
+
+ @Test
+ public void testSpecifiedFactoryWithMatchingFactory() throws Exception
+ {
+ String testObject = "Test object";
+ ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
+ Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
+
+ Reference ref = new Reference("dummy.class.name", factory.getClass().getName(), "");
+
+ bc.registerService(new String[] {ObjectFactory.class.getName(), factory.getClass().getName()},
+ factory, null);
+
+ Object obj = NamingManager.getObjectInstance(ref, null, null, null);
+
+ assertEquals("The naming manager should have returned the test object", testObject, obj);
+ }
+
+ @Test
+ public void testSpecifiedFactoryWithRegisteredButNotMatchingFactory() throws Exception
+ {
+ String testObject = "Test object";
+ ObjectFactory factory = Skeleton.newMock(ObjectFactory.class);
+ Skeleton.getSkeleton(factory).setReturnValue(new MethodCall(ObjectFactory.class, "getObjectInstance", Object.class, Name.class, Context.class, Hashtable.class), testObject);
+
+ Reference ref = new Reference("dummy.class.name", "dummy.factory.class.name", "");
+
+ bc.registerService(new String[] {ObjectFactory.class.getName(), factory.getClass().getName()},
+ factory, null);
+
+ Object obj = NamingManager.getObjectInstance(ref, null, null, null);
+
+ assertSame("The naming manager should have returned the reference object", ref, obj);
+ }
+
+ @Test
+ public void testSpecifiedFactoryWithNoMatchingFactory() throws Exception
+ {
+ Reference ref = new Reference("dummy.class.name");
+
+ Object obj = NamingManager.getObjectInstance(ref, null, null, null);
+
+ assertSame("The naming manager should have returned the reference object", ref, obj);
+ }
+}
\ No newline at end of file
Modified: incubator/aries/trunk/jndi/jndi-url/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/pom.xml?rev=822563&r1=822562&r2=822563&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/pom.xml (original)
+++ incubator/aries/trunk/jndi/jndi-url/pom.xml Wed Oct 7 02:48:17 2009
@@ -42,6 +42,11 @@
<artifactId>aries-jndi-core</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.aries.testsupport</groupId>
+ <artifactId>aries-testsupport-unit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
Modified: incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceHelperTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceHelperTest.java?rev=822563&r1=822562&r2=822563&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceHelperTest.java (original)
+++ incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceHelperTest.java Wed Oct 7 02:48:17 2009
@@ -1,210 +1,209 @@
-///*
-// * Licensed to the Apache Software Foundation (ASF) under one
-// * or more contributor license agreements. See the NOTICE file
-// * distributed with this work for additional information
-// * regarding copyright ownership. The ASF licenses this file
-// * to you under the Apache License, Version 2.0 (the
-// * "License"); you may not use this file except in compliance
-// * with the License. You may obtain a copy of the License at
-// *
-// * http://www.apache.org/licenses/LICENSE-2.0
-// *
-// * Unless required by applicable law or agreed to in writing,
-// * software distributed under the License is distributed on an
-// * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
-// * KIND, either express or implied. See the License for the
-// * specific language governing permissions and limitations
-// * under the License.
-// */
-//package org.apache.aries.jndi.url;
-//
-//import static org.junit.Assert.assertEquals;
-//import static org.junit.Assert.assertFalse;
-//import static org.junit.Assert.assertNotNull;
-//import static org.junit.Assert.assertTrue;
-//
-//import java.lang.reflect.Field;
-//import java.util.Hashtable;
-//import java.util.List;
-//import java.util.Properties;
-//
-//import mocks.BundleContextMock;
-//import mocks.BundleMock;
-//
-//import org.junit.After;
-//import org.junit.Before;
-//import org.junit.Test;
-//import org.osgi.framework.Bundle;
-//import org.osgi.framework.BundleContext;
-//import org.osgi.framework.ServiceFactory;
-//import org.osgi.framework.ServiceReference;
-//import org.osgi.framework.ServiceRegistration;
-//
-//import com.ibm.aries.unittest.mocks.MethodCall;
-//import com.ibm.aries.unittest.mocks.Skeleton;
-//import org.apache.aries.jndi.services.ServiceHelper;
-//import org.apache.aries.jndi.url.Activator;
-//
-//
-///**
-// * This class contains tests for the ServiceHelper
-// */
-//public class ServiceHelperTest
-//{
-// /** The service we register by default */
-// private Thread service;
-// /** The bundle context for the test */
-// private BundleContext bc;
-//
-// /**
-// * This method does the setup to ensure we always have a service.
-// * @throws NoSuchFieldException
-// * @throws SecurityException
-// * @throws IllegalAccessException
-// * @throws IllegalArgumentException
-// */
-// @Before
-// public void registerService() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
-// {
-//
-// bc = Skeleton.newMock(new BundleContextMock(), BundleContext.class);
-// new Activator().start(bc);
-//
-// Field f = ServiceHelper.class.getDeclaredField("context");
-// f.setAccessible(true);
-// f.set(null, bc);
-//
-// service = new Thread();
-//
-// registerService(service);
-// }
-//
-// /**
-// * Register a service in our map.
-// *
-// * @param service2 The service to register.
-// */
-// private void registerService(Thread service2)
-// {
-// ServiceFactory factory = Skeleton.newMock(ServiceFactory.class);
-// Skeleton skel = Skeleton.getSkeleton(factory);
-//
-// skel.setReturnValue(new MethodCall(ServiceFactory.class, "getService", Bundle.class, ServiceRegistration.class), service2);
-//
-// bc.registerService(new String[] {"java.lang.Runnable"}, factory, new Hashtable<String, String>());
-// }
-//
-// /**
-// * Make sure we clear the caches out before the next test.
-// */
-// @After
-// public void teardown()
-// {
-// BundleContextMock.clear();
-// }
-//
-// /**
-// * Check that the basic behaviour is correct. Do we call ungetService, do
-// * we call getService the right number of times.
-// */
-// @Test
-// public void getAService()
-// {
-// Bundle b = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
-//
-// Skeleton skel = Skeleton.getSkeleton(b);
-//
-// Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
-//
-// skel = Skeleton.getSkeleton(b.getBundleContext());
-//
-// Object retrievedService = ServiceHelper.getService("java.lang.Runnable", null);
-//
-// assertNotNull("We could not locate the service in the registry", retrievedService);
-//
-// assertTrue("We didn't get back the service we expected", service == retrievedService);
-//
-// MethodCall getService = new MethodCall(BundleContext.class, "getService", ServiceReference.class);
-// MethodCall ungetService = new MethodCall(BundleContext.class, "ungetService", ServiceReference.class);
-//
-// skel.assertNotCalled(ungetService);
-// skel.assertCalledExactNumberOfTimes(getService, 1);
-//
-// Object retrievedService2 = ServiceHelper.getService("java.lang.Runnable", null);
-//
-// assertTrue("We got different objects, which we did not want", retrievedService == retrievedService2);
-// skel.assertCalledExactNumberOfTimes(getService, 2);
-// skel.assertCalledExactNumberOfTimes(ungetService, 1);
-// }
-//
-// /**
-// * This method checks that we get two different services from different
-// * bundles if the thread context classloader is different.
-// */
-// @Test
-// public void getAServiceFromTwoDifferentApplications()
-// {
-// Bundle b = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
-//
-// Skeleton skel = Skeleton.getSkeleton(b);
-//
-// Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
-//
-// Object retrievedService = ServiceHelper.getService("java.lang.Runnable", null);
-// Bundle b2 = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
-//
-// skel = Skeleton.getSkeleton(b2);
-//
-// Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
-// Object retrievedService2 = ServiceHelper.getService("java.lang.Runnable", null);
-//
-// assertNotNull("We could not locate the service in the registry", retrievedService);
-// assertNotNull("We could not locate the service in the registry", retrievedService2);
-//
-// assertTrue("We got different objects, which we did not want", retrievedService == retrievedService2);
-//
-// assertFalse("We expected different bundles from our calls to the BundleMaker, but we got the same one", b == b2);
-//
-// MethodCall getService = new MethodCall(BundleContext.class, "getService", ServiceReference.class);
-// Skeleton.getSkeleton(b.getBundleContext()).assertCalledExactNumberOfTimes(getService, 1);
-// Skeleton.getSkeleton(b2.getBundleContext()).assertCalledExactNumberOfTimes(getService, 1);
-// }
-//
-// /**
-// * This test checks that the getServices method returns the expected number of
-// * services, and that it changes when new services are registered.
-// */
-// @Test
-// public void getMultipleServices()
-// {
-// Bundle b = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
-//
-// Skeleton skel = Skeleton.getSkeleton(b);
-//
-// Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
-//
-// MethodCall getService = new MethodCall(BundleContext.class, "getService", ServiceReference.class);
-// MethodCall ungetService = new MethodCall(BundleContext.class, "ungetService", ServiceReference.class);
-//
-// List<?> services = ServiceHelper.getServices("java.lang.Runnable", null);
-//
-// skel = Skeleton.getSkeleton(b.getBundleContext());
-//
-// skel.assertNotCalled(ungetService);
-// skel.assertCalledExactNumberOfTimes(getService, 1);
-//
-// assertEquals("At this point we really should only have one service.", 1, services.size());
-// Thread anotherService = new Thread();
-// registerService(anotherService);
-//
-// services = ServiceHelper.getServices("java.lang.Runnable", null);
-//
-// assertEquals("At this point we really should have two services.", 2, services.size());
-// assertTrue("The master service was not there, odd.", services.contains(service));
-// assertTrue("The service we added just for this test was not there, odd.", services.contains(anotherService));
-//
-// // this should be 3 times, once for the first call to getServices, and twice for the second
-// skel.assertCalledExactNumberOfTimes(getService, 3);
-// skel.assertCalledExactNumberOfTimes(ungetService, 1);
-// }
-//}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.jndi.url;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Field;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+import org.apache.aries.unittest.mocks.MethodCall;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.apache.aries.jndi.services.ServiceHelper;
+import org.apache.aries.jndi.url.Activator;
+import org.apache.aries.mocks.BundleContextMock;
+import org.apache.aries.mocks.BundleMock;
+
+
+/**
+ * This class contains tests for the ServiceHelper
+ */
+public class ServiceHelperTest
+{
+ /** The service we register by default */
+ private Thread service;
+ /** The bundle context for the test */
+ private BundleContext bc;
+
+ /**
+ * This method does the setup to ensure we always have a service.
+ * @throws NoSuchFieldException
+ * @throws SecurityException
+ * @throws IllegalAccessException
+ * @throws IllegalArgumentException
+ */
+ @Before
+ public void registerService() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
+ {
+
+ bc = Skeleton.newMock(new BundleContextMock(), BundleContext.class);
+ new Activator().start(bc);
+
+ Field f = ServiceHelper.class.getDeclaredField("context");
+ f.setAccessible(true);
+ f.set(null, bc);
+
+ service = new Thread();
+
+ registerService(service);
+ }
+
+ /**
+ * Register a service in our map.
+ *
+ * @param service2 The service to register.
+ */
+ private void registerService(Thread service2)
+ {
+ ServiceFactory factory = Skeleton.newMock(ServiceFactory.class);
+ Skeleton skel = Skeleton.getSkeleton(factory);
+
+ skel.setReturnValue(new MethodCall(ServiceFactory.class, "getService", Bundle.class, ServiceRegistration.class), service2);
+
+ bc.registerService(new String[] {"java.lang.Runnable"}, factory, new Hashtable<String, String>());
+ }
+
+ /**
+ * Make sure we clear the caches out before the next test.
+ */
+ @After
+ public void teardown()
+ {
+ BundleContextMock.clear();
+ }
+
+ /**
+ * Check that the basic behaviour is correct. Do we call ungetService, do
+ * we call getService the right number of times.
+ */
+ @Test
+ public void getAService()
+ {
+ Bundle b = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
+
+ Skeleton skel = Skeleton.getSkeleton(b);
+
+ Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
+
+ skel = Skeleton.getSkeleton(b.getBundleContext());
+
+ Object retrievedService = ServiceHelper.getService("java.lang.Runnable", null);
+
+ assertNotNull("We could not locate the service in the registry", retrievedService);
+
+ assertTrue("We didn't get back the service we expected", service == retrievedService);
+
+ MethodCall getService = new MethodCall(BundleContext.class, "getService", ServiceReference.class);
+ MethodCall ungetService = new MethodCall(BundleContext.class, "ungetService", ServiceReference.class);
+
+ skel.assertNotCalled(ungetService);
+ skel.assertCalledExactNumberOfTimes(getService, 1);
+
+ Object retrievedService2 = ServiceHelper.getService("java.lang.Runnable", null);
+
+ assertTrue("We got different objects, which we did not want", retrievedService == retrievedService2);
+ skel.assertCalledExactNumberOfTimes(getService, 2);
+ skel.assertCalledExactNumberOfTimes(ungetService, 1);
+ }
+
+ /**
+ * This method checks that we get two different services from different
+ * bundles if the thread context classloader is different.
+ */
+ @Test
+ public void getAServiceFromTwoDifferentApplications()
+ {
+ Bundle b = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
+
+ Skeleton skel = Skeleton.getSkeleton(b);
+
+ Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
+
+ Object retrievedService = ServiceHelper.getService("java.lang.Runnable", null);
+ Bundle b2 = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
+
+ skel = Skeleton.getSkeleton(b2);
+
+ Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
+ Object retrievedService2 = ServiceHelper.getService("java.lang.Runnable", null);
+
+ assertNotNull("We could not locate the service in the registry", retrievedService);
+ assertNotNull("We could not locate the service in the registry", retrievedService2);
+
+ assertTrue("We got different objects, which we did not want", retrievedService == retrievedService2);
+
+ assertFalse("We expected different bundles from our calls to the BundleMaker, but we got the same one", b == b2);
+
+ MethodCall getService = new MethodCall(BundleContext.class, "getService", ServiceReference.class);
+ Skeleton.getSkeleton(b.getBundleContext()).assertCalledExactNumberOfTimes(getService, 1);
+ Skeleton.getSkeleton(b2.getBundleContext()).assertCalledExactNumberOfTimes(getService, 1);
+ }
+
+ /**
+ * This test checks that the getServices method returns the expected number of
+ * services, and that it changes when new services are registered.
+ */
+ @Test
+ public void getMultipleServices()
+ {
+ Bundle b = Skeleton.newMock(new BundleMock("scooby.doo", new Properties()), Bundle.class);
+
+ Skeleton skel = Skeleton.getSkeleton(b);
+
+ Thread.currentThread().setContextClassLoader(((BundleMock)skel.getTemplateObject()).getClassLoader());
+
+ MethodCall getService = new MethodCall(BundleContext.class, "getService", ServiceReference.class);
+ MethodCall ungetService = new MethodCall(BundleContext.class, "ungetService", ServiceReference.class);
+
+ List<?> services = ServiceHelper.getServices("java.lang.Runnable", null);
+
+ skel = Skeleton.getSkeleton(b.getBundleContext());
+
+ skel.assertNotCalled(ungetService);
+ skel.assertCalledExactNumberOfTimes(getService, 1);
+
+ assertEquals("At this point we really should only have one service.", 1, services.size());
+ Thread anotherService = new Thread();
+ registerService(anotherService);
+
+ services = ServiceHelper.getServices("java.lang.Runnable", null);
+
+ assertEquals("At this point we really should have two services.", 2, services.size());
+ assertTrue("The master service was not there, odd.", services.contains(service));
+ assertTrue("The service we added just for this test was not there, odd.", services.contains(anotherService));
+
+ // this should be 3 times, once for the first call to getServices, and twice for the second
+ skel.assertCalledExactNumberOfTimes(getService, 3);
+ skel.assertCalledExactNumberOfTimes(ungetService, 1);
+ }
+}
\ No newline at end of file
Modified: incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java?rev=822563&r1=822562&r2=822563&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java (original)
+++ incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java Wed Oct 7 02:48:17 2009
@@ -1,354 +1,353 @@
-///*
-// * Licensed to the Apache Software Foundation (ASF) under one
-// * or more contributor license agreements. See the NOTICE file
-// * distributed with this work for additional information
-// * regarding copyright ownership. The ASF licenses this file
-// * to you under the Apache License, Version 2.0 (the
-// * "License"); you may not use this file except in compliance
-// * with the License. You may obtain a copy of the License at
-// *
-// * http://www.apache.org/licenses/LICENSE-2.0
-// *
-// * Unless required by applicable law or agreed to in writing,
-// * software distributed under the License is distributed on an
-// * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
-// * KIND, either express or implied. See the License for the
-// * specific language governing permissions and limitations
-// * under the License.
-// */
-//package org.apache.aries.jndi.url;
-//
-//import static org.junit.Assert.assertEquals;
-//import static org.junit.Assert.assertFalse;
-//import static org.junit.Assert.assertNotNull;
-//import static org.junit.Assert.assertTrue;
-//
-//import java.lang.reflect.Field;
-//import java.util.Hashtable;
-//import java.util.Properties;
-//
-//import javax.naming.Binding;
-//import javax.naming.Context;
-//import javax.naming.InitialContext;
-//import javax.naming.NameClassPair;
-//import javax.naming.NameNotFoundException;
-//import javax.naming.NamingEnumeration;
-//import javax.naming.NamingException;
-//import javax.naming.spi.ObjectFactory;
-//
-//import mocks.BundleContextMock;
-//import mocks.BundleMock;
-//
-//import org.junit.After;
-//import org.junit.Before;
-//import org.junit.Test;
-//import org.osgi.framework.Bundle;
-//import org.osgi.framework.BundleContext;
-//import org.osgi.framework.ServiceFactory;
-//import org.osgi.framework.ServiceReference;
-//import org.osgi.framework.ServiceRegistration;
-//
-//import com.ibm.aries.unittest.mocks.MethodCall;
-//import com.ibm.aries.unittest.mocks.Skeleton;
-//import org.apache.aries.jndi.ContextHelper;
-//import org.apache.aries.jndi.OSGiObjectFactoryBuilder;
-//import org.apache.aries.jndi.services.ServiceHelper;
-//import org.apache.aries.jndi.url.Activator;
-//
-///**
-// * Tests for our JNDI implementation for the service registry.
-// */
-//public class ServiceRegistryContextTest
-//{
-// /** The service we register by default */
-// private Thread service;
-// /** The bundle context for the test */
-// private BundleContext bc;
-// /** The service registration for the service */
-// private ServiceRegistration reg;
-//
-// /**
-// * This method does the setup to ensure we always have a service.
-// * @throws NamingException
-// * @throws NoSuchFieldException
-// * @throws SecurityException
-// * @throws IllegalAccessException
-// * @throws IllegalArgumentException
-// */
-// @Before
-// public void registerService() throws NamingException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
-// {
-// bc = Skeleton.newMock(new BundleContextMock(), BundleContext.class);
-// new Activator().start(bc);
-// new com.ibm.osgi.jndi.startup.Activator().start(bc);
-//
-// Field f = ServiceHelper.class.getDeclaredField("context");
-// f.setAccessible(true);
-// f.set(null, bc);
-// f = ContextHelper.class.getDeclaredField("context");
-// f.setAccessible(true);
-// f.set(null, bc);
-// f = OSGiObjectFactoryBuilder.class.getDeclaredField("context");
-// f.setAccessible(true);
-// f.set(null, bc);
-//
-//
-// service = new Thread();
-//
-// registerService(service);
-// }
-//
-// /**
-// * Register a service in our map.
-// *
-// * @param service2 The service to register.
-// */
-// private void registerService(Thread service2)
-// {
-// ServiceFactory factory = Skeleton.newMock(ServiceFactory.class);
-// Skeleton skel = Skeleton.getSkeleton(factory);
-//
-// skel.setReturnValue(new MethodCall(ServiceFactory.class, "getService", Bundle.class, ServiceRegistration.class), service2);
-//
-// Hashtable<String, String> props = new Hashtable<String, String>();
-// props.put("rubbish", "smelly");
-//
-// reg = bc.registerService(new String[] {"java.lang.Runnable"}, factory, props);
-// }
-//
-// /**
-// * Make sure we clear the caches out before the next test.
-// */
-// @After
-// public void teardown()
-// {
-// BundleContextMock.clear();
-//
-// Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
-// }
-//
-// /**
-// * This test checks that we correctly register and deregister the url context
-// * object factory in the service registry.
-// */
-// @Test
-// public void testJNDIRegistration()
-// {
-// ServiceReference ref = bc.getServiceReference(ObjectFactory.class.getName());
-//
-// assertNotNull("The aries url context object factory was not registered", ref);
-//
-// ObjectFactory factory = (ObjectFactory) bc.getService(ref);
-//
-// assertNotNull("The aries url context object factory was null", factory);
-// }
-//
-// /**
-// * This test does a simple JNDI lookup to prove that works.
-// * @throws NamingException
-// */
-// @Test
-// public void simpleJNDILookup() throws NamingException
-// {
-// System.setProperty(Context.URL_PKG_PREFIXES, "helloMatey");
-//
-// InitialContext ctx = new InitialContext(new Hashtable<Object, Object>());
-//
-// BundleMock mock = new BundleMock("scooby.doo", new Properties());
-//
-// Thread.currentThread().setContextClassLoader(mock.getClassLoader());
-//
-// Object s = ctx.lookup("aries:services/java.lang.Runnable");
-//
-// assertNotNull("We didn't get a service back from our lookup :(", s);
-//
-// assertEquals("The SR did not return the object we expected", service, s);
-//
-// Skeleton skel = Skeleton.getSkeleton(mock.getBundleContext());
-//
-// skel.assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", null));
-//
-// mock = new BundleMock("scooby.doo", new Properties());
-//
-// Thread.currentThread().setContextClassLoader(mock.getClassLoader());
-//
-// s = ctx.lookup("osgi:services/java.lang.Runnable");
-//
-// // Check we have the packages set correctly
-//
-// String packages = System.getProperty(Context.URL_PKG_PREFIXES, null);
-//
-// assertTrue(ctx.getEnvironment().containsValue(packages));
-//
-//
-// assertNotNull("We didn't get a service back from our lookup :(", s);
-//
-// assertEquals("The SR did not return the object we expected", service, s);
-//
-// skel = Skeleton.getSkeleton(mock.getBundleContext());
-// skel.assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", null));
-// }
-//
-// /**
-// * This test checks that we can pass a filter in without things blowing up.
-// * Right now our mock service registry does not implement filtering, so the
-// * effect is the same as simpleJNDILookup, but we at least know it is not
-// * blowing up.
-// *
-// * @throws NamingException
-// */
-// @Test
-// public void jndiLookupWithFilter() throws NamingException
-// {
-// InitialContext ctx = new InitialContext();
-//
-// Object s = ctx.lookup("aries:services/java.lang.Runnable/(rubbish=smelly)");
-//
-// assertNotNull("We didn't get a service back from our lookup :(", s);
-//
-// assertEquals("The SR did not return the object we expected", service, s);
-//
-// Skeleton.getSkeleton(bc).assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", "(rubbish=smelly)"));
-// }
-//
-// /**
-// * Check that we get a NameNotFoundException if we lookup after the service
-// * has been unregistered.
-// *
-// * @throws NamingException
-// */
-// @Test(expected=NameNotFoundException.class)
-// public void testLookupWhenServiceHasBeenRemoved() throws NamingException
-// {
-// reg.unregister();
-// InitialContext ctx = new InitialContext();
-//
-// ctx.lookup("aries:services/java.lang.Runnable");
-// }
-//
-// /**
-// * Check that we get a NameNotFoundException if we lookup something not in the
-// * registry.
-// *
-// * @throws NamingException
-// */
-// @Test(expected=NameNotFoundException.class)
-// public void testLookupForServiceWeNeverHad() throws NamingException
-// {
-// InitialContext ctx = new InitialContext();
-//
-// ctx.lookup("aries:services/java.lang.Integer");
-// }
-//
-// /**
-// * This test checks that we can list the contents of the repository using the
-// * list method
-// *
-// * @throws NamingException
-// */
-// @Test
-// public void listRepositoryContents() throws NamingException
-// {
-// InitialContext ctx = new InitialContext();
-//
-// NamingEnumeration<NameClassPair> serviceList = ctx.list("aries:services/java.lang.Runnable/(rubbish=smelly)");
-//
-// checkThreadRetrievedViaListMethod(serviceList);
-//
-// assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
-//
-// //Now add a second service
-//
-// registerService(new Thread());
-//
-// serviceList = ctx.list("aries:services/java.lang.Runnable/(rubbish=smelly)");
-//
-// checkThreadRetrievedViaListMethod(serviceList);
-//
-// checkThreadRetrievedViaListMethod(serviceList);
-//
-// assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
-//
-// }
-//
-// /**
-// * Check that the NamingEnumeration passed in has another element, which represents a java.lang.Thread
-// * @param serviceList
-// * @throws NamingException
-// */
-// private void checkThreadRetrievedViaListMethod(NamingEnumeration<NameClassPair> serviceList)
-// throws NamingException
-// {
-// assertTrue("The repository was empty", serviceList.hasMoreElements());
-//
-// NameClassPair ncp = serviceList.next();
-//
-// assertNotNull("We didn't get a service back from our lookup :(", ncp);
-//
-// assertNotNull("The object from the SR was null", ncp.getClassName());
-//
-// assertEquals("The service retrieved was not of the correct type", "java.lang.Thread", ncp.getClassName());
-//
-// assertEquals("aries:services/java.lang.Runnable/(rubbish=smelly)", ncp.getName().toString());
-// }
-//
-// /**
-// * This test checks that we can list the contents of the repository using the
-// * list method
-// *
-// * @throws NamingException
-// */
-// @Test
-// public void listRepositoryBindings() throws NamingException
-// {
-// InitialContext ctx = new InitialContext();
-//
-// NamingEnumeration<Binding> serviceList = ctx.listBindings("aries:services/java.lang.Runnable/(rubbish=smelly)");
-//
-// Object returnedService = checkThreadRetrievedViaListBindingsMethod(serviceList);
-//
-// assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
-//
-// assertTrue("The returned service was not the service we expected", returnedService == service);
-//
-// //Now add a second service
-// Thread secondService = new Thread();
-// registerService(secondService);
-//
-// serviceList = ctx.listBindings("aries:services/java.lang.Runnable/(rubbish=smelly)");
-//
-// Object returnedService1 = checkThreadRetrievedViaListBindingsMethod(serviceList);
-//
-// Object returnedService2 = checkThreadRetrievedViaListBindingsMethod(serviceList);
-//
-// assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
-//
-// assertTrue("The services were not the ones we expected!",(returnedService1 == service || returnedService2 == service) && (returnedService1 == secondService || returnedService2 == secondService) && (returnedService1 != returnedService2));
-//
-// }
-//
-// /**
-// * Check that the NamingEnumeration passed in has another element, which represents a java.lang.Thread
-// * @param serviceList
-// * @return the object in the registry
-// * @throws NamingException
-// */
-// private Object checkThreadRetrievedViaListBindingsMethod(NamingEnumeration<Binding> serviceList)
-// throws NamingException
-// {
-// assertTrue("The repository was empty", serviceList.hasMoreElements());
-//
-// Binding binding = serviceList.nextElement();
-//
-// assertNotNull("We didn't get a service back from our lookup :(", binding);
-//
-// assertNotNull("The object from the SR was null", binding.getObject());
-//
-// assertTrue("The service retrieved was not of the correct type", binding.getObject() instanceof Thread);
-//
-// assertEquals("aries:services/java.lang.Runnable/(rubbish=smelly)", binding.getName().toString());
-//
-// return binding.getObject();
-// }
-//
-//}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.jndi.url;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Field;
+import java.util.Hashtable;
+import java.util.Properties;
+
+import javax.naming.Binding;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameClassPair;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.spi.ObjectFactory;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+import org.apache.aries.unittest.mocks.MethodCall;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.apache.aries.jndi.ContextHelper;
+import org.apache.aries.jndi.OSGiObjectFactoryBuilder;
+import org.apache.aries.jndi.services.ServiceHelper;
+import org.apache.aries.jndi.url.Activator;
+import org.apache.aries.mocks.BundleContextMock;
+import org.apache.aries.mocks.BundleMock;
+
+/**
+ * Tests for our JNDI implementation for the service registry.
+ */
+public class ServiceRegistryContextTest
+{
+ /** The service we register by default */
+ private Thread service;
+ /** The bundle context for the test */
+ private BundleContext bc;
+ /** The service registration for the service */
+ private ServiceRegistration reg;
+
+ /**
+ * This method does the setup to ensure we always have a service.
+ * @throws NamingException
+ * @throws NoSuchFieldException
+ * @throws SecurityException
+ * @throws IllegalAccessException
+ * @throws IllegalArgumentException
+ */
+ @Before
+ public void registerService() throws NamingException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
+ {
+ bc = Skeleton.newMock(new BundleContextMock(), BundleContext.class);
+ new Activator().start(bc);
+ new org.apache.aries.jndi.startup.Activator().start(bc);
+
+ Field f = ServiceHelper.class.getDeclaredField("context");
+ f.setAccessible(true);
+ f.set(null, bc);
+ f = ContextHelper.class.getDeclaredField("context");
+ f.setAccessible(true);
+ f.set(null, bc);
+ f = OSGiObjectFactoryBuilder.class.getDeclaredField("context");
+ f.setAccessible(true);
+ f.set(null, bc);
+
+
+ service = new Thread();
+
+ registerService(service);
+ }
+
+ /**
+ * Register a service in our map.
+ *
+ * @param service2 The service to register.
+ */
+ private void registerService(Thread service2)
+ {
+ ServiceFactory factory = Skeleton.newMock(ServiceFactory.class);
+ Skeleton skel = Skeleton.getSkeleton(factory);
+
+ skel.setReturnValue(new MethodCall(ServiceFactory.class, "getService", Bundle.class, ServiceRegistration.class), service2);
+
+ Hashtable<String, String> props = new Hashtable<String, String>();
+ props.put("rubbish", "smelly");
+
+ reg = bc.registerService(new String[] {"java.lang.Runnable"}, factory, props);
+ }
+
+ /**
+ * Make sure we clear the caches out before the next test.
+ */
+ @After
+ public void teardown()
+ {
+ BundleContextMock.clear();
+
+ Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+ }
+
+ /**
+ * This test checks that we correctly register and deregister the url context
+ * object factory in the service registry.
+ */
+ @Test
+ public void testJNDIRegistration()
+ {
+ ServiceReference ref = bc.getServiceReference(ObjectFactory.class.getName());
+
+ assertNotNull("The aries url context object factory was not registered", ref);
+
+ ObjectFactory factory = (ObjectFactory) bc.getService(ref);
+
+ assertNotNull("The aries url context object factory was null", factory);
+ }
+
+ /**
+ * This test does a simple JNDI lookup to prove that works.
+ * @throws NamingException
+ */
+ @Test
+ public void simpleJNDILookup() throws NamingException
+ {
+ System.setProperty(Context.URL_PKG_PREFIXES, "helloMatey");
+
+ InitialContext ctx = new InitialContext(new Hashtable<Object, Object>());
+
+ BundleMock mock = new BundleMock("scooby.doo", new Properties());
+
+ Thread.currentThread().setContextClassLoader(mock.getClassLoader());
+
+ Object s = ctx.lookup("aries:services/java.lang.Runnable");
+
+ assertNotNull("We didn't get a service back from our lookup :(", s);
+
+ assertEquals("The SR did not return the object we expected", service, s);
+
+ Skeleton skel = Skeleton.getSkeleton(mock.getBundleContext());
+
+ skel.assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", null));
+
+ mock = new BundleMock("scooby.doo", new Properties());
+
+ Thread.currentThread().setContextClassLoader(mock.getClassLoader());
+
+ s = ctx.lookup("osgi:services/java.lang.Runnable");
+
+ // Check we have the packages set correctly
+
+ String packages = System.getProperty(Context.URL_PKG_PREFIXES, null);
+
+ assertTrue(ctx.getEnvironment().containsValue(packages));
+
+
+ assertNotNull("We didn't get a service back from our lookup :(", s);
+
+ assertEquals("The SR did not return the object we expected", service, s);
+
+ skel = Skeleton.getSkeleton(mock.getBundleContext());
+ skel.assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", null));
+ }
+
+ /**
+ * This test checks that we can pass a filter in without things blowing up.
+ * Right now our mock service registry does not implement filtering, so the
+ * effect is the same as simpleJNDILookup, but we at least know it is not
+ * blowing up.
+ *
+ * @throws NamingException
+ */
+ @Test
+ public void jndiLookupWithFilter() throws NamingException
+ {
+ InitialContext ctx = new InitialContext();
+
+ Object s = ctx.lookup("aries:services/java.lang.Runnable/(rubbish=smelly)");
+
+ assertNotNull("We didn't get a service back from our lookup :(", s);
+
+ assertEquals("The SR did not return the object we expected", service, s);
+
+ Skeleton.getSkeleton(bc).assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", "(rubbish=smelly)"));
+ }
+
+ /**
+ * Check that we get a NameNotFoundException if we lookup after the service
+ * has been unregistered.
+ *
+ * @throws NamingException
+ */
+ @Test(expected=NameNotFoundException.class)
+ public void testLookupWhenServiceHasBeenRemoved() throws NamingException
+ {
+ reg.unregister();
+ InitialContext ctx = new InitialContext();
+
+ ctx.lookup("aries:services/java.lang.Runnable");
+ }
+
+ /**
+ * Check that we get a NameNotFoundException if we lookup something not in the
+ * registry.
+ *
+ * @throws NamingException
+ */
+ @Test(expected=NameNotFoundException.class)
+ public void testLookupForServiceWeNeverHad() throws NamingException
+ {
+ InitialContext ctx = new InitialContext();
+
+ ctx.lookup("aries:services/java.lang.Integer");
+ }
+
+ /**
+ * This test checks that we can list the contents of the repository using the
+ * list method
+ *
+ * @throws NamingException
+ */
+ @Test
+ public void listRepositoryContents() throws NamingException
+ {
+ InitialContext ctx = new InitialContext();
+
+ NamingEnumeration<NameClassPair> serviceList = ctx.list("aries:services/java.lang.Runnable/(rubbish=smelly)");
+
+ checkThreadRetrievedViaListMethod(serviceList);
+
+ assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
+
+ //Now add a second service
+
+ registerService(new Thread());
+
+ serviceList = ctx.list("aries:services/java.lang.Runnable/(rubbish=smelly)");
+
+ checkThreadRetrievedViaListMethod(serviceList);
+
+ checkThreadRetrievedViaListMethod(serviceList);
+
+ assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
+
+ }
+
+ /**
+ * Check that the NamingEnumeration passed in has another element, which represents a java.lang.Thread
+ * @param serviceList
+ * @throws NamingException
+ */
+ private void checkThreadRetrievedViaListMethod(NamingEnumeration<NameClassPair> serviceList)
+ throws NamingException
+ {
+ assertTrue("The repository was empty", serviceList.hasMoreElements());
+
+ NameClassPair ncp = serviceList.next();
+
+ assertNotNull("We didn't get a service back from our lookup :(", ncp);
+
+ assertNotNull("The object from the SR was null", ncp.getClassName());
+
+ assertEquals("The service retrieved was not of the correct type", "java.lang.Thread", ncp.getClassName());
+
+ assertEquals("aries:services/java.lang.Runnable/(rubbish=smelly)", ncp.getName().toString());
+ }
+
+ /**
+ * This test checks that we can list the contents of the repository using the
+ * list method
+ *
+ * @throws NamingException
+ */
+ @Test
+ public void listRepositoryBindings() throws NamingException
+ {
+ InitialContext ctx = new InitialContext();
+
+ NamingEnumeration<Binding> serviceList = ctx.listBindings("aries:services/java.lang.Runnable/(rubbish=smelly)");
+
+ Object returnedService = checkThreadRetrievedViaListBindingsMethod(serviceList);
+
+ assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
+
+ assertTrue("The returned service was not the service we expected", returnedService == service);
+
+ //Now add a second service
+ Thread secondService = new Thread();
+ registerService(secondService);
+
+ serviceList = ctx.listBindings("aries:services/java.lang.Runnable/(rubbish=smelly)");
+
+ Object returnedService1 = checkThreadRetrievedViaListBindingsMethod(serviceList);
+
+ Object returnedService2 = checkThreadRetrievedViaListBindingsMethod(serviceList);
+
+ assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
+
+ assertTrue("The services were not the ones we expected!",(returnedService1 == service || returnedService2 == service) && (returnedService1 == secondService || returnedService2 == secondService) && (returnedService1 != returnedService2));
+
+ }
+
+ /**
+ * Check that the NamingEnumeration passed in has another element, which represents a java.lang.Thread
+ * @param serviceList
+ * @return the object in the registry
+ * @throws NamingException
+ */
+ private Object checkThreadRetrievedViaListBindingsMethod(NamingEnumeration<Binding> serviceList)
+ throws NamingException
+ {
+ assertTrue("The repository was empty", serviceList.hasMoreElements());
+
+ Binding binding = serviceList.nextElement();
+
+ assertNotNull("We didn't get a service back from our lookup :(", binding);
+
+ assertNotNull("The object from the SR was null", binding.getObject());
+
+ assertTrue("The service retrieved was not of the correct type", binding.getObject() instanceof Thread);
+
+ assertEquals("aries:services/java.lang.Runnable/(rubbish=smelly)", binding.getName().toString());
+
+ return binding.getObject();
+ }
+
+}
\ No newline at end of file
Modified: incubator/aries/trunk/jndi/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/pom.xml?rev=822563&r1=822562&r2=822563&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/pom.xml (original)
+++ incubator/aries/trunk/jndi/pom.xml Wed Oct 7 02:48:17 2009
@@ -59,6 +59,11 @@
<version>${version}</version>
</dependency>
<dependency>
+ <groupId>org.apache.aries.testsupport</groupId>
+ <artifactId>aries-testsupport-unit</artifactId>
+ <version>${version}</version>
+ </dependency>
+ <dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
Modified: incubator/aries/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/pom.xml?rev=822563&r1=822562&r2=822563&view=diff
==============================================================================
--- incubator/aries/trunk/pom.xml (original)
+++ incubator/aries/trunk/pom.xml Wed Oct 7 02:48:17 2009
@@ -37,6 +37,7 @@
<modules>
<module>parent</module>
+ <module>testsupport</module>
<module>blueprint</module>
<module>jndi</module>
</modules>