-
Notifications
You must be signed in to change notification settings - Fork 73
/
TestJavaAPI.java
1432 lines (1249 loc) · 65 KB
/
TestJavaAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.daffodil.example;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import scala.collection.JavaConverters;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.daffodil.japi.*;
import org.apache.daffodil.japi.infoset.XMLTextInfosetOutputter;
import org.jdom2.output.Format;
import org.junit.Test;
import org.apache.daffodil.japi.infoset.JDOMInfosetInputter;
import org.apache.daffodil.japi.infoset.JDOMInfosetOutputter;
import org.apache.daffodil.japi.io.InputSourceDataInputStream;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import javax.xml.XMLConstants;
import java.nio.charset.StandardCharsets;
import org.apache.daffodil.japi.infoset.*;
public class TestJavaAPI {
/**
* Best practices for XML loading are to turn off anything that could lead to
* insecurity.
*
* This is probably unnecessary in the case of these tests, but as these tests
* are also used to illustrate API usage, this exemplifies best practice.
*/
public static void setSecureDefaults(XMLReader xmlReader)
throws SAXNotSupportedException, SAXNotRecognizedException {
xmlReader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// since we're not really sure what they mean by secure processing
// we make doubly sure by setting these ourselves also.
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
}
String SAX_NAMESPACES_FEATURE = "http://xml.org/sax/features/namespaces";
String SAX_NAMESPACE_PREFIXES_FEATURE = "http://xml.org/sax/features/namespace-prefixes";
private java.io.File getResource(String resPath) {
try {
return new java.io.File(this.getClass().getResource(resPath).toURI());
} catch (Exception e) {
return null;
}
}
/**
* This is a test-only helper function used to serialize and deserialize a
* DataProcessor to ensure all JAPI classes that need to extend
* Serializable do so appropriately.
*
* All of the JAPI tests create a DataProcessor. To test that we correctly
* made all the necessary changes to make the JAPI DataProcessor
* serializable, it is important to serialize and deserialize that
* DataProcessor before use in the tests. This function acts as a helper
* function to accomplish that task.
*
* So this functions accepts a JAPI DataProcessor, serializes and deserializes
* that DataProcessor in memory, and then returns the result.
*/
private DataProcessor reserializeDataProcessor(DataProcessor dp) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
dp.save(Channels.newChannel(baos));
baos.close();
DataProcessor result = null;
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try {
result = Daffodil.compiler().reload(Channels.newChannel(bais));
} catch (InvalidParserException e) {
fail("Unable to reload data processor");
}
return result;
}
@Test
public void testJavaAPI1() throws IOException, ClassNotFoundException {
DebuggerRunnerForJAPITest debugger = new DebuggerRunnerForJAPITest();
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
dp = dp.withDebuggerRunner(debugger);
dp = dp.withDebugging(true);
java.io.File file = getResource("/test/japi/myData.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
assertTrue(debugger.lines.size() > 0);
assertTrue(debugger.lines.contains("----------------------------------------------------------------- 1\n"));
assertTrue(debugger.getCommand().equals("trace"));
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertEquals("42", bos.toString());
}
}
// This is a duplicate of test testJavaAPI1 that serializes the parser
// before executing the test.
@Test
public void testJavaAPI1_A() throws Exception {
DebuggerRunnerForJAPITest debugger = new DebuggerRunnerForJAPITest();
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
// Serialize the parser to memory, then deserialize for parsing.
ByteArrayOutputStream os = new ByteArrayOutputStream();
WritableByteChannel output = Channels.newChannel(os);
dp.save(output);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ReadableByteChannel input = Channels.newChannel(is);
org.apache.daffodil.japi.Compiler compiler = Daffodil.compiler();
DataProcessor parser = compiler.reload(input);
parser = parser.withDebuggerRunner(debugger);
parser = parser.withDebugging(true);
File data = getResource("/test/japi/myData.dat");
// This test uses a byte array here, just so as to be sure to exercise
// the constructor for creating an InputSourceDataInputStream from a byte array
// and byte buffer.
byte[] ba = FileUtils.readFileToByteArray(data);
ByteBuffer bb = ByteBuffer.wrap(ba);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(bb)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = parser.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
assertTrue(debugger.lines.size() > 0);
assertTrue(debugger.lines.contains("----------------------------------------------------------------- 1\n"));
assertTrue(debugger.getCommand().equals("trace"));
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertEquals("42", bos.toString());
}
}
// This is a duplicate of test testJavaAPI1 that serializes the parser
// before executing the test.
@Test
public void testJavaAPI1_A_FullFails() throws Exception {
DebuggerRunnerForJAPITest debugger = new DebuggerRunnerForJAPITest();
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = dp.withDebuggerRunner(debugger);
dp = dp.withDebugging(true);
// Serialize the parser to memory, then deserialize for parsing.
ByteArrayOutputStream os = new ByteArrayOutputStream();
WritableByteChannel output = Channels.newChannel(os);
dp.save(output);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ReadableByteChannel input = Channels.newChannel(is);
org.apache.daffodil.japi.Compiler compiler = Daffodil.compiler();
DataProcessor parser = compiler.reload(input);
try {
parser = parser.withValidationMode(ValidationMode.Full);
fail();
} catch (InvalidUsageException e) {
assertEquals("'Full' validation not allowed when using a restored parser.", e.getMessage());
}
}
@Test
public void testJavaAPI2() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myDataBroken.dat");
// This test uses a byte array here, just so as to be sure to exercise
// the constructor for creating an InputSourceDataInputStream from a byte array
// and byte buffer.
byte[] ba = FileUtils.readFileToByteArray(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(ba)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
// TODO: NEED a java friendly way to get the status of the outputter.
// Scala enums don't work well
// assertTrue(outputter.getStatus() != Status.DONE); // This is a hack
// so that Java doesn't have to know about Nope/Maybe, need to figure
// out better api that is Java compatible
assertTrue(res.isError());
java.util.List<Diagnostic> diags = res.getDiagnostics();
assertEquals(1, diags.size());
Diagnostic d = diags.get(0);
// System.err.println(d.getMessage());
assertTrue(d.getMessage().contains("int"));
assertTrue(d.getMessage().contains("Not an int"));
assertTrue(d.getDataLocations().toString().contains("10"));
java.util.List<LocationInSchemaFile> locs = d.getLocationsInSchemaFiles();
assertEquals(1, locs.size());
LocationInSchemaFile loc = locs.get(0);
assertTrue(loc.toString().contains("mySchema1.dfdl.xsd"));
// above is mySchema1 as it reports the element ref location,
// not element decl.
}
}
/**
* Verify that we can detect when the parse did not consume all the data.
*
* @throws IOException
*/
@Test
public void testJavaAPI3() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema3.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
pf = pf.withDistinguishedRootNode("e3", null);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myData16.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
assertEquals(2, res.location().bytePos1b());
assertEquals(9, res.location().bitPos1b());
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertEquals("9", bos.toString());
}
}
// This is a duplicate of test testJavaAPI3 that serializes the parser
// before executing the test.
@Test
public void testJavaAPI3_A() throws Exception {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema3.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
pf = pf.withDistinguishedRootNode("e3", null);
DataProcessor dp = pf.onPath("/");
// Serialize the parser to memory, then deserialize for parsing.
ByteArrayOutputStream os = new ByteArrayOutputStream();
WritableByteChannel output = Channels.newChannel(os);
dp.save(output);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ReadableByteChannel input = Channels.newChannel(is);
org.apache.daffodil.japi.Compiler compiler = Daffodil.compiler();
DataProcessor parser = compiler.reload(input);
java.io.File file = getResource("/test/japi/myData16.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = parser.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
assertEquals(2, res.location().bytePos1b());
assertEquals(9, res.location().bitPos1b());
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertEquals("9", bos.toString());
}
}
@Test
public void testJavaAPI4b() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
File schemaFileName = getResource("/test/japi/mySchema3.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFileName, "e4", null);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myData2.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
assertEquals(5, res.location().bytePos1b());
assertEquals(33, res.location().bitPos1b());
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertEquals("data", bos.toString());
}
}
@Test
public void testJavaAPI5() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
File schemaFileName = getResource("/test/japi/mySchema3.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFileName, "e4", null); // e4 is a 4-byte long string
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myData3.dat"); // contains 5
// bytes
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
assertEquals(5, res.location().bytePos1b());
assertEquals(33, res.location().bitPos1b());
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertEquals("data", bos.toString());
}
}
/***
* Verify that the compiler throws a FileNotFound exception when fed a list
* of schema files that do not exist.
*
* @throws IOException
*/
@Test
public void testJavaAPI6() throws IOException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = new java.io.File("/test/japi/notHere1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
assertTrue(pf.isError());
List<Diagnostic> diags = pf.getDiagnostics();
boolean found1 = false;
for (Diagnostic d : diags) {
if (d.getMessage().contains("notHere1")) {
found1 = true;
}
}
assertTrue(found1);
}
/**
* Tests a user submitted case where the XML appears to be serializing odd
* xml entities into the output.
*
* @throws IOException
*/
@Test
public void testJavaAPI7() throws IOException, ClassNotFoundException {
// TODO: This is due to the fact that we are doing several conversions
// back and forth between Scala.xml.Node and JDOM. And the conversions
// both use XMLOutputter to format the result (which escapes the
// entities).
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/TopLevel.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile, "TopLevel", null);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/01very_simple.txt");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertTrue(bos.toString().contains("Return-Path: <bob@smith.com>"));
}
}
/**
* This test is nearly identical to testJavaAPI7. The only difference is
* that this test uses double newline as a terminator for the first element
* in the sequence rather than double newline as a separator for the
* sequence
*
* @throws IOException
*/
@Test
public void testJavaAPI8() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/TopLevel.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile, "TopLevel2", null);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/01very_simple.txt");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(outputter.getResult());
UnparseResult res2 = dp.unparse(inputter, wbc);
err = res2.isError();
assertFalse(err);
assertTrue(bos.toString().contains("Return-Path: <bob@smith.com>"));
}
}
/**
* Verify that calling result() on the ParseResult multiple times does not
* error.
*/
@Test
public void testJavaAPI9() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/TopLevel.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile, "TopLevel2", null);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/01very_simple.txt");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
org.jdom2.Document doc1 = outputter.getResult();
java.io.ByteArrayOutputStream bos1 = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc1 = java.nio.channels.Channels.newChannel(bos1);
JDOMInfosetInputter inputter1 = new JDOMInfosetInputter(doc1);
UnparseResult res2 = dp.unparse(inputter1, wbc1);
err = res2.isError();
assertFalse(err);
assertTrue(bos1.toString().contains("Return-Path: <bob@smith.com>"));
org.jdom2.Document doc2 = outputter.getResult();
java.io.ByteArrayOutputStream bos2 = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc2 = java.nio.channels.Channels.newChannel(bos2);
JDOMInfosetInputter inputter2 = new JDOMInfosetInputter(doc2);
UnparseResult res3 = dp.unparse(inputter2, wbc2);
err = res3.isError();
assertFalse(err);
assertTrue(bos2.toString().contains("Return-Path: <bob@smith.com>"));
}
}
/**
* Verify that hidden elements do not appear in the resulting infoset
*/
@Test
public void testJavaAPI10() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema4.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myData4.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
org.jdom2.Document doc = outputter.getResult();
org.jdom2.output.XMLOutputter xo = new org.jdom2.output.XMLOutputter();
xo.setFormat(Format.getPrettyFormat());
org.jdom2.Element rootNode = doc.getRootElement();
org.jdom2.Element hidden = rootNode.getChild("hiddenElement", rootNode.getNamespace());
assertTrue(null == hidden);
}
}
/**
* Verify that nested elements do not appear as duplicates
*/
@Test
public void testJavaAPI11() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema5.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myData5.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
org.jdom2.Document doc = outputter.getResult();
org.jdom2.output.XMLOutputter xo = new org.jdom2.output.XMLOutputter();
xo.setFormat(Format.getPrettyFormat());
// xo.output(doc, System.out);
org.jdom2.Element rootNode = doc.getRootElement();
org.jdom2.Element elementGroup = rootNode.getChild("elementGroup", null); // local
// element
// names
// are
// unqualified
assertTrue(null != elementGroup);
org.jdom2.Element groupE2 = elementGroup.getChild("e2", null);
assertTrue(null != groupE2);
org.jdom2.Element groupE3 = elementGroup.getChild("e3", null);
assertTrue(null != groupE3);
org.jdom2.Element rootE2 = rootNode.getChild("e2", null);
assertTrue(null == rootE2);
org.jdom2.Element rootE3 = rootNode.getChild("e3", null);
assertTrue(null == rootE3);
}
}
@Test
public void testJavaAPI12() throws IOException, ClassNotFoundException {
DebuggerRunnerForJAPITest debugger = new DebuggerRunnerForJAPITest();
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
dp = dp.withDebuggerRunner(debugger);
dp = dp.withDebugging(true);
java.io.File file = getResource("/test/japi/myData.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
assertTrue(debugger.lines.size() > 0);
assertTrue(debugger.lines.contains("----------------------------------------------------------------- 1\n"));
}
}
@Test
public void testJavaAPI13() throws IOException, ClassNotFoundException, ExternalVariableException {
// Demonstrates here that we can set external variables
// after compilation but before parsing via Compiler.
DebuggerRunnerForJAPITest debugger = new DebuggerRunnerForJAPITest();
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File extVarsFile = getResource("/test/japi/external_vars_1.xml");
java.io.File schemaFile = getResource("/test/japi/mySchemaWithVars.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
dp = dp.withDebuggerRunner(debugger);
dp = dp.withDebugging(true);
dp = dp.withExternalVariables(extVarsFile);
java.io.File file = getResource("/test/japi/myData.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
org.jdom2.Document doc = outputter.getResult();
org.jdom2.output.XMLOutputter xo = new org.jdom2.output.XMLOutputter();
xo.setFormat(Format.getPrettyFormat());
String docString = xo.outputString(doc);
boolean containsVar1 = docString.contains("var1Value");
boolean containsVar1Value = docString.contains("externallySet");
assertTrue(containsVar1);
assertTrue(containsVar1Value);
}
}
@Test
public void testJavaAPI14() throws IOException, ClassNotFoundException, ExternalVariableException {
// Demonstrates here that we can set external variables
// after compilation but before parsing via DataProcessor.
DebuggerRunnerForJAPITest debugger = new DebuggerRunnerForJAPITest();
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File extVarFile = getResource("/test/japi/external_vars_1.xml");
java.io.File schemaFile = getResource("/test/japi/mySchemaWithVars.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
dp = dp.withDebuggerRunner(debugger);
dp = dp.withDebugging(true);
dp = dp.withExternalVariables(extVarFile);
java.io.File file = getResource("/test/japi/myData.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
boolean err = res.isError();
assertFalse(err);
org.jdom2.Document doc = outputter.getResult();
org.jdom2.output.XMLOutputter xo = new org.jdom2.output.XMLOutputter();
xo.setFormat(Format.getPrettyFormat());
String docString = xo.outputString(doc);
boolean containsVar1 = docString.contains("var1Value");
boolean containsVar1Value = docString.contains("externallySet");
assertTrue(containsVar1);
assertTrue(containsVar1Value);
assertTrue(debugger.lines.size() > 0);
assertTrue(debugger.lines.contains("----------------------------------------------------------------- 1\n"));
}
}
@Test
public void testJavaAPI15() throws IOException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myInfosetBroken.xml");
org.jdom2.input.SAXBuilder builder = new org.jdom2.input.SAXBuilder();
org.jdom2.Document doc = null;
try {
doc = builder.build(file);
} catch (Exception e) {
fail();
}
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
java.nio.channels.WritableByteChannel wbc = java.nio.channels.Channels.newChannel(bos);
JDOMInfosetInputter inputter = new JDOMInfosetInputter(doc);
UnparseResult res = dp.unparse(inputter, wbc);
boolean err = res.isError();
assertTrue(err);
java.util.List<Diagnostic> diags = res.getDiagnostics();
assertEquals(1, diags.size());
Diagnostic d = diags.get(0);
assertTrue(d.getMessage().contains("wrong"));
assertTrue(d.getMessage().contains("e2"));
}
@Test
public void testJavaAPI16() throws IOException, InvalidUsageException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
dp = dp.withValidationMode(ValidationMode.Limited);
java.io.File file = getResource("/test/japi/myData.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
assertTrue(res.isError());
assertFalse(res.isProcessingError());
assertTrue(res.isValidationError());
java.util.List<Diagnostic> diags = res.getDiagnostics();
assertEquals(1, diags.size());
Diagnostic d = diags.get(0);
assertTrue(d.getMessage().contains("maxInclusive"));
assertTrue(d.getMessage().contains("e2"));
assertTrue(d.getMessage().contains("20"));
}
}
@Test
public void testJavaAPI17() throws IOException, InvalidUsageException, ClassNotFoundException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = dp.withValidationMode(ValidationMode.Full);
java.io.File file = getResource("/test/japi/myData.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
assertTrue(res.isError());
assertFalse(res.isProcessingError());
assertTrue(res.isValidationError());
long actualLength = res.location().bytePos1b() - 1;
assertEquals(file.length(), actualLength);
java.util.List<Diagnostic> diags = res.getDiagnostics();
assertEquals(3, diags.size());
Diagnostic d0 = diags.get(0);
Diagnostic d1 = diags.get(1);
Diagnostic d2 = diags.get(2);
assertTrue(d0.getMessage().contains("42"));
assertTrue(d0.getMessage().contains("e2"));
assertTrue(d0.getMessage().contains("not valid"));
assertTrue(d1.getMessage().contains("42"));
assertTrue(d1.getMessage().contains("maxInclusive"));
assertTrue(d1.getMessage().contains("20"));
assertTrue(d2.getMessage().contains("maxInclusive"));
assertTrue(d2.getMessage().contains("e2"));
assertTrue(d2.getMessage().contains("20"));
}
}
@Test
public void testJavaAPI18() throws IOException, ClassNotFoundException {
// Demonstrate that we can use the API to continue a parse where we left off
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema3.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile,"e4", null);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myData2.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream input = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = null;
boolean err = false;
res = dp.parse(input, outputter);
err = res.isError();
assertFalse(err);
assertEquals(5, res.location().bytePos1b());
assertEquals("data", outputter.getResult().getRootElement().getText());
outputter.reset();
res = dp.parse(input, outputter);
err = res.isError();
assertFalse(err);
assertEquals(9, res.location().bytePos1b());
assertEquals("left", outputter.getResult().getRootElement().getText());
outputter.reset();
res = dp.parse(input, outputter);
err = res.isError();
assertFalse(err);
assertFalse(input.hasData());
assertEquals(13, res.location().bytePos1b());
assertEquals("over", outputter.getResult().getRootElement().getText());
}
}
@Test
public void testJavaAPI19() throws IOException, ClassNotFoundException {
// Demonstrate that we cannot use the API to continue a parse with an invalid InputSource
// ie. after a runtime SDE. This test needs to be run with an input file larger than 256MB
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/ambig_elt.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile, "root", null);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
java.io.File file = getResource("/test/japi/myData19.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream input = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = null;
boolean err = false;
res = dp.parse(input, outputter);
err = res.isError();
assertTrue(err);
outputter.reset();
try {
res = dp.parse(input, outputter);
} catch (Exception e) {
assertTrue(e.getMessage().contains("Usage error"));
assertTrue(e.getMessage().contains("invalid input source"));
}
}
}
@Test
public void testJavaAPI20() throws IOException, ClassNotFoundException {
// Test SAX parsing/unparsing
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
DaffodilParseXMLReader parseXMLReader = dp.newXMLReaderInstance();
java.io.File file = getResource("/test/japi/myData.dat");
java.io.FileInputStream fisDP = new java.io.FileInputStream(file);
java.io.FileInputStream fisSAX = new java.io.FileInputStream(file);
try (InputSourceDataInputStream disDP = new InputSourceDataInputStream(fisDP)) {
InputSourceDataInputStream disSAX = new InputSourceDataInputStream(fisSAX);
ByteArrayOutputStream xmlBos = new ByteArrayOutputStream();
XMLTextInfosetOutputter outputter = new XMLTextInfosetOutputter(xmlBos, true);
ParseResult res = dp.parse(disDP, outputter);
String infosetDPString = xmlBos.toString();
org.jdom2.input.sax.SAXHandler contentHandler = new org.jdom2.input.sax.SAXHandler();
SAXErrorHandlerForJAPITest errorHandler = new SAXErrorHandlerForJAPITest();
// since SAXHandler uses a blank prefix when the below isn't set to true, it introduces
// an undesired no-prefixed xmlns mapping
parseXMLReader.setFeature(SAX_NAMESPACE_PREFIXES_FEATURE, true);
parseXMLReader.setContentHandler(contentHandler);
parseXMLReader.setErrorHandler(errorHandler);
parseXMLReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBDIRECTORY(),
Paths.get(System.getProperty("java.io.tmpdir")));
parseXMLReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBPREFIX(),
"daffodil-sapi-");
parseXMLReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBSUFFIX(),
".sax.blob");
parseXMLReader.parse(disSAX);
ParseResult resSAX = (ParseResult) parseXMLReader.getProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_PARSERESULT());
boolean err = errorHandler.isError();
ArrayList<Diagnostic> diags = errorHandler.getDiagnostics();
Format pretty = Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator"));
String infosetSAXString = new org.jdom2.output.XMLOutputter(pretty).outputString(contentHandler.getDocument());
assertFalse(err);
assertTrue(diags.isEmpty());
assertEquals(infosetDPString, infosetSAXString);
// test unparse
ByteArrayOutputStream unparseBos = new java.io.ByteArrayOutputStream();
WritableByteChannel wbc = java.nio.channels.Channels.newChannel(unparseBos);
// prep for SAX unparse
DaffodilUnparseContentHandler unparseContentHandler = dp.newContentHandlerInstance(wbc);
try {
org.xml.sax.XMLReader unparseXMLReader = javax.xml.parsers.SAXParserFactory.newInstance()
.newSAXParser().getXMLReader();
setSecureDefaults(unparseXMLReader);
unparseXMLReader.setContentHandler(unparseContentHandler);
unparseXMLReader.setErrorHandler(errorHandler);
unparseXMLReader.setFeature(SAX_NAMESPACES_FEATURE, true);
unparseXMLReader.setFeature(SAX_NAMESPACE_PREFIXES_FEATURE, true);
ByteArrayInputStream is = new ByteArrayInputStream(infosetSAXString.getBytes());
// kickstart unparse
unparseXMLReader.parse(new org.xml.sax.InputSource(is));
} catch (javax.xml.parsers.ParserConfigurationException | org.xml.sax.SAXException e) {
fail("Error: " + e);
}
UnparseResult saxUr = unparseContentHandler.getUnparseResult();
wbc.close();
boolean saxErr = saxUr.isError();
assertFalse(saxErr);
assertTrue(saxUr.getDiagnostics().isEmpty());
assertEquals("42", unparseBos.toString());
}
}
@Test
public void testJavaAPI21() throws IOException, ClassNotFoundException {
// Test SAX parsing with errors
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
java.io.File schemaFile = getResource("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileFile(schemaFile);
DataProcessor dp = pf.onPath("/");
dp = reserializeDataProcessor(dp);
DaffodilParseXMLReader parseXMLReader = dp.newXMLReaderInstance();
java.io.File file = getResource("/test/japi/myDataBroken.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
org.jdom2.input.sax.SAXHandler contentHandler = new org.jdom2.input.sax.SAXHandler();
SAXErrorHandlerForJAPITest errorHandler = new SAXErrorHandlerForJAPITest();
parseXMLReader.setContentHandler(contentHandler);
parseXMLReader.setErrorHandler(errorHandler);
parseXMLReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBDIRECTORY(),
Paths.get(System.getProperty("java.io.tmpdir")));
parseXMLReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBPREFIX(), "daffodil-sapi-");
parseXMLReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBSUFFIX(), ".sax.blob");
parseXMLReader.parse(dis);
boolean err = errorHandler.isError();
ArrayList<Diagnostic> diags = errorHandler.getDiagnostics();
assertTrue(err);
assertEquals(1, diags.size());
Diagnostic d = diags.get(0);
assertTrue(d.getMessage().contains("int"));
assertTrue(d.getMessage().contains("Not an int"));
assertTrue(d.getDataLocations().toString().contains("10"));
java.util.List<LocationInSchemaFile> locs = d.getLocationsInSchemaFiles();
assertEquals(1, locs.size());
LocationInSchemaFile loc = locs.get(0);
assertTrue(loc.toString().contains("mySchema1.dfdl.xsd"));
}
}
@Test
public void testJavaAPI22_withExternalVariablesUsingAbstractMap() throws IOException, ClassNotFoundException, ExternalVariableException {