From c318209f743202372f5fa8d0c644f3a3fd536e00 Mon Sep 17 00:00:00 2001 From: Dario Cimafonte Date: Mon, 30 Nov 2015 16:32:49 +0000 Subject: [PATCH 1/2] [PAXEXAM-754] Treat Karaf features as dependencies when useDeployFolder(false) --- containers/pax-exam-container-karaf/pom.xml | 8 ++- .../internal/DependenciesDeployer.java | 21 ++++++- .../internal/DependenciesDeployerTest.java | 57 ++++++++++++++++++- 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/containers/pax-exam-container-karaf/pom.xml b/containers/pax-exam-container-karaf/pom.xml index 16471d273..d5509aa38 100644 --- a/containers/pax-exam-container-karaf/pom.xml +++ b/containers/pax-exam-container-karaf/pom.xml @@ -10,7 +10,7 @@ pax-exam-container-karaf OPS4J Pax Exam Karaf Container - + @@ -123,6 +123,10 @@ xml test + + org.mockito + mockito-all + - \ No newline at end of file + diff --git a/containers/pax-exam-container-karaf/src/main/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployer.java b/containers/pax-exam-container-karaf/src/main/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployer.java index eaf443ed3..9736fdd86 100644 --- a/containers/pax-exam-container-karaf/src/main/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployer.java +++ b/containers/pax-exam-container-karaf/src/main/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployer.java @@ -124,7 +124,11 @@ public KarafFeaturesOption getDependenciesFeature() { try { File featuresXmlFile = new File(karafBase, "test-dependencies.xml"); Writer wr = new OutputStreamWriter(new FileOutputStream(featuresXmlFile), "UTF-8"); - writeDependenciesFeature(wr, subsystem.getOptions(ProvisionOption.class)); + + ProvisionOption[] provisionOptions = subsystem.getOptions(ProvisionOption.class); + KarafFeaturesOption[] karafFeaturesOptions = subsystem.getOptions(KarafFeaturesOption.class); + + writeDependenciesFeature(wr, provisionOptions, karafFeaturesOptions); wr.close(); String repoUrl = "file:" + featuresXmlFile.toString().replaceAll("\\\\", "/").replaceAll(" ", "%20"); @@ -140,9 +144,11 @@ public KarafFeaturesOption getDependenciesFeature() { * in system to the given writer * * @param writer where to write the feature xml - * @param provisionOptions dependencies + * @param provisionOptions bundle dependencies + * @param karafFeaturesOptions feature dependencies */ - static void writeDependenciesFeature(Writer writer, ProvisionOption... provisionOptions) { + static void writeDependenciesFeature(Writer writer, ProvisionOption[] provisionOptions, + KarafFeaturesOption[] karafFeaturesOptions) { XMLOutputFactory xof = XMLOutputFactory.newInstance(); xof.setProperty("javax.xml.stream.isRepairingNamespaces", true); XMLStreamWriter sw = null; @@ -157,6 +163,7 @@ static void writeDependenciesFeature(Writer writer, ProvisionOption... provis sw.writeStartElement("feature"); sw.writeAttribute("name", "test-dependencies"); sw.writeCharacters("\n"); + // Write bundle dependencies for (ProvisionOption provisionOption : provisionOptions) { if (provisionOption.getURL().startsWith("link") || provisionOption.getURL().startsWith("scan-features")) { @@ -170,6 +177,14 @@ static void writeDependenciesFeature(Writer writer, ProvisionOption... provis sw.writeCharacters(provisionOption.getURL()); endElement(sw); } + // Write feature dependencies + for (KarafFeaturesOption karafFeaturesOption : karafFeaturesOptions) { + for (String karafFeature : karafFeaturesOption.getFeatures()) { + sw.writeStartElement("feature"); + sw.writeCharacters(karafFeature); + endElement(sw); + } + } endElement(sw); endElement(sw); sw.writeEndDocument(); diff --git a/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java b/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java index 79ddf6613..f936a4235 100644 --- a/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java +++ b/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java @@ -3,12 +3,20 @@ import static org.ops4j.pax.exam.CoreOptions.mavenBundle; import static org.ops4j.pax.exam.CoreOptions.wrappedBundle; +import java.io.File; +import java.io.FileNotFoundException; import java.io.StringWriter; +import java.util.Scanner; import org.hamcrest.core.StringContains; import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; +import org.ops4j.pax.exam.ExamSystem; +import org.ops4j.pax.exam.karaf.options.KarafDistributionOption; +import org.ops4j.pax.exam.karaf.options.KarafFeaturesOption; import org.ops4j.pax.exam.options.MavenArtifactProvisionOption; +import org.ops4j.pax.exam.options.ProvisionOption; import org.ops4j.pax.exam.options.WrappedUrlProvisionOption; import org.ops4j.pax.exam.options.WrappedUrlProvisionOption.OverwriteMode; @@ -23,7 +31,7 @@ public void testEncoding() { option.instructions("Export-Package=my.package.*"); option.overwriteManifest(OverwriteMode.MERGE); StringWriter wr = new StringWriter(); - DependenciesDeployer.writeDependenciesFeature(wr, option); + DependenciesDeployer.writeDependenciesFeature(wr, new ProvisionOption[] {option}, new KarafFeaturesOption[] {}); Assert.assertThat(wr.toString(), StringContains.containsString("wrap:mvn:mygroup/myArtifactId/1.0$overwrite=MERGE&Export-Package=my.package.*")); } @@ -31,7 +39,8 @@ public void testEncoding() { public void testDependencyFeature() { MavenArtifactProvisionOption option = mavenBundle().groupId("mygroup").artifactId("myArtifactId").version("1.0"); StringWriter wr = new StringWriter(); - DependenciesDeployer.writeDependenciesFeature(wr, option); + DependenciesDeployer.writeDependenciesFeature(wr, new ProvisionOption[] { option }, + new KarafFeaturesOption[] {}); Assert.assertEquals( "\n" + "\n" + @@ -45,7 +54,8 @@ public void testDependencyFeature() { public void testDependencyFeatureWithBundleStartLevel() { MavenArtifactProvisionOption option = mavenBundle().groupId("mygroup").artifactId("myArtifactId").version("1.0").startLevel(42); StringWriter wr = new StringWriter(); - DependenciesDeployer.writeDependenciesFeature(wr, option); + DependenciesDeployer.writeDependenciesFeature(wr, new ProvisionOption[] { option }, + new KarafFeaturesOption[] {}); Assert.assertEquals( "\n" + "\n" + @@ -54,4 +64,45 @@ public void testDependencyFeatureWithBundleStartLevel() { "\n" + "\n", wr.toString()); } + + @Test + public void testDependencyFeatureWithFeatureDependencies() throws FileNotFoundException { + + // Given a KarafFeatureOption exists + KarafFeaturesOption option = KarafDistributionOption + .features("mvn:mygroup/myArtifactId/1.0/xml/features", "myFeature"); + + ExamSystem subsystem = Mockito.mock(ExamSystem.class); + Mockito.when(subsystem.getOptions(Mockito.eq(ProvisionOption.class))) + .thenReturn(new ProvisionOption[0]); + Mockito.when(subsystem.getOptions(Mockito.eq(KarafFeaturesOption.class))) + .thenReturn(new KarafFeaturesOption[] { option }); + File karafBase = new File(System.getProperty("java.io.tmpdir")); + File karafHome = karafBase; + DependenciesDeployer dependenciesDeployer = new DependenciesDeployer(subsystem, karafBase, + karafHome); + + // When test-dependencies.xml is generated + KarafFeaturesOption dependenciesFeature = dependenciesDeployer.getDependenciesFeature(); + + // Then include any feature dependencies configured + File outputFeaturesFile = new File(karafBase, "test-dependencies.xml"); + Assert.assertEquals("file:" + outputFeaturesFile, dependenciesFeature.getURL()); + Assert.assertArrayEquals(new String[] { "test-dependencies" }, + dependenciesFeature.getFeatures()); + + // Read XML output into a String + String xmlOutputString; + try (Scanner scanner = new Scanner(outputFeaturesFile)) { + xmlOutputString = scanner.useDelimiter("\\Z").next() + "\n"; + } + + Assert.assertEquals("\n" + + "\n" + + "\n" + + "myFeature\n" + + "\n" + + "\n", + xmlOutputString); + } } From 4f5baea5713bee5ac932483536273fd603eb2272 Mon Sep 17 00:00:00 2001 From: Dario Cimafonte Date: Mon, 30 Nov 2015 16:50:11 +0000 Subject: [PATCH 2/2] Fixing indentation and spacing --- .../karaf/container/internal/DependenciesDeployerTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java b/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java index f936a4235..519e4675e 100644 --- a/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java +++ b/containers/pax-exam-container-karaf/src/test/java/org/ops4j/pax/exam/karaf/container/internal/DependenciesDeployerTest.java @@ -31,7 +31,8 @@ public void testEncoding() { option.instructions("Export-Package=my.package.*"); option.overwriteManifest(OverwriteMode.MERGE); StringWriter wr = new StringWriter(); - DependenciesDeployer.writeDependenciesFeature(wr, new ProvisionOption[] {option}, new KarafFeaturesOption[] {}); + DependenciesDeployer.writeDependenciesFeature(wr, new ProvisionOption[] { option }, + new KarafFeaturesOption[] {}); Assert.assertThat(wr.toString(), StringContains.containsString("wrap:mvn:mygroup/myArtifactId/1.0$overwrite=MERGE&Export-Package=my.package.*")); }