You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/07/01 17:45:35 UTC

svn commit: r1498529 - /sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java

Author: bdelacretaz
Date: Mon Jul  1 15:45:35 2013
New Revision: 1498529

URL: http://svn.apache.org/r1498529
Log:
SLING-2938 - more readable tests

Modified:
    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/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=1498529&r1=1498528&r2=1498529&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 Mon Jul  1 15:45:35 2013
@@ -31,12 +31,16 @@ import static org.ops4j.pax.exam.CoreOpt
 import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.inject.Inject;
 
 import org.apache.sling.api.adapter.AdapterMethodsProvider;
 import org.apache.sling.api.adapter.SlingAdaptable;
 import org.apache.sling.api.annotations.Adapter;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Option;
@@ -51,6 +55,8 @@ public class AdapterMethodManagerIT {
     @Inject
     BundleContext bundleContext;
     
+    private List<ServiceRegistration> registeredServices;
+    
     static class TestAdaptable extends SlingAdaptable {
         final long value;
         
@@ -62,31 +68,15 @@ public class AdapterMethodManagerIT {
     /** Example/test AdapterMethodsProvider that converts TestAdaptable to Long */
     public static class SingleMethodProvider implements AdapterMethodsProvider {
         
-        private final ServiceRegistration reg;
-        
-        SingleMethodProvider(BundleContext ctx) {
-            reg = ctx.registerService(AdapterMethodsProvider.class.getName(), this, null);
-        }
-        
         @Adapter
         public Long adapt(TestAdaptable src) {
             return new Long(src.value);
         }
-        
-        public void unregister() {
-            reg.unregister();
-        }
     }
     
     /** Example/test AdapterMethodsProvider that converts TestAdaptable to Integer and URL */
     public static class MultipleMethodsProvider implements AdapterMethodsProvider {
         
-        private final ServiceRegistration reg;
-        
-        MultipleMethodsProvider(BundleContext ctx) {
-            reg = ctx.registerService(AdapterMethodsProvider.class.getName(), this, null);
-        }
-        
         @Adapter
         public Integer adaptToInt(TestAdaptable src) {
             return new Integer((int)src.value);
@@ -101,10 +91,6 @@ public class AdapterMethodManagerIT {
                 throw new RuntimeException("Invalid URL " + url, mfu);
             }
         }
-        
-        public void unregister() {
-            reg.unregister();
-        }
     }
     
     @org.ops4j.pax.exam.Configuration
@@ -133,59 +119,69 @@ public class AdapterMethodManagerIT {
                 )); 
     }
     
+    private void registerProvider(AdapterMethodsProvider p) {
+        registeredServices.add(bundleContext.registerService(AdapterMethodsProvider.class.getName(), p, null));
+    }
+    
+    @Before
+    public void setup() {
+        registeredServices = new ArrayList<ServiceRegistration>();
+    }
+    
+    @After
+    public void cleanup() {
+        for(ServiceRegistration reg : registeredServices) {
+            reg.unregister();
+        }
+    }
+    
     @Test
-    public void testWithoutAdapter() {
-        final Long result = new TestAdaptable(12).adaptTo(Long.class);
-        assertNull("Expecting null adapted Long", result);
+    public void testNoAdapters() {
+        final TestAdaptable t = new TestAdaptable(42);
+        assertNull("Expecting no Long adaptation", t.adaptTo(Long.class));
+        assertNull("Expecting no Integer adaptation", t.adaptTo(Integer.class));
+        assertNull("Expecting no URL adaptation", t.adaptTo(URL.class));
     }
     
     @Test
-    public void testWithSingleAdapter() {
-        final SingleMethodProvider tamp = new SingleMethodProvider(bundleContext);
-        try {
-            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, result.longValue());
-            assertNull("Expecting no Integer adaptation", t.adaptTo(Integer.class));
-            assertNull("Expecting no URL adaptation", t.adaptTo(URL.class));
-        } finally {
-            tamp.unregister();
-        }
+    public void testSingleAdapter() {
+        registerProvider(new SingleMethodProvider());
+        
+        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, result.longValue());
+        assertNull("Expecting no Integer adaptation", t.adaptTo(Integer.class));
+        assertNull("Expecting no URL adaptation", t.adaptTo(URL.class));
     }
     
     @Test
-    public void testWithMultipleAdapters() throws MalformedURLException {
-        final SingleMethodProvider smp = new SingleMethodProvider(bundleContext);
-        final MultipleMethodsProvider mmp = new MultipleMethodsProvider(bundleContext);
-        try {
-            final int value = (int)System.currentTimeMillis();
-            final TestAdaptable ta = new TestAdaptable(value);
-            
-            {
-                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);
-                assertNotNull("Expecting non-null adapted Integer", result);
-                assertEquals("Expecting correct Integer adapted value", value, result.intValue());
-            }
-            
-            {
-                final URL result = ta.adaptTo(URL.class);
-                final URL expected = new URL("http://example.com/" + value);
-                assertNotNull("Expecting non-null adapted URL", result);
-                assertEquals("Expecting correctly adapted URL", expected, result);
-            }
-            
-            
-        } finally {
-            smp.unregister();
-            mmp.unregister();
+    public void testMultipleAdapters() throws MalformedURLException {
+        registerProvider(new SingleMethodProvider());
+        registerProvider(new MultipleMethodsProvider());
+        
+        final int value = (int)System.currentTimeMillis();
+        final TestAdaptable ta = new TestAdaptable(value);
+        
+        {
+            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);
+            assertNotNull("Expecting non-null adapted Integer", result);
+            assertEquals("Expecting correct Integer adapted value", value, result.intValue());
+        }
+        
+        {
+            final URL result = ta.adaptTo(URL.class);
+            final URL expected = new URL("http://example.com/" + value);
+            assertNotNull("Expecting non-null adapted URL", result);
+            assertEquals("Expecting correctly adapted URL", expected, result);
         }
     }
 }
\ No newline at end of file