Skip to content

Commit e3b6d7d

Browse files
Refactor tests (#47)
* Add base fixtures * Add based refactor * Current version * Change files to random uuuids * Correct the actual bug --------- Co-authored-by: Karolina Bogacka <[email protected]>
1 parent f277d0e commit e3b6d7d

6 files changed

Lines changed: 194 additions & 209 deletions

File tree

src/main/scala/eu/neverblink/jelly/cli/util/IoUtil.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ object IoUtil:
3131
val file = File(fileName)
3232
val suppFile = file.getParentFile
3333
val parentFile = if (suppFile != null) suppFile else File(".")
34-
if !parentFile.canWrite then throw OutputFileCannotBeCreated(fileName)
34+
if !parentFile.canWrite || !file.canWrite then throw OutputFileCannotBeCreated(fileName)
3535
FileOutputStream(file, true)

src/test/scala/eu/neverblink/jelly/cli/command/RdfFromJellySpec.scala

Lines changed: 95 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,64 @@ package eu.neverblink.jelly.cli.command
22

33
import com.google.protobuf.InvalidProtocolBufferException
44
import eu.neverblink.jelly.cli.*
5-
65
import eu.neverblink.jelly.cli.command.helpers.*
76
import eu.neverblink.jelly.cli.command.rdf.*
8-
import org.apache.jena.riot.RDFLanguages
97
import org.scalatest.matchers.should.Matchers
108
import org.scalatest.wordspec.AnyWordSpec
119

12-
import java.nio.file.attribute.PosixFilePermissions
1310
import java.nio.file.{Files, Paths}
11+
import java.nio.file.attribute.PosixFilePermissions
1412
import scala.io.Source
1513
import scala.util.Using
1614

17-
class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
15+
class RdfFromJellySpec extends AnyWordSpec with Matchers with TestFixtureHelper:
1816

19-
protected val dHelper: DataGenHelper = DataGenHelper("testRdfFromJelly")
17+
protected val testCardinality: Integer = 33
2018

2119
"rdf from-jelly command" should {
2220
"handle conversion of Jelly to NTriples" when {
23-
"a file to output stream" in {
24-
val jellyFile = dHelper.generateJellyFile(3)
25-
val nQuadString = dHelper.generateNQuadString(3)
21+
"a file to output stream" in withFullJellyFile { j =>
22+
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
2623
val (out, err) =
27-
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", jellyFile))
24+
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", j))
2825
val sortedOut = out.split("\n").map(_.trim).sorted
2926
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
3027
sortedOut should contain theSameElementsAs sortedQuads
3128
}
3229

3330
"input stream to output stream" in {
34-
val input = dHelper.generateJellyInputStream(3)
31+
val input = DataGenHelper.generateJellyInputStream(testCardinality)
3532
RdfFromJelly.setStdIn(input)
36-
val nQuadString = dHelper.generateNQuadString(3)
33+
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
3734
val (out, err) = RdfFromJelly.runTestCommand(
3835
List("rdf", "from-jelly", "--out-format", RdfFormatOption.NQuads.cliOptions.head),
3936
)
4037
val sortedOut = out.split("\n").map(_.trim).sorted
4138
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
4239
sortedOut should contain theSameElementsAs sortedQuads
4340
}
44-
"a file to file" in {
45-
val jellyFile = dHelper.generateJellyFile(3)
46-
val nQuadString = dHelper.generateNQuadString(3)
47-
val outputFile = dHelper.generateFile(RDFLanguages.NQUADS)
48-
val (out, err) =
49-
RdfFromJelly.runTestCommand(
50-
List("rdf", "from-jelly", jellyFile, "--to", outputFile),
51-
)
52-
val sortedOut = Using.resource(Source.fromFile(outputFile)) { content =>
53-
content.getLines().toList.map(_.trim).sorted
41+
"a file to file" in withFullJellyFile { j =>
42+
withEmptyQuadFile { q =>
43+
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
44+
val (out, err) =
45+
RdfFromJelly.runTestCommand(
46+
List("rdf", "from-jelly", j, "--to", q),
47+
)
48+
val sortedOut = Using.resource(Source.fromFile(q)) { content =>
49+
content.getLines().toList.map(_.trim).sorted
50+
}
51+
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
52+
sortedOut should contain theSameElementsAs sortedQuads
53+
out.length should be(0)
5454
}
55-
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
56-
sortedOut should contain theSameElementsAs sortedQuads
57-
out.length should be(0)
5855
}
59-
"an input stream to file" in {
60-
val input = dHelper.generateJellyInputStream(3)
56+
"an input stream to file" in withEmptyQuadFile { q =>
57+
val input = DataGenHelper.generateJellyInputStream(testCardinality)
6158
RdfFromJelly.setStdIn(input)
62-
val outputFile = dHelper.generateFile(RDFLanguages.NQUADS)
63-
val nQuadString = dHelper.generateNQuadString(3)
59+
val nQuadString = DataGenHelper.generateNQuadString(testCardinality)
6460
val (out, err) =
65-
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", "--to", outputFile))
66-
val sortedOut = Using.resource(Source.fromFile(outputFile)) { content =>
61+
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", "--to", q))
62+
val sortedOut = Using.resource(Source.fromFile(q)) { content =>
6763
content.getLines().toList.map(_.trim).sorted
6864
}
6965
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
@@ -72,14 +68,13 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
7268
}
7369
}
7470
"handle conversion of Jelly binary to text" when {
75-
"a file to output stream" in {
76-
val jellyFile = dHelper.generateJellyFile(3)
71+
"a file to output stream" in withFullJellyFile { j =>
7772
val (out, err) =
7873
RdfFromJelly.runTestCommand(
7974
List(
8075
"rdf",
8176
"from-jelly",
82-
jellyFile,
77+
j,
8378
"--out-format",
8479
RdfFormatOption.JellyText.cliOptions.head,
8580
),
@@ -99,7 +94,7 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
9994
| }
10095
|}""".stripMargin
10196
out should include(outString)
102-
"rows".r.findAllIn(out).length should be(10)
97+
"rows".r.findAllIn(out).length should be(70)
10398
"http://example.org/predicate/".r.findAllIn(out).length should be(1)
10499
}
105100
}
@@ -114,87 +109,87 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
114109
RdfFromJelly.getErrString should include(msg)
115110
exception.code should be(1)
116111
}
117-
"input file is not accessible" in {
118-
val jellyFile = dHelper.generateJellyFile(3)
112+
"input file is not accessible" in withFullJellyFile { j =>
119113
val permissions = PosixFilePermissions.fromString("---------")
120114
Files.setPosixFilePermissions(
121-
Paths.get(jellyFile),
115+
Paths.get(j),
122116
permissions,
123117
)
124118
val exception =
125119
intercept[ExitException] {
126120

127-
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", jellyFile))
121+
RdfFromJelly.runTestCommand(List("rdf", "from-jelly", j))
128122
}
129-
val msg = InputFileInaccessible(jellyFile).getMessage
123+
val msg = InputFileInaccessible(j).getMessage
130124
RdfFromJelly.getErrString should include(msg)
131125
exception.code should be(1)
132126
}
133-
"output file cannot be created" in {
134-
val jellyFile = dHelper.generateJellyFile(3)
135-
val unreachableDir = dHelper.makeTestDir()
136-
Paths.get(unreachableDir).toFile.setWritable(false)
137-
val quadFile = dHelper.generateFile()
138-
val exception =
139-
intercept[ExitException] {
127+
"output file cannot be created" in withFullJellyFile { j =>
128+
withEmptyQuadFile { q =>
129+
Paths.get(q).toFile.setWritable(false)
130+
val exception =
131+
intercept[ExitException] {
132+
133+
RdfFromJelly.runTestCommand(
134+
List("rdf", "from-jelly", j, "--to", q),
135+
)
136+
}
137+
val msg = OutputFileCannotBeCreated(q).getMessage
138+
Paths.get(q).toFile.setWritable(true)
139+
RdfFromJelly.getErrString should include(msg)
140+
exception.code should be(1)
141+
142+
}
140143

141-
RdfFromJelly.runTestCommand(
142-
List("rdf", "from-jelly", jellyFile, "--to", quadFile),
143-
)
144-
}
145-
val msg = OutputFileCannotBeCreated(quadFile).getMessage
146-
Paths.get(unreachableDir).toFile.setWritable(true)
147-
RdfFromJelly.getErrString should include(msg)
148-
exception.code should be(1)
149144
}
150-
"deserializing error occurs" in {
151-
val jellyFile = dHelper.generateJellyFile(3)
152-
val quadFile = dHelper.generateFile()
153-
RdfFromJelly.runTestCommand(
154-
List("rdf", "from-jelly", jellyFile, "--to", quadFile),
155-
)
156-
val exception =
157-
intercept[ExitException] {
158-
RdfFromJelly.runTestCommand(
159-
List("rdf", "from-jelly", quadFile),
160-
)
161-
}
162-
val msg = InvalidJellyFile(new InvalidProtocolBufferException("")).getMessage
163-
val errContent = RdfFromJelly.getErrString
164-
errContent should include(msg)
165-
errContent should include("Run with --debug to see the complete stack trace.")
166-
exception.code should be(1)
145+
"deserializing error occurs" in withFullJellyFile { j =>
146+
withEmptyQuadFile { q =>
147+
RdfFromJelly.runTestCommand(
148+
List("rdf", "from-jelly", j, "--to", q),
149+
)
150+
val exception =
151+
intercept[ExitException] {
152+
RdfFromJelly.runTestCommand(
153+
List("rdf", "from-jelly", q),
154+
)
155+
}
156+
val msg = InvalidJellyFile(new InvalidProtocolBufferException("")).getMessage
157+
val errContent = RdfFromJelly.getErrString
158+
errContent should include(msg)
159+
errContent should include("Run with --debug to see the complete stack trace.")
160+
exception.code should be(1)
161+
}
167162
}
168-
"parsing error occurs with debug set" in {
169-
val jellyFile = dHelper.generateJellyFile(3)
170-
val quadFile = dHelper.generateFile()
171-
RdfFromJelly.runTestCommand(
172-
List("rdf", "from-jelly", jellyFile, "--to", quadFile),
173-
)
174-
val exception =
175-
intercept[ExitException] {
176-
RdfFromJelly.runTestCommand(
177-
List("rdf", "from-jelly", quadFile, "--debug"),
178-
)
179-
}
180-
val msg = InvalidJellyFile(new InvalidProtocolBufferException("")).getMessage
181-
val errContent = RdfFromJelly.getErrString
182-
errContent should include(msg)
183-
errContent should include("eu.neverblink.jelly.cli.InvalidJellyFile")
184-
exception.code should be(1)
163+
"parsing error occurs with debug set" in withFullJellyFile { j =>
164+
withEmptyQuadFile { q =>
165+
RdfFromJelly.runTestCommand(
166+
List("rdf", "from-jelly", j, "--to", q),
167+
)
168+
val exception =
169+
intercept[ExitException] {
170+
RdfFromJelly.runTestCommand(
171+
List("rdf", "from-jelly", q, "--debug"),
172+
)
173+
}
174+
val msg = InvalidJellyFile(new InvalidProtocolBufferException("")).getMessage
175+
val errContent = RdfFromJelly.getErrString
176+
errContent should include(msg)
177+
errContent should include("eu.neverblink.jelly.cli.InvalidJellyFile")
178+
exception.code should be(1)
179+
}
185180
}
186-
"invalid output format supplied" in {
187-
val jellyFile = dHelper.generateJellyFile(3)
188-
val quadFile = dHelper.generateFile()
189-
val exception =
190-
intercept[ExitException] {
191-
RdfFromJelly.runTestCommand(
192-
List("rdf", "from-jelly", jellyFile, "--to", quadFile, "--out-format", "invalid"),
193-
)
194-
}
195-
val msg = InvalidFormatSpecified("invalid", RdfFromJellyPrint.validFormatsString)
196-
RdfFromJelly.getErrString should include(msg.getMessage)
197-
exception.code should be(1)
181+
"invalid output format supplied" in withFullJellyFile { j =>
182+
withEmptyQuadFile { q =>
183+
val exception =
184+
intercept[ExitException] {
185+
RdfFromJelly.runTestCommand(
186+
List("rdf", "from-jelly", j, "--to", q, "--out-format", "invalid"),
187+
)
188+
}
189+
val msg = InvalidFormatSpecified("invalid", RdfFromJellyPrint.validFormatsString)
190+
RdfFromJelly.getErrString should include(msg.getMessage)
191+
exception.code should be(1)
192+
}
198193
}
199194
}
200195
}
Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package eu.neverblink.jelly.cli.command
22

33
import eu.neverblink.jelly.cli.{ExitException, InvalidFormatSpecified}
4-
import eu.neverblink.jelly.cli.command.helpers.{CleanUpAfterTest, DataGenHelper}
4+
import eu.neverblink.jelly.cli.command.helpers.{DataGenHelper, TestFixtureHelper}
55
import eu.neverblink.jelly.cli.command.rdf.{RdfFormatOption, RdfToJelly, RdfToJellyPrint}
66
import eu.ostrzyciel.jelly.convert.jena.riot.JellyLanguage
77
import org.apache.jena.rdf.model.{Model, ModelFactory}
@@ -12,9 +12,9 @@ import org.apache.jena.riot.RDFParser
1212
import java.io.{ByteArrayInputStream, FileInputStream, InputStream}
1313
import scala.util.Using
1414

15-
class RdfToJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
15+
class RdfToJellySpec extends AnyWordSpec with TestFixtureHelper with Matchers:
1616

17-
protected val dHelper: DataGenHelper = DataGenHelper("testRdfToJelly")
17+
protected val testCardinality: Integer = 33
1818

1919
def translateJellyBack(inputStream: InputStream): Model =
2020
Using(inputStream) { content =>
@@ -28,60 +28,52 @@ class RdfToJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
2828

2929
"rdf to-jelly command" should {
3030
"handle conversion of NTriples to Jelly" when {
31-
"a file to output stream" in {
32-
val nQuadFile = dHelper.generateNQuadFile(3)
33-
val tripleModel = dHelper.generateTripleModel(3)
31+
"a file to output stream" in withFullQuadFile { f =>
3432
val (out, err) =
35-
RdfToJelly.runTestCommand(List("rdf", "to-jelly", nQuadFile))
33+
RdfToJelly.runTestCommand(List("rdf", "to-jelly", f))
3634
val newIn = new ByteArrayInputStream(RdfToJelly.getOutBytes)
3735
val content = translateJellyBack(newIn)
38-
content.containsAll(tripleModel.listStatements())
36+
content.containsAll(DataGenHelper.generateTripleModel(testCardinality).listStatements())
3937
}
4038

41-
"a file to file" in {
42-
val nQuadFile = dHelper.generateNQuadFile(3)
43-
val newFile = dHelper.generateFile(JellyLanguage.JELLY)
44-
val tripleModel = dHelper.generateTripleModel(3)
45-
val (out, err) =
46-
RdfToJelly.runTestCommand(List("rdf", "to-jelly", nQuadFile, "--to", newFile))
47-
val content = translateJellyBack(new FileInputStream(newFile))
48-
content.containsAll(tripleModel.listStatements())
39+
"a file to file" in withFullQuadFile { f =>
40+
withEmptyJellyFile { j =>
41+
val (out, err) =
42+
RdfToJelly.runTestCommand(List("rdf", "to-jelly", f, "--to", j))
43+
val content = translateJellyBack(new FileInputStream(j))
44+
content.containsAll(DataGenHelper.generateTripleModel(testCardinality).listStatements())
45+
}
4946
}
50-
5147
"input stream to output stream" in {
52-
val testNumber = 10
53-
val input = dHelper.generateNQuadInputStream(testNumber)
48+
val input = DataGenHelper.generateNQuadInputStream(testCardinality)
5449
RdfToJelly.setStdIn(input)
55-
val tripleModel = dHelper.generateTripleModel(testNumber)
50+
val tripleModel = DataGenHelper.generateTripleModel(testCardinality)
5651
val (out, err) = RdfToJelly.runTestCommand(
5752
List("rdf", "to-jelly", "--in-format", RdfFormatOption.NQuads.cliOptions.head),
5853
)
5954
val newIn = new ByteArrayInputStream(RdfToJelly.getOutBytes)
6055
val content = translateJellyBack(newIn)
6156
content.containsAll(tripleModel.listStatements())
6257
}
63-
64-
"an input stream to file" in {
65-
val testNumber = 23
66-
val input = dHelper.generateNQuadInputStream(testNumber)
58+
"an input stream to file" in withEmptyJellyFile { j =>
59+
val input = DataGenHelper.generateNQuadInputStream(testCardinality)
6760
RdfToJelly.setStdIn(input)
68-
val newFile = dHelper.generateFile(JellyLanguage.JELLY)
69-
val tripleModel = dHelper.generateTripleModel(testNumber)
70-
val (out, err) = RdfToJelly.runTestCommand(List("rdf", "to-jelly", "--to", newFile))
71-
val content = translateJellyBack(new FileInputStream(newFile))
61+
val tripleModel = DataGenHelper.generateTripleModel(testCardinality)
62+
val (out, err) = RdfToJelly.runTestCommand(List("rdf", "to-jelly", "--to", j))
63+
val content = translateJellyBack(new FileInputStream(j))
7264
content.containsAll(tripleModel.listStatements())
7365
}
7466
}
75-
"throw proper exception" when {
76-
"invalid format is specified" in {
77-
val jellyFile = dHelper.generateNQuadFile(3)
78-
val exception =
79-
intercept[ExitException] {
80-
RdfToJelly.runTestCommand(List("rdf", "to-jelly", jellyFile, "--in-format", "invalid"))
81-
}
82-
val msg = InvalidFormatSpecified("invalid", RdfToJellyPrint.validFormatsString)
83-
RdfToJelly.getErrString should include(msg.getMessage)
84-
exception.code should be(1)
85-
}
67+
}
68+
"throw proper exception" when {
69+
"invalid format is specified" in withFullQuadFile { f =>
70+
val exception =
71+
intercept[ExitException] {
72+
RdfToJelly.runTestCommand(List("rdf", "to-jelly", f, "--in-format", "invalid"))
73+
}
74+
val msg = InvalidFormatSpecified("invalid", RdfToJellyPrint.validFormatsString)
75+
RdfToJelly.getErrString should include(msg.getMessage)
76+
exception.code should be(1)
8677
}
78+
8779
}

0 commit comments

Comments
 (0)