@@ -2,68 +2,64 @@ package eu.neverblink.jelly.cli.command
22
33import com .google .protobuf .InvalidProtocolBufferException
44import eu .neverblink .jelly .cli .*
5-
65import eu .neverblink .jelly .cli .command .helpers .*
76import eu .neverblink .jelly .cli .command .rdf .*
8- import org .apache .jena .riot .RDFLanguages
97import org .scalatest .matchers .should .Matchers
108import org .scalatest .wordspec .AnyWordSpec
119
12- import java .nio .file .attribute .PosixFilePermissions
1310import java .nio .file .{Files , Paths }
11+ import java .nio .file .attribute .PosixFilePermissions
1412import scala .io .Source
1513import 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 }
0 commit comments