You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2014/11/14 10:55:13 UTC
svn commit: r1639589 - in /sling/trunk/testing/mocks: osgi-mock/
osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/
osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/
osgi-mock/src/test/java/org/apache/sling/testing/mock...
Author: sseifert
Date: Fri Nov 14 09:55:05 2014
New Revision: 1639589
URL: http://svn.apache.org/r1639589
Log:
SLING-4162 Introduce "OsgiContext" junit rule for OSGi and OsgiContextImpl
Added:
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java (with props)
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java (with props)
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java (with props)
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java (with props)
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java (with props)
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java (with props)
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java (with props)
Modified:
sling/trunk/testing/mocks/osgi-mock/pom.xml
sling/trunk/testing/mocks/sling-mock/pom.xml
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java
sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java
Modified: sling/trunk/testing/mocks/osgi-mock/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/pom.xml?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/pom.xml (original)
+++ sling/trunk/testing/mocks/osgi-mock/pom.xml Fri Nov 14 09:55:05 2014
@@ -92,7 +92,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <scope>test</scope>
+ <scope>compile</scope>
</dependency>
</dependencies>
Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java?rev=1639589&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java Fri Nov 14 09:55:05 2014
@@ -0,0 +1,185 @@
+/*
+ * 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.testing.mock.osgi.context;
+
+import java.lang.reflect.Array;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+
+import aQute.bnd.annotation.ConsumerType;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Defines OSGi context objects and helper methods. Should not be used directly
+ * but via the {@link org.apache.sling.testing.mock.osgi.junit.OsgiContext} JUnit rule.
+ */
+@ConsumerType
+public class OsgiContextImpl {
+
+ protected ComponentContext componentContext;
+
+ /**
+ * Setup actions before test method execution
+ */
+ protected void setUp() {
+ // can be overridden by subclasses
+ }
+
+ /**
+ * Teardown actions after test method execution
+ */
+ protected void tearDown() {
+ // can be overridden by subclasses
+ }
+
+ /**
+ * @return OSGi component context
+ */
+ public final ComponentContext componentContext() {
+ if (this.componentContext == null) {
+ this.componentContext = MockOsgi.newComponentContext();
+ }
+ return this.componentContext;
+ }
+
+ /**
+ * @return OSGi Bundle context
+ */
+ public final BundleContext bundleContext() {
+ return componentContext().getBundleContext();
+ }
+
+ /**
+ * Registers a service in the mocked OSGi environment.
+ * @param <T> Service type
+ * @param service Service instance
+ * @return Registered service instance
+ */
+ public final <T> T registerService(final T service) {
+ return registerService(null, service, null);
+ }
+
+ /**
+ * Registers a service in the mocked OSGi environment.
+ * @param <T> Service type
+ * @param serviceClass Service class
+ * @param service Service instance
+ * @return Registered service instance
+ */
+ public final <T> T registerService(final Class<T> serviceClass, final T service) {
+ return registerService(serviceClass, service, null);
+ }
+
+ /**
+ * Registers a service in the mocked OSGi environment.
+ * @param <T> Service type
+ * @param serviceClass Service class
+ * @param service Service instance
+ * @param properties Service properties (optional)
+ * @return Registered service instance
+ */
+ public final <T> T registerService(final Class<T> serviceClass, final T service, final Map<String, Object> properties) {
+ Dictionary<String, Object> serviceProperties = null;
+ if (properties != null) {
+ serviceProperties = new Hashtable<String, Object>(properties);
+ }
+ bundleContext().registerService(serviceClass != null ? serviceClass.getName() : null, service,
+ serviceProperties);
+ return service;
+ }
+
+ /**
+ * Injects dependencies, activates and registers a service in the mocked
+ * OSGi environment.
+ * @param <T> Service type
+ * @param service Service instance
+ * @return Registered service instance
+ */
+ public final <T> T registerInjectActivateService(final T service) {
+ return registerInjectActivateService(service, ImmutableMap.<String, Object> of());
+ }
+
+ /**
+ * Injects dependencies, activates and registers a service in the mocked
+ * OSGi environment.
+ * @param <T> Service type
+ * @param service Service instance
+ * @param properties Service properties (optional)
+ * @return Registered service instance
+ */
+ public final <T> T registerInjectActivateService(final T service, final Map<String, Object> properties) {
+ MockOsgi.injectServices(service, bundleContext());
+ MockOsgi.activate(service, bundleContext(), properties);
+ registerService(null, service, null);
+ return service;
+ }
+
+ /**
+ * Lookup a single service
+ * @param <ServiceType> Service type
+ * @param serviceType The type (interface) of the service.
+ * @return The service instance, or null if the service is not available.
+ */
+ @SuppressWarnings("unchecked")
+ public final <ServiceType> ServiceType getService(final Class<ServiceType> serviceType) {
+ ServiceReference serviceReference = bundleContext().getServiceReference(serviceType.getName());
+ if (serviceReference != null) {
+ return (ServiceType)bundleContext().getService(serviceReference);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Lookup one or several services
+ * @param <ServiceType> Service type
+ * @param serviceType The type (interface) of the service.
+ * @param filter An optional filter (LDAP-like, see OSGi spec)
+ * @return The services instances or an empty array.
+ * @throws RuntimeException If the <code>filter</code> string is not a valid OSGi service filter string.
+ */
+ @SuppressWarnings("unchecked")
+ public final <ServiceType> ServiceType[] getServices(final Class<ServiceType> serviceType, final String filter) {
+ try {
+ ServiceReference[] serviceReferences = bundleContext().getServiceReferences(serviceType.getName(),
+ filter);
+ if (serviceReferences != null) {
+ ServiceType[] services = (ServiceType[])Array.newInstance(serviceType, serviceReferences.length);
+ for (int i = 0; i < serviceReferences.length; i++) {
+ services[i] = (ServiceType)bundleContext().getService(serviceReferences[i]);
+ }
+ return services;
+ } else {
+ return (ServiceType[])ArrayUtils.EMPTY_OBJECT_ARRAY;
+ }
+ } catch (InvalidSyntaxException ex) {
+ throw new RuntimeException("Invalid filter syntax: " + filter, ex);
+ }
+ }
+
+}
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Nov 14 09:55:05 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java?rev=1639589&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java Fri Nov 14 09:55:05 2014
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+/**
+ * OSGi context implementation for unit tests.
+ */
+@aQute.bnd.annotation.Version("1.0")
+package org.apache.sling.testing.mock.osgi.context;
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Nov 14 09:55:05 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/context/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java?rev=1639589&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java Fri Nov 14 09:55:05 2014
@@ -0,0 +1,111 @@
+/*
+ * 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.testing.mock.osgi.junit;
+
+import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
+import org.junit.rules.ExternalResource;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * JUnit rule for setting up and tearing down OSGi context for unit tests.
+ */
+public final class OsgiContext extends OsgiContextImpl implements TestRule {
+
+ private final OsgiContextCallback setUpCallback;
+ private final OsgiContextCallback tearDownCallback;
+ private final TestRule delegate;
+
+ /**
+ * Initialize Sling context with default resource resolver type:
+ * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}.
+ */
+ public OsgiContext() {
+ this(null, null);
+ }
+
+ /**
+ * Initialize Sling context with default resource resolver type:
+ * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}.
+ * @param setUpCallback Allows the application to register an own callback
+ * function that is called after the built-in setup rules are
+ * executed.
+ */
+ public OsgiContext(final OsgiContextCallback setUpCallback) {
+ this(setUpCallback, null);
+ }
+
+ /**
+ * Initialize Sling context with resource resolver type.
+ * @param setUpCallback Allows the application to register an own callback
+ * function that is called after the built-in setup rules are
+ * executed.
+ * @param tearDownCallback Allows the application to register an own
+ * callback function that is called before the built-in teardown
+ * rules are executed.
+ * @param resourceResolverType Resource resolver type.
+ */
+ public OsgiContext(final OsgiContextCallback setUpCallback, final OsgiContextCallback tearDownCallback) {
+
+ this.setUpCallback = setUpCallback;
+ this.tearDownCallback = tearDownCallback;
+
+ // wrap {@link ExternalResource} rule executes each test method once
+ this.delegate = new ExternalResource() {
+ @Override
+ protected void before() {
+ OsgiContext.this.setUp();
+ OsgiContext.this.executeSetUpCallback();
+ }
+
+ @Override
+ protected void after() {
+ OsgiContext.this.executeTearDownCallback();
+ OsgiContext.this.tearDown();
+ }
+ };
+ }
+
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return this.delegate.apply(base, description);
+ }
+
+ private void executeSetUpCallback() {
+ if (this.setUpCallback != null) {
+ try {
+ this.setUpCallback.execute(this);
+ } catch (Throwable ex) {
+ throw new RuntimeException("Executing setup callback failed.", ex);
+ }
+ }
+ }
+
+ private void executeTearDownCallback() {
+ if (this.tearDownCallback != null) {
+ try {
+ this.tearDownCallback.execute(this);
+ } catch (Throwable ex) {
+ throw new RuntimeException("Executing setup callback failed.", ex);
+ }
+ }
+ }
+
+}
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Nov 14 09:55:05 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java?rev=1639589&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java Fri Nov 14 09:55:05 2014
@@ -0,0 +1,37 @@
+/*
+ * 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.testing.mock.osgi.junit;
+
+import java.io.IOException;
+
+/**
+ * Callback-interface for application-specific setup and teardown operations to
+ * customize the {@link OsgiContext} JUnit rule.
+ */
+public interface OsgiContextCallback {
+
+ /**
+ * Execute callback action
+ * @param context Sling context
+ * @throws IOException
+ * @throws PersistenceException
+ */
+ void execute(OsgiContext context) throws IOException;
+
+}
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Nov 14 09:55:05 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java?rev=1639589&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java Fri Nov 14 09:55:05 2014
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+/**
+ * Rule for providing easy access to OSGi context in JUnit tests.
+ */
+@aQute.bnd.annotation.Version("1.0")
+package org.apache.sling.testing.mock.osgi.junit;
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Nov 14 09:55:05 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java?rev=1639589&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java Fri Nov 14 09:55:05 2014
@@ -0,0 +1,96 @@
+/*
+ * 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.testing.mock.osgi.context;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.ServiceReference;
+
+public class OsgiContextImplTest {
+
+ private OsgiContextImpl context;
+
+ @Before
+ public void setUp() throws Exception {
+ this.context = new OsgiContextImpl();
+ this.context.setUp();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ this.context.tearDown();
+ }
+
+ @Test
+ public void testContextObjects() {
+ assertNotNull(context.componentContext());
+ assertNotNull(context.bundleContext());
+ }
+
+ @Test
+ public void testRegisterService() {
+ Set<String> myService = new HashSet<String>();
+ context.registerService(Set.class, myService);
+
+ Set<?> serviceResult = context.getService(Set.class);
+ assertSame(myService, serviceResult);
+ }
+
+ @Test
+ public void testRegisterServiceWithProperties() {
+ Map<String, Object> props = new HashMap<String, Object>();
+ props.put("prop1", "value1");
+
+ Set<String> myService = new HashSet<String>();
+ context.registerService(Set.class, myService, props);
+
+ ServiceReference serviceReference = context.bundleContext().getServiceReference(Set.class.getName());
+ Object serviceResult = context.bundleContext().getService(serviceReference);
+ assertSame(myService, serviceResult);
+ assertEquals("value1", serviceReference.getProperty("prop1"));
+ }
+
+ @Test
+ public void testRegisterMultipleServices() {
+ Set<String> myService1 = new HashSet<String>();
+ context.registerService(Set.class, myService1);
+ Set<String> myService2 = new HashSet<String>();
+ context.registerService(Set.class, myService2);
+
+ Set[] serviceResults = context.getServices(Set.class, null);
+ assertSame(myService1, serviceResults[0]);
+ assertSame(myService2, serviceResults[1]);
+ }
+
+ @Test
+ public void testRegisterInjectActivate() {
+ context.registerInjectActivateService(new Object());
+ }
+
+}
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Nov 14 09:55:05 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java?rev=1639589&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java Fri Nov 14 09:55:05 2014
@@ -0,0 +1,53 @@
+/*
+ * 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.testing.mock.osgi.junit;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.io.IOException;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class OsgiContextTest {
+
+ private final OsgiContextCallback contextSetup = mock(OsgiContextCallback.class);
+ private final OsgiContextCallback contextTeardown = mock(OsgiContextCallback.class);
+
+ // Run all unit tests for each resource resolver types listed here
+ @Rule
+ public OsgiContext context = new OsgiContext(contextSetup, contextTeardown);
+
+ @Before
+ public void setUp() throws IOException {
+ verify(contextSetup).execute(context);
+ }
+
+ @Test
+ public void testBundleContext() {
+ assertNotNull(context.bundleContext());
+ }
+
+}
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Nov 14 09:55:05 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: sling/trunk/testing/mocks/sling-mock/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/pom.xml?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/pom.xml (original)
+++ sling/trunk/testing/mocks/sling-mock/pom.xml Fri Nov 14 09:55:05 2014
@@ -49,7 +49,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.osgi-mock</artifactId>
- <version>1.0.0</version>
+ <version>1.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java Fri Nov 14 09:55:05 2014
@@ -18,9 +18,6 @@
*/
package org.apache.sling.testing.mock.sling.context;
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
@@ -47,7 +44,7 @@ import org.apache.sling.models.impl.inje
import org.apache.sling.models.spi.ImplementationPicker;
import org.apache.sling.models.spi.Injector;
import org.apache.sling.settings.SlingSettingsService;
-import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
import org.apache.sling.testing.mock.sling.MockSling;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.builder.ContentBuilder;
@@ -58,11 +55,10 @@ import org.apache.sling.testing.mock.sli
import org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse;
-import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
-import org.osgi.service.component.ComponentContext;
-import com.google.common.collect.ImmutableMap;
+import aQute.bnd.annotation.ConsumerType;
+
import com.google.common.collect.ImmutableSet;
/**
@@ -70,7 +66,8 @@ import com.google.common.collect.Immutab
* directly but via the {@link org.apache.sling.testing.mock.sling.junit.SlingContext} JUnit
* rule.
*/
-public class SlingContextImpl {
+@ConsumerType
+public class SlingContextImpl extends OsgiContextImpl {
// default to publish instance run mode
static final Set<String> DEFAULT_RUN_MODES = ImmutableSet.<String> builder().add("publish").build();
@@ -78,7 +75,6 @@ public class SlingContextImpl {
protected ResourceResolverFactory resourceResolverFactory;
protected MockModelAdapterFactory modelAdapterFactory;
protected ResourceResolverType resourceResolverType;
- protected ComponentContext componentContext;
protected ResourceResolver resourceResolver;
protected MockSlingHttpServletRequest request;
protected MockSlingHttpServletResponse response;
@@ -97,6 +93,7 @@ public class SlingContextImpl {
* Setup actions before test method execution
*/
protected void setUp() {
+ super.setUp();
MockSling.setAdapterManagerBundleContext(bundleContext());
this.resourceResolverFactory = newResourceResolverFactory();
registerDefaultServices();
@@ -171,6 +168,8 @@ public class SlingContextImpl {
this.contentBuilder = null;
MockSling.clearAdapterManagerBundleContext();
+
+ super.tearDown();
}
/**
@@ -181,23 +180,6 @@ public class SlingContextImpl {
}
/**
- * @return OSGi component context
- */
- public final ComponentContext componentContext() {
- if (this.componentContext == null) {
- this.componentContext = MockOsgi.newComponentContext();
- }
- return this.componentContext;
- }
-
- /**
- * @return OSGi Bundle context
- */
- public final BundleContext bundleContext() {
- return componentContext().getBundleContext();
- }
-
- /**
* @return Resource resolver
*/
public final ResourceResolver resourceResolver() {
@@ -277,94 +259,6 @@ public class SlingContextImpl {
}
/**
- * Registers a service in the mocked OSGi environment.
- * @param <T> Service type
- * @param service Service instance
- * @return Registered service instance
- */
- public final <T> T registerService(final T service) {
- return registerService(null, service, null);
- }
-
- /**
- * Registers a service in the mocked OSGi environment.
- * @param <T> Service type
- * @param serviceClass Service class
- * @param service Service instance
- * @return Registered service instance
- */
- public final <T> T registerService(final Class<T> serviceClass, final T service) {
- return registerService(serviceClass, service, null);
- }
-
- /**
- * Registers a service in the mocked OSGi environment.
- * @param <T> Service type
- * @param serviceClass Service class
- * @param service Service instance
- * @param properties Service properties (optional)
- * @return Registered service instance
- */
- public final <T> T registerService(final Class<T> serviceClass, final T service, final Map<String, Object> properties) {
- Dictionary<String, Object> serviceProperties = null;
- if (properties != null) {
- serviceProperties = new Hashtable<String, Object>(properties);
- }
- bundleContext().registerService(serviceClass != null ? serviceClass.getName() : null, service,
- serviceProperties);
- return service;
- }
-
- /**
- * Injects dependencies, activates and registers a service in the mocked
- * OSGi environment.
- * @param <T> Service type
- * @param service Service instance
- * @return Registered service instance
- */
- public final <T> T registerInjectActivateService(final T service) {
- return registerInjectActivateService(service, ImmutableMap.<String, Object> of());
- }
-
- /**
- * Injects dependencies, activates and registers a service in the mocked
- * OSGi environment.
- * @param <T> Service type
- * @param service Service instance
- * @param properties Service properties (optional)
- * @return Registered service instance
- */
- public final <T> T registerInjectActivateService(final T service, final Map<String, Object> properties) {
- MockOsgi.injectServices(service, bundleContext());
- MockOsgi.activate(service, bundleContext(), properties);
- registerService(null, service, null);
- return service;
- }
-
- /**
- * Lookup a single service
- * @param <ServiceType> Service type
- * @param serviceType The type (interface) of the service.
- * @return The service instance, or null if the service is not available.
- */
- public final <ServiceType> ServiceType getService(final Class<ServiceType> serviceType) {
- return slingScriptHelper().getService(serviceType);
- }
-
- /**
- * Lookup one or several services
- * @param <ServiceType> Service type
- * @param serviceType The type (interface) of the service.
- * @param filter An optional filter (LDAP-like, see OSGi spec)
- * @return The services object or null.
- * @throws org.apache.sling.api.scripting.InvalidServiceFilterSyntaxException If the <code>filter</code>
- * string is not a valid OSGi service filter string.
- */
- public final <ServiceType> ServiceType[] getServices(final Class<ServiceType> serviceType, final String filter) {
- return slingScriptHelper().getServices(serviceType, filter);
- }
-
- /**
* @return Current resource
*/
public final Resource currentResource() {
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java Fri Nov 14 09:55:05 2014
@@ -19,5 +19,5 @@
/**
* Sling context implementation for unit tests.
*/
-@aQute.bnd.annotation.Version("1.0")
+@aQute.bnd.annotation.Version("2.0")
package org.apache.sling.testing.mock.sling.context;
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java Fri Nov 14 09:55:05 2014
@@ -33,7 +33,6 @@ public final class SlingContext extends
private final SlingContextCallback setUpCallback;
private final SlingContextCallback tearDownCallback;
- private final ResourceResolverType resourceResolverType;
private final TestRule delegate;
/**
@@ -103,10 +102,11 @@ public final class SlingContext extends
this.setUpCallback = setUpCallback;
this.tearDownCallback = tearDownCallback;
- this.resourceResolverType = resourceResolverType;
- // user default rule that directly executes each test method once
+ // set resource resolver type in parent context
setResourceResolverType(this.resourceResolverType);
+
+ // wrap {@link ExternalResource} rule executes each test method once
this.delegate = new ExternalResource() {
@Override
protected void before() {
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java Fri Nov 14 09:55:05 2014
@@ -19,5 +19,5 @@
/**
* Rule for providing easy access to Sling context in JUnit tests.
*/
-@aQute.bnd.annotation.Version("1.0")
+@aQute.bnd.annotation.Version("2.0")
package org.apache.sling.testing.mock.sling.junit;
Modified: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java Fri Nov 14 09:55:05 2014
@@ -24,9 +24,6 @@ import static org.junit.Assert.assertNul
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
@@ -47,7 +44,6 @@ import org.apache.sling.testing.mock.sli
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.osgi.framework.ServiceReference;
public class SlingContextImplTest {
@@ -89,41 +85,6 @@ public class SlingContextImplTest {
}
@Test
- public void testRegisterService() {
- Set<String> myService = new HashSet<String>();
- context.registerService(Set.class, myService);
-
- Set<?> serviceResult = context.getService(Set.class);
- assertSame(myService, serviceResult);
- }
-
- @Test
- public void testRegisterServiceWithProperties() {
- Map<String, Object> props = new HashMap<String, Object>();
- props.put("prop1", "value1");
-
- Set<String> myService = new HashSet<String>();
- context.registerService(Set.class, myService, props);
-
- ServiceReference serviceReference = context.bundleContext().getServiceReference(Set.class.getName());
- Object serviceResult = context.bundleContext().getService(serviceReference);
- assertSame(myService, serviceResult);
- assertEquals("value1", serviceReference.getProperty("prop1"));
- }
-
- @Test
- public void testRegisterMultipleServices() {
- Set<String> myService1 = new HashSet<String>();
- context.registerService(Set.class, myService1);
- Set<String> myService2 = new HashSet<String>();
- context.registerService(Set.class, myService2);
-
- Set[] serviceResults = context.getServices(Set.class, null);
- assertSame(myService1, serviceResults[0]);
- assertSame(myService2, serviceResults[1]);
- }
-
- @Test
public void testSetCurrentResource() {
context.currentResource("/content/sample/en/jcr:content/par/colctrl");
assertEquals("/content/sample/en/jcr:content/par/colctrl", context.currentResource().getPath());
@@ -178,11 +139,6 @@ public class SlingContextImplTest {
}
@Test
- public void testRegisterInjectActivate() {
- context.registerInjectActivateService(new Object());
- }
-
- @Test
public void testRunModes() {
SlingSettingsService slingSettings = context.getService(SlingSettingsService.class);
assertEquals(SlingContextImpl.DEFAULT_RUN_MODES, slingSettings.getRunModes());
Modified: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java?rev=1639589&r1=1639588&r2=1639589&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java Fri Nov 14 09:55:05 2014
@@ -20,14 +20,12 @@ package org.apache.sling.testing.mock.sl
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import java.io.IOException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
-import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -55,12 +53,4 @@ public class SlingContextTest {
assertNotNull(context.request());
}
- @After
- public void tearDown() {
- // reset required because mockito gets puzzled with the parameterized
- // JUnit rule
- // TODO: better solution?
- reset(contextSetup);
- }
-
}