This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.sling-mock-2.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 57b714b7529adb77c7e9b7bae40b29a4495cc263 Author: Stefan Seifert <[email protected]> AuthorDate: Fri Apr 29 17:05:13 2016 +0000 SLING-5667 adapt to changes service ranking order in osgi-mock git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1741667 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 13 +++---- .../mock/sling/context/SlingContextImpl.java | 3 ++ .../testing/mock/sling/junit/SlingContextTest.java | 42 +++++++++++++--------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index decc8fc..e6df110 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,13 @@ <dependency> <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.commons.osgi</artifactId> + <version>2.4.0</version> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.models.api</artifactId> <version>1.2.2</version> <scope>compile</scope> @@ -118,12 +125,6 @@ </dependency> <dependency> <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.commons.osgi</artifactId> - <version>2.4.0</version> - <scope>compile</scope> - </dependency> - <dependency> - <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.commons.classloader</artifactId> <version>1.3.2</version> <scope>compile</scope> diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java index e22b34e..cfaad87 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java @@ -53,6 +53,7 @@ import org.apache.sling.testing.mock.sling.services.MockSlingSettingService; 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.Constants; import org.osgi.framework.ServiceReference; import aQute.bnd.annotation.ConsumerType; @@ -370,6 +371,8 @@ public class SlingContextImpl extends OsgiContextImpl { .put(AdapterFactory.ADAPTER_CLASSES, new String[] { adapterClass.getName() }) + // make sure this overlay has higher ranking than other adapter factories + .put(Constants.SERVICE_RANKING, Integer.MAX_VALUE) .build()); } diff --git a/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java b/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java index 7d61856..2a25e7c 100644 --- a/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java +++ b/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java @@ -80,30 +80,20 @@ public class SlingContextTest { } @Test - public void testRegisterAdapterOverlay() { - - // register "traditional" adapter factory without specific service ranking - AdapterFactory adapterFactory = new AdapterFactory() { - @SuppressWarnings("unchecked") - @Override - public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) { - return (AdapterType)(((TestAdaptable)adaptable).getMessage() + "-initial"); - } - }; - context.registerService(AdapterFactory.class, adapterFactory, ImmutableMap.<String, Object>builder() - .put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { TestAdaptable.class.getName() }) - .put(AdapterFactory.ADAPTER_CLASSES, new String[] { String.class.getName() }) - .build()); - - // test initial adapter factory - assertEquals("testMessage1-initial", new TestAdaptable("testMessage1").adaptTo(String.class)); + public void testRegisterAdapterOverlayStatic() { + prepareInitialAdapterFactory(); // register overlay adapter with static adaption context.registerAdapter(TestAdaptable.class, String.class, "static-adaption"); // test overlay adapter with static adaption assertEquals("static-adaption", new TestAdaptable("testMessage2").adaptTo(String.class)); + } + @Test + public void testRegisterAdapterOverlayDynamic() { + prepareInitialAdapterFactory(); + // register overlay adapter with dynamic adaption context.registerAdapter(TestAdaptable.class, String.class, new Function<TestAdaptable, String>() { @Override @@ -115,6 +105,24 @@ public class SlingContextTest { // test overlay adapter with dynamic adaption assertEquals("testMessage3-dynamic", new TestAdaptable("testMessage3").adaptTo(String.class)); } + + private void prepareInitialAdapterFactory() { + // register "traditional" adapter factory without specific service ranking + AdapterFactory adapterFactory = new AdapterFactory() { + @SuppressWarnings("unchecked") + @Override + public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) { + return (AdapterType)(((TestAdaptable)adaptable).getMessage() + "-initial"); + } + }; + context.registerService(AdapterFactory.class, adapterFactory, ImmutableMap.<String, Object>builder() + .put(AdapterFactory.ADAPTABLE_CLASSES, new String[] { TestAdaptable.class.getName() }) + .put(AdapterFactory.ADAPTER_CLASSES, new String[] { String.class.getName() }) + .build()); + + // test initial adapter factory + assertEquals("testMessage1-initial", new TestAdaptable("testMessage1").adaptTo(String.class)); + } private static class TestAdaptable extends SlingAdaptable { -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
