Skip to content
This repository was archived by the owner on Oct 8, 2020. It is now read-only.

Commit 01c1589

Browse files
Merge pull request #8 from SANSA-Stack/test-runner
test cases running with Maven from CLI and IDE
2 parents 447d576 + fbb45f0 commit 01c1589

File tree

3 files changed

+122
-17
lines changed

3 files changed

+122
-17
lines changed

sansa-inference-tests/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<groupId>org.scalatest</groupId>
5555
<artifactId>scalatest_${scala.binary.version}</artifactId>
5656
</dependency>
57+
58+
<dependency>
59+
<groupId>com.typesafe.scala-logging</groupId>
60+
<artifactId>scala-logging_${scala.binary.version}</artifactId>
61+
</dependency>
5762
</dependencies>
5863

5964
<build>

sansa-inference-tests/src/test/scala/net/sansa_stack/test/conformance/ConformanceTestBase.scala

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package net.sansa_stack.test.conformance
22

3-
import java.io.File
3+
import java.io.{File, StringWriter}
4+
import java.nio.file.{Path, Paths}
45

56
import net.sansa_stack.inference.data.{RDF, RDFOps}
67
import org.apache.jena.rdf.model.Model
78
import org.apache.jena.shared.PrefixMapping
89
import org.junit.runner.RunWith
910
import org.scalatest.junit.JUnitRunner
1011
import org.scalatest.{BeforeAndAfterAll, FlatSpec}
11-
1212
import scala.collection.mutable
1313

14+
import net.sansa_stack.test.conformance.TestCases.getClass
15+
1416
/**
1517
* The class is to test the conformance of each materialization rule of RDFS(simple) entailment.
1618
*
@@ -20,15 +22,15 @@ import scala.collection.mutable
2022
@RunWith(classOf[JUnitRunner])
2123
abstract class ConformanceTestBase[Rdf <: RDF](val rdfOps: RDFOps[Rdf]) extends FlatSpec with BeforeAndAfterAll {
2224

25+
val logger = com.typesafe.scalalogging.Logger("ConformanceTestBase")
26+
2327
behavior of ""
2428

2529
// the test case IDs
2630
def testCaseIds: Set[String]
2731

28-
// the base directory of the test cases
29-
def testsCasesFolder: File = {
30-
new File(this.getClass.getClassLoader.getResource(testCasesPath).getPath)
31-
}
32+
def testsCasesFolder: String = testCasesPath // this.getClass.getClassLoader.getResource(testCasesPath).getPath
33+
// def testsCasesFolder: File = null // new File(this.getClass.getClassLoader.getResource(testCasesPath).getPath)
3234

3335
def testCasesPath: String
3436

@@ -38,12 +40,10 @@ abstract class ConformanceTestBase[Rdf <: RDF](val rdfOps: RDFOps[Rdf]) extends
3840
.withDefaultMappings(PrefixMapping.Standard)
3941

4042
// load the test cases
41-
private val testCases = TestCases.loadTestCases(testsCasesFolder, testCaseIds)
43+
lazy val testCases = TestCases.loadTestCasesJar(testsCasesFolder, testCaseIds)
4244

4345
// scalastyle:off println
4446
testCases.foreach { testCase =>
45-
println(testCase.id)
46-
4747
testCase.id should "produce the same graph" in {
4848
val triples = new mutable.HashSet[Rdf#Triple]()
4949

@@ -71,14 +71,20 @@ abstract class ConformanceTestBase[Rdf <: RDF](val rdfOps: RDFOps[Rdf]) extends
7171
// remove the input triples such that we can compare only the conclusion graph
7272
inferredModel.remove(testCase.inputGraph)
7373

74-
println("#" * 80 + "\ninput:")
75-
testCase.inputGraph.write(System.out, "TURTLE")
74+
logger.whenDebugEnabled {
75+
println("#" * 80 + "\ninput:")
76+
testCase.inputGraph.write(System.out, "TURTLE")
77+
}
7678

77-
println("#" * 80 + "\nexpected output:")
78-
testCase.outputGraph.write(System.out, "TURTLE")
79+
logger.whenDebugEnabled {
80+
println("#" * 80 + "\nexpected output:")
81+
testCase.outputGraph.write(System.out, "TURTLE")
82+
}
7983

80-
println("#" * 80 + "\ngot output:")
81-
inferredModel.write(System.out, "TURTLE")
84+
logger.whenDebugEnabled {
85+
println("#" * 80 + "\ngot output:")
86+
inferredModel.write(System.out, "TURTLE")
87+
}
8288

8389
// compare models, i.e. the inferred model should contain exactly the triples of the conclusion graph
8490
val correctOutput = inferredModel.containsAll(testCase.outputGraph)

sansa-inference-tests/src/test/scala/net/sansa_stack/test/conformance/TestCases.scala

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package net.sansa_stack.test.conformance
22

33
import java.io.File
4+
import java.nio.file.{FileSystem, Path}
45

5-
import org.apache.jena.riot.RDFDataMgr
6-
6+
import org.apache.jena.riot.{Lang, RDFDataMgr}
77
import scala.collection.mutable.ListBuffer
88
import scala.xml.XML
99

10+
import org.apache.commons.io.IOUtils
11+
import org.apache.jena.rdf.model.ModelFactory
12+
import org.scalatest.path
13+
1014
/**
1115
* Test cases loader.
1216
*
1317
* @author Lorenz Buehmann
1418
*/
1519
object TestCases {
1620

21+
val logger = com.typesafe.scalalogging.Logger("TestCases")
22+
23+
var fs: FileSystem = null
24+
1725
/**
1826
* Loads test cases from the given root folder.
1927
*
@@ -25,6 +33,8 @@ object TestCases {
2533

2634
val testCases = new ListBuffer[TestCase]()
2735

36+
println(directory)
37+
2838
directory.listFiles().filter(f => f.isDirectory && (ids.isEmpty || ids.contains(f.getName))).foreach { subDirectory =>
2939

3040
// the files in the directory
@@ -61,4 +71,88 @@ object TestCases {
6171

6272
testCases
6373
}
74+
75+
/**
76+
* Loads test cases from the given root folder.
77+
*
78+
* @param directory the root folder containing sub-folders for each test case
79+
* @return test cases
80+
*/
81+
def loadTestCasesJar(directory: String, ids: Set[String] = Set.empty): Seq[TestCase] = {
82+
logger.info(s"loading test cases from ${directory}...")
83+
84+
val testCases = new ListBuffer[TestCase]()
85+
86+
listFiles(directory).filter(f => ids.isEmpty || ids.contains(f.getFileName.toString.replace("/", ""))).map { p =>
87+
88+
// the files in the directory
89+
val files = listFiles(
90+
if (p.toUri.getScheme == "jar") p.toString.substring(1) else p.toString, true)
91+
92+
// get the metadata file
93+
val metadataFile = files.filter(_.toString.endsWith(".metadata.properties")).head
94+
95+
// load metadata XML
96+
val metadata = XML.load(metadataFile.toUri.toURL.openStream())
97+
98+
// id
99+
val id = (metadata \\ "entry").filter(n => n.attribute("key").get.text == "testcase.id").text
100+
101+
// description
102+
val description = (metadata \\ "entry").filter(n => n.attribute("key").get.text == "testcase.description").text
103+
104+
// test case type
105+
val entailmentType = (metadata \\ "entry").filter(n => n.attribute("key").get.text == "testcase.type").text
106+
107+
// currently we support only entailment test cases
108+
if (entailmentType == "POSITIVE_ENTAILMENT") {
109+
// load input data
110+
111+
val inputGraph = ModelFactory.createDefaultModel()
112+
inputGraph.read(files.filter(_.toString.endsWith(".premisegraph.ttl")).head.toUri.toURL.openStream(), null, "Turtle")
113+
114+
// load output data
115+
val outputGraph = ModelFactory.createDefaultModel()
116+
outputGraph.read(files.filter(_.toString.endsWith(".conclusiongraph.ttl")).head.toUri.toURL.openStream(), null, "Turtle")
117+
118+
testCases += TestCase(id, description, entailmentType, inputGraph, outputGraph)
119+
}
120+
}
121+
// directory.listFiles().filter(f => f.isDirectory && (ids.isEmpty || ids.contains(f.getName))).foreach { subDirectory =>
122+
123+
124+
println(s"loaded ${testCases.size} test cases")
125+
126+
if(fs != null) fs.close()
127+
128+
testCases
129+
}
130+
131+
private def listFiles(path: String, subDir: Boolean = false): Seq[Path] = {
132+
import java.nio.file.FileSystems
133+
import java.nio.file.Files
134+
import java.nio.file.Paths
135+
import java.util.Collections
136+
137+
// println(s"path: $path")
138+
val uri = if (path.startsWith("/")) new File(path).toURI else classOf[TestCase].getClassLoader.getResource(path).toURI
139+
// println(s"uri: $uri")
140+
var myPath: Path = null
141+
if (uri.getScheme == "jar" && !subDir) {
142+
fs = FileSystems.newFileSystem(uri, Collections.emptyMap[String, Any])
143+
myPath = fs.getPath(path)
144+
}
145+
else myPath = Paths.get(uri)
146+
val walk = Files.walk(myPath, 1)
147+
val it = walk.iterator
148+
var files = Seq[Path]()
149+
while ({it.hasNext}) {
150+
val subPath = it.next()
151+
if(!subPath.equals(myPath)) {
152+
files :+= subPath
153+
}
154+
}
155+
156+
files
157+
}
64158
}

0 commit comments

Comments
 (0)