From df333669e74ca9b4a39e5fa808f3fedcc7e241cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Kr=C3=BCger?= <martin.krueger@rohde-schwarz.com>
Date: Tue, 8 Jan 2019 15:48:12 +0100
Subject: [PATCH] KARAF-6082 Remove hard-coded version string from test case

... and replace with dynamically calculated version from pom file

With that patch the test uses the code of the current version and not
some part version when the code was last touched. This may lead to
unexpected behavior and/or failing test because of missing artifacts
of past Karaf versions.
---
 .../karaf/examples/itests/ExampleITest.java   | 18 +++++---
 .../KarafMinimalMonitoredTestSupport.java     | 46 +++++++++----------
 2 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/examples/karaf-itest-example/src/test/java/org/apache/karaf/examples/itests/ExampleITest.java b/examples/karaf-itest-example/src/test/java/org/apache/karaf/examples/itests/ExampleITest.java
index ddbc172d46..51f105ec69 100644
--- a/examples/karaf-itest-example/src/test/java/org/apache/karaf/examples/itests/ExampleITest.java
+++ b/examples/karaf-itest-example/src/test/java/org/apache/karaf/examples/itests/ExampleITest.java
@@ -13,6 +13,7 @@
  */
 package org.apache.karaf.examples.itests;
 
+import java.io.File;
 import org.apache.karaf.features.Feature;
 import org.apache.karaf.features.FeaturesService;
 import org.apache.karaf.itests.KarafTestSupport;
@@ -20,21 +21,23 @@ import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.MavenUtils;
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.karaf.container.internal.JavaVersionUtil;
 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
+import org.ops4j.pax.exam.options.extra.VMOption;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
 
-import java.io.File;
-
 import static org.ops4j.pax.exam.CoreOptions.maven;
 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
-import org.ops4j.pax.exam.karaf.container.internal.JavaVersionUtil;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureSecurity;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
-import org.ops4j.pax.exam.options.extra.VMOption;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
 
 @RunWith(PaxExam.class)
 @ExamReactorStrategy(PerClass.class)
@@ -44,6 +47,7 @@ public class ExampleITest extends KarafTestSupport {
     @Configuration
     public Option[] config() {
         MavenArtifactUrlReference karafUrl = maven().groupId("org.apache.karaf").artifactId("apache-karaf").versionAsInProject().type("tar.gz");
+        String karafVersion = MavenUtils.getArtifactVersion("org.apache.karaf", "apache-karaf");
 
         String httpPort = Integer.toString(getAvailablePort(Integer.parseInt(MIN_HTTP_PORT), Integer.parseInt(MAX_HTTP_PORT)));
         String rmiRegistryPort = Integer.toString(getAvailablePort(Integer.parseInt(MIN_RMI_REG_PORT), Integer.parseInt(MAX_RMI_REG_PORT)));
@@ -75,10 +79,10 @@ public class ExampleITest extends KarafTestSupport {
                 new VMOption("--add-exports=java.base/org.apache.karaf.specs.locator=java.xml,ALL-UNNAMED"),
                 new VMOption("--patch-module"),
                 new VMOption("java.base=lib/endorsed/org.apache.karaf.specs.locator-" 
-                + System.getProperty("karaf.version", "4.2.3-SNAPSHOT") + ".jar"),
+                + System.getProperty("karaf.version", karafVersion) + ".jar"),
                 new VMOption("--patch-module"),
                 new VMOption("java.xml=lib/endorsed/org.apache.karaf.specs.java.xml-" 
-                + System.getProperty("karaf.version", "4.2.3-SNAPSHOT") + ".jar"),
+                + System.getProperty("karaf.version", karafVersion) + ".jar"),
                 new VMOption("--add-opens"),
                 new VMOption("java.base/java.security=ALL-UNNAMED"),
                 new VMOption("--add-opens"),
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java b/itests/test/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java
index 8d8efff226..c833dd5ead 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/mavenresolver/KarafMinimalMonitoredTestSupport.java
@@ -16,38 +16,22 @@
  */
 package org.apache.karaf.itests.mavenresolver;
 
-import static org.apache.karaf.itests.KarafTestSupport.MAX_RMI_REG_PORT;
-import static org.apache.karaf.itests.KarafTestSupport.MAX_RMI_SERVER_PORT;
-import static org.apache.karaf.itests.KarafTestSupport.MIN_RMI_REG_PORT;
-import static org.apache.karaf.itests.KarafTestSupport.MIN_RMI_SERVER_PORT;
-import static org.ops4j.pax.exam.CoreOptions.composite;
-import static org.ops4j.pax.exam.CoreOptions.maven;
-import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureSecurity;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
-import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle;
-import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd;
-import static org.osgi.framework.Constants.OBJECTCLASS;
-
 import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.function.Function;
-
 import javax.inject.Inject;
-
 import org.apache.karaf.itests.KarafTestSupport;
 import org.apache.karaf.itests.monitoring.Activator;
 import org.apache.karaf.itests.monitoring.ServiceMonitor;
+import org.ops4j.pax.exam.MavenUtils;
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.ProbeBuilder;
 import org.ops4j.pax.exam.TestProbeBuilder;
 import org.ops4j.pax.exam.karaf.container.internal.JavaVersionUtil;
 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
-import org.ops4j.pax.exam.options.extra.VMOption;
 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
+import org.ops4j.pax.exam.options.extra.VMOption;
 import org.ops4j.store.Handle;
 import org.ops4j.store.Store;
 import org.ops4j.store.intern.TemporaryStore;
@@ -56,6 +40,21 @@ import org.osgi.framework.ServiceEvent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.karaf.itests.KarafTestSupport.MAX_RMI_REG_PORT;
+import static org.apache.karaf.itests.KarafTestSupport.MAX_RMI_SERVER_PORT;
+import static org.apache.karaf.itests.KarafTestSupport.MIN_RMI_REG_PORT;
+import static org.apache.karaf.itests.KarafTestSupport.MIN_RMI_SERVER_PORT;
+import static org.ops4j.pax.exam.CoreOptions.composite;
+import static org.ops4j.pax.exam.CoreOptions.maven;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureSecurity;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
+import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle;
+import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd;
+import static org.osgi.framework.Constants.OBJECTCLASS;
+
 // Don't extend KarafTestSupport, because we don't want @Rule Retry
 public abstract class KarafMinimalMonitoredTestSupport {
 
@@ -63,7 +62,7 @@ public abstract class KarafMinimalMonitoredTestSupport {
 
     @Inject
     protected ServiceMonitor serviceMonitor;
-    
+
     @ProbeBuilder
     public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
         probe.setHeader(Constants.IMPORT_PACKAGE, ServiceMonitor.class.getPackage().getName());
@@ -74,6 +73,7 @@ public abstract class KarafMinimalMonitoredTestSupport {
         MavenArtifactUrlReference karafUrl = maven()
                 .groupId("org.apache.karaf").artifactId("apache-karaf-minimal")
                 .versionAsInProject().type("tar.gz");
+        String karafVersion = MavenUtils.getArtifactVersion("org.apache.karaf", "apache-karaf");
 
         String rmiRegistryPort = Integer.toString(KarafTestSupport.getAvailablePort(Integer.parseInt(MIN_RMI_REG_PORT), Integer.parseInt(MAX_RMI_REG_PORT)));
         String rmiServerPort = Integer.toString(KarafTestSupport.getAvailablePort(Integer.parseInt(MIN_RMI_SERVER_PORT), Integer.parseInt(MAX_RMI_SERVER_PORT)));
@@ -96,11 +96,11 @@ public abstract class KarafMinimalMonitoredTestSupport {
                 new VMOption("--add-reads=java.xml=java.logging"),
                 new VMOption("--add-exports=java.base/org.apache.karaf.specs.locator=java.xml,ALL-UNNAMED"),
                 new VMOption("--patch-module"),
-                new VMOption("java.base=lib/endorsed/org.apache.karaf.specs.locator-" 
-                + System.getProperty("karaf.version", "4.2.3-SNAPSHOT") + ".jar"),
+                new VMOption("java.base=lib/endorsed/org.apache.karaf.specs.locator-"
+                + System.getProperty("karaf.version", karafVersion) + ".jar"),
                 new VMOption("--patch-module"),
-                new VMOption("java.xml=lib/endorsed/org.apache.karaf.specs.java.xml-" 
-                + System.getProperty("karaf.version", "4.2.3-SNAPSHOT") + ".jar"),
+                new VMOption("java.xml=lib/endorsed/org.apache.karaf.specs.java.xml-"
+                + System.getProperty("karaf.version", karafVersion) + ".jar"),
                 new VMOption("--add-opens"),
                 new VMOption("java.base/java.security=ALL-UNNAMED"),
                 new VMOption("--add-opens"),
-- 
2.19.1

