You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:25:38 UTC

[sling-org-apache-sling-testing-sling-mock] 06/14: add unit test to test adapter factory overlays

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 8476a669e08f6760b407896302d0b686824ac6e5
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Fri Mar 11 13:11:47 2016 +0000

    add unit test to test adapter factory overlays
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1734551 13f79535-47bb-0310-9956-ffa450edef68
---
 .../testing/mock/sling/junit/SlingContextTest.java | 55 ++++++++++++++++++++++
 1 file changed, 55 insertions(+)

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 b785f28..7d61856 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
@@ -26,6 +26,8 @@ import static org.mockito.Mockito.verify;
 
 import java.io.IOException;
 
+import org.apache.sling.api.adapter.AdapterFactory;
+import org.apache.sling.api.adapter.SlingAdaptable;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
@@ -36,6 +38,7 @@ import org.junit.runner.RunWith;
 import org.mockito.runners.MockitoJUnitRunner;
 
 import com.google.common.base.Function;
+import com.google.common.collect.ImmutableMap;
 
 @RunWith(MockitoJUnitRunner.class)
 public class SlingContextTest {
@@ -76,4 +79,56 @@ public class SlingContextTest {
         assertNull(context.resourceResolver().adaptTo(Double.class));
     }
 
+    @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));
+        
+        // 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));
+
+        // register overlay adapter with dynamic adaption
+        context.registerAdapter(TestAdaptable.class, String.class, new Function<TestAdaptable, String>() {
+            @Override
+            public String apply(TestAdaptable input) {
+                return input.getMessage() + "-dynamic";
+            }
+        });
+
+        // test overlay adapter with dynamic adaption
+        assertEquals("testMessage3-dynamic", new TestAdaptable("testMessage3").adaptTo(String.class));
+    }
+
+
+    private static class TestAdaptable extends SlingAdaptable {
+        
+        private final String message;
+
+        public TestAdaptable(String message) {
+            this.message = message;
+        }
+
+        public String getMessage() {
+            return message;
+        }
+        
+    }
+
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.