Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CF09E10157 for ; Mon, 1 Jul 2013 15:46:01 +0000 (UTC) Received: (qmail 52987 invoked by uid 500); 1 Jul 2013 15:46:01 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 52907 invoked by uid 500); 1 Jul 2013 15:45:58 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 52644 invoked by uid 99); 1 Jul 2013 15:45:57 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Jul 2013 15:45:57 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Jul 2013 15:45:55 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 852AC2388831; Mon, 1 Jul 2013 15:45:35 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1498529 - /sling/whiteboard/bdelacretaz/adapter-methods/extensions-adapter/src/test/java/org/apache/sling/adapter/internal/AdapterMethodManagerIT.java Date: Mon, 01 Jul 2013 15:45:35 -0000 To: commits@sling.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130701154535.852AC2388831@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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(); + } + + @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