Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

49 junit integration #51

Merged
merged 27 commits into from
Sep 24, 2015
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>rdfunit-validate</module>
<!--module>rdfunit-webdemo</module--> <!-- removed from aggregation, kept as inheritance -->
<module>rdfunit-examples</module>
<module>rdfunit-junit</module>

</modules>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public Builder addLocalResourceOrSchemaURI(String prefix, String localResource,
return this;
}

public Builder addSchemaURI(String prefix, String schemaUri, RDFReader rdfReader) {
schemas.add(createSource(prefix, checkNotNull(schemaUri), checkNotNull(rdfReader)));
return this;
}

public RDFUnitTestSuiteGenerator build() {
return new RDFUnitTestSuiteGenerator(schemas, enableAutoTests, enableManualTests);
}
Expand Down
40 changes: 40 additions & 0 deletions rdfunit-junit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
RDFUnit - JUnit Integration
==========

RDFUnit-JUnit Integration provides an interface to JUnit via a dedicated Runner which exposes single RDFUnit Test Cases as Unit-Tests.

### Usage
```java

@RunWith(RdfUnitJunitRunner.class)
@Schema(uri = "ontologies/foaf.rdf")
public static class TestRunner {

@ControlledVocabulary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear what @ControlledVocabulary does

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is meant to be loaded along with the data (which is what I guess from the code) should be renamed to something like @AdditionalData but this can also be constructed directly on the @InputModel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Point - 'ControlledVocabulary' is just our use case (as we're loading CVs as additional data) but in general terms @AdditionalData is more suited.

In fact it gets merged with each InputModel later, but we need to keep both separated to figure out if the offending resources found during test execution is actually part of the InputModel or not.

public RDFReader getCVs() {
return new RDFModelReader(ModelFactory
.createDefaultModel()
.read("cv/required-cv.rdf"));
}

@TestInput
public RDFReader getInputData() {
return new RDFModelReader(ModelFactory
.createDefaultModel()
.read("inputmodels/foaf.rdf"));
}

}
```

### What the RdfUnitJunitRunner will do:

1. Validate the Test-Class: `@Schema` and `@TestInput` must be declared; `@ControlledVocabulary` is optional
2. Run the Test Generators against the Schema (can be local file or dereferencable URI)
3. Create generated TestCases for each `@TestInput`
4. Run tests individually hooked into the JUnit-Runtime

### What is currently not supported
- Manual Tests
- LOV integration
- JUnit: `@Rule`, `@Before`, `@After`
31 changes: 31 additions & 0 deletions rdfunit-junit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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>rdfunit-parent</artifactId>
<groupId>org.aksw.rdfunit</groupId>
<version>0.7.19-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>rdfunit-junit</artifactId>
<name>RDFUnit - JUnit Integration</name>

<dependencies>
<dependency>
<groupId>org.aksw.rdfunit</groupId>
<artifactId>rdfunit-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.aksw.rdfunit.junit;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ControlledVocabulary {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.aksw.rdfunit.junit;

import org.junit.runners.model.InitializationError;

/**
* Simplify initialization checks and error message formatting.
*
* @see <a href="https://code.google.com/p/guava-libraries/wiki/PreconditionsExplained">Guava Preconditions</a>
*/
class InitializationSupport {

static <T> T checkNotNull(
T t,
String stringFormatMessagePattern,
Object... patternReplacements
) throws InitializationError {
checkState(
t != null,
stringFormatMessagePattern,
patternReplacements
);
return t;
}

static void checkState(
boolean expressionResult,
String stringFormatMessagePattern,
Object... patternReplacements
) throws InitializationError {
if (expressionResult) {
return;
}
throw new InitializationError(String.format(stringFormatMessagePattern, patternReplacements));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.aksw.rdfunit.junit;

import java.util.ArrayList;
import java.util.Collection;

import com.hp.hpl.jena.rdf.model.ResourceFactory;
import org.aksw.rdfunit.tests.results.RLOGTestCaseResult;
import org.aksw.rdfunit.tests.results.TestCaseResult;
import org.junit.runners.model.Statement;

import static org.hamcrest.MatcherAssert.assertThat;

class RLOGStatement extends Statement {

private final RdfUnitJunitStatusTestExecutor rdfUnitJunitStatusTestExecutor;
private final RdfUnitJunitTestCase testCase;

RLOGStatement(RdfUnitJunitStatusTestExecutor rdfUnitJunitStatusTestExecutor, RdfUnitJunitTestCase testCase) {
this.rdfUnitJunitStatusTestExecutor = rdfUnitJunitStatusTestExecutor;
this.testCase = testCase;
}

@Override
public void evaluate() throws Throwable {
final Collection<TestCaseResult> testCaseResults = rdfUnitJunitStatusTestExecutor.runTest(testCase);
final Collection<RLOGTestCaseResult> remainingResults = new ArrayList<>();
for (TestCaseResult t : testCaseResults) {
RLOGTestCaseResult r = (RLOGTestCaseResult) t;
if (!resourceIsPartOfInputModel(r)) {
continue;
}
remainingResults.add(r);
}
final StringBuilder b = new StringBuilder();
b.append(testCase.getTestCase().getResultMessage()).append(":\n");
for (RLOGTestCaseResult r : remainingResults) {
b.append("\t").append(r.getResource()).append("\n");
}
assertThat(b.toString(), remainingResults.isEmpty());
}

private boolean resourceIsPartOfInputModel(RLOGTestCaseResult r) {
return testCase.getTestInputModel().contains(
ResourceFactory.createResource(r.getResource()), null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks if the failing resource is part of the @InputModel to not report things coming from the Schema or Additional Data. This says: "does the input model contain a statement, where the subject is the failing resource?"

I wonder if we should change this to be: "does the input model contain a statement, which defines the failing resource?", i.e. something along:

inputModel.contains(failingResource, RDF.TYPE);

}

}
Loading