You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2013/05/11 17:27:05 UTC

svn commit: r1481356 [2/2] - in /felix/trunk/ipojo/runtime: core-it/src/it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/components/context/ core-it/src/it/ipojo-core-service-dependency-test/src/test/java/org...

Modified: felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/util/Tracker.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/util/Tracker.java?rev=1481356&r1=1481355&r2=1481356&view=diff
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/util/Tracker.java (original)
+++ felix/trunk/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/util/Tracker.java Sat May 11 15:27:05 2013
@@ -239,8 +239,8 @@ public class Tracker implements TrackerC
             /* In case the context was stopped. */
         }
         if (references != null) {
-            for (int i = 0; i < references.length; i++) {
-                outgoing.untrack(references[i]);
+            for (ServiceReference reference : references) {
+                outgoing.untrack(reference);
             }
         }
         m_tracked = null;
@@ -350,7 +350,7 @@ public class Tracker implements TrackerC
      * Gets the list of stored service reference.
      * @return the list containing used service reference
      */
-    public List/*<ServiceReference>*/getServiceReferencesList() {
+    public List<ServiceReference> getServiceReferencesList() {
         Tracked tracked = this.m_tracked; // use local var since we are not synchronized
         if (tracked == null) { // if Tracker is not open
             return null;
@@ -358,11 +358,8 @@ public class Tracker implements TrackerC
         synchronized (tracked) {
             int length = tracked.size();
             if (length == 0) { return null; }
-            List references = new ArrayList(length);
-            Iterator keys = tracked.keySet().iterator();
-            for (int i = 0; i < length; i++) {
-                references.add(keys.next());
-            }
+            List<ServiceReference> references = new ArrayList<ServiceReference>(length);
+            references.addAll(tracked.keySet());
             // The resulting array is sorted by ranking.
             return references;
         }
@@ -374,20 +371,16 @@ public class Tracker implements TrackerC
      * called getService on this reference.
      * @return the list of used references.
      */
-    public List/*<ServiceReference>*/getUsedServiceReferences() {
+    public List<ServiceReference> getUsedServiceReferences() {
         Tracked tracked = this.m_tracked; // use local var since we are not synchronized
         if (tracked == null || tracked.size() == 0) { // if Tracker is not open or empty
             return null;
         }
         synchronized (tracked) {
-            int length = tracked.size();
-            List references = new ArrayList();
-            Iterator keys = tracked.entrySet().iterator();
-            for (int i = 0; i < length; i++) {
-                Map.Entry entry = (Map.Entry) keys.next();
-                Object key = entry.getKey();
+            List<ServiceReference> references = new ArrayList<ServiceReference>();
+            for (Map.Entry<ServiceReference, Object> entry : tracked.entrySet()) {
                 if (entry.getValue() != null) {
-                    references.add(key);
+                    references.add(entry.getKey());
                 }
             }
             return references;
@@ -433,9 +426,8 @@ public class Tracker implements TrackerC
         if (tracked == null) { /* if Tracker is not open */
             return null;
         }
-        Object object = null;
         synchronized (tracked) {
-            object = tracked.get(reference);
+            Object object = tracked.get(reference);
             if (object == null) {
                 if (tracked.containsKey(reference)) { // Not already get but already tracked.
                     object = m_context.getService(reference);
@@ -479,7 +471,7 @@ public class Tracker implements TrackerC
         }
         synchronized (tracked) {
             ServiceReference[] references = getServiceReferences();
-            int length = 0;
+            int length;
             if (references == null) {
                 return null;
             } else {
@@ -537,18 +529,14 @@ public class Tracker implements TrackerC
      * class is the ServiceListener object for the tracker. This class is used to synchronize access to the tracked services. This is not a public class. It is only for use by the implementation of the Tracker
      * class.
      */
-    class Tracked extends HashMap implements ServiceListener {
-        /**
-         * UID.
-         */
-        static final long serialVersionUID = -7420065199791006079L;
+    class Tracked extends HashMap<ServiceReference, Object> implements ServiceListener {
 
         /**
          * The list of ServiceReferences in the process of being added. This is used to deal with nesting of ServiceEvents. Since ServiceEvents are synchronously delivered, ServiceEvents can be nested. For example, when processing the adding of a service
          * and the customizer causes the service to be unregistered, notification to the nested call to untrack that the service was unregistered can be made to the track method. Since the ArrayList implementation is not synchronized, all access to
          * this list must be protected by the same synchronized object for thread safety.
          */
-        private List m_adding;
+        private List<ServiceReference> m_adding;
 
         /**
          * <code>true</code> if the tracked object is closed. This field is volatile because it is set by one thread and read by another.
@@ -560,7 +548,7 @@ public class Tracker implements TrackerC
          * "announced" by ServiceEvents and therefore the ServiceEvent for unregistration could be delivered before we track the service. A service must not be in both the initial and adding lists at the same time. A service must be moved from the
          * initial list to the adding list "atomically" before we begin tracking it. Since the LinkedList implementation is not synchronized, all access to this list must be protected by the same synchronized object for thread safety.
          */
-        private List m_initial;
+        private List<ServiceReference> m_initial;
 
         /**
          * Tracked constructor.
@@ -568,8 +556,8 @@ public class Tracker implements TrackerC
         protected Tracked() {
             super();
             m_closed = false;
-            m_adding = new ArrayList(6);
-            m_initial = new LinkedList();
+            m_adding = new ArrayList<ServiceReference>(6);
+            m_initial = new LinkedList<ServiceReference>();
         }
 
         /**
@@ -579,9 +567,7 @@ public class Tracker implements TrackerC
         protected void setInitialServices(ServiceReference[] references) {
             if (references == null) { return; }
             int size = references.length;
-            for (int i = 0; i < size; i++) {
-                m_initial.add(references[i]);
-            }
+            m_initial.addAll(Arrays.asList(references).subList(0, size));
         }
 
         /**
@@ -591,7 +577,7 @@ public class Tracker implements TrackerC
             while (true) {
                 ServiceReference reference;
                 synchronized (this) {
-                    if (m_initial.isEmpty()) { //  if there are no more inital services
+                    if (m_initial.isEmpty()) { //  if there are no more initial services
                         return; // we are done
                     }
 

Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/ContextSourceManagerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/ContextSourceManagerTest.java?rev=1481356&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/ContextSourceManagerTest.java (added)
+++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/ContextSourceManagerTest.java Sat May 11 15:27:05 2013
@@ -0,0 +1,115 @@
+/*
+ * 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.util;
+
+import org.junit.Test;
+import org.osgi.framework.InvalidSyntaxException;
+
+import java.util.Hashtable;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * Checks the behavior of the context source manager.
+ */
+public class ContextSourceManagerTest {
+
+    @Test
+    public void testSubstitute() throws Exception {
+        String filter_with_one_var = "(id=${id})";
+        String filter_with_two_vars = "(&(id=${id})(count=${system.count}))";
+
+        Hashtable<String, Object> context = new Hashtable<String, Object>();
+        context.put("id", "my.id");
+        context.put("system.count", 1);
+
+        String filter = ContextSourceManager.substitute(filter_with_one_var, context);
+        assertThat(filter).isEqualTo("(id=my.id)");
+
+        filter = ContextSourceManager.substitute(filter_with_two_vars, context);
+        assertThat(filter).isEqualTo("(&(id=my.id)(count=1))");
+    }
+
+    @Test
+    public void testEmptySubstitution() {
+        String filter_with_two_vars = "(&(id=${id})(count=${system.count}))";
+        Hashtable<String, Object> context = new Hashtable<String, Object>();
+        String filter = ContextSourceManager.substitute(filter_with_two_vars, context);
+        assertThat(filter).isEqualTo(filter_with_two_vars);
+    }
+
+    @Test
+    public void testTwoStepsSubstitution() {
+        String filter_with_vars_equality = "(${attr}=${var})";
+
+        Hashtable<String, Object> context1 = new Hashtable<String, Object>();
+        context1.put("var", "value");
+
+        Hashtable<String, Object> context2 = new Hashtable<String, Object>();
+        context2.put("attr", "prop");
+
+        String filter = ContextSourceManager.substitute(filter_with_vars_equality, context1);
+        assertThat(filter).isEqualTo("(${attr}=value)");
+
+        filter = ContextSourceManager.substitute(filter, context2);
+        assertThat(filter).isEqualTo("(prop=value)");
+    }
+
+    @Test
+    public void testExtractVariablesFromFilter() throws Exception {
+        String filter_with_one_var = "(id=${id})";
+        String filter_with_two_vars = "(&(id=${id})(count=${system.count}))";
+        String filter_with_vars_equality = "(${attr}=${var})";
+
+        assertThat(ContextSourceManager.extractVariablesFromFilter(filter_with_one_var)).containsExactly("id");
+        assertThat(ContextSourceManager.extractVariablesFromFilter(filter_with_two_vars)).contains("id",
+                "system.count");
+        assertThat(ContextSourceManager.extractVariablesFromFilter(filter_with_vars_equality)).contains("attr",
+                "var");
+    }
+
+    @Test
+    public void testBrokenFilters() throws Exception {
+        String broken = "(id=${i)";
+        try {
+            ContextSourceManager.extractVariablesFromFilter(broken);
+            fail("Unfinished variable undetected");
+        } catch (InvalidSyntaxException e) {
+            // OK
+        }
+
+        String broken2 = "(id=${})";
+        try {
+            ContextSourceManager.extractVariablesFromFilter(broken2);
+            fail("Empty variable undetected");
+        } catch (InvalidSyntaxException e) {
+            // OK
+        }
+
+        String broken3 = "(id=${I contain a space})";
+        try {
+            ContextSourceManager.extractVariablesFromFilter(broken3);
+            fail("Spaced variable undetected");
+        } catch (InvalidSyntaxException e) {
+            // OK
+        }
+    }
+}

Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/InstanceConfigurationSourceTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/InstanceConfigurationSourceTest.java?rev=1481356&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/InstanceConfigurationSourceTest.java (added)
+++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/InstanceConfigurationSourceTest.java Sat May 11 15:27:05 2013
@@ -0,0 +1,169 @@
+/*
+ * 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.util;
+
+import org.apache.felix.ipojo.ContextListener;
+import org.apache.felix.ipojo.ContextSource;
+import org.junit.After;
+import org.junit.Test;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test the instance configuration context source.
+ */
+public class InstanceConfigurationSourceTest {
+
+    @Test
+    public void getProperties() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        configuration.put("prop1", "value");
+        configuration.put("prop2", 1);
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+        assertEquals(cs.getContext().get("prop1"), "value");
+        assertEquals(cs.getContext().get("prop2"), 1);
+    }
+
+    @Test
+    public void getProperty() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        configuration.put("prop1", "value");
+        configuration.put("prop2", 1);
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+        assertEquals(cs.getProperty("prop1"), "value");
+        assertEquals(cs.getProperty("prop2"), 1);
+    }
+
+    @Test
+    public void getMissingProperty() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        configuration.put("prop1", "value");
+        configuration.put("prop2", 1);
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+        assertNull(cs.getProperty("__property"));
+    }
+
+    @Test
+    public void emptyConfiguration() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+        assertNull(cs.getProperty("__property"));
+    }
+
+    @Test
+    public void addPropertyOnConfiguration() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        configuration.put("prop1", "value");
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+
+        SpyContextListener spy = new SpyContextListener();
+        cs.registerContextListener(spy, null);
+
+        Dictionary<String, Object> newConfiguration = new Hashtable<String, Object>();
+        newConfiguration.put("prop1", "value");
+        newConfiguration.put("prop2", "value2");
+
+        cs.reconfigure(newConfiguration);
+
+        assertEquals(spy.variables.size(), 1);
+        assertEquals(spy.variables.get("prop2"), "value2");
+    }
+
+    @Test
+    public void removePropertyOnConfiguration() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        configuration.put("prop1", "value");
+        configuration.put("prop2", "value2");
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+
+        SpyContextListener spy = new SpyContextListener();
+        cs.registerContextListener(spy, null);
+
+        Dictionary<String, Object> newConfiguration = new Hashtable<String, Object>();
+        newConfiguration.put("prop1", "value");
+
+        cs.reconfigure(newConfiguration);
+
+        assertEquals(spy.variables.size(), 1);
+        assertTrue(spy.variables.containsKey("prop2"));
+        assertEquals(spy.variables.get("prop2"), null);
+    }
+
+    @Test
+    public void updatePropertyOnConfiguration() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        configuration.put("prop1", "value");
+        configuration.put("prop2", "value2");
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+
+        SpyContextListener spy = new SpyContextListener();
+        cs.registerContextListener(spy, null);
+
+        Dictionary<String, Object> newConfiguration = new Hashtable<String, Object>();
+        newConfiguration.put("prop1", "new value");
+        newConfiguration.put("prop2", "value2");
+
+        cs.reconfigure(newConfiguration);
+
+        assertEquals(spy.variables.size(), 1);
+        assertEquals(spy.variables.get("prop1"), "new value");
+    }
+
+    @Test
+    public void add_remove_update() {
+        Dictionary<String, Object> configuration = new Hashtable<String, Object>();
+        configuration.put("prop1", "value");
+        configuration.put("prop2", "value2");
+        InstanceConfigurationSource cs = new InstanceConfigurationSource(configuration);
+
+        SpyContextListener spy = new SpyContextListener();
+        cs.registerContextListener(spy, null);
+
+        Dictionary<String, Object> newConfiguration = new Hashtable<String, Object>();
+        newConfiguration.put("prop1", "new value");
+        newConfiguration.put("prop3", "value3");
+
+        cs.reconfigure(newConfiguration);
+
+        assertEquals(spy.variables.size(), 3);
+        assertEquals(spy.variables.get("prop1"), "new value");
+        assertEquals(spy.variables.get("prop3"), "value3");
+        assertEquals(spy.variables.get("prop2"), null);
+
+    }
+
+    private class SpyContextListener implements ContextListener {
+
+        Map<String, Object> variables = new HashMap<String, Object>();
+
+        public void update(ContextSource source, String property, Object value) {
+            variables.put(property, value);
+        }
+    }
+
+
+}

Added: felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/SystemPropertiesSourceTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/SystemPropertiesSourceTest.java?rev=1481356&view=auto
==============================================================================
--- felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/SystemPropertiesSourceTest.java (added)
+++ felix/trunk/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/util/SystemPropertiesSourceTest.java Sat May 11 15:27:05 2013
@@ -0,0 +1,57 @@
+/*
+ * 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.util;
+
+import org.junit.After;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Test the system property context source.
+ */
+public class SystemPropertiesSourceTest {
+
+    @After
+    public void tearDown() {
+        System.clearProperty("__property");
+    }
+
+    @Test
+    public void getProperties() {
+        System.setProperty("__property", "__value");
+        SystemPropertiesSource cs = new SystemPropertiesSource();
+        assertEquals(cs.getContext().get("__property"), "__value");
+    }
+
+    @Test
+    public void getProperty() {
+        System.setProperty("__property", "__value");
+        SystemPropertiesSource cs = new SystemPropertiesSource();
+        assertEquals(cs.getProperty("__property"), "__value");
+    }
+
+    @Test
+    public void getMissingProperty() {
+        SystemPropertiesSource cs = new SystemPropertiesSource();
+        assertNull(cs.getProperty("__property"));
+    }
+}