FELIX-3377 Applied patch provided by David Jencks (thanks alot) slightly modified:
- Added some more JavaDoc to ExtComponentContext
- Have ExtComponentContext extend ComponentContext
- Internally tread property overwrites like supplied CM configuration
This allows for properly transient property modification instead
of overwriting the actual descriptor properties which would cause
the properties for all instances created from the same descriptor
to be subsequently modified.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1298275 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java b/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
index b0be98f..c134fd7 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
@@ -97,6 +97,8 @@
@ProbeBuilder
public TestProbeBuilder extendProbe(TestProbeBuilder builder) {
builder.setHeader("Export-Package", "org.apache.felix.scr.integration.components,org.apache.felix.scr.integration.components.activatesignature");
+ builder.setHeader("Import-Package", "org.apache.felix.scr,org.apache.felix.scr.component;mandatory:=\"status\"; status=\"provisional\"");
+ builder.setHeader("Bundle-ManifestVersion", "2");
return builder;
}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/MutablePropertiesTest.java b/scr/src/test/java/org/apache/felix/scr/integration/MutablePropertiesTest.java
new file mode 100644
index 0000000..e406c01
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/MutablePropertiesTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.scr.integration;
+
+
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+import org.apache.felix.scr.Component;
+import org.apache.felix.scr.integration.components.MutatingService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.ServiceReference;
+
+
+@RunWith(JUnit4TestRunner.class)
+public class MutablePropertiesTest extends ComponentTestBase
+{
+
+ static
+ {
+ // uncomment to enable debugging of this test class
+// paxRunnerVmOption = DEBUG_VM_OPTION;
+
+ descriptorFile = "/integration_test_mutable_properties.xml";
+ }
+
+
+ @Test
+ public void test_mutable_properties()
+ {
+ final Component component = findComponentByName( "components.mutable.properties" );
+ TestCase.assertNotNull( component );
+ TestCase.assertEquals( Component.STATE_REGISTERED, component.getState() );
+
+ ServiceReference serviceReference = bundleContext.getServiceReference( MutatingService.class.getName() );
+ checkProperties( serviceReference, 8, "otherValue", "p1", "p2" );
+
+ //update theValue
+ MutatingService s = ( MutatingService ) bundleContext.getService(serviceReference );
+ Assert.assertNotNull(s);
+ TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+ Dictionary d = new Hashtable(Collections.singletonMap( PROP_NAME, "anotherValue" ));
+ s.updateProperties(d);
+ checkProperties(serviceReference, 8, "anotherValue", "p1", "p2");
+
+ //configure with configAdmin
+ configure( "components.mutable.properties" );
+ delay();
+ checkProperties(serviceReference, 8, PROP_NAME, "p1", "p2");
+
+ //check that a property from config admin can't be changed
+ s.updateProperties(d);
+ checkProperties(serviceReference, 8, PROP_NAME, "p1", "p2");
+
+ //check that another one can
+ s.updateProperties(new Hashtable(Collections.singletonMap( "p1", "changed" )));
+ checkProperties(serviceReference, 8, PROP_NAME, "changed", "p2");
+
+ bundleContext.ungetService(serviceReference);
+ }
+
+ private void checkProperties(ServiceReference serviceReference, int count, String otherValue, String p1, String p2) {
+ Assert.assertEquals("wrong property count", count, serviceReference.getPropertyKeys().length);
+ Assert.assertEquals(otherValue, serviceReference.getProperty(PROP_NAME));
+ Assert.assertEquals(p1, serviceReference.getProperty("p1"));
+ Assert.assertEquals(p2, serviceReference.getProperty("p2"));
+ }
+
+
+}
\ No newline at end of file
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingService.java b/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingService.java
new file mode 100644
index 0000000..b212457
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingService.java
@@ -0,0 +1,29 @@
+/*
+ * 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.scr.integration.components;
+
+
+import java.util.Dictionary;
+
+public interface MutatingService
+{
+
+ void updateProperties(Dictionary changes);
+
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java b/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java
new file mode 100644
index 0000000..6ae2ff6
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java
@@ -0,0 +1,46 @@
+/*
+ * 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.scr.integration.components;
+
+
+import java.util.Dictionary;
+
+import org.apache.felix.scr.component.ExtComponentContext;
+import org.osgi.service.component.ComponentContext;
+
+
+public class MutatingServiceImpl implements MutatingService
+{
+ private ComponentContext activateContext;
+
+ private void activate( ComponentContext activateContext )
+ {
+ this.activateContext = activateContext;
+ }
+
+ private void modified( ComponentContext activateContext )
+ {
+
+ }
+
+ public void updateProperties(Dictionary changes) {
+ ((ExtComponentContext)activateContext).updateProperties(changes);
+ }
+
+}
diff --git a/scr/src/test/resources/integration_test_mutable_properties.xml b/scr/src/test/resources/integration_test_mutable_properties.xml
new file mode 100644
index 0000000..5dc0473
--- /dev/null
+++ b/scr/src/test/resources/integration_test_mutable_properties.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<components>
+ <scr:component name="components.mutable.properties"
+ xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
+ enabled="true"
+ configuration-policy="optional"
+ activate="activate"
+ modified="modified">
+ <implementation class="org.apache.felix.scr.integration.components.MutatingServiceImpl" />
+ <service>
+ <provide interface="org.apache.felix.scr.integration.components.MutatingService" />
+ </service>
+ <property name="service.pid" value="components.mutable.properties" />
+ <property name="theValue" value="otherValue" />
+ <property name="p1" value="p1" />
+ <property name="p2" value="p2" />
+ </scr:component>
+</components>