Specify karaf version 31/90431/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 12 Jun 2020 11:16:26 +0000 (13:16 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 12 Jun 2020 16:10:00 +0000 (18:10 +0200)
We need karaf.version in SFT, as that is pointing to the correct
specs locator.

JIRA: ODLPARENT-228
Change-Id: Icc3f1ad024372b52efd4351069756b1dfefbb83d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
features-test/src/main/java/org/opendaylight/odlparent/featuretest/KarafConstants.java [new file with mode: 0644]
features-test/src/main/java/org/opendaylight/odlparent/featuretest/SingleFeatureTest.java
features-test/src/main/resources/singlefeaturetest.properties
features-test/src/test/java/org/opendaylight/odlparent/featuretest/KarafConstantsTest.java [new file with mode: 0644]

diff --git a/features-test/src/main/java/org/opendaylight/odlparent/featuretest/KarafConstants.java b/features-test/src/main/java/org/opendaylight/odlparent/featuretest/KarafConstants.java
new file mode 100644 (file)
index 0000000..54b80aa
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.odlparent.featuretest;
+
+import static java.util.Objects.requireNonNull;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+import org.eclipse.jdt.annotation.NonNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class KarafConstants {
+    private static final Logger LOG = LoggerFactory.getLogger(KarafConstants.class);
+
+    /**
+     * Property file used to store the Karaf distribution version.
+     */
+    private static final String PROPERTIES_FILENAME = "/singlefeaturetest.properties";
+    private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
+    private static final String KARAF_RELEASE_VERSION_PROP = "karaf.release.version";
+
+    private static final @NonNull String KARAF_DISTRO_VERSION;
+    private static final @NonNull String KARAF_RELEASE_VERSION;
+
+    static {
+        final InputStream input = KarafConstants.class.getResourceAsStream(PROPERTIES_FILENAME);
+        if (input == null) {
+            throw new IllegalStateException("Failed to open resource \"" + PROPERTIES_FILENAME + "\"");
+        }
+
+        final Properties props = new Properties();
+        try {
+            // We use a properties file to retrieve Karaf's version, instead of .versionAsInProject()
+            // This avoids forcing all users to depend on Karaf in their POMs
+            props.load(input);
+        } catch (IOException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+
+        KARAF_DISTRO_VERSION = nonnullProp(props, KARAF_DISTRO_VERSION_PROP);
+        KARAF_RELEASE_VERSION = nonnullProp(props, KARAF_RELEASE_VERSION_PROP);
+    }
+
+    private KarafConstants() {
+        // Hidden on purpose
+    }
+
+    /**
+     * Return Karaf distribution version. This defaults to this project version, as we use opendaylight-karaf-empty
+     * from this project. It can be overridden via a system property.
+     *
+     * @return Distribution version
+     */
+    static @NonNull String karafDistroVersion() {
+        return systemOrFile(KARAF_DISTRO_VERSION_PROP, KARAF_DISTRO_VERSION);
+    }
+
+    /**
+     * Return Karaf release version. This is upstream karaf release which is contained within the distribution. Note
+     * this is distinct from {@link #karafDistroVersion()}.
+     *
+     * @return Karaf version
+     */
+    static @NonNull String karafReleaseVersion() {
+        return systemOrFile(KARAF_RELEASE_VERSION_PROP, KARAF_RELEASE_VERSION);
+    }
+
+    private static @NonNull String systemOrFile(final String key, final @NonNull String fileVal) {
+        String ret = System.getProperty(key);
+        if (ret == null) {
+            ret = fileVal;
+            LOG.info("Retrieved {} value {} from properties file {}", key, ret, key);
+        } else {
+            LOG.info("Retrieved {} value {} from system properties", key, ret);
+        }
+        return ret;
+    }
+
+    private static @NonNull String nonnullProp(final Properties props, final String key) {
+        final String ret = props.getProperty(requireNonNull(key));
+        if (ret == null) {
+            throw new IllegalStateException("Property \"" + key + "\" not found");
+        }
+        return ret;
+    }
+}
index ff8044a439f458d6963139e6c9f93cc61783d079..d105e5a9b2264452f286c8eaabbf27af3c480316 100644 (file)
@@ -24,11 +24,9 @@ import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.EnumSet;
-import java.util.Properties;
 import javax.inject.Inject;
 import org.apache.karaf.bundle.core.BundleService;
 import org.apache.karaf.features.Feature;
@@ -91,16 +89,10 @@ public class SingleFeatureTest {
     /*
      * Property names to override defaults for karaf distro artifactId, groupId, version, and type
      */
-    private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
 
-    /**
-     * Property file used to store the Karaf distribution version.
-     */
-    private static final String PROPERTIES_FILENAME = "singlefeaturetest.properties";
-
     /**
      * <p>List of Karaf 4.2.6 default maven repositories with snapshot repositories excluded.</p>
      * <p>Unfortunately this must be hard-coded since declarative model which uses Options,
@@ -120,7 +112,7 @@ public class SingleFeatureTest {
     @Inject @NonNull
     private FeaturesService featuresService;
 
-    private String karafVersion;
+    private String karafReleaseVersion;
     private String karafDistroVersion;
 
     @ProbeBuilder
@@ -237,7 +229,7 @@ public class SingleFeatureTest {
             return baseConfig;
         }
 
-        final String version = getKarafVersion();
+        final String version = getKarafReleaseVersion();
         return OptionUtils.combine(baseConfig, new VMOption[] {
             new VMOption("--add-reads=java.xml=java.logging"),
             new VMOption("--add-exports=java.base/org.apache.karaf.specs.locator=java.xml,ALL-UNNAMED"),
@@ -270,40 +262,17 @@ public class SingleFeatureTest {
         return File.createTempFile("SingleFeatureTest-Karaf-JavaFlightRecorder", ".jfr").getAbsolutePath();
     }
 
-    private String getKarafVersion() throws IOException {
-        if (karafVersion == null) {
-            // We use a properties file to retrieve Karaf's version, instead of .versionAsInProject()
-            // This avoids forcing all users to depend on Karaf in their POMs
-            Properties singleFeatureTestProps = new Properties();
-            try (InputStream singleFeatureTestInputStream = Thread.currentThread().getContextClassLoader()
-                    .getResourceAsStream(PROPERTIES_FILENAME)) {
-                if (singleFeatureTestInputStream == null) {
-                    throw new IOException("Resource not found; expected to be present on current thread classloader: "
-                            + PROPERTIES_FILENAME);
-                }
-                singleFeatureTestProps.load(singleFeatureTestInputStream);
-            }
-            karafVersion = singleFeatureTestProps.getProperty(KARAF_DISTRO_VERSION_PROP);
-
-            LOG.info("Retrieved karafVersion {} from properties file {}", karafVersion, PROPERTIES_FILENAME);
-        } else {
-            LOG.info("Retrieved karafVersion {} from system property {}", karafVersion, KARAF_DISTRO_VERSION_PROP);
+    private String getKarafReleaseVersion() throws IOException {
+        if (karafReleaseVersion == null) {
+            karafReleaseVersion = KarafConstants.karafReleaseVersion();
         }
-
-        return karafVersion;
+        return karafReleaseVersion;
     }
 
-    private String getKarafDistroVersion() throws IOException {
+    private String getKarafDistroVersion() {
         if (karafDistroVersion == null) {
-            karafDistroVersion = System.getProperty(KARAF_DISTRO_VERSION_PROP);
-            if (karafDistroVersion == null) {
-                karafDistroVersion = getKarafVersion();
-            } else {
-                LOG.info("Retrieved karafDistroVersion {} from system property {}", karafVersion,
-                        KARAF_DISTRO_VERSION_PROP);
-            }
+            karafDistroVersion = KarafConstants.karafDistroVersion();
         }
-
         return karafDistroVersion;
     }
 
index e21655798d9a088f1c85e20cb9238e3c622dcd08..ad300793c8e4542de054f99d20a26273061cbede 100644 (file)
@@ -1 +1,2 @@
 karaf.distro.version=${project.version}
+karaf.release.version=${karaf.version}
diff --git a/features-test/src/test/java/org/opendaylight/odlparent/featuretest/KarafConstantsTest.java b/features-test/src/test/java/org/opendaylight/odlparent/featuretest/KarafConstantsTest.java
new file mode 100644 (file)
index 0000000..93568d3
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.odlparent.featuretest;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+
+public class KarafConstantsTest {
+    @Test
+    public void testKarafVersions() {
+        assertNotNull(KarafConstants.karafDistroVersion());
+        assertNotNull(KarafConstants.karafReleaseVersion());
+    }
+}