Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAXEXAM-754] Treat Karaf features as dependencies when useDeployFolder(false) #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions containers/pax-exam-container-karaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<artifactId>pax-exam-container-karaf</artifactId>

<name>OPS4J Pax Exam Karaf Container</name>

<dependencies>

<dependency>
Expand Down Expand Up @@ -123,6 +123,10 @@
<type>xml</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is <scope>test</scope> missing here? (Or is it in dependencyManagement of the parent?)

</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -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")) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,15 +31,17 @@ 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("<bundle>wrap:mvn:mygroup/myArtifactId/1.0$overwrite=MERGE&amp;Export-Package=my.package.*</bundle>"));
}

@Test
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(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<features xmlns=\"http://karaf.apache.org/xmlns/features/v1.0.0\" name=\"test-dependencies\">\n" +
Expand All @@ -45,7 +55,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(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<features xmlns=\"http://karaf.apache.org/xmlns/features/v1.0.0\" name=\"test-dependencies\">\n" +
Expand All @@ -54,4 +65,45 @@ public void testDependencyFeatureWithBundleStartLevel() {
"</feature>\n" +
"</features>\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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<features xmlns=\"http://karaf.apache.org/xmlns/features/v1.0.0\" name=\"test-dependencies\">\n"
+ "<feature name=\"test-dependencies\">\n"
+ "<feature>myFeature</feature>\n"
+ "</feature>\n"
+ "</features>\n",
xmlOutputString);
}
}