18
18
package org .apache .daffodil .core .util
19
19
20
20
import java .io .ByteArrayInputStream
21
- import java .io .File
22
- import java .io .FileNotFoundException
23
21
import java .io .InputStream
24
22
import java .nio .channels .Channels
25
23
import java .nio .channels .ReadableByteChannel
26
24
import java .nio .channels .WritableByteChannel
27
- import java .nio .file .Paths
28
25
import scala .collection .mutable .ArrayBuffer
29
26
import scala .util .Try
30
27
import scala .xml ._
31
28
32
29
import org .apache .daffodil .core .compiler .Compiler
30
+ import org .apache .daffodil .core .compiler .ProcessorFactory
33
31
import org .apache .daffodil .core .dsom ._
34
32
import org .apache .daffodil .io .InputSourceDataInputStream
35
33
import org .apache .daffodil .lib .Implicits ._
36
34
import org .apache .daffodil .lib .api ._
35
+ import org .apache .daffodil .lib .exceptions .MultiException
37
36
import org .apache .daffodil .lib .externalvars .Binding
38
37
import org .apache .daffodil .lib .util ._
39
- import org .apache .daffodil .lib .xml .XMLUtils
40
38
import org .apache .daffodil .lib .xml ._
41
39
import org .apache .daffodil .runtime1 .api .DFDL
42
40
import org .apache .daffodil .runtime1 .api .MetadataHandler
@@ -48,9 +46,6 @@ import org.apache.daffodil.runtime1.infoset.ScalaXMLInfosetOutputter
48
46
import org .apache .daffodil .runtime1 .processors .DataProcessor
49
47
import org .apache .daffodil .runtime1 .processors .VariableMap
50
48
51
- import org .apache .commons .io .output .NullOutputStream
52
- import org .junit .Assert .assertEquals
53
-
54
49
object INoWarnU2 { ImplicitsSuppressUnusedImportWarning () }
55
50
56
51
/*
@@ -60,31 +55,8 @@ object INoWarnU2 { ImplicitsSuppressUnusedImportWarning() }
60
55
*/
61
56
object TestUtils {
62
57
63
- /**
64
- * Compares two XML Elements, after having (optionally) stripped off all attributes.
65
- */
66
- def assertEqualsXMLElements (expected : Node , actual : Node ): Unit = {
67
- XMLUtils .compareAndReport(expected, actual)
68
- }
69
-
70
- /**
71
- * We want to be able to run tests from Eclipse or from batch builds that
72
- * are rooted in a different directory, so, since Java/JVMs don't have a notion
73
- * of setting the current directory to a specific value for interpreting things,
74
- * we have to do that ourselves manually like this.
75
- *
76
- * When you specify a file for use in a test, you want to specify it
77
- * relative to the root of the sub-project of which it is part. I.e., within core,
78
- * the file you specify should be relative to daffodil/sub-projects/core.
79
- *
80
- * Returns null if the file cannot be found.
81
- */
82
- def findFile (fn : String ): File = findFile(new File (fn))
83
- def findFile (f : File ): File = {
84
- if (f.exists()) return f
85
- val cwd = new File (" " ).getAbsolutePath
86
- throw new FileNotFoundException (" Couldn't find file " + f + " relative to " + cwd + " ." )
87
- }
58
+ def assertEquals [T ](expected : T , actual : T ) =
59
+ if (expected != actual) throw new AssertionError (" assertEquals failed." )
88
60
89
61
def testString (testSchema : Node , data : String , areTracing : Boolean = false ) = {
90
62
runSchemaOnRBC(testSchema, Misc .stringToReadableByteChannel(data), areTracing)
@@ -108,11 +80,7 @@ object TestUtils {
108
80
runSchemaOnRBC(testSchema, rbc, areTracing)
109
81
}
110
82
111
- def testFile (testSchema : Node , fileName : String ) = {
112
- runSchemaOnRBC(testSchema, Misc .fileToReadableByteChannel(new java.io.File (fileName)))
113
- }
114
-
115
- val useSerializedProcessor = true
83
+ private val useSerializedProcessor = true
116
84
117
85
def testUnparsing (
118
86
testSchema : scala.xml.Elem ,
@@ -122,25 +90,17 @@ object TestUtils {
122
90
): Seq [Diagnostic ] = {
123
91
val compiler = Compiler ().withTunable(" allowExternalPathExpressions" , " true" )
124
92
val pf = compiler.compileNode(testSchema)
125
- if (pf.isError) {
126
- val msgs = pf.getDiagnostics.map(_.getMessage()).mkString(" \n " )
127
- throw new Exception (msgs)
128
- }
93
+ if (pf.isError) throwDiagnostics(pf.getDiagnostics)
129
94
var u = saveAndReload(pf.onPath(" /" ).asInstanceOf [DataProcessor ])
130
- if (u.isError) {
131
- val msgs = u.getDiagnostics.map(_.getMessage()).mkString(" \n " )
132
- throw new Exception (msgs)
133
- }
95
+ if (u.isError) throwDiagnostics(u.getDiagnostics)
134
96
val outputStream = new java.io.ByteArrayOutputStream ()
135
97
val out = java.nio.channels.Channels .newChannel(outputStream)
136
98
u = if (areTracing) {
137
99
u.withDebugger(builtInTracer).withDebugging(true )
138
100
} else u
139
101
val inputter = new ScalaXMLInfosetInputter (infosetXML)
140
102
val actual = u.unparse(inputter, out)
141
- if (actual.isProcessingError) {
142
- throwDiagnostics(actual.getDiagnostics)
143
- }
103
+ if (actual.isProcessingError) throwDiagnostics(actual.getDiagnostics)
144
104
val unparsed = outputStream.toString
145
105
// System.err.println("parsed: " + infoset)
146
106
// System.err.println("unparsed: " + unparsed)
@@ -149,12 +109,8 @@ object TestUtils {
149
109
actual.getDiagnostics
150
110
}
151
111
152
- def throwDiagnostics (ds : Seq [Diagnostic ]): Unit = {
153
- if (ds.length == 1 ) throw (ds(0 ))
154
- else {
155
- val msgs = ds.map(_.getMessage()).mkString(" \n " )
156
- throw new Exception (msgs)
157
- }
112
+ private def throwDiagnostics (ds : Seq [Diagnostic ]): Nothing = {
113
+ new MultiException (ds).toss
158
114
}
159
115
160
116
def testUnparsingBinary (
@@ -208,30 +164,21 @@ object TestUtils {
208
164
def compileSchema (testSchema : Node ) = {
209
165
val compiler = Compiler ()
210
166
val pf = compiler.compileNode(testSchema)
211
- val isError = pf.isError
212
- val msgs = pf.getDiagnostics.map(_.getMessage()).mkString(" \n " )
213
-
214
- if (isError) {
215
- throw new Exception (msgs)
216
- }
167
+ if (pf.isError) throwDiagnostics(pf.getDiagnostics)
217
168
val p = saveAndReload(pf.onPath(" /" ).asInstanceOf [DataProcessor ])
218
- val pIsError = p.isError
219
- if (pIsError) {
220
- val msgs = pf.getDiagnostics.map(_.getMessage()).mkString(" \n " )
221
- throw new Exception (msgs)
222
- }
169
+ if (p.isError) throwDiagnostics(p.getDiagnostics)
223
170
p
224
171
}
225
172
226
- def runSchemaOnRBC (
173
+ private def runSchemaOnRBC (
227
174
testSchema : Node ,
228
175
data : ReadableByteChannel ,
229
176
areTracing : Boolean = false
230
177
): (DFDL .ParseResult , Node ) = {
231
178
runSchemaOnInputStream(testSchema, Channels .newInputStream(data), areTracing)
232
179
}
233
180
234
- def runSchemaOnInputStream (
181
+ private def runSchemaOnInputStream (
235
182
testSchema : Node ,
236
183
is : InputStream ,
237
184
areTracing : Boolean = false
@@ -255,12 +202,7 @@ object TestUtils {
255
202
val outputter = new ScalaXMLInfosetOutputter ()
256
203
val input = InputSourceDataInputStream (is)
257
204
val actual = p.parse(input, outputter)
258
- if (actual.isProcessingError) {
259
- val diags = actual.getDiagnostics
260
- if (diags.length == 1 ) throw diags(0 )
261
- val msgs = diags.map(_.getMessage()).mkString(" \n " )
262
- throw new Exception (msgs)
263
- }
205
+ if (actual.isProcessingError) throwDiagnostics(actual.getDiagnostics)
264
206
(actual, outputter.getResult)
265
207
}
266
208
@@ -292,52 +234,16 @@ object TestUtils {
292
234
compiler : Compiler ,
293
235
schemaSource : URISchemaSource ,
294
236
output : WritableByteChannel
295
- ) = {
237
+ ): Try [( ProcessorFactory , DFDL . DataProcessor )] = {
296
238
Try {
297
239
val pf = compiler.compileSource(schemaSource)
298
- if (! pf.isError) {
299
- val dp = pf.onPath(" /" )
300
- dp.save(output)
301
- if (! dp.isError) {
302
- (pf, dp)
303
- } else {
304
- throw new Exception (
305
- (dp.getDiagnostics ++ pf.getDiagnostics).map { _.getMessage() }.mkString(" \n " )
306
- )
307
- }
308
- } else
309
- throw new Exception (pf.getDiagnostics.map { _.getMessage() }.mkString(" \n " ))
240
+ if (pf.isError) throwDiagnostics(pf.getDiagnostics)
241
+ val dp = pf.onPath(" /" )
242
+ dp.save(output)
243
+ if (dp.isError) throwDiagnostics(dp.getDiagnostics ++ pf.getDiagnostics)
244
+ (pf, dp)
310
245
}
311
246
}
312
-
313
- def testCompileTime (resourcePathString : String ): Unit = {
314
- val nos = NullOutputStream .INSTANCE
315
- val nullChannel = java.nio.channels.Channels .newChannel(nos)
316
- val compiler = Compiler ()
317
- val uri = Misc .getRequiredResource(resourcePathString)
318
- val schemaSource = URISchemaSource (Paths .get(resourcePathString).toFile, uri)
319
- val theTry = Timer .getResult(compileAndSave(compiler, schemaSource, nullChannel))
320
- theTry.get
321
- }
322
-
323
- /**
324
- * Gets some sort of a message from a throwable. Includes messages from all
325
- * classes in the cause chain. If there is no message, just uses the class name.
326
- * @param cause
327
- * @return
328
- */
329
- def getAMessage (cause : Throwable ): String = {
330
- val n1 = Misc .getNameFromClass(cause)
331
- val m = cause.getMessage
332
- val c = cause.getCause
333
- val s = (m, c) match {
334
- case (null , null ) => Misc .getNameFromClass(cause)
335
- case (null , c) => getAMessage(c)
336
- case (m, null ) => m
337
- case (m, c) => m + " " + getAMessage(c)
338
- }
339
- n1 + s " ( $s) "
340
- }
341
247
}
342
248
343
249
/**
@@ -397,7 +303,7 @@ class Fakes private () {
397
303
lazy val fakeSequenceGroupRef = fs3.asInstanceOf [SequenceGroupRef ]
398
304
lazy val fakeGroupRefFactory = GroupRefFactory (fs1.xml, fs1, 1 , false )
399
305
400
- class FakeDataProcessor extends DFDL .DataProcessor {
306
+ private class FakeDataProcessor extends DFDL .DataProcessor {
401
307
override def save (output : DFDL .Output ): Unit = {}
402
308
override def parse (
403
309
input : InputSourceDataInputStream ,
@@ -428,18 +334,15 @@ class Fakes private () {
428
334
): DFDL .DaffodilUnparseContentHandler = null
429
335
430
336
}
431
- lazy val fakeDP = new FakeDataProcessor
337
+ lazy val fakeDP : DFDL . DataProcessor = new FakeDataProcessor
432
338
433
339
}
434
340
435
341
/**
436
342
* Testing class for streaming message parse behavior
437
343
*/
438
344
object StreamParser {
439
- case class CompileFailure (diags : Seq [Diagnostic ])
440
- extends Exception (" DFDL Schema Compile Failure" ) {
441
- override def getMessage () = diags.map { _.toString }.mkString(" ,\n " )
442
- }
345
+ case class CompileFailure (diags : Seq [Diagnostic ]) extends MultiException (diags)
443
346
444
347
/**
445
348
* Result object for parse calls. Just a tuple.
@@ -473,37 +376,38 @@ object StreamParser {
473
376
}
474
377
}
475
378
476
- def doStreamTest (schema : Node , data : String ): Seq [Result ] = {
379
+ def doStreamTest (schema : Node , data : String ): Stream [Result ] = {
477
380
val mp = new StreamParser (schema)
478
381
val is : InputStream = new ByteArrayInputStream (data.getBytes(" ascii" ))
479
382
mp.setInputStream(is)
480
383
var r : StreamParser .Result = null
481
384
val results = new ArrayBuffer [Result ]
482
385
val resStream = Stream .continually(mp.parse).takeWhile(r => ! r.isProcessingError)
483
- resStream.toSeq
386
+ resStream
484
387
}
485
388
}
486
389
487
- class StreamParser (val schema : Node ) {
390
+ class StreamParser private (val schema : Node ) {
391
+
392
+ private lazy val outputter = new ScalaXMLInfosetOutputter ()
393
+ private var dis : InputSourceDataInputStream = _
488
394
489
- val outputter = new ScalaXMLInfosetOutputter ()
490
- var dis : InputSourceDataInputStream = _
491
- var dp : DFDL .DataProcessor = _
492
395
//
493
396
// First compile the DFDL Schema
494
- val c = Compiler ()
495
- val pf = c.compileNode(schema)
496
- val pfDiags = pf.getDiagnostics
497
- if (pf.isError) throw new StreamParser .CompileFailure (pfDiags)
498
- dp = pf
499
- .onPath(" /" )
500
- .withValidationMode(ValidationMode .Full )
501
- // .withDebuggerRunner(new TraceDebuggerRunner()) // DAFFODIL-2624 - cannot trace in streaming SAPI
502
- // .withDebugging(true)
503
- val dpDiags = dp.getDiagnostics
504
- if (dp.isError) throw new StreamParser .CompileFailure (dpDiags)
505
- val compilationWarnings =
506
- if (! pfDiags.isEmpty) pfDiags else dpDiags // dpDiags might be empty. That's ok.
397
+ private lazy val c = Compiler ()
398
+ private lazy val pf = c.compileNode(schema)
399
+ private val dp = {
400
+ if (pf.isError) throw new StreamParser .CompileFailure (pf.getDiagnostics)
401
+ val dataproc = pf
402
+ .onPath(" /" )
403
+ .withValidationMode(ValidationMode .Full )
404
+ // .withDebuggerRunner(new TraceDebuggerRunner()) // DAFFODIL-2624 - cannot trace in streaming SAPI
405
+ // .withDebugging(true)
406
+ if (dataproc.isError) throw new StreamParser .CompileFailure (dataproc.getDiagnostics)
407
+ dataproc
408
+ }
409
+
410
+ lazy val compilationWarnings : Seq [Diagnostic ] = pf.getDiagnostics ++ dp.getDiagnostics
507
411
508
412
def setInputStream (inputStream : InputStream ): Unit = {
509
413
dis = InputSourceDataInputStream (inputStream)
0 commit comments