20
20
import java .io .FileInputStream ;
21
21
import java .io .FileOutputStream ;
22
22
import java .io .FileReader ;
23
+ import java .io .InputStream ;
23
24
import java .net .URL ;
25
+ import java .nio .file .Files ;
26
+ import java .nio .file .Path ;
27
+ import java .nio .file .Paths ;
24
28
import java .util .ArrayList ;
25
29
import java .util .Collections ;
26
30
import java .util .HashMap ;
42
46
import javax .xml .transform .stream .StreamSource ;
43
47
44
48
import org .apache .commons .io .FileUtils ;
49
+ import org .apache .commons .io .file .PathUtils ;
45
50
import org .apache .commons .scxml2 .PathResolver ;
46
51
import org .apache .commons .scxml2 .SCXMLExecutor ;
47
52
import org .apache .commons .scxml2 .env .Tracer ;
@@ -241,7 +246,7 @@ public static Datamodel fromValue(final String value) {
241
246
Datamodel (final String value , final String label ) {
242
247
this .value = value ;
243
248
this .label = label ;
244
- this .testDir = TESTS_SRC_DIR + value + "/" ;
249
+ this .testDir = TESTS_SRC_DIR_STR + value + "/" ;
245
250
}
246
251
247
252
public String label () {
@@ -371,9 +376,10 @@ public LinkedHashMap<String, Test> getTests() {
371
376
private static final String SCXML_IRP_BASE_URL = "http://www.w3.org/Voice/2013/scxml-irp/" ;
372
377
private static final String SCXML_IRP_MANIFEST_URI = "manifest.xml" ;
373
378
private static final String SCXML_IRP_ECMA_XSL_URI = "confEcma.xsl" ;
374
- private static final String TESTS_SRC_DIR = "src/w3c/scxml-irp/" ;
375
- private static final String TXML_TESTS_DIR = TESTS_SRC_DIR + "txml/" ;
376
- private static final String PACKAGE_PATH = "/" +W3CTests .class .getPackage ().getName ().replace ('.' ,'/' );
379
+ private static final String TESTS_SRC_DIR_STR = "src/w3c/scxml-irp/" ;
380
+ private static final Path TESTS_SRC_DIR = Paths .get (TESTS_SRC_DIR_STR );
381
+ private static final Path TXML_TESTS_DIR = TESTS_SRC_DIR .resolve ("txml/" );
382
+ private static final String PACKAGE_PATH = "/" + W3CTests .class .getPackage ().getName ().replace ('.' , '/' );
377
383
378
384
private static final String TESTS_FILENAME = PACKAGE_PATH + "/tests.xml" ;
379
385
@@ -430,29 +436,29 @@ protected void createCleanDirectory(final String path) throws Exception {
430
436
}
431
437
432
438
/**
433
- * Downloads the W3C IRP manifest.xml, the IRP ecma stylesheet to transform the tests, and the
434
- * actual test templates (.txml) as defined in the manifest.xml
439
+ * Downloads the W3C IRP manifest.xml, the IRP ecma stylesheet to transform the tests, and the actual test templates (.txml) as defined in the manifest.xml
440
+ *
435
441
* @throws Exception
436
442
*/
437
443
protected void getTests () throws Exception {
438
- final File testsSrcDir = new File (TESTS_SRC_DIR );
444
+ final File testsSrcDir = new File (TESTS_SRC_DIR_STR );
439
445
if (!testsSrcDir .mkdirs ()) {
440
446
FileUtils .cleanDirectory (testsSrcDir );
441
447
}
442
- new File ( TXML_TESTS_DIR ). mkdirs ( );
448
+ Files . createDirectories ( TESTS_SRC_DIR );
443
449
for (final Datamodel dm : Datamodel .values ()) {
444
450
new File (dm .testDir ()).mkdirs ();
445
451
}
446
452
System .out .println ("Downloading IRP manifest: " + SCXML_IRP_BASE_URL + SCXML_IRP_MANIFEST_URI );
447
- FileUtils . copyURLToFile (new URL (SCXML_IRP_BASE_URL + SCXML_IRP_MANIFEST_URI ), new File ( testsSrcDir , SCXML_IRP_MANIFEST_URI ));
453
+ PathUtils . copyFile (new URL (SCXML_IRP_BASE_URL + SCXML_IRP_MANIFEST_URI ), TESTS_SRC_DIR . resolve ( SCXML_IRP_MANIFEST_URI ));
448
454
System .out .println ("Downloading ecma stylesheet: " + SCXML_IRP_BASE_URL + SCXML_IRP_ECMA_XSL_URI );
449
- FileUtils . copyURLToFile (new URL (SCXML_IRP_BASE_URL + SCXML_IRP_ECMA_XSL_URI ), new File ( testsSrcDir , SCXML_IRP_ECMA_XSL_URI ));
455
+ PathUtils . copyFile (new URL (SCXML_IRP_BASE_URL + SCXML_IRP_ECMA_XSL_URI ), TESTS_SRC_DIR . resolve ( SCXML_IRP_ECMA_XSL_URI ));
450
456
final Assertions assertions = loadAssertions ();
451
457
for (final Assertions .Assertion entry : assertions .getAssertions ().values ()) {
452
458
for (final Assertions .TestCase test : entry .getTestCases ()) {
453
459
for (final Assertions .Resource resource : test .getResources ()) {
454
460
System .out .println ("Downloading IRP test file: " + SCXML_IRP_BASE_URL + resource .getUri ());
455
- FileUtils . copyURLToFile (new URL (SCXML_IRP_BASE_URL + resource .getUri ()), new File ( TXML_TESTS_DIR + resource .getFileName ()));
461
+ PathUtils . copyFile (new URL (SCXML_IRP_BASE_URL + resource .getUri ()), TXML_TESTS_DIR . resolve ( resource .getFileName ()));
456
462
}
457
463
}
458
464
}
@@ -466,7 +472,7 @@ protected void getTests() throws Exception {
466
472
protected Assertions loadAssertions () throws Exception {
467
473
final JAXBContext jaxbContext = JAXBContext .newInstance (Assertions .class );
468
474
final Unmarshaller jaxbUnmarshaller = jaxbContext .createUnmarshaller ();
469
- return (Assertions )jaxbUnmarshaller .unmarshal (new File ( TESTS_SRC_DIR , SCXML_IRP_MANIFEST_URI ));
475
+ return (Assertions )jaxbUnmarshaller .unmarshal (TESTS_SRC_DIR . resolve ( SCXML_IRP_MANIFEST_URI ). toFile ( ));
470
476
}
471
477
472
478
/**
@@ -488,12 +494,10 @@ protected Tests loadTests() throws Exception {
488
494
* @throws Exception
489
495
*/
490
496
protected void makeTests () throws Exception {
491
- final File testsSrcDir = new File (TESTS_SRC_DIR );
492
-
493
497
final TransformerFactory factory = TransformerFactory .newInstance ("net.sf.saxon.TransformerFactoryImpl" ,null );
494
498
factory .setFeature ("http://saxon.sf.net/feature/suppressXsltNamespaceCheck" , true );
495
499
final Map <Datamodel , Transformer > transformers = new HashMap <>();
496
- transformers .put (Datamodel .ECMA , factory .newTransformer (new StreamSource (new FileInputStream ( new File ( testsSrcDir , SCXML_IRP_ECMA_XSL_URI )))));
500
+ transformers .put (Datamodel .ECMA , factory .newTransformer (new StreamSource (Files . newInputStream ( TESTS_SRC_DIR . resolve ( SCXML_IRP_ECMA_XSL_URI )))));
497
501
transformers .put (Datamodel .MINIMAL , factory .newTransformer (new StreamSource (getClass ().getResourceAsStream (SCXML_IRP_MINIMAL_XSL_FILENAME ))));
498
502
transformers .put (Datamodel .JEXL , factory .newTransformer (new StreamSource (getClass ().getResourceAsStream (SCXML_IRP_JEXL_XSL_FILENAME ))));
499
503
transformers .put (Datamodel .GROOVY , factory .newTransformer (new StreamSource (getClass ().getResourceAsStream (SCXML_IRP_GROOVY_XSL_FILENAME ))));
@@ -509,30 +513,30 @@ protected void makeTests() throws Exception {
509
513
510
514
/**
511
515
* Download and transform a W3C IRP test resource file
512
- * @param specid the SCXML 1.0 spec id (anchor) for the current assertion,
513
- * which is used to determine if, how and where the resource should be transformed.
514
- * @param resource The test resource definition
516
+ *
517
+ * @param specid the SCXML 1.0 spec id (anchor) for the current assertion, which is used to determine if, how and where the resource should be
518
+ * transformed.
519
+ * @param resource The test resource definition
515
520
* @param transformers map of datamodel transformers to produce a datamodel specific SCXML document from the txml resource
516
521
* @throws Exception
517
522
*/
518
- protected void processResource (final String specid , final Assertions .Resource resource , final Map <Datamodel , Transformer > transformers )
519
- throws Exception {
523
+ protected void processResource (final String specid , final Assertions .Resource resource , final Map <Datamodel , Transformer > transformers ) throws Exception {
520
524
System .out .println ("processing IRP test file " + resource .getFileName ());
521
- FileUtils . copyURLToFile (new URL (SCXML_IRP_BASE_URL + resource .getUri ()), new File ( TXML_TESTS_DIR + resource .getFileName ()));
525
+ PathUtils . copyFile (new URL (SCXML_IRP_BASE_URL + resource .getUri ()), TXML_TESTS_DIR . resolve ( resource .getFileName ()));
522
526
switch (specid ) {
523
- case "#minimal-profile" :
524
- transformResource (resource , transformers .get (Datamodel .MINIMAL ), Datamodel .MINIMAL .testDir ());
525
- break ;
526
- case "#ecma-profile" :
527
- transformResource (resource , transformers .get (Datamodel .ECMA ), Datamodel .ECMA .testDir ());
528
- break ;
529
- default :
530
- for (final Datamodel dm : transformers .keySet ()) {
531
- if (dm != Datamodel .MINIMAL ) {
532
- transformResource (resource , transformers .get (dm ), dm .testDir ());
533
- }
527
+ case "#minimal-profile" :
528
+ transformResource (resource , transformers .get (Datamodel .MINIMAL ), Datamodel .MINIMAL .testDir ());
529
+ break ;
530
+ case "#ecma-profile" :
531
+ transformResource (resource , transformers .get (Datamodel .ECMA ), Datamodel .ECMA .testDir ());
532
+ break ;
533
+ default :
534
+ for (final Datamodel dm : transformers .keySet ()) {
535
+ if (dm != Datamodel .MINIMAL ) {
536
+ transformResource (resource , transformers .get (dm ), dm .testDir ());
534
537
}
535
- break ;
538
+ }
539
+ break ;
536
540
}
537
541
}
538
542
@@ -675,21 +679,21 @@ protected void runTests(final String testId, final Datamodel datamodel) throws E
675
679
}
676
680
677
681
/**
678
- * XSL transform a W3C IRP test SCXML resource to a datamodel specific location and format,
679
- * or simply copy a non SCXML resource to that location.
680
- * @param resource the test resource definition
682
+ * XSL transform a W3C IRP test SCXML resource to a datamodel specific location and format, or simply copy a non SCXML resource to that location.
683
+ *
684
+ * @param resource the test resource definition
681
685
* @param transformer the XSL transformer to use
682
- * @param targetDir the target location for the transformed SCXML document, or the non-SCXML resource
686
+ * @param targetDir the target location for the transformed SCXML document, or the non-SCXML resource
683
687
* @throws Exception
684
688
*/
685
- protected void transformResource (final Assertions .Resource resource , final Transformer transformer ,
686
- final String targetDir ) throws Exception {
689
+ protected void transformResource (final Assertions .Resource resource , final Transformer transformer , final String targetDir ) throws Exception {
687
690
if (resource .getFileName ().endsWith (".txml" )) {
688
- final StreamSource txmlSource = new StreamSource (new FileInputStream (new File (TXML_TESTS_DIR , resource .getFileName ())));
689
- transformer .transform (txmlSource , new StreamResult (new FileOutputStream (new File (targetDir , resource .getName () + ".scxml" ))));
690
- }
691
- else {
692
- FileUtils .copyFile (new File (TXML_TESTS_DIR , resource .getFileName ()), new File (targetDir , resource .getFileName ()));
691
+ try (InputStream source = Files .newInputStream (TXML_TESTS_DIR .resolve (resource .getFileName ()));
692
+ FileOutputStream result = new FileOutputStream (new File (targetDir , resource .getName () + ".scxml" ))) {
693
+ transformer .transform (new StreamSource (source ), new StreamResult (result ));
694
+ }
695
+ } else {
696
+ Files .copy (TXML_TESTS_DIR .resolve (resource .getFileName ()), Paths .get (targetDir , resource .getFileName ()));
693
697
}
694
698
}
695
699
}
0 commit comments