Skip to content

Commit

Permalink
cucumber-osgi and pax-exam example
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikSP authored and Hendrik Spohr committed Jun 5, 2015
1 parent 7db983e commit d034b7e
Show file tree
Hide file tree
Showing 19 changed files with 824 additions and 1 deletion.
29 changes: 29 additions & 0 deletions examples/pax-exam/calculator-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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>info.cukes</groupId>
<artifactId>pax-exam</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2.3-SNAPSHOT</version>
</parent>

<artifactId>pax-exam-calculator-api</artifactId>
<packaging>bundle</packaging>
<name>Examples: Pax Exam: Calculator API</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>cucumber.examples.java.paxexam</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cucumber.examples.java.paxexam;

public interface CalculatorService {

int add(int a, int b);

}
43 changes: 43 additions & 0 deletions examples/pax-exam/calculator-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<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>info.cukes</groupId>
<artifactId>pax-exam</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2.3-SNAPSHOT</version>
</parent>

<artifactId>pax-exam-calculator-service</artifactId>
<packaging>bundle</packaging>
<name>Examples: Pax Exam: Calculator Service</name>

<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>pax-exam-calculator-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Activator>cucumber.examples.java.paxexam.service.Activator</Bundle-Activator>
<Private-Package>cucumber.examples.java.paxexam.service</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cucumber.examples.java.paxexam.service;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import cucumber.examples.java.paxexam.CalculatorService;

public class Activator implements BundleActivator {

private volatile ServiceRegistration<CalculatorService> serviceRegistration;

@Override
public void start(BundleContext context) throws Exception {
serviceRegistration = context.registerService(CalculatorService.class, new CalculatorServiceImpl(), null);
}

@Override
public void stop(BundleContext context) throws Exception {
if (serviceRegistration != null) {
serviceRegistration.unregister();
serviceRegistration = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cucumber.examples.java.paxexam.service;

import cucumber.examples.java.paxexam.CalculatorService;

public class CalculatorServiceImpl implements CalculatorService {

@Override
public int add(int a, int b) {
return a + b;
}

}
12 changes: 12 additions & 0 deletions examples/pax-exam/calculator-test/features/calculator.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Calculate a result

Scenario Outline: Add two numbers
Given I set a to <num1>
Given I set b to <num2>
When I call the calculator service
Then the result is <result>

Examples:
| num1 | num2 | result |
| 9 | 8 | 17 |
| 5 | 4 | 9 |
75 changes: 75 additions & 0 deletions examples/pax-exam/calculator-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<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>info.cukes</groupId>
<artifactId>pax-exam</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2.3-SNAPSHOT</version>
</parent>

<artifactId>pax-exam-calculator-test</artifactId>
<packaging>jar</packaging>
<name>Examples: Pax Exam: Calculator Test</name>

<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>pax-exam-calculator-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>pax-exam-calculator-service</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-osgi</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-aether</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cucumber.examples.java.paxexam.test;

import static org.junit.Assert.assertEquals;

import javax.inject.Inject;

import cucumber.api.java.Before;
import cucumber.api.java.en.When;
import cucumber.examples.java.paxexam.CalculatorService;

public class CalculatorSteps {

@Inject
private CalculatorService calculatorService;

private int a, b, result;

@Before
public void before() {
a = 0;
b = 0;
result = 0;
}

@When("^I set a to (\\d+)$")
public void i_set_a_to(int v) throws Throwable {
a = v;
}

@When("^I set b to (\\d+)$")
public void i_set_b_to(int v) throws Throwable {
b = v;
}

@When("^I call the calculator service$")
public void i_call_the_calculator_service() throws Throwable {
result = calculatorService.add(a, b);
}

@When("^the result is (\\d+)$")
public void the_result_is(int expected) throws Throwable {
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cucumber.examples.java.paxexam.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.options;

import java.util.Collections;

import javax.inject.Inject;

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.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.ops4j.pax.exam.util.Injector;
import org.osgi.framework.BundleContext;

import cucumber.api.CucumberOptions;
import cucumber.java.runtime.osgi.OsgiClassFinder;
import cucumber.java.runtime.osgi.PaxExamObjectFactory;
import cucumber.runtime.Backend;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.CucumberException;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.RuntimeOptionsFactory;
import cucumber.runtime.io.FileResourceLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.java.JavaBackend;
import cucumber.runtime.java.ObjectFactory;

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerMethod.class)
@CucumberOptions(features = "features")
public class CalculatorTest {

@Inject
private Injector injector;

@Inject
private BundleContext bundleContext;

@Configuration
public Option[] config() {

return options(
mavenBundle("info.cukes", "pax-exam-calculator-api"),
mavenBundle("info.cukes", "pax-exam-calculator-service"),

mavenBundle("info.cukes", "gherkin"),
mavenBundle("info.cukes", "cucumber-jvm-deps", "1.0.4-SNAPSHOT"),
mavenBundle("info.cukes", "cucumber-core"),
mavenBundle("info.cukes", "cucumber-java"),
mavenBundle("info.cukes", "cucumber-osgi"),

mavenBundle("org.slf4j", "slf4j-api"),
mavenBundle("ch.qos.logback", "logback-core"),
mavenBundle("ch.qos.logback", "logback-classic"),

junitBundles()
);
}

@Test
public void cucumber() throws Exception {
assertNotNull(injector);
assertNotNull(bundleContext);
final ResourceLoader resourceLoader = new FileResourceLoader();
final ClassLoader classLoader = Runtime.class.getClassLoader();
final ObjectFactory objectFactory = new PaxExamObjectFactory(injector);
final ClassFinder classFinder = new OsgiClassFinder(bundleContext);
final Backend backend = new JavaBackend(objectFactory, classFinder);

final RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(getClass());
final RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

final Runtime runtime = new Runtime(resourceLoader, classLoader, Collections.singleton(backend), runtimeOptions);

runtime.run();

if (!runtime.getErrors().isEmpty()) {
throw new CucumberException(runtime.getErrors().get(0));
} else if (runtime.exitStatus() != 0x00) {
throw new CucumberException("There are pending or undefined steps.");
}
assertEquals(runtime.getErrors().size(), 0);
}
}
20 changes: 20 additions & 0 deletions examples/pax-exam/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<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>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>1.2.3-SNAPSHOT</version>
</parent>

<artifactId>pax-exam</artifactId>
<packaging>pom</packaging>
<name>Examples: Pax Exam</name>

<modules>
<module>calculator-api</module>
<module>calculator-service</module>
<module>calculator-test</module>
</modules>
</project>
Loading

0 comments on commit d034b7e

Please sign in to comment.