Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from rombert/feature/pax-exam-tests
Add OSGiSmokeTest using pax-exam
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |