Skip to content

Commit

Permalink
Merge pull request #121 from rombert/feature/pax-exam-tests
Browse files Browse the repository at this point in the history
Add OSGiSmokeTest using pax-exam
  • Loading branch information
fbacchella committed Nov 5, 2018
2 parents cd0bbe5 + 05c1c10 commit 8d3546e
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pom.xml
Expand Up @@ -27,6 +27,7 @@
<jdk.home>${java.home}</jdk.home>
<!-- current year. -->
<year>2018</year>
<pax-exam.version>4.11.0</pax-exam.version>
</properties>
<licenses>
<license>
Expand Down Expand Up @@ -68,6 +69,37 @@
<version>3.6</version>
<scope>test</scope>
</dependency>
<!-- Test dependencies for OSGi setup -->
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>${pax-exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>${pax-exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>${pax-exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/org/rrd4j/osgi/OSGiSmokeTest.java
@@ -0,0 +1,76 @@
package org.rrd4j.osgi;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assume.assumeThat;
import static org.ops4j.pax.exam.CoreOptions.bundle;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.rrd4j.ConsolFun.AVERAGE;
import static org.rrd4j.ConsolFun.MAX;

import java.io.IOException;
import java.util.Random;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
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.rrd4j.DsType;
import org.rrd4j.core.RrdBackendFactory;
import org.rrd4j.core.RrdDb;
import org.rrd4j.core.RrdDef;
import org.rrd4j.core.Sample;

/**
* This test validates that the rrd4j bundle resolves in a minimal OSGi container
*
* <p>The bundle is picked up from the <tt>target/classes</tt> folder, so the classes <i>and</i>
* the MANIFEST.MF file should be present there. This is usually the case when using Eclipse with
* m2e, but in some scenarios there might be a need to run <tt>mvn compile</tt>.</p>
*
* <p>This is intentionally not a full-fledged test of rrd4j, it just ensures that the bundle
* works in an OSGi container.</p>
*
*/
@RunWith(PaxExam.class)
public class OSGiSmokeTest {

@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Configuration
public Option[] config() {
return options(bundle("reference:file:target/classes"), junitBundles());
}

@Test
public void basicUsage() throws IOException {

// OSGi metadata is only added for Java8+, so skip this test on Java 7
String javaVersion = System.getProperty("java.version");
assumeThat(javaVersion, not(startsWith((("1.7")))));

RrdDef rrdDef = new RrdDef(temp.newFile().getAbsolutePath(), 300);
rrdDef.addArchive(AVERAGE, 0.5, 1, 600); // 1 step, 600 rows
rrdDef.addArchive(AVERAGE, 0.5, 6, 700); // 6 steps, 700 rows
rrdDef.addDatasource("inbytes", DsType.GAUGE, 600, Double.NaN, Double.NaN);
rrdDef.addArchive(MAX, 0.5, 1, 600);

Random rnd = new Random();

// due to sun.misc usually not being exported, default to the FILE backend
try (RrdDb rrdDb = new RrdDb(rrdDef, RrdBackendFactory.getFactory("FILE"))) {
long time = System.currentTimeMillis() / 1000;
Sample sample = rrdDb.createSample();
for ( int i = 0 ; i < 10; i++ ) {
sample.setTime(time + i * 1000);
sample.setValue("inbytes", rnd.nextDouble());
sample.update();
}
}
}
}
11 changes: 11 additions & 0 deletions src/test/resources/logback-test.xml
@@ -0,0 +1,11 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%t] %level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit 8d3546e

Please sign in to comment.