incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1498941 - in /sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src: main/java/org/apache/sling/adapter/internal/AdapterMethodsManagerImpl.java test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java
Date Tue, 02 Jul 2013 13:55:42 GMT
Author: bdelacretaz
Date: Tue Jul  2 13:55:42 2013
New Revision: 1498941

URL: http://svn.apache.org/r1498941
Log:
SLING-2938 - support adapting from child classes

Modified:
    sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/main/java/org/apache/sling/adapter/internal/AdapterMethodsManagerImpl.java
    sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java

Modified: sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/main/java/org/apache/sling/adapter/internal/AdapterMethodsManagerImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/main/java/org/apache/sling/adapter/internal/AdapterMethodsManagerImpl.java?rev=1498941&r1=1498940&r2=1498941&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/main/java/org/apache/sling/adapter/internal/AdapterMethodsManagerImpl.java
(original)
+++ sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/main/java/org/apache/sling/adapter/internal/AdapterMethodsManagerImpl.java
Tue Jul  2 13:55:42 2013
@@ -128,7 +128,7 @@ public class AdapterMethodsManagerImpl {
             final AdapterFactory factory = new AdapterFactory() {
                 @SuppressWarnings("unchecked")
                 public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType>
type) {
-                    if(adaptable.getClass().equals(fromClass) && type.equals(toClass))
{
+                    if(fromClass.isAssignableFrom(adaptable.getClass()) && toClass.equals(type))
{
                         try {
                             log.debug("Adapting using method {} from {}", method, provider);
                             return (AdapterType)method.invoke(provider, adaptable);

Modified: sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java?rev=1498941&r1=1498940&r2=1498941&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java
(original)
+++ sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java
Tue Jul  2 13:55:42 2013
@@ -56,6 +56,8 @@ public class AdapterMethodManagerIT {
     BundleContext bundleContext;
     
     private List<ServiceRegistration> registeredServices;
+    final TestAdaptable parent = new TestAdaptable(System.currentTimeMillis());
+    final TestAdaptableChild child = new TestAdaptableChild(System.currentTimeMillis() +
1);
     
     static class TestAdaptable extends SlingAdaptable {
         final long value;
@@ -65,6 +67,14 @@ public class AdapterMethodManagerIT {
         }
     }
     
+    static class TestAdaptableChild extends TestAdaptable {
+        final String someAdditionalField = "FOO";
+        
+        TestAdaptableChild(long value) {
+            super(value);
+        }
+    }
+    
     /** Example/test AdapterMethodsProvider that converts TestAdaptable to Long */
     public static class SingleMethodProvider implements AdapterMethodsProvider {
         
@@ -184,15 +194,9 @@ public class AdapterMethodManagerIT {
     
     public void testInvalidMethodsIgnored() {
         registerProvider(new ProviderWithInvalidMethods());
-        
-        final long value = System.currentTimeMillis();
-        final TestAdaptable t = new TestAdaptable(value); 
-        final Long result = t.adaptTo(Long.class);
-        
-        assertNotNull("Expecting non-null adapted Long", result);
-        assertEquals("Expecting correct adapted value", value + 1, result.longValue());
-        assertNull("Expecting no Integer adaptation", t.adaptTo(Integer.class));
-        assertNull("Expecting no URL adaptation", t.adaptTo(URL.class));
+        assertAdapted(parent);
+        assertNull("Expecting no Integer adaptation", parent.adaptTo(Integer.class));
+        assertNull("Expecting no URL adaptation", parent.adaptTo(URL.class));
     }
     
     @Test
@@ -200,26 +204,37 @@ public class AdapterMethodManagerIT {
         registerProvider(new SingleMethodProvider());
         registerProvider(new MultipleMethodsProvider());
         
-        final int value = (int)System.currentTimeMillis();
-        final TestAdaptable ta = new TestAdaptable(value);
+        assertAdapted(parent);
         
         {
-            final Long result = ta.adaptTo(Long.class);
-            assertNotNull("Expecting non-null adapted Long", result);
-            assertEquals("Expecting correct Long adapted value", value, result.longValue());
-        }
-        
-        {
-            final Integer result = ta.adaptTo(Integer.class);
+            final Integer result = parent.adaptTo(Integer.class);
             assertNotNull("Expecting non-null adapted Integer", result);
-            assertEquals("Expecting correct Integer adapted value", value, result.intValue());
+            assertEquals("Expecting correct Integer adapted value", (int)parent.value, result.intValue());
         }
         
         {
-            final URL result = ta.adaptTo(URL.class);
-            final URL expected = new URL("http://example.com/" + value);
+            final URL result = parent.adaptTo(URL.class);
+            final URL expected = new URL("http://example.com/" + parent.value);
             assertNotNull("Expecting non-null adapted URL", result);
             assertEquals("Expecting correctly adapted URL", expected, result);
         }
     }
-}
\ No newline at end of file
+    
+    private void assertAdapted(TestAdaptable src) {
+            final Long result = src.adaptTo(Long.class);
+            assertNotNull("Expecting non-null result", result);
+            assertEquals("Expecting correct value", src.value, result.longValue());
+    }
+    
+    @Test
+    public void testParentToLong() {
+        registerProvider(new SingleMethodProvider());
+        assertAdapted(parent);
+    }
+    
+    @Test
+    public void testChildToLong() {
+        registerProvider(new SingleMethodProvider());
+        assertAdapted(parent);
+    }
+ }
\ No newline at end of file



Mime
View raw message