diff options
author | gjoranv <gv@verizonmedia.com> | 2019-10-19 16:31:03 +0200 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2019-10-23 17:06:02 +0200 |
commit | 663c9322ed4c27c4328b44b74145f94e007ceff3 (patch) | |
tree | dd8b4ce4bd394eb48194a030f1813441bfa5a06b /bundle-plugin-test/integration-test | |
parent | 21442dddfdbe1c42a17abf7298d39dfc56da5357 (diff) |
Restructure bundle-plugin-test to allow multiple test bundles.
Diffstat (limited to 'bundle-plugin-test/integration-test')
-rw-r--r-- | bundle-plugin-test/integration-test/pom.xml | 91 | ||||
-rw-r--r-- | bundle-plugin-test/integration-test/src/test/java/com/yahoo/BundleIT.java | 178 |
2 files changed, 269 insertions, 0 deletions
diff --git a/bundle-plugin-test/integration-test/pom.xml b/bundle-plugin-test/integration-test/pom.xml new file mode 100644 index 00000000000..62204f23999 --- /dev/null +++ b/bundle-plugin-test/integration-test/pom.xml @@ -0,0 +1,91 @@ +<?xml version="1.0"?> +<!-- Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>com.yahoo.vespa.bundle-plugin</groupId> + <artifactId>bundle-plugin-test</artifactId> + <version>7-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <artifactId>integration-test</artifactId> + <version>7-SNAPSHOT</version> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>config</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>vespajlib</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>com.yahoo.vespa.bundle-plugin</groupId> + <artifactId>main</artifactId> + <classifier>bundle</classifier> + <version>${project.version}</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <version>2.9</version> + <executions> + <execution> + <id>integration-test</id> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + </execution> + </executions> + <configuration> + <trimStackTrace>false</trimStackTrace> + <systemPropertyVariables> + <test.bundle.path>${project.build.directory}/dependency</test.bundle.path> + <redirectTestOutputToFile>${test.hide}</redirectTestOutputToFile> + </systemPropertyVariables> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copy-dependencies</id> + <phase>process-test-classes</phase> + <goals> + <goal>copy-dependencies</goal> + </goals> + <configuration> + <includeScope>compile</includeScope> + <excludeTransitive>true</excludeTransitive> + <stripVersion>true</stripVersion> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> diff --git a/bundle-plugin-test/integration-test/src/test/java/com/yahoo/BundleIT.java b/bundle-plugin-test/integration-test/src/test/java/com/yahoo/BundleIT.java new file mode 100644 index 00000000000..2ed1d7f0bc6 --- /dev/null +++ b/bundle-plugin-test/integration-test/src/test/java/com/yahoo/BundleIT.java @@ -0,0 +1,178 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo; + +import com.yahoo.osgi.maven.ProjectBundleClassPaths; +import com.yahoo.vespa.config.VespaVersion; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collection; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; + +import static com.yahoo.osgi.maven.ProjectBundleClassPaths.CLASSPATH_MAPPINGS_FILENAME; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.endsWith; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; + +/** + * Verifies the bundle jar file built and its manifest. + * + * @author Tony Vaagenes + */ +public class BundleIT { + static final String TEST_BUNDLE_PATH = System.getProperty("test.bundle.path", ".") + "/"; + + private JarFile jarFile; + private Attributes mainAttributes; + + @Before + public void setup() { + try { + File componentJar = findBundleJar("main"); + jarFile = new JarFile(componentJar); + Manifest manifest = jarFile.getManifest(); + mainAttributes = manifest.getMainAttributes(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private File findBundleJar(String bundleName) { + File[] componentFile = new File(TEST_BUNDLE_PATH).listFiles(new FilenameFilter() { + @Override + public boolean accept(File file, String fileName) { + return fileName.endsWith("-bundle.jar"); + } + }); + + if (componentFile.length != 1) { + throw new RuntimeException("Failed finding component jar file"); + } + + return componentFile[0]; + } + + @Test + public void require_that_bundle_version_is_added_to_manifest() { + String bundleVersion = mainAttributes.getValue("Bundle-Version"); + + // Because of snapshot builds, we can only verify the major version. + int majorBundleVersion = Integer.valueOf(bundleVersion.substring(0, bundleVersion.indexOf('.'))); + assertThat(majorBundleVersion, is(VespaVersion.major)); + } + + @Test + public void require_that_bundle_symbolic_name_matches_pom_artifactId() { + assertThat(mainAttributes.getValue("Bundle-SymbolicName"), is("main")); + } + + @Test + public void require_that_manifest_contains_inferred_imports() { + String importPackage = mainAttributes.getValue("Import-Package"); + + // From SimpleSearcher + assertThat(importPackage, containsString("com.yahoo.prelude.hitfield")); + assertThat(importPackage, containsString("org.json")); + + // From SimpleSearcher2 + assertThat(importPackage, containsString("com.yahoo.processing")); + assertThat(importPackage, containsString("com.yahoo.metrics.simple")); + assertThat(importPackage, containsString("com.google.inject")); + } + + @Test + public void require_that_manifest_contains_manual_imports() { + String importPackage = mainAttributes.getValue("Import-Package"); + + assertThat(importPackage, containsString("manualImport.withoutVersion")); + assertThat(importPackage, containsString("manualImport.withVersion;version=\"12.3.4\"")); + + for (int i=1; i<=2; ++i) + assertThat(importPackage, containsString("multiple.packages.with.the.same.version" + i + ";version=\"[1,2)\"")); + } + + @Test + public void require_that_manifest_contains_exports() { + String exportPackage = mainAttributes.getValue("Export-Package"); + assertThat(exportPackage, containsString("com.yahoo.test;version=1.2.3.RELEASE")); + } + + @Test + // TODO: use another jar than jrt, which now pulls in a lot of dependencies that pollute the manifest of the + // generated bundle. (It's compile scoped in pom.xml to be added to the bundle-cp.) + public void require_that_manifest_contains_bundle_class_path() { + String bundleClassPath = mainAttributes.getValue("Bundle-ClassPath"); + assertThat(bundleClassPath, containsString(".,")); + // If bundle-plugin-test is compiled in a mvn command that also built jrt, + // the jrt artifact is jrt.jar, otherwise the installed and versioned artifact + // is used: jrt-7-SNAPSHOT.jar. + assertThat(bundleClassPath, anyOf( + containsString("dependencies/jrt-7-SNAPSHOT.jar"), + containsString("dependencies/jrt.jar"))); + } + + @Test + public void require_that_component_jar_file_contains_compile_artifacts() { + ZipEntry versionedEntry = jarFile.getEntry("dependencies/jrt-7-SNAPSHOT.jar"); + ZipEntry unversionedEntry = jarFile.getEntry("dependencies/jrt.jar"); + if (versionedEntry == null) { + assertNotNull(unversionedEntry); + } else { + assertNull(unversionedEntry); + } + } + + + @Test + public void require_that_web_inf_url_is_propagated_to_the_manifest() { + String webInfUrl = mainAttributes.getValue("WebInfUrl"); + assertThat(webInfUrl, containsString("/WEB-INF/web.xml")); + } + + // TODO Vespa 8: Remove, the classpath mappings file is only needed for jersey resources to work in the application test framework. + // When this test is removed, also remove the maven-resources-plugin from the 'main' test bundle's pom. + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + @SuppressWarnings("unchecked") + @Test + public void bundle_class_path_mappings_are_generated() throws Exception { + ZipEntry classpathMappingsEntry = jarFile.getEntry(CLASSPATH_MAPPINGS_FILENAME); + + assertNotNull( + "Could not find " + CLASSPATH_MAPPINGS_FILENAME + " in the test bundle", + classpathMappingsEntry); + + Path mappingsFile = tempFolder.newFile(CLASSPATH_MAPPINGS_FILENAME).toPath(); + Files.copy(jarFile.getInputStream(classpathMappingsEntry), mappingsFile, REPLACE_EXISTING); + + ProjectBundleClassPaths bundleClassPaths = ProjectBundleClassPaths.load(mappingsFile); + + assertThat(bundleClassPaths.mainBundle.bundleSymbolicName, is("main")); + + Collection<String> mainBundleClassPaths = bundleClassPaths.mainBundle.classPathElements; + + assertThat(mainBundleClassPaths, + hasItems( + endsWith("target/classes"), + anyOf( + allOf(containsString("jrt"), containsString(".jar"), containsString("m2/repository")), + containsString("jrt/target/jrt.jar")))); + } +} |