Skip to content

Commit 8076652

Browse files
committed
Use try-with-resources
Use NIO
1 parent 3e5d580 commit 8076652

File tree

2 files changed

+69
-77
lines changed

2 files changed

+69
-77
lines changed

src/test/java/org/apache/commons/scxml2/SCXMLTestHelper.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
*/
1717
package org.apache.commons.scxml2;
1818

19-
import java.io.File;
20-
import java.io.FileInputStream;
21-
import java.io.FileOutputStream;
2219
import java.io.ObjectInputStream;
2320
import java.io.ObjectOutputStream;
2421
import java.io.Reader;
2522
import java.net.URL;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
2828
import java.util.List;
@@ -48,9 +48,9 @@ public class SCXMLTestHelper {
4848
* Assumes the default build artifacts are generated in the
4949
* "target" directory (so it can be removed via a clean build).
5050
*/
51-
public static final String SERIALIZATION_DIR = "target/serialization";
52-
public static final String SERIALIZATION_FILE_PREFIX = SERIALIZATION_DIR + "/scxml";
53-
public static final String SERIALIZATION_FILE_SUFFIX = ".ser";
51+
private static final Path SERIALIZATION_DIR = Paths.get("target/serialization");
52+
private static final String SERIALIZATION_FILE_PREFIX = "scxml";
53+
private static final String SERIALIZATION_FILE_SUFFIX = ".ser";
5454

5555
// Generate a unique sequence number for the serialization files
5656
private static int sequence=0;
@@ -222,39 +222,27 @@ public static SCXML parse(final URL url, final List<CustomAction> customActions)
222222
}
223223

224224
public static SCXMLExecutor testInstanceSerializability(final SCXMLExecutor exec) throws Exception {
225-
final File fileDir = new File(SERIALIZATION_DIR);
226-
if (!fileDir.exists()) {
227-
fileDir.mkdirs();
225+
Files.createDirectories(SERIALIZATION_DIR);
226+
final Path file = SERIALIZATION_DIR.resolve(SERIALIZATION_FILE_PREFIX + getSequenceNumber() + SERIALIZATION_FILE_SUFFIX);
227+
try (ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(file))) {
228+
out.writeObject(exec.detachInstance());
229+
}
230+
try (ObjectInputStream in = new SCInstanceObjectInputStream(Files.newInputStream(file))) {
231+
exec.attachInstance((SCInstance) in.readObject());
228232
}
229-
final String filename = SERIALIZATION_FILE_PREFIX
230-
+ getSequenceNumber() + SERIALIZATION_FILE_SUFFIX;
231-
final ObjectOutputStream out =
232-
new ObjectOutputStream(new FileOutputStream(filename));
233-
out.writeObject(exec.detachInstance());
234-
out.close();
235-
final ObjectInputStream in =
236-
new SCInstanceObjectInputStream(new FileInputStream(filename));
237-
exec.attachInstance((SCInstance) in.readObject());
238-
in.close();
239233
return exec;
240234
}
241235

242236
public static SCXML testModelSerializability(final SCXML scxml) throws Exception {
243-
final File fileDir = new File(SERIALIZATION_DIR);
244-
if (!fileDir.exists()) {
245-
fileDir.mkdirs();
246-
}
247-
final String filename = SERIALIZATION_FILE_PREFIX
248-
+ getSequenceNumber() + SERIALIZATION_FILE_SUFFIX;
237+
Files.createDirectories(SERIALIZATION_DIR);
238+
final Path file = SERIALIZATION_DIR.resolve(SERIALIZATION_FILE_PREFIX + getSequenceNumber() + SERIALIZATION_FILE_SUFFIX);
249239
SCXML roundtrip;
250-
final ObjectOutputStream out =
251-
new ObjectOutputStream(new FileOutputStream(filename));
252-
out.writeObject(scxml);
253-
out.close();
254-
final ObjectInputStream in =
255-
new ObjectInputStream(new FileInputStream(filename));
256-
roundtrip = (SCXML) in.readObject();
257-
in.close();
240+
try (ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(file))) {
241+
out.writeObject(scxml);
242+
}
243+
try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(file))) {
244+
roundtrip = (SCXML) in.readObject();
245+
}
258246
return roundtrip;
259247
}
260248

src/test/java/org/apache/commons/scxml2/w3c/W3CTests.java

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import java.io.FileInputStream;
2121
import java.io.FileOutputStream;
2222
import java.io.FileReader;
23+
import java.io.InputStream;
2324
import java.net.URL;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
2428
import java.util.ArrayList;
2529
import java.util.Collections;
2630
import java.util.HashMap;
@@ -42,6 +46,7 @@
4246
import javax.xml.transform.stream.StreamSource;
4347

4448
import org.apache.commons.io.FileUtils;
49+
import org.apache.commons.io.file.PathUtils;
4550
import org.apache.commons.scxml2.PathResolver;
4651
import org.apache.commons.scxml2.SCXMLExecutor;
4752
import org.apache.commons.scxml2.env.Tracer;
@@ -241,7 +246,7 @@ public static Datamodel fromValue(final String value) {
241246
Datamodel(final String value, final String label) {
242247
this.value = value;
243248
this.label = label;
244-
this.testDir = TESTS_SRC_DIR + value + "/";
249+
this.testDir = TESTS_SRC_DIR_STR + value + "/";
245250
}
246251

247252
public String label() {
@@ -371,9 +376,10 @@ public LinkedHashMap<String, Test> getTests() {
371376
private static final String SCXML_IRP_BASE_URL = "http://www.w3.org/Voice/2013/scxml-irp/";
372377
private static final String SCXML_IRP_MANIFEST_URI = "manifest.xml";
373378
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('.', '/');
377383

378384
private static final String TESTS_FILENAME = PACKAGE_PATH + "/tests.xml";
379385

@@ -430,29 +436,29 @@ protected void createCleanDirectory(final String path) throws Exception {
430436
}
431437

432438
/**
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+
*
435441
* @throws Exception
436442
*/
437443
protected void getTests() throws Exception {
438-
final File testsSrcDir = new File(TESTS_SRC_DIR);
444+
final File testsSrcDir = new File(TESTS_SRC_DIR_STR);
439445
if (!testsSrcDir.mkdirs()) {
440446
FileUtils.cleanDirectory(testsSrcDir);
441447
}
442-
new File(TXML_TESTS_DIR).mkdirs();
448+
Files.createDirectories(TESTS_SRC_DIR);
443449
for (final Datamodel dm : Datamodel.values()) {
444450
new File(dm.testDir()).mkdirs();
445451
}
446452
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));
448454
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));
450456
final Assertions assertions = loadAssertions();
451457
for (final Assertions.Assertion entry : assertions.getAssertions().values()) {
452458
for (final Assertions.TestCase test : entry.getTestCases()) {
453459
for (final Assertions.Resource resource : test.getResources()) {
454460
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()));
456462
}
457463
}
458464
}
@@ -466,7 +472,7 @@ protected void getTests() throws Exception {
466472
protected Assertions loadAssertions() throws Exception {
467473
final JAXBContext jaxbContext = JAXBContext.newInstance(Assertions.class);
468474
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());
470476
}
471477

472478
/**
@@ -488,12 +494,10 @@ protected Tests loadTests() throws Exception {
488494
* @throws Exception
489495
*/
490496
protected void makeTests() throws Exception {
491-
final File testsSrcDir = new File(TESTS_SRC_DIR);
492-
493497
final TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);
494498
factory.setFeature("http://saxon.sf.net/feature/suppressXsltNamespaceCheck", true);
495499
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)))));
497501
transformers.put(Datamodel.MINIMAL, factory.newTransformer(new StreamSource(getClass().getResourceAsStream(SCXML_IRP_MINIMAL_XSL_FILENAME))));
498502
transformers.put(Datamodel.JEXL, factory.newTransformer(new StreamSource(getClass().getResourceAsStream(SCXML_IRP_JEXL_XSL_FILENAME))));
499503
transformers.put(Datamodel.GROOVY, factory.newTransformer(new StreamSource(getClass().getResourceAsStream(SCXML_IRP_GROOVY_XSL_FILENAME))));
@@ -509,30 +513,30 @@ protected void makeTests() throws Exception {
509513

510514
/**
511515
* 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
515520
* @param transformers map of datamodel transformers to produce a datamodel specific SCXML document from the txml resource
516521
* @throws Exception
517522
*/
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 {
520524
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()));
522526
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());
534537
}
535-
break;
538+
}
539+
break;
536540
}
537541
}
538542

@@ -675,21 +679,21 @@ protected void runTests(final String testId, final Datamodel datamodel) throws E
675679
}
676680

677681
/**
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
681685
* @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
683687
* @throws Exception
684688
*/
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 {
687690
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()));
693697
}
694698
}
695699
}

0 commit comments

Comments
 (0)