Use JMock to mock test classes to avoid problems with changing OSGi interfaces.
git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk@783237 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 0ca2ec8..ad298d9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -107,5 +107,9 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock-junit4</artifactId>
+ </dependency>
</dependencies>
</project>
diff --git a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
index 0e13738..f5a77f5 100644
--- a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
+++ b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
@@ -18,60 +18,121 @@
*/
package org.apache.sling.adapter.internal;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
import java.util.Map;
-import junit.framework.TestCase;
-
import org.apache.sling.adapter.SlingAdaptable;
-import org.apache.sling.adapter.mock.MockBundle;
-import org.apache.sling.adapter.mock.MockComponentContext;
-import org.apache.sling.adapter.mock.MockServiceReference;
+import org.apache.sling.adapter.mock.MockAdapterFactory;
import org.apache.sling.api.adapter.AdapterFactory;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
-public class AdapterManagerTest extends TestCase {
+@RunWith(JMock.class)
+public class AdapterManagerTest {
private AdapterManagerImpl am;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
+
+ protected final Mockery context = new JUnit4Mockery();
+
+ @org.junit.Before public void setUp() {
am = new AdapterManagerImpl();
}
-
- @Override
- protected void tearDown() throws Exception {
+
+ @org.junit.After public void tearDown() {
if (AdapterManagerImpl.getInstance() == am) {
am.deactivate(null); // not correct, but argument unused
}
-
- super.tearDown();
}
-
- public void testUnitialized() {
+
+ /**
+ * Helper method to create a mock bundle
+ */
+ protected Bundle createBundle() {
+ final Bundle bundle = this.context.mock(Bundle.class);
+ this.context.checking(new Expectations() {{
+ allowing(bundle).getBundleId();
+ will(returnValue(1L));
+ }});
+ return bundle;
+ }
+
+ /**
+ * Helper method to create a mock component context
+ */
+ protected ComponentContext createComponentContext() {
+ final ComponentContext ctx = this.context.mock(ComponentContext.class);
+ this.context.checking(new Expectations() {{
+ allowing(ctx).locateService(with(any(String.class)), with(any(ServiceReference.class)));
+ will(returnValue(new MockAdapterFactory()));
+ }});
+ return ctx;
+ }
+
+ /**
+ * Helper method to create a mock service reference
+ */
+ protected ServiceReference createServiceReference() {
+ final Bundle bundle = this.createBundle();
+ final ServiceReference ref = this.context.mock(ServiceReference.class);
+ this.context.checking(new Expectations() {{
+ allowing(ref).getProperty(Constants.SERVICE_ID);
+ will(returnValue(1L));
+ allowing(ref).getProperty(AdapterFactory.ADAPTABLE_CLASSES);
+ will(returnValue(new String[]{ TestSlingAdaptable.class.getName() }));
+ allowing(ref).getProperty(AdapterFactory.ADAPTER_CLASSES);
+ will(returnValue(ITestAdapter.class.getName()));
+ allowing(ref).getBundle();
+ will(returnValue(bundle));
+ }});
+ return ref;
+ }
+
+ /**
+ * Helper method to create a mock service reference
+ */
+ protected ServiceReference createServiceReference2() {
+ final Bundle bundle = this.createBundle();
+ final ServiceReference ref = this.context.mock(ServiceReference.class);
+ this.context.checking(new Expectations() {{
+ allowing(ref).getProperty(Constants.SERVICE_ID);
+ will(returnValue(2L));
+ allowing(ref).getProperty(AdapterFactory.ADAPTABLE_CLASSES);
+ will(returnValue(new String[]{ TestSlingAdaptable2.class.getName() }));
+ allowing(ref).getProperty(AdapterFactory.ADAPTER_CLASSES);
+ will(returnValue(TestAdapter.class.getName()));
+ allowing(ref).getBundle();
+ will(returnValue(bundle));
+ }});
+ return ref;
+ }
+
+ @org.junit.Test public void testUnitialized() {
assertNotNull("AdapterFactoryDescriptors must not be null", am.getFactories());
assertTrue("AdapterFactoryDescriptors must be empty", am.getFactories().isEmpty());
assertNull("AdapterFactory cache must be null", am.getFactoryCache());
}
- public void testInitialized() {
- ComponentContext cc = new MockComponentContext();
- am.activate(cc);
+ @org.junit.Test public void testInitialized() {
+ am.activate(this.createComponentContext());
assertNotNull("AdapterFactoryDescriptors must not be null", am.getFactories());
assertTrue("AdapterFactoryDescriptors must be empty", am.getFactories().isEmpty());
assertNull("AdapterFactory cache must be null", am.getFactoryCache());
}
- public void testBindBeforeActivate() {
- Bundle bundle = new MockBundle(1L);
- MockServiceReference ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 1L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, ITestAdapter.class.getName());
+ @org.junit.Test public void testBindBeforeActivate() {
+ final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
// no cache and no factories yet
@@ -79,9 +140,7 @@
assertTrue("AdapterFactoryDescriptors must be empty", am.getFactories().isEmpty());
assertNull("AdapterFactory cache must be null", am.getFactoryCache());
- // this should register the factory
- ComponentContext cc = new MockComponentContext();
- am.activate(cc);
+ am.activate(this.createComponentContext());
// expect the factory, but cache is empty
assertNotNull("AdapterFactoryDescriptors must not be null", am.getFactories());
@@ -89,20 +148,15 @@
assertNull("AdapterFactory cache must be null", am.getFactoryCache());
}
- public void testBindAfterActivate() {
- ComponentContext cc = new MockComponentContext();
- am.activate(cc);
+ @org.junit.Test public void testBindAfterActivate() {
+ am.activate(this.createComponentContext());
// no cache and no factories yet
assertNotNull("AdapterFactoryDescriptors must not be null", am.getFactories());
assertTrue("AdapterFactoryDescriptors must be empty", am.getFactories().isEmpty());
assertNull("AdapterFactory cache must be null", am.getFactoryCache());
- Bundle bundle = new MockBundle(1L);
- MockServiceReference ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 1L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, ITestAdapter.class.getName());
+ final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
// expect the factory, but cache is empty
@@ -124,19 +178,13 @@
assertNull(f.get(TestSlingAdaptable2.class.getName()));
}
- public void testAdaptBase() {
-
- ComponentContext cc = new MockComponentContext();
- am.activate(cc);
+ @org.junit.Test public void testAdaptBase() {
+ am.activate(this.createComponentContext());
TestSlingAdaptable data = new TestSlingAdaptable();
assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
- Bundle bundle = new MockBundle(1L);
- MockServiceReference ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 1L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, ITestAdapter.class.getName());
+ final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
Object adapter = am.getAdapter(data, ITestAdapter.class);
@@ -144,19 +192,13 @@
assertTrue(adapter instanceof ITestAdapter);
}
- public void testAdaptExtended() {
-
- ComponentContext cc = new MockComponentContext();
- am.activate(cc);
+ @org.junit.Test public void testAdaptExtended() {
+ am.activate(this.createComponentContext());
TestSlingAdaptable2 data = new TestSlingAdaptable2();
assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
- Bundle bundle = new MockBundle(1L);
- MockServiceReference ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 1L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, ITestAdapter.class.getName());
+ final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
Object adapter = am.getAdapter(data, ITestAdapter.class);
@@ -164,49 +206,31 @@
assertTrue(adapter instanceof ITestAdapter);
}
- public void testAdaptBase2() {
-
- ComponentContext cc = new MockComponentContext();
- am.activate(cc);
+ @org.junit.Test public void testAdaptBase2() {
+ am.activate(this.createComponentContext());
TestSlingAdaptable data = new TestSlingAdaptable();
assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
- Bundle bundle = new MockBundle(1L);
- MockServiceReference ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 1L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, ITestAdapter.class.getName());
+ final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
- ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 2L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable2.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, TestAdapter.class.getName());
- am.bindAdapterFactory(ref);
+ final ServiceReference ref2 = createServiceReference2();
+ am.bindAdapterFactory(ref2);
Object adapter = am.getAdapter(data, ITestAdapter.class);
assertNotNull(adapter);
assertTrue(adapter instanceof ITestAdapter);
}
- public void testAdaptExtended2() {
+ @org.junit.Test public void testAdaptExtended2() {
+ am.activate(this.createComponentContext());
- ComponentContext cc = new MockComponentContext();
- am.activate(cc);
-
- Bundle bundle = new MockBundle(1L);
- MockServiceReference ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 1L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, ITestAdapter.class.getName());
+ final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
- ref = new MockServiceReference(bundle);
- ref.setProperty(Constants.SERVICE_ID, 2L);
- ref.setProperty(AdapterFactory.ADAPTABLE_CLASSES, new String[]{ TestSlingAdaptable2.class.getName() });
- ref.setProperty(AdapterFactory.ADAPTER_CLASSES, TestAdapter.class.getName());
- am.bindAdapterFactory(ref);
+ final ServiceReference ref2 = createServiceReference2();
+ am.bindAdapterFactory(ref2);
TestSlingAdaptable data = new TestSlingAdaptable();
Object adapter = am.getAdapter(data, ITestAdapter.class);
diff --git a/src/test/java/org/apache/sling/adapter/mock/MockBundle.java b/src/test/java/org/apache/sling/adapter/mock/MockBundle.java
deleted file mode 100644
index 137cfc0..0000000
--- a/src/test/java/org/apache/sling/adapter/mock/MockBundle.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
- * 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 WARRANTIES OR 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.sling.adapter.mock;
-
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Dictionary;
-import java.util.Enumeration;
-
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.ServiceReference;
-
-public class MockBundle implements Bundle {
-
- private long bundleId;
-
- public MockBundle(long bundleId) {
- this.bundleId = bundleId;
- }
-
- public long getBundleId() {
- return bundleId;
- }
-
- public Enumeration<?> findEntries(String path, String filePattern,
- boolean recurse) {
- return null;
- }
-
- public URL getEntry(String name) {
- return null;
- }
-
- public Enumeration<?> getEntryPaths(String path) {
- return null;
- }
-
- public Dictionary<?, ?> getHeaders() {
- return null;
- }
-
- public Dictionary<?, ?> getHeaders(String locale) {
- return null;
- }
-
- public long getLastModified() {
- return 0;
- }
-
- public String getLocation() {
- return null;
- }
-
- public ServiceReference[] getRegisteredServices() {
- return null;
- }
-
- public URL getResource(String name) {
- return null;
- }
-
- public Enumeration<?> getResources(String name) {
- return null;
- }
-
- public ServiceReference[] getServicesInUse() {
- return null;
- }
-
- public int getState() {
- return 0;
- }
-
- public String getSymbolicName() {
- return null;
- }
-
- public boolean hasPermission(Object permission) {
- return false;
- }
-
- public Class<?> loadClass(String name) throws ClassNotFoundException {
- throw new ClassNotFoundException(name);
- }
-
- public void start() {
-
- }
-
- public void stop() {
-
- }
-
- public void uninstall() {
-
- }
-
- public void update() {
-
- }
-
- public void update(InputStream in) {
-
- }
-
- public BundleContext getBundleContext() {
- return null;
- }
-
- public void start(int options) throws BundleException {
- }
-
- public void stop(int options) throws BundleException {
- }
-}
diff --git a/src/test/java/org/apache/sling/adapter/mock/MockComponentContext.java b/src/test/java/org/apache/sling/adapter/mock/MockComponentContext.java
deleted file mode 100644
index d822e1f..0000000
--- a/src/test/java/org/apache/sling/adapter/mock/MockComponentContext.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * 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 WARRANTIES OR 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.sling.adapter.mock;
-
-import java.util.Dictionary;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.sling.api.adapter.AdapterFactory;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.component.ComponentContext;
-import org.osgi.service.component.ComponentInstance;
-
-public class MockComponentContext implements ComponentContext {
-
- private Map<ServiceReference, AdapterFactory> services = new HashMap<ServiceReference, AdapterFactory>();
-
- public Object locateService(String name, ServiceReference reference) {
- AdapterFactory af = services.get(reference);
- if (af == null) {
- af = new MockAdapterFactory();
- services.put(reference, af);
- }
- return af;
- }
-
- public void disableComponent(String name) {
- }
-
- public void enableComponent(String name) {
- }
-
- public BundleContext getBundleContext() {
- return null;
- }
-
- public ComponentInstance getComponentInstance() {
- return null;
- }
-
- public Dictionary<?, ?> getProperties() {
- return null;
- }
-
- public ServiceReference getServiceReference() {
- return null;
- }
-
- public Bundle getUsingBundle() {
- return null;
- }
-
- public Object locateService(String name) {
- return null;
- }
-
- public Object[] locateServices(String name) {
- return null;
- }
-
-}
diff --git a/src/test/java/org/apache/sling/adapter/mock/MockServiceReference.java b/src/test/java/org/apache/sling/adapter/mock/MockServiceReference.java
deleted file mode 100644
index 93da985..0000000
--- a/src/test/java/org/apache/sling/adapter/mock/MockServiceReference.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * 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 WARRANTIES OR 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.sling.adapter.mock;
-
-import java.util.Collections;
-import java.util.Dictionary;
-import java.util.Hashtable;
-
-import org.osgi.framework.Bundle;
-import org.osgi.framework.ServiceReference;
-
-public class MockServiceReference implements ServiceReference {
-
- private Bundle bundle;
- private Dictionary<String, Object> props;
-
- public MockServiceReference(Bundle bundle) {
- this.bundle = bundle;
- this.props = new Hashtable<String, Object>();
- }
-
- public Bundle getBundle() {
- return bundle;
- }
-
- public void setProperty(String key, Object value) {
- props.put(key, value);
- }
-
- public Object getProperty(String key) {
- return props.get(key);
- }
-
- public String[] getPropertyKeys() {
- return Collections.list(props.keys()).toArray(new String[props.size()]);
- }
-
- public Bundle[] getUsingBundles() {
- return null;
- }
-
- public boolean isAssignableTo(Bundle bundle, String className) {
- return false;
- }
-
- public int compareTo(Object reference) {
- return 0;
- }
-}