You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by tj...@apache.org on 2020/11/05 15:56:26 UTC
[felix-dev] 03/06: Move satisfying condition tests to integration
tests
This is an automated email from the ASF dual-hosted git repository.
tjwatson pushed a commit to branch scrR8
in repository https://gitbox.apache.org/repos/asf/felix-dev.git
commit 7723726bac2546ccba9ad165ded9ac744571463c
Author: Thomas Watson <tj...@us.ibm.com>
AuthorDate: Wed Oct 28 10:05:50 2020 -0500
Move satisfying condition tests to integration tests
---
.../scr/impl/truecondition/TrueConditionTest.java | 213 ---------------------
.../scr/integration/SatisfyingConditionTest.java | 160 ++++++++++++++++
.../SatisfyingConditionComponentClass.java} | 4 +-
.../integration_test_satisfying_condition.xml | 41 ++++
.../test/resources/satisfying_condition_test1.xml | 27 ---
.../test/resources/satisfying_condition_test2.xml | 28 ---
6 files changed, 203 insertions(+), 270 deletions(-)
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/truecondition/TrueConditionTest.java b/scr/src/test/java/org/apache/felix/scr/impl/truecondition/TrueConditionTest.java
deleted file mode 100644
index 5d58777..0000000
--- a/scr/src/test/java/org/apache/felix/scr/impl/truecondition/TrueConditionTest.java
+++ /dev/null
@@ -1,213 +0,0 @@
-/*
- * 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.impl.truecondition;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle;
-import static org.ops4j.pax.tinybundles.core.TinyBundles.withClassicBuilder;
-
-import java.io.File;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.ServiceLoader;
-
-import org.apache.felix.scr.impl.Activator;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.ops4j.pax.tinybundles.core.TinyBundle;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.launch.Framework;
-import org.osgi.service.component.runtime.ServiceComponentRuntime;
-import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
-import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO;
-import org.osgi.service.component.runtime.dto.ReferenceDTO;
-import org.osgi.service.component.runtime.dto.SatisfiedReferenceDTO;
-import org.osgi.service.component.runtime.dto.UnsatisfiedReferenceDTO;
-import org.osgi.service.condition.Condition;
-
-import aQute.bnd.osgi.Builder;
-import aQute.bnd.osgi.Jar;
-import aQute.bnd.osgi.JarResource;
-import aQute.lib.io.IO;
-
-public class TrueConditionTest
-{
-
- private static Framework framework;
- private static Bundle scr;
- private BundleActivator scrActivator;
- private ServiceComponentRuntime scRuntime;
- private Collection<Bundle> installed = new ArrayList<>();
-
- @BeforeClass
- public static void setup() throws Exception
- {
- File tmp = IO.getFile("target/tmp/loggertest");
- Map<String, String> configuration = new HashMap<>();
- configuration.put(Constants.FRAMEWORK_STORAGE,
- tmp.getAbsolutePath());
- configuration.put(Constants.FRAMEWORK_STORAGE_CLEAN,
- Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
- configuration.put(Constants.FRAMEWORK_BUNDLE_PARENT,
- Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK);
- configuration.put(Constants.FRAMEWORK_BOOTDELEGATION, "*");
- framework = ServiceLoader.load(
- org.osgi.framework.launch.FrameworkFactory.class).iterator().next().newFramework(
- configuration);
- framework.init();
- framework.start();
-
- // fake out a true condition for an R8 framework impl
- Hashtable<String, String> trueConditionProps = new Hashtable<>();
- trueConditionProps.put(Condition.CONDITION_ID, Condition.CONDITION_ID_TRUE);
- framework.getBundleContext().registerService(Condition.class, Condition.INSTANCE,
- trueConditionProps);
-
- scr = framework.getBundleContext().installBundle("scr",
- makeBundle("scr").openInputStream());
- scr.start();
- }
-
- @AfterClass
- public static void after() throws Exception
- {
- framework.stop();
- framework.waitForStop(100000);
- }
-
- @Before
- public void startScr() throws Exception
- {
- scrActivator = new Activator();
- scrActivator.start(scr.getBundleContext());
- scRuntime = getServiceComponentRuntime();
- }
-
- @After
- public void cleanup() throws Exception
- {
- for (Bundle b : installed)
- {
- b.uninstall();
- }
- scrActivator.stop(scr.getBundleContext());
- }
-
- private static JarResource makeBundle(String bsn) throws Exception
- {
- @SuppressWarnings("resource")
- Builder b = new Builder();
- b.setBundleSymbolicName(bsn);
- Jar jar = b.build();
- b.removeClose(jar);
- return new JarResource(jar);
- }
-
- private Bundle installComponentBundle(String bsn,
- String componentXML) throws BundleException, Exception
- {
- Bundle b = framework.getBundleContext().installBundle(bsn,
- makeComponent(bsn, componentXML));
- installed.add(b);
- return b;
- }
-
- private InputStream makeComponent(String bsn, String componentXML) throws Exception
- {
-
-
- TinyBundle bundle = bundle() //
- .add("OSGI-INF/components.xml", getClass().getResource("/" + componentXML)) //
- .set(Constants.BUNDLE_MANIFESTVERSION, "2") //
- .set(Constants.BUNDLE_SYMBOLICNAME, bsn) //
- .set(Constants.BUNDLE_VERSION, "1.0.0") //
- .set("Service-Component", "OSGI-INF/components.xml");
- return bundle.build(withClassicBuilder());
- }
-
- @Test
- public void testDefaultSatisfyingCondition() throws BundleException, Exception
- {
- Bundle componentBundle = installComponentBundle("component.test.bundle", //
- "satisfying_condition_test1.xml");
- componentBundle.start();
- ComponentDescriptionDTO descriptionDTO = scRuntime.getComponentDescriptionDTO(
- componentBundle, "satisfying-condition-test1");
- assertNotNull("No component DTO found.", descriptionDTO);
- assertEquals("Wrong number of references.", 1, descriptionDTO.references.length);
-
- ReferenceDTO trueReference = descriptionDTO.references[0];
- assertEquals("Wrong name.", "osgi.ds.satisfying.condition", trueReference.name);
- assertEquals("Wrong interface.", "org.osgi.service.condition.Condition",
- trueReference.interfaceName);
- assertEquals("Wrong policy.", "dynamic", trueReference.policy);
- assertEquals("Wrong target.", "(osgi.condition.id=true)", trueReference.target);
-
- ComponentConfigurationDTO configDTO = scRuntime.getComponentConfigurationDTOs(
- descriptionDTO).iterator().next();
- SatisfiedReferenceDTO satisfiedDTO = configDTO.satisfiedReferences[0];
- assertEquals("Wrong target.", "(osgi.condition.id=true)", satisfiedDTO.target);
- }
-
- @Test
- public void testTargetPropSatisfyingCondition() throws BundleException, Exception
- {
- Bundle componentBundle = installComponentBundle("component.test.bundle", //
- "satisfying_condition_test2.xml");
- componentBundle.start();
- ComponentDescriptionDTO descriptionDTO = scRuntime.getComponentDescriptionDTO(
- componentBundle, "satisfying-condition-test2");
- assertNotNull("No component DTO found.", descriptionDTO);
- assertEquals("Wrong number of references.", 1, descriptionDTO.references.length);
-
- ReferenceDTO trueReference = descriptionDTO.references[0];
- assertEquals("Wrong name.", "osgi.ds.satisfying.condition", trueReference.name);
- assertEquals("Wrong interface.", "org.osgi.service.condition.Condition",
- trueReference.interfaceName);
- assertEquals("Wrong policy.", "dynamic", trueReference.policy);
- assertEquals("Wrong target.", "(osgi.condition.id=true)", trueReference.target);
-
- ComponentConfigurationDTO configDTO = scRuntime.getComponentConfigurationDTOs(
- descriptionDTO).iterator().next();
- UnsatisfiedReferenceDTO unsatisfiedDTO = configDTO.unsatisfiedReferences[0];
- assertEquals("Wrong target.", "(foo=bar)", unsatisfiedDTO.target);
- }
-
- private ServiceComponentRuntime getServiceComponentRuntime()
- {
- ServiceReference<ServiceComponentRuntime> ref = framework.getBundleContext().getServiceReference(
- ServiceComponentRuntime.class);
- assertNotNull("No registered ServiceComponentRuntime.", ref);
- ServiceComponentRuntime scRuntime = framework.getBundleContext().getService(ref);
- assertNotNull("No registered SerivceComponentRuntime.", scRuntime);
- return scRuntime;
- }
-}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/SatisfyingConditionTest.java b/scr/src/test/java/org/apache/felix/scr/integration/SatisfyingConditionTest.java
new file mode 100644
index 0000000..58c589c
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/SatisfyingConditionTest.java
@@ -0,0 +1,160 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.runtime.ServiceComponentRuntime;
+import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
+import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO;
+import org.osgi.service.component.runtime.dto.ReferenceDTO;
+import org.osgi.service.component.runtime.dto.SatisfiedReferenceDTO;
+import org.osgi.service.component.runtime.dto.UnsatisfiedReferenceDTO;
+import org.osgi.service.condition.Condition;
+
+
+@RunWith(PaxExam.class)
+public class SatisfyingConditionTest extends ComponentTestBase
+{
+ static
+ {
+ // uncomment to enable debugging of this test class
+ // paxRunnerVmOption = DEBUG_VM_OPTION;
+ descriptorFile = "/integration_test_satisfying_condition.xml";
+ }
+
+ @Before
+ public void registerTrueCondition() throws BundleException, InvalidSyntaxException
+ {
+ ServiceReference<ServiceComponentRuntime> ref = scrTracker.getServiceReference();
+ Bundle scrBundle = ref.getBundle();
+ File f = scrBundle.getDataFile("componentMetadataStore");
+ assertFalse("Cache " + f.getAbsolutePath() + " should not exist", f.exists());
+
+ // fake out a true condition for an R8 framework impl
+ if (bundleContext.getServiceReferences(Condition.class,
+ "(&(service.bundleid=0)(osgi.condition.id=true))").isEmpty())
+ {
+ // stop SCR first
+ scrBundle.stop();
+ // register true condition
+ Hashtable<String, String> trueConditionProps = new Hashtable<>();
+ trueConditionProps.put(Condition.CONDITION_ID, Condition.CONDITION_ID_TRUE);
+ // use system bundle to register true condition
+ bundleContext.getBundle(
+ Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext().registerService(
+ Condition.class, Condition.INSTANCE,
+ trueConditionProps);
+ // restart SCR to have it recognize the true condition
+ scrBundle.start();
+
+ }
+ }
+
+ @Test
+ public void test_default_satisfying_condition() throws Exception
+ {
+ final String componentname = "satisfying.condition.default";
+
+ ComponentConfigurationDTO configDTO = getDisabledConfigurationAndEnable(
+ componentname,
+ ComponentConfigurationDTO.ACTIVE);
+
+ assertEquals("Wrong number of references.", 1,
+ configDTO.satisfiedReferences.length);
+ SatisfiedReferenceDTO satisfiedDTO = configDTO.satisfiedReferences[0];
+ assertEquals("Wrong target.", "(osgi.condition.id=true)", satisfiedDTO.target);
+
+ ComponentDescriptionDTO descriptionDTO = configDTO.description;
+ assertEquals("Wrong number of references.", 1, descriptionDTO.references.length);
+
+ ReferenceDTO trueReference = descriptionDTO.references[0];
+ assertEquals("Wrong name.", "osgi.ds.satisfying.condition", trueReference.name);
+ assertEquals("Wrong interface.", "org.osgi.service.condition.Condition",
+ trueReference.interfaceName);
+ assertEquals("Wrong policy.", "dynamic", trueReference.policy);
+ assertEquals("Wrong target.", "(osgi.condition.id=true)", trueReference.target);
+
+ Dictionary<String, Object> configProps = new Hashtable<>();
+ configProps.put("osgi.ds.satisfying.condition.target", "(foo=baz)");
+ configure(componentname, null, configProps);
+ delay();
+
+ configDTO = findComponentConfigurationByName(componentname,
+ ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
+
+ UnsatisfiedReferenceDTO unsatisfiedDTO = configDTO.unsatisfiedReferences[0];
+ assertEquals("Wrong target.", "(foo=baz)", unsatisfiedDTO.target);
+ }
+
+ @Test
+ public void test_specified_satisfying_condition() throws Exception
+ {
+ final String componentname = "satisfying.condition.target.specified";
+
+ ComponentConfigurationDTO configDTO = getDisabledConfigurationAndEnable(
+ componentname, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
+ ComponentDescriptionDTO descriptionDTO = configDTO.description;
+
+ UnsatisfiedReferenceDTO unsatisfiedDTO = configDTO.unsatisfiedReferences[0];
+ assertEquals("Wrong target.", "(foo=bar)", unsatisfiedDTO.target);
+
+ assertNotNull("No component DTO found.", descriptionDTO);
+ assertEquals("Wrong number of references.", 1, descriptionDTO.references.length);
+
+ ReferenceDTO trueReference = descriptionDTO.references[0];
+ assertEquals("Wrong name.", "osgi.ds.satisfying.condition", trueReference.name);
+ assertEquals("Wrong interface.", "org.osgi.service.condition.Condition",
+ trueReference.interfaceName);
+ assertEquals("Wrong policy.", "dynamic", trueReference.policy);
+ assertEquals("Wrong target.", "(osgi.condition.id=true)", trueReference.target);
+
+ // register condition to satisfy component
+ Hashtable<String, String> testConditionProps = new Hashtable<>();
+ testConditionProps.put(Condition.CONDITION_ID, "test");
+ testConditionProps.put("foo", "bar");
+ // use system bundle to register true condition
+ bundleContext.registerService(Condition.class, Condition.INSTANCE,
+ testConditionProps);
+
+ configDTO = findComponentConfigurationByName(componentname,
+ ComponentConfigurationDTO.ACTIVE);
+ assertEquals("Wrong number of references.", 1,
+ configDTO.satisfiedReferences.length);
+ SatisfiedReferenceDTO satisfiedDTO = configDTO.satisfiedReferences[0];
+ assertEquals("Wrong target.", "(foo=bar)", satisfiedDTO.target);
+ }
+
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/truecondition/ComponentTestClass.java b/scr/src/test/java/org/apache/felix/scr/integration/components/SatisfyingConditionComponentClass.java
similarity index 92%
rename from scr/src/test/java/org/apache/felix/scr/impl/truecondition/ComponentTestClass.java
rename to scr/src/test/java/org/apache/felix/scr/integration/components/SatisfyingConditionComponentClass.java
index 1f78e54..e68eb29 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/truecondition/ComponentTestClass.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/SatisfyingConditionComponentClass.java
@@ -16,12 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.felix.scr.impl.truecondition;
+package org.apache.felix.scr.integration.components;
import org.apache.felix.scr.impl.metadata.ReferenceMetadata;
import org.osgi.service.component.ComponentContext;
-public class ComponentTestClass
+public class SatisfyingConditionComponentClass
{
void activate(ComponentContext cc)
diff --git a/scr/src/test/resources/integration_test_satisfying_condition.xml b/scr/src/test/resources/integration_test_satisfying_condition.xml
new file mode 100644
index 0000000..addeaca
--- /dev/null
+++ b/scr/src/test/resources/integration_test_satisfying_condition.xml
@@ -0,0 +1,41 @@
+<?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 xmlns:scr="http://www.osgi.org/xmlns/scr/v1.5.0">
+
+ <!--
+ Components used in this test use the satisfying condition
+ -->
+
+ <!-- no declaration of satisfying condition -->
+ <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.5.0"
+ name="satisfying.condition.default"
+ immediate="true"
+ enabled="false">
+ <implementation class="org.apache.felix.scr.integration.components.SatisfyingConditionComponentClass"/>
+ </scr:component>
+
+ <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.4.0"
+ name="satisfying.condition.target.specified"
+ immediate="true"
+ enabled="false">
+ <property name="osgi.ds.satisfying.condition.target" value="(foo=bar)"/>
+ <implementation class="org.apache.felix.scr.integration.components.SatisfyingConditionComponentClass"/>
+ </scr:component>
+</components>
diff --git a/scr/src/test/resources/satisfying_condition_test1.xml b/scr/src/test/resources/satisfying_condition_test1.xml
deleted file mode 100755
index 2d1b763..0000000
--- a/scr/src/test/resources/satisfying_condition_test1.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?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.
--->
-<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.4.0"
- name="satisfying-condition-test1"
- immediate="true">
- <implementation class="org.apache.felix.scr.impl.truecondition.ComponentTestClass"/>
- <service>
- <provide interface="org.apache.felix.scr.impl.truecondition.ComponentTestClass" />
- </service>
-</scr:component>
\ No newline at end of file
diff --git a/scr/src/test/resources/satisfying_condition_test2.xml b/scr/src/test/resources/satisfying_condition_test2.xml
deleted file mode 100755
index 20c1d3c..0000000
--- a/scr/src/test/resources/satisfying_condition_test2.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?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.
--->
-<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.4.0"
- name="satisfying-condition-test2"
- immediate="true">
- <implementation class="org.apache.felix.scr.impl.truecondition.ComponentTestClass"/>
- <property name="osgi.ds.satisfying.condition.target" value="(foo=bar)"/>
- <service>
- <provide interface="org.apache.felix.scr.impl.truecondition.ComponentTestClass" />
- </service>
-</scr:component>
\ No newline at end of file