MARMOTTA-376: added some minimum tests
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/8841aea5 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/8841aea5 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/8841aea5 Branch: refs/heads/develop Commit: 8841aea52544424c55f2e9e2a36e90c188e5ffd9 Parents: 8c4b0a6 Author: Sergio Fernández <[email protected]> Authored: Mon Nov 25 10:07:01 2013 +0100 Committer: Sergio Fernández <[email protected]> Committed: Mon Nov 25 10:07:01 2013 +0100 ---------------------------------------------------------------------- .../apache/marmotta/kiwi/loader/KiWiLoader.java | 23 ++++++++++++-- .../marmotta/kiwi/loader/KiWiLoaderTest.java | 33 +++++++++++++++++--- 2 files changed, 49 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/8841aea5/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/KiWiLoader.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/KiWiLoader.java b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/KiWiLoader.java index 7d61045..24d8de7 100644 --- a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/KiWiLoader.java +++ b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/KiWiLoader.java @@ -403,15 +403,32 @@ public class KiWiLoader { /** * Load the triples from the file using the provided format. - * @param file the file to read from + * @param path the file to read from * @param format the expected {@link RDFFormat} * @param gzip force using a {@link GZIPInputStream} (will try to guess based on the filename if false) * @throws RDFParseException if the stream is invalid (e.g. does not fit with the syntax) * @throws IOException * @see {@link #load(InputStream, RDFFormat)} */ - public void load(String file, RDFFormat format, boolean gzip) throws IOException, RDFParseException { - final File f = new File(file); + public void load(String path, RDFFormat format, boolean gzip) throws IOException, RDFParseException { + final File f = new File(path); + if (f.exists() && f.isDirectory() && format != null) { + final String extension = "." + format.getDefaultFileExtension() + (gzip ? ".gz" : ""); + File [] files = f.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith(extension); + } + }); + for (File file: files) { + load(file, format, gzip); + } + } else { + load(f, format, gzip); + } + } + + private void load (File f, RDFFormat format, boolean gzip) throws IOException, RDFParseException { InputStream is = new FileInputStream(f); if (gzip || f.getName().endsWith(".gz")) { is = new GZIPInputStream(is); http://git-wip-us.apache.org/repos/asf/marmotta/blob/8841aea5/libraries/kiwi/kiwi-loader/src/test/java/org/apache/marmotta/kiwi/loader/KiWiLoaderTest.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/test/java/org/apache/marmotta/kiwi/loader/KiWiLoaderTest.java b/libraries/kiwi/kiwi-loader/src/test/java/org/apache/marmotta/kiwi/loader/KiWiLoaderTest.java index 00eb9d1..80279e4 100644 --- a/libraries/kiwi/kiwi-loader/src/test/java/org/apache/marmotta/kiwi/loader/KiWiLoaderTest.java +++ b/libraries/kiwi/kiwi-loader/src/test/java/org/apache/marmotta/kiwi/loader/KiWiLoaderTest.java @@ -57,7 +57,7 @@ public class KiWiLoaderTest { fos.close(); // input file - dataFile = temp.newFile("test-data.xml"); + dataFile = temp.newFile("test-data.rdf"); copyResourceToFile("/org/apache/marmotta/kiwi/test/demo-data.foaf", dataFile); } @@ -75,7 +75,6 @@ public class KiWiLoaderTest { return props; } - @Test public void testMain_NoArgs() { exit.expectSystemExitWithStatus(3); @@ -172,8 +171,7 @@ public class KiWiLoaderTest { @Test public void testLoadInputStream() throws RepositoryException, RDFParseException, IOException { - KiWiTestLoader loader = new KiWiTestLoader(getKiWiConfig(), - "http://example.com/test/", null); + KiWiTestLoader loader = new KiWiTestLoader(getKiWiConfig(), "http://example.com/test/", null); loader.initialize(); loader.load(new FileInputStream(dataFile), RDFFormat.RDFXML); @@ -191,6 +189,33 @@ public class KiWiLoaderTest { } loader.shutdown(); } + + @Test + public void testEmptyDirectory() throws IOException { + File dir = temp.newFolder("empty-dir"); + final String fName = dir.getAbsolutePath(); + KiWiLoader.main(new String[] {"-c", confFile.getAbsolutePath(), fName}); + assertThat(stdOut.getLog(), containsString("Could not read file " + fName + ", skipping...")); + } + + @Test + public void testLoadFileFromDirectory() throws RepositoryException, RDFParseException, IOException { + KiWiTestLoader loader = new KiWiTestLoader(getKiWiConfig(), "http://example.com/test/", null); + loader.initialize(); + loader.load(dataFile.getParent(), RDFFormat.RDFXML, false); + final RepositoryConnection con = loader.getRepository().getConnection(); + try { + con.begin(); + testRepoContent(con); + con.commit(); + } catch (final Throwable t) { + con.rollback(); + throw t; + } finally { + con.close(); + } + loader.shutdown(); + } private static void copyResourceToFile(String resource, File file) throws IOException { final FileOutputStream os = new FileOutputStream(file);
