You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/10/20 13:52:44 UTC

[plc4x] 09/25: [OSGi] Added initial Implementation of a Exam Test.

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch feature/resolve-split-package-osgi
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 6389e9a41d0ce43dbf3228ccd44ae3e3b1ead8fc
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Tue Oct 15 21:35:46 2019 +0200

    [OSGi] Added initial Implementation of a Exam Test.
---
 plc4j/karaf-features/karaf-itest/pom.xml           | 142 +++++++++++++++++++++
 .../java/org/apache/plc4x/karaf/FeatureITest.java  | 131 +++++++++++++++++++
 .../karaf-itest/src/test/resources/exam.properties |   1 +
 plc4j/karaf-features/pom.xml                       |   1 +
 plc4j/karaf-features/s7/pom.xml                    |  99 ++++++++++++++
 .../karaf-features/s7/src/main/feature/feature.xml |   2 +-
 .../java/org/apache/plc4x/karaf/FeatureTest.java   |  91 +++++++++++++
 7 files changed, 466 insertions(+), 1 deletion(-)

diff --git a/plc4j/karaf-features/karaf-itest/pom.xml b/plc4j/karaf-features/karaf-itest/pom.xml
new file mode 100644
index 0000000..9d4e3f5
--- /dev/null
+++ b/plc4j/karaf-features/karaf-itest/pom.xml
@@ -0,0 +1,142 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+  <parent>
+    <artifactId>karaf-features</artifactId>
+    <groupId>org.apache.plc4x</groupId>
+    <version>0.5.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>karaf-itest</artifactId>
+
+  <properties>
+    <dependency.karaf.version>4.2.1</dependency.karaf.version>
+    <pax.version>4.13.1</pax.version>
+  </properties>
+
+  <dependencies>
+<!--    &lt;!&ndash; S7 Driver for Build &ndash;&gt;-->
+<!--    <dependency>-->
+<!--      <groupId>org.apache.plc4x</groupId>-->
+<!--      <artifactId>driver-s7-feature</artifactId>-->
+<!--      <version>0.5.0-SNAPSHOT</version>-->
+<!--      <scope>test</scope>-->
+<!--    </dependency>-->
+
+    <!-- Pax Exam Test setup -->
+    <dependency>
+      <groupId>org.ops4j.pax.exam.samples</groupId>
+      <artifactId>pax-exam-sample8-ds</artifactId>
+      <version>${pax.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.karaf.features</groupId>
+      <artifactId>standard</artifactId>
+      <version>${dependency.karaf.version}</version>
+      <classifier>features</classifier>
+      <type>xml</type>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>6.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <!-- Dependencies for pax exam karaf container -->
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-container-karaf</artifactId>
+      <version>${pax.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-junit4</artifactId>
+      <version>${pax.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam</artifactId>
+      <version>${pax.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.url</groupId>
+      <artifactId>pax-url-aether</artifactId>
+      <version>2.6.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-api</artifactId>
+      <version>0.5.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.karaf.features</groupId>
+      <artifactId>org.apache.karaf.features.core</artifactId>
+      <version>${dependency.karaf.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.karaf.bundle</groupId>
+      <artifactId>org.apache.karaf.bundle.core</artifactId>
+      <version>${dependency.karaf.version}</version>
+      <scope>test</scope>
+    </dependency>
+
+  </dependencies>
+
+  <build>
+    <plugins>
+      <!-- Needed if you use versionAsInProject() -->
+      <plugin>
+        <groupId>org.apache.servicemix.tooling</groupId>
+        <artifactId>depends-maven-plugin</artifactId>
+        <version>1.4.0</version>
+        <executions>
+          <execution>
+            <id>generate-depends-file</id>
+            <goals>
+              <goal>generate-depends-file</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables>
+            <pax.exam.karaf.version>${dependency.karaf.version}</pax.exam.karaf.version>
+          </systemPropertyVariables>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
\ No newline at end of file
diff --git a/plc4j/karaf-features/karaf-itest/src/test/java/org/apache/plc4x/karaf/FeatureITest.java b/plc4j/karaf-features/karaf-itest/src/test/java/org/apache/plc4x/karaf/FeatureITest.java
new file mode 100644
index 0000000..1efc1db
--- /dev/null
+++ b/plc4j/karaf-features/karaf-itest/src/test/java/org/apache/plc4x/karaf/FeatureITest.java
@@ -0,0 +1,131 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.plc4x.karaf;
+
+import org.apache.karaf.bundle.core.BundleService;
+import org.apache.karaf.features.FeaturesService;
+import org.apache.plc4x.java.spi.PlcDriver;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.ConfigurationManager;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
+import org.ops4j.pax.exam.options.MavenUrlReference;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import java.io.File;
+import java.util.Arrays;
+import java.util.Optional;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.ops4j.pax.exam.CoreOptions.bundle;
+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.configureConsole;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
+
+@RunWith(PaxExam.class)
+public class FeatureITest {
+
+    private static Logger LOG = LoggerFactory.getLogger(FeatureITest.class);
+
+    @Inject
+    private FeaturesService featuresService;
+
+    @Inject
+    private BundleContext bundleContext;
+
+    @Configuration
+    public Option[] config() {
+        MavenArtifactUrlReference karafUrl = maven()
+            .groupId("org.apache.karaf")
+            .artifactId("apache-karaf")
+            .version(karafVersion())
+            .type("zip");
+
+        MavenUrlReference karafStandardRepo = maven()
+            .groupId("org.apache.karaf.features")
+            .artifactId("standard")
+            .version(karafVersion())
+            .classifier("features")
+            .type("xml");
+
+        final MavenArtifactUrlReference plc4xRepo = maven()
+            .groupId("org.apache.plc4x")
+            .artifactId("driver-s7-feature")
+            .version("0.5.0-SNAPSHOT")
+            .classifier("features")
+            .type("xml");
+        return new Option[] {
+            // KarafDistributionOption.debugConfiguration("5005", true),
+            karafDistributionConfiguration()
+                .frameworkUrl(karafUrl)
+                .unpackDirectory(new File("target", "exam"))
+                .useDeployFolder(false),
+            keepRuntimeFolder(),
+            configureConsole().ignoreLocalConsole(),
+            features(karafStandardRepo , "scr"),
+            features(plc4xRepo, "driver-s7-feature"),
+            mavenBundle()
+                .groupId("org.apache.plc4x")
+                .artifactId("plc4j-driver-s7")
+                .version("0.5.0-SNAPSHOT")
+                .start()
+        };
+    }
+
+    public static String karafVersion() {
+        ConfigurationManager cm = new ConfigurationManager();
+        String karafVersion = cm.getProperty("pax.exam.karaf.version", "3.0.0");
+        return karafVersion;
+    }
+
+
+    @Test
+    public void checkFeatureInstalled() throws Exception {
+        assertTrue(
+            featuresService.isInstalled(featuresService.getFeature("driver-s7-feature"))
+        );
+    }
+
+    @Test
+    public void checkBundleStarted() throws Exception {
+        for (Bundle bundle : bundleContext.getBundles()) {
+            System.out.println(bundle.getSymbolicName());
+        }
+
+        // Try to find the bundle
+        final Optional<Bundle> optionalBundle = Arrays.stream(bundleContext.getBundles())
+            .filter(bundle -> "org.apache.plc4x.plc4j-driver-s7".equals(bundle.getSymbolicName()))
+            .findFirst();
+
+        // Ensure that the bundle is resolved
+        assertTrue(optionalBundle.isPresent());
+
+        // Check if the bundle is active
+        assertEquals(Bundle.ACTIVE, optionalBundle.get().getState());
+    }
+
+}
diff --git a/plc4j/karaf-features/karaf-itest/src/test/resources/exam.properties b/plc4j/karaf-features/karaf-itest/src/test/resources/exam.properties
new file mode 100644
index 0000000..db2c7db
--- /dev/null
+++ b/plc4j/karaf-features/karaf-itest/src/test/resources/exam.properties
@@ -0,0 +1 @@
+pax.exam.karaf.version = 4.2.1
\ No newline at end of file
diff --git a/plc4j/karaf-features/pom.xml b/plc4j/karaf-features/pom.xml
index 76ab877..1400fd1 100644
--- a/plc4j/karaf-features/pom.xml
+++ b/plc4j/karaf-features/pom.xml
@@ -33,6 +33,7 @@
 
   <modules>
     <module>s7</module>
+    <module>karaf-itest</module>
   </modules>
 
   <build>
diff --git a/plc4j/karaf-features/s7/pom.xml b/plc4j/karaf-features/s7/pom.xml
index 36043d4..472b868 100644
--- a/plc4j/karaf-features/s7/pom.xml
+++ b/plc4j/karaf-features/s7/pom.xml
@@ -30,6 +30,105 @@
   <artifactId>driver-s7-feature</artifactId>
   <packaging>feature</packaging>
 
+  <dependencies>
+    <dependency>
+      <groupId>org.ops4j.pax.exam.samples</groupId>
+      <artifactId>pax-exam-sample8-ds</artifactId>
+      <version>4.13.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.karaf.features</groupId>
+      <artifactId>standard</artifactId>
+      <version>4.2.1</version>
+      <classifier>features</classifier>
+      <type>xml</type>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-container-karaf</artifactId>
+      <version>4.13.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-junit4</artifactId>
+      <version>4.13.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam</artifactId>
+      <version>4.13.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.url</groupId>
+      <artifactId>pax-url-aether</artifactId>
+      <version>1.6.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.11</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>6.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <!-- -->
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-api</artifactId>
+      <version>0.5.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <!-- Needed if you use versionAsInProject() -->
+      <plugin>
+        <groupId>org.apache.servicemix.tooling</groupId>
+        <artifactId>depends-maven-plugin</artifactId>
+        <version>1.2</version>
+        <executions>
+          <execution>
+            <id>generate-depends-file</id>
+            <goals>
+              <goal>generate-depends-file</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables>
+            <pax.exam.karaf.version>4.2.1</pax.exam.karaf.version>
+          </systemPropertyVariables>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
   <!-- This would be necessary with the kar goal which works not fine, yet -->
 <!--  <dependencies>-->
 <!--    <dependency>-->
diff --git a/plc4j/karaf-features/s7/src/main/feature/feature.xml b/plc4j/karaf-features/s7/src/main/feature/feature.xml
index 6bf41bb..aebf22a 100644
--- a/plc4j/karaf-features/s7/src/main/feature/feature.xml
+++ b/plc4j/karaf-features/s7/src/main/feature/feature.xml
@@ -24,7 +24,7 @@
     </details>
     <feature prerequisite="true" dependency="false">wrap</feature>
     <feature>scr</feature>
-    <bundle>mvn:org.apache.plc4x/plc4j-driver-s7/0.5.0-SNAPSHOT</bundle>
+<!--    <bundle>mvn:org.apache.plc4x/plc4j-driver-s7/0.5.0-SNAPSHOT</bundle>-->
     <bundle>mvn:org.apache.plc4x/plc4j-api/0.5.0-SNAPSHOT</bundle>
     <bundle>mvn:org.apache.plc4x/plc4j-protocol-iso-on-tcp/0.5.0-SNAPSHOT</bundle>
     <bundle>mvn:org.apache.plc4x/plc4j-protocol-iso-tp/0.5.0-SNAPSHOT</bundle>
diff --git a/plc4j/karaf-features/s7/src/test/java/org/apache/plc4x/karaf/FeatureTest.java b/plc4j/karaf-features/s7/src/test/java/org/apache/plc4x/karaf/FeatureTest.java
new file mode 100644
index 0000000..eb74608
--- /dev/null
+++ b/plc4j/karaf-features/s7/src/test/java/org/apache/plc4x/karaf/FeatureTest.java
@@ -0,0 +1,91 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+package org.apache.plc4x.karaf;
+
+import org.apache.plc4x.java.spi.PlcDriver;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
+import org.ops4j.pax.exam.options.MavenUrlReference;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.exam.spi.reactors.PerMethod;
+
+import javax.inject.Inject;
+
+import java.io.File;
+
+import static org.junit.Assert.assertNotNull;
+import static org.ops4j.pax.exam.CoreOptions.bundle;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.maven;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerMethod.class)
+public class FeatureTest {
+
+    @Inject
+    private PlcDriver plcDriver;
+
+    @Configuration
+    public Option[] config() {
+        MavenArtifactUrlReference karafUrl = maven()
+            .groupId("org.apache.karaf")
+            .artifactId("apache-karaf")
+            .version("3.0.0")
+            .type("tar.gz");
+        MavenUrlReference karafStandardRepo = maven()
+            .groupId("org.apache.karaf.features")
+            .artifactId("standard")
+            .classifier("features")
+            .version("4.2.1")
+            .type("xml")
+            .versionAsInProject();
+        return new Option[] {
+            // KarafDistributionOption.debugConfiguration("5005", true),
+            karafDistributionConfiguration()
+                .frameworkUrl(karafUrl)
+                .unpackDirectory(new File("target/exam"))
+                .useDeployFolder(false),
+            keepRuntimeFolder(),
+            features(karafStandardRepo , "scr"),
+            mavenBundle()
+                .groupId("org.ops4j.pax.exam.samples")
+                .artifactId("pax-exam-sample8-ds")
+                .version("4.2.1")
+                //.versionAsInProject()
+            .start(),
+        };
+    }
+
+    @Test
+    public void getHelloService() {
+        assertNotNull(plcDriver);
+        // assertEquals("Hello Pax!", helloService.getMessage());
+    }
+
+}