Author: sseifert
Date: Thu Oct  1 22:00:32 2015
New Revision: 1706325

URL: http://svn.apache.org/viewvc?rev=1706325&view=rev
Log:
SLING-5086 sling-mock: Add SlingContext.registerAdapter convenience method

Modified:
    
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/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java

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=1706325&r1=1706324&r2=1706325&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
 Thu Oct  1 22:00:32 2015
@@ -23,6 +23,7 @@ import java.util.Set;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.sling.api.adapter.AdapterFactory;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -56,6 +57,8 @@ import org.osgi.framework.ServiceReferen
 
 import aQute.bnd.annotation.ConsumerType;
 
+import com.google.common.base.Function;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
 /**
@@ -326,5 +329,48 @@ public class SlingContextImpl extends Os
         }
         return uniqueRoot;
     }
+    
+    /**
+     * Create a Sling AdapterFactory on the fly which can adapt from 
<code>adaptableClass</code>
+     * to <code>adapterClass</code> and just returns the given value as result.
+     * @param adaptableClass Class to adapt from
+     * @param adapterClass Class to adapt to
+     * @param adapter Object which is always returned for this adaption.
+     */
+    public <T1, T2> void registerAdapter(final Class<T1> adaptableClass, final 
Class<T2> adapterClass,
+            final T2 adapter) {
+        registerAdapter(adaptableClass, adapterClass, new Function<T1, T2>() {
+            @Override
+            public T2 apply(T1 input) {
+                return adapter;
+            }
+        });
+    }
+
+    /**
+     * Create a Sling AdapterFactory on the fly which can adapt from 
<code>adaptableClass</code>
+     * to <code>adapterClass</code> and delegates the adapter mapping to the 
given <code>adaptHandler</code> function.
+     * @param adaptableClass Class to adapt from
+     * @param adapterClass Class to adapt to
+     * @param adaptHandler Function to handle the adaption
+     */
+    public <T1, T2> void registerAdapter(final Class<T1> adaptableClass, final 
Class<T2> adapterClass,
+            final Function<T1,T2> adaptHandler) {
+        AdapterFactory adapterFactory = new AdapterFactory() {
+            @SuppressWarnings("unchecked")
+            @Override
+            public <AdapterType> AdapterType getAdapter(Object adaptable, 
Class<AdapterType> type) {
+                return (AdapterType)adaptHandler.apply((T1)adaptable);
+            }
+        };
+        registerService(AdapterFactory.class, adapterFactory, 
ImmutableMap.<String, Object>builder()
+                .put(AdapterFactory.ADAPTABLE_CLASSES, new String[] {
+                    adaptableClass.getName()
+                })
+                .put(AdapterFactory.ADAPTER_CLASSES, new String[] {
+                        adapterClass.getName()
+                })
+                .build());
+    }
 
 }

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=1706325&r1=1706324&r2=1706325&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
 Thu Oct  1 22:00:32 2015
@@ -18,13 +18,16 @@
  */
 package org.apache.sling.testing.mock.sling.junit;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
 import java.io.IOException;
 
 import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.junit.Before;
 import org.junit.Rule;
@@ -32,6 +35,8 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.runners.MockitoJUnitRunner;
 
+import com.google.common.base.Function;
+
 @RunWith(MockitoJUnitRunner.class)
 public class SlingContextTest {
 
@@ -53,4 +58,22 @@ public class SlingContextTest {
         assertNotNull(context.request());
     }
 
+    @Test
+    public void testRegisterAdapter() {
+        
+        // prepare some adapter factories
+        context.registerAdapter(ResourceResolver.class, Integer.class, 5);
+        context.registerAdapter(ResourceResolver.class, String.class, new 
Function<ResourceResolver,String>() {
+            @Override
+            public String apply(ResourceResolver input) {
+                return ">" + input.toString();
+            }
+        });
+        
+        // test adaption
+        assertEquals(Integer.valueOf(5), 
context.resourceResolver().adaptTo(Integer.class));
+        assertEquals(">" + context.resourceResolver().toString(), 
context.resourceResolver().adaptTo(String.class));
+        assertNull(context.resourceResolver().adaptTo(Double.class));
+    }
+
 }


Reply via email to