From 2e3c26f3e959fa2a72334158286ee207873443d2 Mon Sep 17 00:00:00 2001 From: urwa01 Date: Wed, 13 May 2020 09:33:30 +0200 Subject: [PATCH] Fixes issue #53: do not fail when testDir does not exist --- .../maven/xspecMavenPlugin/XSpecRunner.java | 16 +++++++-------- .../xspecMavenPlugin/XSpecRunnerTest.java | 20 ++++++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/xspec/maven/xspecMavenPlugin/XSpecRunner.java b/src/main/java/io/xspec/maven/xspecMavenPlugin/XSpecRunner.java index 2102d33..25985fc 100644 --- a/src/main/java/io/xspec/maven/xspecMavenPlugin/XSpecRunner.java +++ b/src/main/java/io/xspec/maven/xspecMavenPlugin/XSpecRunner.java @@ -45,6 +45,7 @@ import java.net.URISyntaxException; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Properties; import javax.xml.parsers.ParserConfigurationException; @@ -92,6 +93,7 @@ import io.xspec.maven.xspecMavenPlugin.utils.extenders.CatalogWriterExtender; import java.io.PrintStream; import java.util.Iterator; +import java.util.stream.Collectors; import javax.xml.transform.ErrorListener; import net.sf.saxon.lib.TraceListener; import net.sf.saxon.s9api.XdmDestination; @@ -827,17 +829,15 @@ private static Configuration getSaxonConfiguration() { * Package private to allow unit tests */ List findAllXSpecs() throws XSpecPluginException { + if (!options.testDir.exists()) { + return Collections.emptyList(); + } FileFinder finder = new FileFinder(options.testDir, "**/*.xspec", options.excludes, getLog()); final Path testPath = options.testDir.toPath(); try { - List found = finder.search(); - List ret = new ArrayList<>(found.size()); - found.stream().forEach((p) -> { - File resolved = testPath.resolve(p).toFile(); - ret.add(resolved); - } - ); - return ret; + return finder.search().stream() + .map(p -> testPath.resolve(p).toFile()) + .collect(Collectors.toList()); } catch(IOException ex) { throw new XSpecPluginException(ex); } diff --git a/src/test/java/io/xspec/maven/xspecMavenPlugin/XSpecRunnerTest.java b/src/test/java/io/xspec/maven/xspecMavenPlugin/XSpecRunnerTest.java index c3be59b..dcce9a1 100644 --- a/src/test/java/io/xspec/maven/xspecMavenPlugin/XSpecRunnerTest.java +++ b/src/test/java/io/xspec/maven/xspecMavenPlugin/XSpecRunnerTest.java @@ -131,7 +131,25 @@ public void findAllXSpecsTests() throws Exception { assertEquals("wrong number of XSpecFiles found", 1, xspecFiles.size()); assertEquals("wrong file found", "xsl1.xspec", xspecFiles.get(0).getName()); } - + + @Test + public void findAllXSpecsTestsNonExistingDir() throws Exception { + RunnerOptions runnerOptions = new RunnerOptions(getProjectDirectory()); + runnerOptions.testDir = new File(getTestDirectory(), "unexisting"); + XSpecRunner runner = getNewRunner(new SaxonOptions(), runnerOptions); + List xspecFiles = runner.findAllXSpecs(); + assertEquals("wrong number of XSpecFiles found", 0, xspecFiles.size()); + } + + @Test + public void findAllXSpecsTestsDirWithoutXspecFiles() throws Exception { + RunnerOptions runnerOptions = new RunnerOptions(getProjectDirectory()); + runnerOptions.testDir = new File(getTestDirectory(), "samples"); + XSpecRunner runner = getNewRunner(new SaxonOptions(), runnerOptions); + List xspecFiles = runner.findAllXSpecs(); + assertEquals("wrong number of XSpecFiles found", 0, xspecFiles.size()); + } + private XSpecRunner getNewRunner(SaxonOptions saxonOptions, RunnerOptions runnerOptions) throws IllegalStateException, XSpecPluginException, MalformedURLException, URISyntaxException { XSpecRunner runner = new XSpecRunner(getLog(), getBaseDirectory()); runner.setResources(