felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clem...@apache.org
Subject svn commit: r1549051 - in /felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src: main/java/org/apache/felix/ipojo/runtime/core/test/interceptors/ test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/
Date Sun, 08 Dec 2013 16:07:38 GMT
Author: clement
Date: Sun Dec  8 16:07:37 2013
New Revision: 1549051

URL: http://svn.apache.org/r1549051
Log:
Add a test checking an interceptor implementing both tracking and ranking.

Added:
    felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/interceptors/TrackerAndRankerInterceptor.java
    felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestCombinationOfInterceptors.java
      - copied, changed from r1532285, felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestRankingServices.java

Added: felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/interceptors/TrackerAndRankerInterceptor.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/interceptors/TrackerAndRankerInterceptor.java?rev=1549051&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/interceptors/TrackerAndRankerInterceptor.java
(added)
+++ felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/interceptors/TrackerAndRankerInterceptor.java
Sun Dec  8 16:07:37 2013
@@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.ipojo.runtime.core.test.interceptors;
+
+import org.apache.felix.ipojo.annotations.Component;
+import org.apache.felix.ipojo.annotations.Provides;
+import org.apache.felix.ipojo.annotations.ServiceProperty;
+import org.apache.felix.ipojo.dependency.interceptors.DefaultDependencyInterceptor;
+import org.apache.felix.ipojo.dependency.interceptors.ServiceRankingInterceptor;
+import org.apache.felix.ipojo.dependency.interceptors.ServiceTrackingInterceptor;
+import org.apache.felix.ipojo.dependency.interceptors.TransformedServiceReference;
+import org.apache.felix.ipojo.runtime.core.test.services.Setter;
+import org.apache.felix.ipojo.util.DependencyModel;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * An interceptor implements both ranking and tracking interfaces.
+ */
+@Component(immediate = true)
+@Provides
+public class TrackerAndRankerInterceptor extends DefaultDependencyInterceptor implements
Setter,
+        ServiceTrackingInterceptor, ServiceRankingInterceptor {
+
+    private static final int HIGH = 1;
+    private static final int LOW = 0;
+
+    @ServiceProperty
+    private String target;
+
+    GradeComparator comparator = new GradeComparator();
+    private int mode = HIGH;
+    private boolean reverse = false;
+
+    @Override
+    public List<ServiceReference> getServiceReferences(DependencyModel dependency,
List<ServiceReference> matching) {
+        List<ServiceReference> references = new ArrayList<ServiceReference>();
+        for (ServiceReference ref : matching) {
+            if (ref.getProperty("grade") != null) {
+                references.add(ref);
+            }
+        }
+
+        Collections.sort(references, comparator);
+        if (reverse) {
+            Collections.reverse(references);
+        }
+        return references;
+    }
+
+    @Override
+    public List<ServiceReference> onServiceArrival(DependencyModel dependency, List<ServiceReference>
matching, ServiceReference<?> reference) {
+        return getServiceReferences(dependency, matching);
+    }
+
+    @Override
+    public List<ServiceReference> onServiceDeparture(DependencyModel dependency, List<ServiceReference>
matching, ServiceReference<?> reference) {
+        return getServiceReferences(dependency, matching);
+    }
+
+    @Override
+    public List<ServiceReference> onServiceModified(DependencyModel dependency, List<ServiceReference>
matching, ServiceReference<?> reference) {
+        return getServiceReferences(dependency, matching);
+    }
+
+    @Override
+    public <S> TransformedServiceReference<S> accept(DependencyModel dependency,
BundleContext context, TransformedServiceReference<S> ref) {
+        // Only accept services having a grade in LOW or HIGH according to the mode.
+        if (mode == HIGH) {
+           if (((Integer) ref.get("grade")) > 3) {
+               return ref;
+           }
+        } else {
+            if (((Integer) ref.get("grade")) <= 3) {
+                return ref;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public void set(String newValue) {
+        if (newValue.contains("HIGH")) {
+            mode = HIGH;
+            invalidateMatchingServices();
+        }
+        if (newValue.contains("LOW")) {
+            mode = LOW;
+            invalidateMatchingServices();
+        }
+        if (newValue.contains("REVERSE")) {
+            reverse = true;
+            invalidateSelectedServices();
+        }
+    }
+
+    /**
+     * Notifies the managed dependencies of a change in the set of services selected by this
interceptor.
+     * The dependency will call the getServiceReferences method to recompute the set of selected
services.
+     */
+    public void invalidateSelectedServices() {
+        List<DependencyModel> list = new ArrayList<DependencyModel>();
+        synchronized (this) {
+            list.addAll(dependencies);
+        }
+
+        for (DependencyModel dep : list) {
+            dep.invalidateSelectedServices();
+        }
+    }
+
+    /**
+     * Notifies the managed dependencies of a change in the set of services accepted by this
interceptor.
+     * The dependency will call the accept method to recompute the set of matching services.
+     */
+    public void invalidateMatchingServices() {
+        List<DependencyModel> list = new ArrayList<DependencyModel>();
+        synchronized (this) {
+            list.addAll(dependencies);
+        }
+
+        for (DependencyModel dep : list) {
+            dep.invalidateMatchingServices();
+        }
+    }
+}

Copied: felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestCombinationOfInterceptors.java
(from r1532285, felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestRankingServices.java)
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestCombinationOfInterceptors.java?p2=felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestCombinationOfInterceptors.java&p1=felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestRankingServices.java&r1=1532285&r2=1549051&rev=1549051&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestRankingServices.java
(original)
+++ felix/trunk/ipojo/runtime/core-it/ipojo-core-service-dependency-interceptor-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/TestCombinationOfInterceptors.java
Sun Dec  8 16:07:37 2013
@@ -31,25 +31,35 @@ import java.util.Properties;
 import static org.fest.assertions.Assertions.assertThat;
 
 /**
- * Checks Tracking interceptor transforming services
+ * Checks the combination of tracking and ranking interceptors.
+ *
+ * First, the tracking interceptor selects the references, it also transforms them.
+ * Then, the ranking interceptor sorts the remaining references.
  */
-public class TestRankingServices extends Common {
+public class TestCombinationOfInterceptors extends Common {
 
     private ComponentInstance provider1;
     private ComponentInstance provider2;
+    private ComponentInstance provider3;
+    private ComponentInstance provider4;
+    private ComponentInstance provider5;
+    private ComponentInstance provider6;
 
     @Test
-    public void testRanking() {
+    public void test() {
 
-        // Provider with grade 0 first
         provider1 = provider(0);
         provider2 = provider(1);
+        provider3 = provider(2);
+        provider4 = provider(3);
+        provider5 = provider(4);
+        provider6 = provider(5);
 
         // Create the interceptor
         Properties configuration = new Properties();
         configuration.put("target", "(dependency.id=foo)");
         ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors"
+
-                ".FilterRankingInterceptor", configuration);
+                ".TrackerAndRankerInterceptor", configuration);
 
         // Create the FooConsumer
         ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.components.FooConsumer");
@@ -57,160 +67,24 @@ public class TestRankingServices extends
         // Check we are using provider 2
         osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
         CheckService check = osgiHelper.getServiceObject(CheckService.class);
-        assertThat(check.getProps().get("grade")).isEqualTo(1);
+        assertThat(check.getProps().get("grade")).isEqualTo(5);
 
-        provider2.dispose();
+        provider6.dispose();
 
-        assertThat(check.getProps().get("grade")).isEqualTo(0);
-    }
-
-    @Test
-    public void testRankingWhenInterceptorIsComingAfterTheBattle() {
-
-        // Provider with grade 0 first
-        provider1 = provider(0);
-        provider2 = provider(1);
-
-        // Create the FooConsumer
-        ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.components.FooConsumer");
-
-        osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
-        CheckService check = osgiHelper.getServiceObject(CheckService.class);
-        assertThat(check.getProps().get("grade")).isEqualTo(0);
-
-        // Create the interceptor
-        Properties configuration = new Properties();
-        configuration.put("target", "(dependency.id=foo)");
-        ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors"
+
-                ".FilterRankingInterceptor", configuration);
-
-        // Check we are using provider 2
-        assertThat(check.getProps().get("grade")).isEqualTo(1);
-
-        provider2.dispose();
-
-        assertThat(check.getProps().get("grade")).isEqualTo(0);
-    }
-
-    @Test
-    public void testRankingChanges() {
-
-        // Provider with grade 0 first
-        provider1 = provider(0);
-        provider2 = provider(1);
-
-        // Create the interceptor
-        Properties configuration = new Properties();
-        configuration.put("target", "(dependency.id=foo)");
-        ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors"
+
-                ".FilterRankingInterceptor", configuration);
-
-        // Create the FooConsumer
-        ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.components.FooConsumer");
-
-        // Check we are using provider 2
-        osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
-        CheckService check = osgiHelper.getServiceObject(CheckService.class);
-        assertThat(check.getProps().get("grade")).isEqualTo(1);
+        assertThat(check.getProps().get("grade")).isEqualTo(4);
 
+        // Change range
         Setter setter = osgiHelper.getServiceObject(Setter.class);
-        setter.set("true");
-
-        assertThat(check.getProps().get("grade")).isEqualTo(0);
-    }
-
-    @Test
-    public void testRestorationOfTheComparatorWhenTheInterceptorLeaves() {
-
-        // Provider with grade 0 first
-        provider1 = provider(0);
-        provider2 = provider(1);
+        setter.set("LOW REVERSE");
 
-        // Create the interceptor
-        Properties configuration = new Properties();
-        configuration.put("target", "(dependency.id=foo)");
-        ComponentInstance interceptor = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core"
+
-                ".test.interceptors.FilterRankingInterceptor", configuration);
-
-        // Create the FooConsumer
-        ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.components.FooConsumer");
-
-        // Check we are using provider 2
-        osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
-        CheckService check = osgiHelper.getServiceObject(CheckService.class);
-        assertThat(check.getProps().get("grade")).isEqualTo(1);
-
-        interceptor.dispose();
-
-        assertThat(check.getProps().get("grade")).isEqualTo(0);
-    }
-
-    @Test
-    public void testDynamicServices() {
-
-        // Create the interceptor
-        Properties configuration = new Properties();
-        configuration.put("target", "(dependency.id=foo)");
-        ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors"
+
-                ".FilterRankingInterceptor", configuration);
-
-        // Create the FooConsumer
-        ComponentInstance consumer = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test"
+
-                ".components.FooConsumer");
-
-        assertThat(consumer.getState()).isEqualTo(ComponentInstance.INVALID);
-
-        provider1 = provider(0);
-
-        assertThat(consumer.getState()).isEqualTo(ComponentInstance.VALID);
-        CheckService check = osgiHelper.getServiceObject(CheckService.class);
+//        check = osgiHelper.getServiceObject(CheckService.class);
+//        assertThat(check.getProps().get("grade")).isEqualTo(3);
+//
+//        setter.set("REVERSE");
+//        check = osgiHelper.getServiceObject(CheckService.class);
         assertThat(check.getProps().get("grade")).isEqualTo(0);
-
-        provider2 = provider(1);
-
-        assertThat(consumer.getState()).isEqualTo(ComponentInstance.VALID);
-        check = osgiHelper.getServiceObject(CheckService.class);
-        assertThat(check.getProps().get("grade")).isEqualTo(1);
-
-        provider2.dispose();
-
-        assertThat(consumer.getState()).isEqualTo(ComponentInstance.VALID);
-        check = osgiHelper.getServiceObject(CheckService.class);
-        assertThat(check.getProps().get("grade")).isEqualTo(0);
-
-        provider1.dispose();
-
-        assertThat(consumer.getState()).isEqualTo(ComponentInstance.INVALID);
     }
 
-    @Test
-    public void testArchitecture() {
-        // Provider with grade 0 first
-        provider1 = provider(0);
-        provider2 = provider(1);
-
-        // Create the interceptor
-        Properties configuration = new Properties();
-        configuration.put("target", "(dependency.id=foo)");
-        ComponentInstance interceptor = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core"
+
-                ".test.interceptors.FilterRankingInterceptor", configuration);
-
-        // Create the FooConsumer
-        ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test"
+
-                ".components.FooConsumer");
-
-        // Check we are using provider 2
-        osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
-
-        assertThat(instance.getInstanceDescription().getDescription().toString()).contains
-                ("servicerankinginterceptor");
-        assertThat(instance.getInstanceDescription().getDescription().toString()).contains
-                ("target=\"(dependency.id=foo)\"");
-        assertThat(instance.getInstanceDescription().getDescription().toString()).contains
-                ("instance.name=\"" + interceptor.getInstanceName() + "\"");
-        assertThat(instance.getInstanceDescription().getDescription().toString()).contains
-                ("bundle.id=\"" + getTestBundle().getBundleId() + "\"");
-    }
 
     private ComponentInstance provider(int i) {
         Dictionary<String, String> configuration = new Hashtable<String, String>();



Mime
View raw message