[FELIX-4563] Turn xml reference target attributes into properties, with a test
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1611157 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java b/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java
index 92326f6..7600cea 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java
@@ -390,8 +390,15 @@
ref.setScope( attributes.getAttribute( "scope" ) );
}
- //if
- ref.setTarget( attributes.getAttribute( "target" ) );
+ if ( attributes.getAttribute( "target" ) != null)
+ {
+ ref.setTarget( attributes.getAttribute( "target" ) );
+ PropertyMetadata prop = new PropertyMetadata();
+ prop.setName( (ref.getName() == null? ref.getInterface(): ref.getName()) + ".target");
+ prop.setValue( attributes.getAttribute( "target" ) );
+ m_currentComponent.addProperty( prop );
+
+ }
ref.setBind( attributes.getAttribute( "bind" ) );
ref.setUpdated( attributes.getAttribute( "updated" ) );
ref.setUnbind( attributes.getAttribute( "unbind" ) );
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 7a03baf..e5c054c 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
@@ -187,6 +187,7 @@
);
final Option vmOption = ( paxRunnerVmOption != null ) ? CoreOptions.vmOption( paxRunnerVmOption ) : null;
+ NONSTANDARD_COMPONENT_FACTORY_BEHAVIOR = false;
return OptionUtils.combine( base, vmOption );
}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/TargetPropertyTest.java b/scr/src/test/java/org/apache/felix/scr/integration/TargetPropertyTest.java
new file mode 100644
index 0000000..be05106
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/TargetPropertyTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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.Hashtable;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.scr.integration.components.SimpleComponent;
+import org.apache.felix.scr.integration.components.SimpleServiceImpl;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentConstants;
+import org.osgi.service.component.ComponentFactory;
+import org.osgi.service.component.ComponentInstance;
+import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
+
+@RunWith(JUnit4TestRunner.class)
+public class TargetPropertyTest extends ComponentTestBase
+{
+
+ static
+ {
+ // uncomment to enable debugging of this test class
+// paxRunnerVmOption = DEBUG_VM_OPTION;
+
+ descriptorFile = "/integration_test_target_properties.xml";
+ }
+
+ @Test
+ public void test_1() throws Exception
+ {
+ String pid = "components.target.properties.1";
+ singleTest( pid, "foo" );
+ }
+
+ @Test
+ public void test_1_2() throws Exception
+ {
+ String pid = "components.target.properties.1.2";
+ singleTest( pid, "foo" );
+ }
+
+ @Test
+ public void test_1_3() throws Exception
+ {
+ String pid = "components.target.properties.1.3";
+ singleTest( pid, "foo" );
+ }
+
+ @Test
+ public void test_2() throws Exception
+ {
+ String pid = "components.target.properties.2";
+ singleTest( pid, "foo" );
+ }
+
+ @Test
+ public void test_3() throws Exception
+ {
+ String pid = "components.target.properties.3";
+ singleTest( pid, "bar" );
+ }
+
+
+ private void singleTest(String pid, String expected) throws Exception
+ {
+ final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, expected );
+ final SimpleServiceImpl srv2 = SimpleServiceImpl.create( bundleContext, "baz" );
+
+ getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
+ checkTarget(expected, srv1);
+
+ //configuration not setting target property does not change it
+ configure( pid );
+ delay();//all cm event to complete
+ checkTarget(expected, srv1);
+
+ // update configuration to target srv2
+ theConfig.put("one.target", "(value=baz)");
+ configure( pid );
+ delay();
+ checkTarget("baz", srv2);
+
+ //update configuration removing target property
+ theConfig.remove("one.target");
+ configure( pid );
+ delay();//all cm event to complete
+ checkTarget(expected, srv1);
+
+ }
+
+ void checkTarget(String expected, final SimpleServiceImpl srv1)
+ {
+ final SimpleComponent comp10 = SimpleComponent.INSTANCE;
+ TestCase.assertNotNull( comp10 );
+ TestCase.assertEquals("(value=" + expected + ")", comp10.getProperty("one.target"));
+ TestCase.assertEquals( srv1, comp10.m_singleRef );
+ }
+
+
+}
diff --git a/scr/src/test/resources/integration_test_target_properties.xml b/scr/src/test/resources/integration_test_target_properties.xml
new file mode 100644
index 0000000..65ca31d
--- /dev/null
+++ b/scr/src/test/resources/integration_test_target_properties.xml
@@ -0,0 +1,133 @@
+<?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.target.properties.1"
+ xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
+ immediate="true"
+ enabled="false"
+ configuration-policy="optional"
+ activate="activate"
+ modified="modified">
+ <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+ <service>
+ <provide interface="org.apache.felix.scr.integration.components.SimpleComponent" />
+ </service>
+ <reference
+ name="one"
+ interface="org.apache.felix.scr.integration.components.SimpleService"
+ cardinality="0..1"
+ policy="dynamic"
+ bind="setSimpleService"
+ unbind="unsetSimpleService"
+ target="(value=foo)"
+ />
+ </scr:component>
+
+ <scr:component name="components.target.properties.2"
+ xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
+ immediate="true"
+ enabled="false"
+ configuration-policy="optional"
+ activate="activate"
+ modified="modified">
+ <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+ <service>
+ <provide interface="org.apache.felix.scr.integration.components.SimpleComponent" />
+ </service>
+ <property name="one.target" value="(value=bar)" />
+ <reference
+ name="one"
+ interface="org.apache.felix.scr.integration.components.SimpleService"
+ cardinality="0..1"
+ policy="dynamic"
+ bind="setSimpleService"
+ unbind="unsetSimpleService"
+ target="(value=foo)"
+ />
+ </scr:component>
+
+ <scr:component name="components.target.properties.3"
+ xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
+ immediate="true"
+ enabled="false"
+ configuration-policy="optional"
+ activate="activate"
+ modified="modified">
+ <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+ <service>
+ <provide interface="org.apache.felix.scr.integration.components.SimpleComponent" />
+ </service>
+ <reference
+ name="one"
+ interface="org.apache.felix.scr.integration.components.SimpleService"
+ cardinality="0..1"
+ policy="dynamic"
+ bind="setSimpleService"
+ unbind="unsetSimpleService"
+ target="(value=foo)"
+ />
+ <property name="one.target" value="(value=bar)" />
+ </scr:component>
+
+ <scr:component name="components.target.properties.1.2"
+ xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0"
+ immediate="true"
+ enabled="false"
+ configuration-policy="optional"
+ activate="activate"
+ modified="modified">
+ <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+ <service>
+ <provide interface="org.apache.felix.scr.integration.components.SimpleComponent" />
+ </service>
+ <reference
+ name="one"
+ interface="org.apache.felix.scr.integration.components.SimpleService"
+ cardinality="0..1"
+ policy="dynamic"
+ bind="setSimpleService"
+ unbind="unsetSimpleService"
+ target="(value=foo)"
+ />
+ </scr:component>
+
+ <scr:component name="components.target.properties.1.3"
+ xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0"
+ immediate="true"
+ enabled="false"
+ configuration-policy="optional"
+ activate="activate"
+ modified="modified">
+ <implementation class="org.apache.felix.scr.integration.components.SimpleComponent" />
+ <service>
+ <provide interface="org.apache.felix.scr.integration.components.SimpleComponent" />
+ </service>
+ <reference
+ name="one"
+ interface="org.apache.felix.scr.integration.components.SimpleService"
+ cardinality="0..1"
+ policy="dynamic"
+ bind="setSimpleService"
+ unbind="unsetSimpleService"
+ target="(value=foo)"
+ />
+ </scr:component>
+
+</components>