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:51 UTC

[plc4x] 16/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 68476f402648c5ab16e6423cbeba85d8bf142659
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           | 124 ++++++++---------
 .../java/org/apache/plc4x/karaf/FeatureITest.java  | 155 ++++++++++++---------
 .../karaf-itest/src/test/resources/exam.properties |   1 +
 plc4j/karaf-features/s7/pom.xml                    |  99 +++++++++++++
 .../karaf-features/s7/src/main/feature/feature.xml |   2 +-
 5 files changed, 252 insertions(+), 129 deletions(-)

diff --git a/plc4j/karaf-features/karaf-itest/pom.xml b/plc4j/karaf-features/karaf-itest/pom.xml
index b42c5b6..9d4e3f5 100644
--- a/plc4j/karaf-features/karaf-itest/pom.xml
+++ b/plc4j/karaf-features/karaf-itest/pom.xml
@@ -1,22 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!--
-
-  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.
-
--->
 <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">
@@ -30,49 +12,55 @@
   <artifactId>karaf-itest</artifactId>
 
   <properties>
-    <karaf.version>4.2.6</karaf.version>
+    <dependency.karaf.version>4.2.1</dependency.karaf.version>
     <pax.version>4.13.1</pax.version>
   </properties>
 
   <dependencies>
-    <!-- Provide the KarafTestSupport -->
+<!--    &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.apache.karaf.itests</groupId>
-      <artifactId>common</artifactId>
-      <version>${karaf.version}</version>
+      <groupId>org.ops4j.pax.exam.samples</groupId>
+      <artifactId>pax-exam-sample8-ds</artifactId>
+      <version>${pax.version}</version>
       <scope>test</scope>
     </dependency>
-    <!-- Define the Apache Karaf version to download and use for the test -->
-    <!-- We use a released version here to avoid SNAPSHOT resolution -->
     <dependency>
-      <groupId>org.apache.karaf</groupId>
-      <artifactId>apache-karaf</artifactId>
-      <version>${karaf.version}</version>
+      <groupId>org.apache.karaf.features</groupId>
+      <artifactId>standard</artifactId>
+      <version>${dependency.karaf.version}</version>
+      <classifier>features</classifier>
+      <type>xml</type>
       <scope>test</scope>
-      <type>tar.gz</type>
     </dependency>
 
     <dependency>
       <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-log4j12</artifactId>
-      <version>1.7.26</version>
+      <artifactId>slf4j-api</artifactId>
       <scope>test</scope>
     </dependency>
-    <!-- Required to use shell commands in the tests -->
+
     <dependency>
-      <groupId>org.apache.karaf.shell</groupId>
-      <artifactId>org.apache.karaf.shell.core</artifactId>
-      <version>${karaf.version}</version>
-      <scope>test</scope>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>6.0.0</version>
+      <scope>provided</scope>
     </dependency>
-    <!-- Provide the PaxExam Karaf support -->
+
+    <!-- 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>
-    <!-- Provide the PaxExam JUnit extension -->
     <dependency>
       <groupId>org.ops4j.pax.exam</groupId>
       <artifactId>pax-exam-junit4</artifactId>
@@ -80,21 +68,28 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>org.apache.geronimo.specs</groupId>
-      <artifactId>geronimo-atinject_1.0_spec</artifactId>
-      <version>1.1</version>
+      <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>org.awaitility</groupId>
-      <artifactId>awaitility</artifactId>
-      <version>3.1.6</version>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+      <scope>test</scope>
     </dependency>
+
     <dependency>
-      <groupId>org.apache.servicemix.bundles</groupId>
-      <artifactId>org.apache.servicemix.bundles.hamcrest</artifactId>
-      <version>1.3_1</version>
-      <scope>runtime</scope>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.apache.plc4x</groupId>
@@ -102,10 +97,24 @@
       <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>
@@ -119,28 +128,11 @@
           </execution>
         </executions>
       </plugin>
-      <!-- See https://issues.apache.org/jira/browse/KARAF-6457 -->
       <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
-          <forkCount>1</forkCount>
-          <reuseForks>false</reuseForks>
           <systemPropertyVariables>
-            <org.ops4j.pax.logging.DefaultServiceLog.level>INFO</org.ops4j.pax.logging.DefaultServiceLog.level>
-            <spring31.version>3.1.4.RELEASE</spring31.version>
-            <spring32.version>3.2.18.RELEASE_1</spring32.version>
-            <spring40.version>4.0.9.RELEASE_1</spring40.version>
-            <spring41.version>4.1.9.RELEASE_1</spring41.version>
-            <spring42.version>4.2.9.RELEASE_1</spring42.version>
-            <spring43.version>4.3.25.RELEASE_1</spring43.version>
-            <spring50.version>5.0.15.RELEASE_1</spring50.version>
-            <spring51.version>5.1.9.RELEASE_1</spring51.version>
-            <spring.security31.version>3.1.4.RELEASE</spring.security31.version>
-            <spring.security42.version>4.2.4.RELEASE_1</spring.security42.version>
-            <spring.security51.version>5.1.5.RELEASE_1</spring.security51.version>
-            <activemq.version>5.15.9</activemq.version>
-            <plc4x.version>${project.version}</plc4x.version>
+            <pax.exam.karaf.version>${dependency.karaf.version}</pax.exam.karaf.version>
           </systemPropertyVariables>
         </configuration>
       </plugin>
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
index 33a7722..1efc1db 100644
--- 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
@@ -13,88 +13,119 @@
  */
 package org.apache.plc4x.karaf;
 
-import org.apache.karaf.itests.KarafTestSupport;
+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.karaf.options.KarafDistributionOption;
-import org.ops4j.pax.exam.karaf.options.LogLevelOption;
 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
-import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
-import org.ops4j.pax.exam.spi.reactors.PerMethod;
+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 java.util.stream.Stream;
+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.karaf.options.KarafDistributionOption.logLevel;
+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)
-@ExamReactorStrategy(PerMethod.class)
-public class FeatureITest extends KarafTestSupport {
+public class FeatureITest {
+
+    private static Logger LOG = LoggerFactory.getLogger(FeatureITest.class);
+
+    @Inject
+    private FeaturesService featuresService;
+
+    @Inject
+    private BundleContext bundleContext;
 
     @Configuration
     public Option[] config() {
-        Option[] options = new Option[]{
-            logLevel(LogLevelOption.LogLevel.DEBUG),
-            KarafDistributionOption.editConfigurationFilePut("etc/system.properties", "plc4x.version", System.getProperty("plc4x.version")),
+        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()
         };
-        return Stream.of(super.config(), options).flatMap(Stream::of).toArray(Option[]::new);
     }
 
-    /**
-     * Checks:
-     * - the feature can be installed
-     * - the Driver Bundle is there and active
-     * - the Bundle contains the DS for PlcDriver
-     * - the feature is installed
-     */
+    public static String karafVersion() {
+        ConfigurationManager cm = new ConfigurationManager();
+        String karafVersion = cm.getProperty("pax.exam.karaf.version", "3.0.0");
+        return karafVersion;
+    }
+
+
     @Test
-    public void installAndAssertFeature() throws Exception {
-        // Assemble Maven URL
-        final MavenArtifactUrlReference featureRepo = maven()
-            .groupId("org.apache.plc4x")
-            .artifactId("driver-s7-feature")
-            .version(System.getProperty("plc4x.version"))
-            .type("xml")
-            .classifier("features");
-        System.out.println("Installing feature repo " + featureRepo.getURL());
-
-        // Install the feature-repo
-        addFeaturesRepository(featureRepo.getURL());
-
-        // Install the feature
-        installAndAssertFeature("driver-s7-feature");
-
-        // Print Bundles and fetch result
-        String bundles = executeCommand("bundle:list -t 0");
-
-        // Find that line
-        // 84 │ Active │  80 │ 0.5.0.SNAPSHOT │ PLC4J: Driver: S7
-        assertContains("PLC4J: Driver: S7", bundles);
-        System.out.println(bundles);
-
-        // Find Bundle for more detailed check
-        final Bundle bundle = findBundleByName("org.apache.plc4x.plc4j-driver-s7");
-
-        // Bundle has to be ACTIVE
-        assertNotNull(bundle);
-        assertEquals(Bundle.ACTIVE, bundle.getState());
-
-        // Check declarative service is present
-        String services = executeCommand("services -p " + bundle.getBundleId());
-        System.out.println("Services: " + services);
-        assertContains("component.name = org.apache.plc4x.java.s7.S7PlcDriver", services);
-        assertContains("objectClass = [org.apache.plc4x.java.spi.PlcDriver]", services);
-
-        // Just for Debugging...
-        String features = executeCommand("feature:list -i");
-        System.out.print(features);
-        assertContains("driver-s7-feature", features);
+    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());
     }
 
-}
\ No newline at end of file
+}
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/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>