Author: csierra
Date: Thu May 17 12:56:34 2018
New Revision: 1831777

URL: http://svn.apache.org/viewvc?rev=1831777&view=rev
Log:
[Component-DSL] Rearrange source

Modified:
    
aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
    
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java

Modified: 
aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java?rev=1831777&r1=1831776&r2=1831777&view=diff
==============================================================================
--- 
aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
 (original)
+++ 
aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
 Thu May 17 12:56:34 2018
@@ -220,16 +220,24 @@ public interface OSGi<T> extends OSGiRun
                return new ConfigurationsOSGiImpl(factoryPid);
        }
 
-       static OSGi<Void> effects(Runnable onAdding, Runnable onRemoving) {
-               return new EffectsOSGi(onAdding, onRemoving);
-       }
-
        static OSGi<Void> effect(Effect<Void> effect) {
                return new EffectsOSGi(
                        () -> effect.getOnIncoming().accept(null),
                        () -> effect.getOnLeaving().accept(null));
        }
 
+       static OSGi<Void> effects(Runnable onAdding, Runnable onRemoving) {
+               return new EffectsOSGi(onAdding, onRemoving);
+       }
+
+       static <T> OSGi<T> fromOsgiRunnable(OSGiRunnable<T> runnable) {
+               return getOsgiFactory().create(runnable);
+       }
+
+       static OSGiFactory getOsgiFactory() {
+               return OSGiImpl::create;
+       }
+
        static OSGi<Void> ignore(OSGi<?> program) {
                return new IgnoreImpl(program);
        }
@@ -653,12 +661,4 @@ public interface OSGi<T> extends OSGiRun
                        (bundleContext, op) -> run(bundleContext, 
fun.transform(op)));
        }
 
-       static <T> OSGi<T> fromOsgiRunnable(OSGiRunnable<T> runnable) {
-               return getOsgiFactory().create(runnable);
-       }
-
-       static OSGiFactory getOsgiFactory() {
-               return OSGiImpl::create;
-       }
-
 }

Modified: 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java?rev=1831777&r1=1831776&r2=1831777&view=diff
==============================================================================
--- 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
 (original)
+++ 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
 Thu May 17 12:56:34 2018
@@ -72,8 +72,167 @@ import static org.junit.Assert.assertTru
 
 public class DSLTest {
 
-    static BundleContext bundleContext = FrameworkUtil.getBundle(
-        DSLTest.class).getBundleContext();
+    @Test
+    public void testAccumulate() {
+        ArrayList<List<String>> lists = new ArrayList<>();
+
+        ArrayList<List<String>> expected = new ArrayList<>();
+
+        ArrayList<List<String>> gone = new ArrayList<>();
+
+        expected.add(Collections.emptyList());
+
+        OSGi<List<String>> osgi = accumulate(
+            serviceReferences(Service.class).map(
+                this::getId
+            )
+        ).effects(lists::add, gone::add);
+
+        OSGiResult run = osgi.run(bundleContext);
+
+        ServiceRegistration<Service> serviceRegistrationOne =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        expected.add(
+            Collections.singletonList(getId(serviceRegistrationOne)));
+
+        serviceRegistrationOne.unregister();
+
+        expected.add(Collections.emptyList());
+
+        serviceRegistrationOne =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        expected.add(
+            Collections.singletonList(getId(serviceRegistrationOne)));
+
+        ServiceRegistration<Service> serviceRegistrationTwo =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        expected.add(
+            Arrays.asList(
+                getId(serviceRegistrationOne),
+                getId(serviceRegistrationTwo)));
+
+        serviceRegistrationOne.unregister();
+
+        expected.add(
+            Collections.singletonList(
+                getId(serviceRegistrationTwo)));
+
+        serviceRegistrationTwo.unregister();
+
+        expected.add(Collections.emptyList());
+
+        assertEquals(expected, lists);
+
+        run.close();
+
+        assertEquals(lists, gone);
+    }
+
+    @Test
+    public void testAccumulateAtLeastOne() {
+        ArrayList<List<String>> lists = new ArrayList<>();
+
+        ArrayList<List<String>> expected = new ArrayList<>();
+
+        ArrayList<List<String>> gone = new ArrayList<>();
+
+        OSGi<List<String>> osgi =
+            accumulate(
+            serviceReferences(Service.class).map(this::getId)
+        ).filter(l -> !l.isEmpty()).effects(
+            lists::add, gone::add
+        );
+
+        OSGiResult run = osgi.run(bundleContext);
+
+        ServiceRegistration<Service> serviceRegistrationOne =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        expected.add(
+            Collections.singletonList(getId(serviceRegistrationOne)));
+
+        serviceRegistrationOne.unregister();
+
+        serviceRegistrationOne =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        expected.add(
+            Collections.singletonList(getId(serviceRegistrationOne)));
+
+        ServiceRegistration<Service> serviceRegistrationTwo =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        expected.add(
+            Arrays.asList(
+                getId(serviceRegistrationOne),
+                getId(serviceRegistrationTwo)));
+
+        serviceRegistrationOne.unregister();
+
+        expected.add(
+            Collections.singletonList(
+                getId(serviceRegistrationTwo)));
+
+        serviceRegistrationTwo.unregister();
+
+        assertEquals(expected, lists);
+
+        run.close();
+
+        assertEquals(lists, gone);
+    }
+
+    @Test
+    public void testApplicativeApplyTo() {
+        AtomicInteger integer = new AtomicInteger(0);
+
+        OSGi<Integer> program = just(5).applyTo(just((i) -> i + 5));
+
+        program.run(bundleContext, newValue -> {
+            integer.set(newValue);
+
+            return NOOP;
+        });
+
+        assertEquals(10, integer.get());
+    }
+
+    @Test
+    public void testApply() {
+        AtomicInteger integer = new AtomicInteger(0);
+
+        OSGi<Integer> program = OSGi.combine(
+            (a, b, c) -> a + b + c, just(5), just(5), just(5));
+
+        program.run(bundleContext, newValue -> {
+            integer.set(newValue);
+
+            return NOOP;
+        });
+
+        assertEquals(15, integer.get());
+    }
 
     @Test
     public void testCoalesce() {
@@ -184,221 +343,72 @@ public class DSLTest {
             effects);
     }
 
-
     @Test
-    public void testJust() {
-        AtomicInteger atomicInteger = new AtomicInteger(0);
-
-        OSGi<Integer> just = just(25);
+    public void testConfiguration() throws IOException, InterruptedException {
+        ServiceReference<ConfigurationAdmin> serviceReference =
+            bundleContext.getServiceReference(ConfigurationAdmin.class);
 
-        assertEquals(0, atomicInteger.get());
+        ConfigurationAdmin configurationAdmin = bundleContext.getService(
+            serviceReference);
 
-        try (OSGiResult result = just.run(
-            bundleContext, newValue -> {
-                atomicInteger.set(newValue);
+        AtomicReference<Dictionary<?,?>> atomicReference =
+            new AtomicReference<>(null);
 
-                return NOOP;
-            }))
-        {
-            assertEquals(25, atomicInteger.get());
-        }
+        Configuration configuration = null;
 
-        atomicInteger.set(0);
+        CountDownLatch countDownLatch = new CountDownLatch(1);
 
-        OSGi<Integer> map = just(25).map(s -> s + 5);
+        try(OSGiResult result =
+            configuration("test.configuration").run(
+                bundleContext,
+                x -> {
+                    atomicReference.set(x);
 
-        try (OSGiResult result = map.run(
-            bundleContext, newValue -> {
-                atomicInteger.set(newValue);
+                    countDownLatch.countDown();
 
-                return NOOP;
-            }))
+                    return NOOP;
+                }))
         {
-            assertEquals(30, atomicInteger.get());
-        }
+            assertNull(atomicReference.get());
 
-        atomicInteger.set(0);
+            configuration = configurationAdmin.getConfiguration(
+                "test.configuration");
 
-        OSGi<Integer> flatMap = just(25).flatMap(s -> just(s + 10));
+            configuration.update(new Hashtable<>());
 
-        try (OSGiResult result = flatMap.run(
-            bundleContext, newValue -> {
-                atomicInteger.set(newValue);
+            countDownLatch.await(10, TimeUnit.SECONDS);
 
-                return NOOP;
-            }))
-        {
-            assertEquals(35, atomicInteger.get());
+            assertNotNull(atomicReference.get());
         }
+        finally {
+            bundleContext.ungetService(serviceReference);
 
-        atomicInteger.set(0);
+            if (configuration != null) {
+                configuration.delete();
+            }
+        }
+    }
 
-        OSGi<Integer> filter = just(25).filter(s -> s % 2 == 0);
+    @Test
+    public void testConfigurations() throws IOException, InterruptedException {
+        ServiceReference<ConfigurationAdmin> serviceReference =
+            bundleContext.getServiceReference(ConfigurationAdmin.class);
 
-        try (OSGiResult result = filter.run(
-            bundleContext, newValue -> {
-                atomicInteger.set(newValue);
+        ConfigurationAdmin configurationAdmin = bundleContext.getService(
+            serviceReference);
 
-                return NOOP;
-            }))
-        {
-            assertEquals(0, atomicInteger.get());
-        }
+        AtomicReference<Dictionary<?,?>> atomicReference =
+            new AtomicReference<>(null);
 
-        atomicInteger.set(0);
+        CountDownLatch countDownLatch = new CountDownLatch(1);
 
-        filter = just(25).filter(s -> s % 2 != 0);
+        Configuration configuration = null;
 
-        try (OSGiResult result = filter.run(
-            bundleContext, newValue -> {
-                atomicInteger.set(newValue);
-
-                return NOOP;
-            }))
-        {
-            assertEquals(25, atomicInteger.get());
-        }
-
-    }
-
-    @Test
-    public void testServiceReferences() {
-        AtomicReference<CachingServiceReference<Service>> atomicReference =
-            new AtomicReference<>();
-
-        ServiceRegistration<Service> serviceRegistration = null;
-
-        try(
-            OSGiResult osGiResult =
-                serviceReferences(Service.class).
-                run(bundleContext, newValue -> {
-                    atomicReference.set(newValue);
-
-                    return NOOP;
-                })
-        ) {
-            assertNull(atomicReference.get());
-
-            serviceRegistration = bundleContext.registerService(
-                Service.class, new Service(), new Hashtable<>());
-
-            assertEquals(
-                serviceRegistration.getReference(),
-                atomicReference.get().getServiceReference());
-        }
-        finally {
-            if (serviceRegistration != null) {
-                serviceRegistration.unregister();
-            }
-        }
-    }
-
-    @Test
-    public void testServiceReferencesAndClose() {
-        AtomicReference<CachingServiceReference<Service>> atomicReference =
-            new AtomicReference<>();
-
-        OSGi<CachingServiceReference<Service>> program =
-            serviceReferences(Service.class).flatMap(ref ->
-            onClose(() -> atomicReference.set(null)).
-            then(just(ref))
-        );
-
-        ServiceRegistration<Service> serviceRegistration = null;
-
-        try(
-            OSGiResult osGiResult = program.run(
-            bundleContext, newValue -> {
-                    atomicReference.set(newValue);
-
-                    return NOOP;
-                })
-        ) {
-            assertNull(atomicReference.get());
-
-            serviceRegistration = bundleContext.registerService(
-                Service.class, new Service(), new Hashtable<>());
-
-            assertEquals(
-                serviceRegistration.getReference(),
-                atomicReference.get().getServiceReference());
-        }
-        finally {
-            if (serviceRegistration != null) {
-                serviceRegistration.unregister();
-            }
-        }
-
-        assertNull(atomicReference.get());
-    }
-
-    @Test
-    public void testConfiguration() throws IOException, InterruptedException {
-        ServiceReference<ConfigurationAdmin> serviceReference =
-            bundleContext.getServiceReference(ConfigurationAdmin.class);
-
-        ConfigurationAdmin configurationAdmin = bundleContext.getService(
-            serviceReference);
-
-        AtomicReference<Dictionary<?,?>> atomicReference =
-            new AtomicReference<>(null);
-
-        Configuration configuration = null;
-
-        CountDownLatch countDownLatch = new CountDownLatch(1);
-
-        try(OSGiResult result =
-            configuration("test.configuration").run(
-                bundleContext,
-                x -> {
-                    atomicReference.set(x);
-
-                    countDownLatch.countDown();
-
-                    return NOOP;
-                }))
-        {
-            assertNull(atomicReference.get());
-
-            configuration = configurationAdmin.getConfiguration(
-                "test.configuration");
-
-            configuration.update(new Hashtable<>());
-
-            countDownLatch.await(10, TimeUnit.SECONDS);
-
-            assertNotNull(atomicReference.get());
-        }
-        finally {
-            bundleContext.ungetService(serviceReference);
-
-            if (configuration != null) {
-                configuration.delete();
-            }
-        }
-    }
-
-
-    @Test
-    public void testConfigurations() throws IOException, InterruptedException {
-        ServiceReference<ConfigurationAdmin> serviceReference =
-            bundleContext.getServiceReference(ConfigurationAdmin.class);
-
-        ConfigurationAdmin configurationAdmin = bundleContext.getService(
-            serviceReference);
-
-        AtomicReference<Dictionary<?,?>> atomicReference =
-            new AtomicReference<>(null);
-
-        CountDownLatch countDownLatch = new CountDownLatch(1);
-
-        Configuration configuration = null;
-
-        try(OSGiResult result =
-            configurations("test.configuration").run(
-                bundleContext,
-                x -> {
-                    atomicReference.set(x);
+        try(OSGiResult result =
+            configurations("test.configuration").run(
+                bundleContext,
+                x -> {
+                    atomicReference.set(x);
 
                     countDownLatch.countDown();
 
@@ -427,26 +437,6 @@ public class DSLTest {
     }
 
     @Test
-    public void testRegister() {
-        assertNull(bundleContext.getServiceReference(Service.class));
-
-        Service service = new Service();
-
-        OSGiResult result = register(
-            Service.class, service, new HashMap<>()).
-            run(bundleContext);
-
-        ServiceReference<Service> serviceReference =
-            bundleContext.getServiceReference(Service.class);
-
-        assertEquals(service, bundleContext.getService(serviceReference));
-
-        result.close();
-
-        assertNull(bundleContext.getServiceReference(Service.class));
-    }
-
-    @Test
     public void testConfigurationsAndRegistrations()
         throws InvalidSyntaxException, IOException, InterruptedException {
 
@@ -592,67 +582,77 @@ public class DSLTest {
     }
 
     @Test
-    public void testProgrammaticDependencies() {
-        AtomicBoolean executed = new AtomicBoolean(false);
-        AtomicBoolean closed = new AtomicBoolean(false);
-
-        String[] filters = {
-            "(key=service one)",
-            "(key=service two)",
-            "(key=service three)"
-        };
+    public void testHighestRankingDiscards() {
+        ArrayList<ServiceReference<?>> discards = new ArrayList<>();
 
-        OSGi<?> program =
-            onClose(() -> closed.set(true)).foreach(
-            ign -> executed.set(true)
-        );
+        OSGi<CachingServiceReference<Service>> program = highest(
+            serviceReferences(Service.class),
+            Comparator.naturalOrder(),
+            dp ->
+                dp.map(CachingServiceReference::getServiceReference).effects(
+                    discards::add, discards::remove).then(nothing()));
 
-        for (String filter : filters) {
-            program = services(filter).then(program);
-        }
+        assertTrue(discards.isEmpty());
 
         try (OSGiResult result = program.run(bundleContext)) {
-            assertFalse(closed.get());
-            assertFalse(executed.get());
-
             ServiceRegistration<Service> serviceRegistrationOne =
                 bundleContext.registerService(
                     Service.class, new Service(),
                     new Hashtable<String, Object>() {{
-                        put("key", "service one");
+                        put("service.ranking", 0);
                     }});
 
-            assertFalse(closed.get());
-            assertFalse(executed.get());
+            assertEquals(Collections.emptyList(), discards);
 
             ServiceRegistration<Service> serviceRegistrationTwo =
                 bundleContext.registerService(
                     Service.class, new Service(),
                     new Hashtable<String, Object>() {{
-                        put("key", "service two");
+                        put("service.ranking", 1);
                     }});
 
-            assertFalse(closed.get());
-            assertFalse(executed.get());
+            assertEquals(
+                Collections.singletonList(
+                    serviceRegistrationOne.getReference()),
+                discards);
 
-            ServiceRegistration<Service> serviceRegistrationThree =
+            ServiceRegistration<Service> serviceRegistrationMinusOne =
                 bundleContext.registerService(
                     Service.class, new Service(),
                     new Hashtable<String, Object>() {{
-                        put("key", "service three");
+                        put("service.ranking", -1);
                     }});
 
-            assertFalse(closed.get());
-            assertTrue(executed.get());
+            assertEquals(
+                Arrays.asList(
+                    serviceRegistrationOne.getReference(),
+                    serviceRegistrationMinusOne.getReference()),
+                discards);
+
+            serviceRegistrationTwo.unregister();
+
+            assertEquals(
+                Arrays.asList(serviceRegistrationMinusOne.getReference()),
+                discards);
 
             serviceRegistrationOne.unregister();
 
-            assertTrue(closed.get());
+            assertTrue(discards.isEmpty());
 
-            serviceRegistrationTwo.unregister();
-            serviceRegistrationThree.unregister();
-        }
+            serviceRegistrationOne =
+                bundleContext.registerService(
+                    Service.class, new Service(),
+                    new Hashtable<String, Object>() {{
+                        put("service.ranking", 0);
+                    }});
+
+            assertEquals(
+                Arrays.asList(serviceRegistrationMinusOne.getReference()),
+                discards);
 
+            serviceRegistrationMinusOne.unregister();
+            serviceRegistrationOne.unregister();
+        }
     }
 
     @Test
@@ -730,337 +730,79 @@ public class DSLTest {
     }
 
     @Test
-    public void testHighestRankingDiscards() {
-        ArrayList<ServiceReference<?>> discards = new ArrayList<>();
+    public void testJust() {
+        AtomicInteger atomicInteger = new AtomicInteger(0);
 
-        OSGi<CachingServiceReference<Service>> program = highest(
-            serviceReferences(Service.class),
-            Comparator.naturalOrder(),
-            dp ->
-                dp.map(CachingServiceReference::getServiceReference).effects(
-                    discards::add, discards::remove).then(nothing()));
+        OSGi<Integer> just = just(25);
 
-        assertTrue(discards.isEmpty());
+        assertEquals(0, atomicInteger.get());
 
-        try (OSGiResult result = program.run(bundleContext)) {
-            ServiceRegistration<Service> serviceRegistrationOne =
-                bundleContext.registerService(
-                    Service.class, new Service(),
-                    new Hashtable<String, Object>() {{
-                        put("service.ranking", 0);
-                    }});
-
-            assertEquals(Collections.emptyList(), discards);
-
-            ServiceRegistration<Service> serviceRegistrationTwo =
-                bundleContext.registerService(
-                    Service.class, new Service(),
-                    new Hashtable<String, Object>() {{
-                        put("service.ranking", 1);
-                    }});
-
-            assertEquals(
-                Collections.singletonList(
-                    serviceRegistrationOne.getReference()),
-                discards);
-
-            ServiceRegistration<Service> serviceRegistrationMinusOne =
-                bundleContext.registerService(
-                    Service.class, new Service(),
-                    new Hashtable<String, Object>() {{
-                        put("service.ranking", -1);
-                    }});
-
-            assertEquals(
-                Arrays.asList(
-                    serviceRegistrationOne.getReference(),
-                    serviceRegistrationMinusOne.getReference()),
-                discards);
-
-            serviceRegistrationTwo.unregister();
-
-            assertEquals(
-                Arrays.asList(serviceRegistrationMinusOne.getReference()),
-                discards);
-
-            serviceRegistrationOne.unregister();
-
-            assertTrue(discards.isEmpty());
-
-            serviceRegistrationOne =
-                bundleContext.registerService(
-                    Service.class, new Service(),
-                    new Hashtable<String, Object>() {{
-                        put("service.ranking", 0);
-                    }});
-
-            assertEquals(
-                Arrays.asList(serviceRegistrationMinusOne.getReference()),
-                discards);
+        try (OSGiResult result = just.run(
+            bundleContext, newValue -> {
+                atomicInteger.set(newValue);
 
-            serviceRegistrationMinusOne.unregister();
-            serviceRegistrationOne.unregister();
+                return NOOP;
+            }))
+        {
+            assertEquals(25, atomicInteger.get());
         }
-    }
-
-    @Test
-    public void testAccumulate() {
-        ArrayList<List<String>> lists = new ArrayList<>();
-
-        ArrayList<List<String>> expected = new ArrayList<>();
-
-        ArrayList<List<String>> gone = new ArrayList<>();
 
-        expected.add(Collections.emptyList());
-
-        OSGi<List<String>> osgi = accumulate(
-            serviceReferences(Service.class).map(
-                this::getId
-            )
-        ).effects(lists::add, gone::add);
-
-        OSGiResult run = osgi.run(bundleContext);
-
-        ServiceRegistration<Service> serviceRegistrationOne =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                }});
-
-        expected.add(
-            Collections.singletonList(getId(serviceRegistrationOne)));
-
-        serviceRegistrationOne.unregister();
-
-        expected.add(Collections.emptyList());
-
-        serviceRegistrationOne =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                }});
-
-        expected.add(
-            Collections.singletonList(getId(serviceRegistrationOne)));
-
-        ServiceRegistration<Service> serviceRegistrationTwo =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                }});
-
-        expected.add(
-            Arrays.asList(
-                getId(serviceRegistrationOne),
-                getId(serviceRegistrationTwo)));
-
-        serviceRegistrationOne.unregister();
-
-        expected.add(
-            Collections.singletonList(
-                getId(serviceRegistrationTwo)));
-
-        serviceRegistrationTwo.unregister();
-
-        expected.add(Collections.emptyList());
-
-        assertEquals(expected, lists);
-
-        run.close();
-
-        assertEquals(lists, gone);
-    }
-
-    public String getId(CachingServiceReference<?> csr) {
-        return csr.getProperty("service.id").toString();
-    }
-
-    public String getId(ServiceRegistration<?> sr) {
-        return sr.getReference().getProperty("service.id").toString();
-    }
-
-    @Test
-    public void testAccumulateAtLeastOne() {
-        ArrayList<List<String>> lists = new ArrayList<>();
-
-        ArrayList<List<String>> expected = new ArrayList<>();
-
-        ArrayList<List<String>> gone = new ArrayList<>();
-
-        OSGi<List<String>> osgi =
-            accumulate(
-            serviceReferences(Service.class).map(this::getId)
-        ).filter(l -> !l.isEmpty()).effects(
-            lists::add, gone::add
-        );
-
-        OSGiResult run = osgi.run(bundleContext);
-
-        ServiceRegistration<Service> serviceRegistrationOne =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                }});
-
-        expected.add(
-            Collections.singletonList(getId(serviceRegistrationOne)));
-
-        serviceRegistrationOne.unregister();
-
-        serviceRegistrationOne =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                }});
-
-        expected.add(
-            Collections.singletonList(getId(serviceRegistrationOne)));
-
-        ServiceRegistration<Service> serviceRegistrationTwo =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                }});
-
-        expected.add(
-            Arrays.asList(
-                getId(serviceRegistrationOne),
-                getId(serviceRegistrationTwo)));
-
-        serviceRegistrationOne.unregister();
-
-        expected.add(
-            Collections.singletonList(
-                getId(serviceRegistrationTwo)));
-
-        serviceRegistrationTwo.unregister();
-
-        assertEquals(expected, lists);
-
-        run.close();
-
-        assertEquals(lists, gone);
-    }
-
-    @Test
-    public void testServicesMap() {
-        AtomicReference<Map<String, String>> map = new AtomicReference<>(
-            new HashMap<>());
-        List<Map<String, String>> maps = new ArrayList<>();
-        List<Map<String, String>> gone = new ArrayList<>();
-
-        OSGi<Map<String, String>> mapOSGi =
-            Utils.accumulateInMap(
-                serviceReferences(Service.class),
-                csr -> 
just(Arrays.asList(canonicalize(csr.getProperty("key")))),
-                csr -> just(getId(csr)));
-
-        OSGi<Map<String, String>> effects =
-            mapOSGi.
-                effects(map::set, __ -> {}).
-                effects(maps::add, gone::add);
-
-        OSGiResult result = effects.run(bundleContext);
-
-        assertEquals(Collections.emptyMap(), map.get());
-
-        ServiceRegistration<Service> serviceRegistrationOne =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                    put("key", new String[]{"a"});
-                }});
-
-        assertEquals(
-            new HashMap<String, String>() {{
-                put("a", getId(serviceRegistrationOne));
-            }},
-            map.get());
-
-        ServiceRegistration<Service> serviceRegistrationTwo =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                    put("key", new String[]{"b"});
-                }});
-
-        assertEquals(
-            new HashMap<String, String>() {{
-                put("a", getId(serviceRegistrationOne));
-                put("b", getId(serviceRegistrationTwo));
-            }},
-            map.get());
-
-        ServiceRegistration<Service> serviceRegistrationThree =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                    put("key", new String[]{"a", "b"});
-                    put("service.ranking", 10);
-                }});
-
-        assertEquals(
-            new HashMap<String, String>() {{
-                put("a", getId(serviceRegistrationThree));
-                put("b", getId(serviceRegistrationThree));
-            }},
-            map.get());
-
-        serviceRegistrationThree.unregister();
-
-        assertEquals(
-            new HashMap<String, String>() {{
-                put("a", getId(serviceRegistrationOne));
-                put("b", getId(serviceRegistrationTwo));
-            }},
-            map.get());
+        atomicInteger.set(0);
 
-        serviceRegistrationTwo.unregister();
+        OSGi<Integer> map = just(25).map(s -> s + 5);
 
-        assertEquals(
-            new HashMap<String, String>() {{
-                put("a", getId(serviceRegistrationOne));
-            }},
-            map.get());
+        try (OSGiResult result = map.run(
+            bundleContext, newValue -> {
+                atomicInteger.set(newValue);
 
-        serviceRegistrationOne.unregister();
+                return NOOP;
+            }))
+        {
+            assertEquals(30, atomicInteger.get());
+        }
 
-        assertEquals(Collections.emptyMap(), map.get());
+        atomicInteger.set(0);
 
-        result.close();
+        OSGi<Integer> flatMap = just(25).flatMap(s -> just(s + 10));
 
-        assertEquals(maps, gone);
-    }
+        try (OSGiResult result = flatMap.run(
+            bundleContext, newValue -> {
+                atomicInteger.set(newValue);
 
-    @Test
-    public void testApplicativeApplyTo() {
-        AtomicInteger integer = new AtomicInteger(0);
+                return NOOP;
+            }))
+        {
+            assertEquals(35, atomicInteger.get());
+        }
 
-        OSGi<Integer> program = just(5).applyTo(just((i) -> i + 5));
+        atomicInteger.set(0);
 
-        program.run(bundleContext, newValue -> {
-            integer.set(newValue);
+        OSGi<Integer> filter = just(25).filter(s -> s % 2 == 0);
 
-            return NOOP;
-        });
+        try (OSGiResult result = filter.run(
+            bundleContext, newValue -> {
+                atomicInteger.set(newValue);
 
-        assertEquals(10, integer.get());
-    }
+                return NOOP;
+            }))
+        {
+            assertEquals(0, atomicInteger.get());
+        }
 
-    @Test
-    public void testApply() {
-        AtomicInteger integer = new AtomicInteger(0);
+        atomicInteger.set(0);
 
-        OSGi<Integer> program = OSGi.combine(
-            (a, b, c) -> a + b + c, just(5), just(5), just(5));
+        filter = just(25).filter(s -> s % 2 != 0);
 
-        program.run(bundleContext, newValue -> {
-            integer.set(newValue);
+        try (OSGiResult result = filter.run(
+            bundleContext, newValue -> {
+                atomicInteger.set(newValue);
 
-            return NOOP;
-        });
+                return NOOP;
+            }))
+        {
+            assertEquals(25, atomicInteger.get());
+        }
 
-        assertEquals(15, integer.get());
     }
 
     @Test
@@ -1094,6 +836,36 @@ public class DSLTest {
     }
 
     @Test
+    public void testOnCloseWithError() {
+        ArrayList<Object> result = new ArrayList<>();
+        ArrayList<Object> left = new ArrayList<>();
+
+        OSGi<Integer> program = just(
+            Arrays.asList(1, 2, 3, 4, 5, 6)
+        ).recoverWith(
+            (__, e) -> just(0)
+        ).flatMap(t ->
+            onClose(() -> left.add(t)).then(just(t))
+        ).
+        flatMap(t -> {
+            if (t % 2 != 0) {
+                throw new RuntimeException();
+            }
+
+            return just(t);
+        });
+
+        try (OSGiResult run = program.run(bundleContext, e -> {
+            result.add(e);
+
+            return NOOP;
+        })) {
+            assertEquals(Arrays.asList(0, 2, 0, 4, 0, 6), result);
+            assertEquals(Arrays.asList(1, 3, 5), left);
+        }
+    }
+
+    @Test
     public void testOnce() {
         ProbeImpl<Integer> probe = new ProbeImpl<>();
 
@@ -1139,50 +911,67 @@ public class DSLTest {
     }
 
     @Test
-    public void testServiceReferenceRefresher() {
-        ServiceRegistration<Service> serviceRegistration =
-            bundleContext.registerService(
-                Service.class, new Service(),
-                new Hashtable<String, Object>() {{
-                    put("good", 0);
-                    put("bad", 0);
-                }});
+    public void testProgrammaticDependencies() {
+        AtomicBoolean executed = new AtomicBoolean(false);
+        AtomicBoolean closed = new AtomicBoolean(false);
 
-        AtomicInteger atomicInteger = new AtomicInteger();
+        String[] filters = {
+            "(key=service one)",
+            "(key=service two)",
+            "(key=service three)"
+        };
 
-        try {
-            /* reload only when property "good" has changed */
-            OSGi<?> program = serviceReferences(
-                Service.class, csr -> csr.isDirty("good")).map(
-                    csr -> csr.getProperty("good"));
+        OSGi<?> program =
+            onClose(() -> closed.set(true)).foreach(
+            ign -> executed.set(true)
+        );
 
-            program.run(bundleContext, (__) -> {
-                atomicInteger.incrementAndGet();
+        for (String filter : filters) {
+            program = services(filter).then(program);
+        }
 
-                return NOOP;
-            });
+        try (OSGiResult result = program.run(bundleContext)) {
+            assertFalse(closed.get());
+            assertFalse(executed.get());
 
-            assertEquals(1, atomicInteger.get());
+            ServiceRegistration<Service> serviceRegistrationOne =
+                bundleContext.registerService(
+                    Service.class, new Service(),
+                    new Hashtable<String, Object>() {{
+                        put("key", "service one");
+                    }});
 
-            serviceRegistration.setProperties(
-                new Hashtable<String, Object>() {{
-                    put("good", 0);
-                    put("bad", 1);
-                }});
+            assertFalse(closed.get());
+            assertFalse(executed.get());
 
-            assertEquals(1, atomicInteger.get());
+            ServiceRegistration<Service> serviceRegistrationTwo =
+                bundleContext.registerService(
+                    Service.class, new Service(),
+                    new Hashtable<String, Object>() {{
+                        put("key", "service two");
+                    }});
 
-            serviceRegistration.setProperties(
-                new Hashtable<String, Object>() {{
-                    put("good", 1);
-                    put("bad", 1);
-                }});
+            assertFalse(closed.get());
+            assertFalse(executed.get());
 
-            assertEquals(2, atomicInteger.get());
-        }
-        finally {
-            serviceRegistration.unregister();
+            ServiceRegistration<Service> serviceRegistrationThree =
+                bundleContext.registerService(
+                    Service.class, new Service(),
+                    new Hashtable<String, Object>() {{
+                        put("key", "service three");
+                    }});
+
+            assertFalse(closed.get());
+            assertTrue(executed.get());
+
+            serviceRegistrationOne.unregister();
+
+            assertTrue(closed.get());
+
+            serviceRegistrationTwo.unregister();
+            serviceRegistrationThree.unregister();
         }
+
     }
 
     @Test
@@ -1257,36 +1046,234 @@ public class DSLTest {
     }
 
     @Test
-    public void testOnCloseWithError() {
-        ArrayList<Object> result = new ArrayList<>();
-        ArrayList<Object> left = new ArrayList<>();
+    public void testRegister() {
+        assertNull(bundleContext.getServiceReference(Service.class));
 
-        OSGi<Integer> program = just(
-            Arrays.asList(1, 2, 3, 4, 5, 6)
-        ).recoverWith(
-            (__, e) -> just(0)
-        ).flatMap(t ->
-            onClose(() -> left.add(t)).then(just(t))
-        ).
-        flatMap(t -> {
-            if (t % 2 != 0) {
-                throw new RuntimeException();
+        Service service = new Service();
+
+        OSGiResult result = register(
+            Service.class, service, new HashMap<>()).
+            run(bundleContext);
+
+        ServiceReference<Service> serviceReference =
+            bundleContext.getServiceReference(Service.class);
+
+        assertEquals(service, bundleContext.getService(serviceReference));
+
+        result.close();
+
+        assertNull(bundleContext.getServiceReference(Service.class));
+    }
+
+    @Test
+    public void testServiceReferenceRefresher() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("good", 0);
+                    put("bad", 0);
+                }});
+
+        AtomicInteger atomicInteger = new AtomicInteger();
+
+        try {
+            /* reload only when property "good" has changed */
+            OSGi<?> program = serviceReferences(
+                Service.class, csr -> csr.isDirty("good")).map(
+                    csr -> csr.getProperty("good"));
+
+            program.run(bundleContext, (__) -> {
+                atomicInteger.incrementAndGet();
+
+                return NOOP;
+            });
+
+            assertEquals(1, atomicInteger.get());
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("good", 0);
+                    put("bad", 1);
+                }});
+
+            assertEquals(1, atomicInteger.get());
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("good", 1);
+                    put("bad", 1);
+                }});
+
+            assertEquals(2, atomicInteger.get());
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testServiceReferences() {
+        AtomicReference<CachingServiceReference<Service>> atomicReference =
+            new AtomicReference<>();
+
+        ServiceRegistration<Service> serviceRegistration = null;
+
+        try(
+            OSGiResult osGiResult =
+                serviceReferences(Service.class).
+                run(bundleContext, newValue -> {
+                    atomicReference.set(newValue);
+
+                    return NOOP;
+                })
+        ) {
+            assertNull(atomicReference.get());
+
+            serviceRegistration = bundleContext.registerService(
+                Service.class, new Service(), new Hashtable<>());
+
+            assertEquals(
+                serviceRegistration.getReference(),
+                atomicReference.get().getServiceReference());
+        }
+        finally {
+            if (serviceRegistration != null) {
+                serviceRegistration.unregister();
             }
+        }
+    }
 
-            return just(t);
-        });
+    @Test
+    public void testServiceReferencesAndClose() {
+        AtomicReference<CachingServiceReference<Service>> atomicReference =
+            new AtomicReference<>();
 
-        try (OSGiResult run = program.run(bundleContext, e -> {
-            result.add(e);
+        OSGi<CachingServiceReference<Service>> program =
+            serviceReferences(Service.class).flatMap(ref ->
+            onClose(() -> atomicReference.set(null)).
+            then(just(ref))
+        );
 
-            return NOOP;
-        })) {
-            assertEquals(Arrays.asList(0, 2, 0, 4, 0, 6), result);
-            assertEquals(Arrays.asList(1, 3, 5), left);
+        ServiceRegistration<Service> serviceRegistration = null;
+
+        try(
+            OSGiResult osGiResult = program.run(
+            bundleContext, newValue -> {
+                    atomicReference.set(newValue);
+
+                    return NOOP;
+                })
+        ) {
+            assertNull(atomicReference.get());
+
+            serviceRegistration = bundleContext.registerService(
+                Service.class, new Service(), new Hashtable<>());
+
+            assertEquals(
+                serviceRegistration.getReference(),
+                atomicReference.get().getServiceReference());
+        }
+        finally {
+            if (serviceRegistration != null) {
+                serviceRegistration.unregister();
+            }
         }
+
+        assertNull(atomicReference.get());
     }
 
-    private class Service {}
+    @Test
+    public void testServicesMap() {
+        AtomicReference<Map<String, String>> map = new AtomicReference<>(
+            new HashMap<>());
+        List<Map<String, String>> maps = new ArrayList<>();
+        List<Map<String, String>> gone = new ArrayList<>();
+
+        OSGi<Map<String, String>> mapOSGi =
+            Utils.accumulateInMap(
+                serviceReferences(Service.class),
+                csr -> 
just(Arrays.asList(canonicalize(csr.getProperty("key")))),
+                csr -> just(getId(csr)));
+
+        OSGi<Map<String, String>> effects =
+            mapOSGi.
+                effects(map::set, __ -> {}).
+                effects(maps::add, gone::add);
+
+        OSGiResult result = effects.run(bundleContext);
+
+        assertEquals(Collections.emptyMap(), map.get());
+
+        ServiceRegistration<Service> serviceRegistrationOne =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("key", new String[]{"a"});
+                }});
+
+        assertEquals(
+            new HashMap<String, String>() {{
+                put("a", getId(serviceRegistrationOne));
+            }},
+            map.get());
+
+        ServiceRegistration<Service> serviceRegistrationTwo =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("key", new String[]{"b"});
+                }});
+
+        assertEquals(
+            new HashMap<String, String>() {{
+                put("a", getId(serviceRegistrationOne));
+                put("b", getId(serviceRegistrationTwo));
+            }},
+            map.get());
+
+        ServiceRegistration<Service> serviceRegistrationThree =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("key", new String[]{"a", "b"});
+                    put("service.ranking", 10);
+                }});
+
+        assertEquals(
+            new HashMap<String, String>() {{
+                put("a", getId(serviceRegistrationThree));
+                put("b", getId(serviceRegistrationThree));
+            }},
+            map.get());
+
+        serviceRegistrationThree.unregister();
+
+        assertEquals(
+            new HashMap<String, String>() {{
+                put("a", getId(serviceRegistrationOne));
+                put("b", getId(serviceRegistrationTwo));
+            }},
+            map.get());
+
+        serviceRegistrationTwo.unregister();
+
+        assertEquals(
+            new HashMap<String, String>() {{
+                put("a", getId(serviceRegistrationOne));
+            }},
+            map.get());
+
+        serviceRegistrationOne.unregister();
+
+        assertEquals(Collections.emptyMap(), map.get());
+
+        result.close();
+
+        assertEquals(maps, gone);
+    }
+    static BundleContext bundleContext = FrameworkUtil.getBundle(
+        DSLTest.class).getBundleContext();
 
     private static String[] canonicalize(Object propertyValue) {
         if (propertyValue == null) {
@@ -1308,4 +1295,14 @@ public class DSLTest {
         return new String[]{propertyValue.toString()};
     }
 
+    private String getId(CachingServiceReference<?> csr) {
+        return csr.getProperty("service.id").toString();
+    }
+
+    private String getId(ServiceRegistration<?> sr) {
+        return sr.getReference().getProperty("service.id").toString();
+    }
+
+    private class Service {}
+
 }


Reply via email to