From 1271de4de51368f3fdfe1390ad54577cfac8fc59 Mon Sep 17 00:00:00 2001 From: Matthew Blissett Date: Mon, 19 Mar 2018 17:22:06 +0100 Subject: [PATCH] Tests for existing basic validation done when using ArchiveFactory. Then (regarding https://github.com/gbif/dwca-io/issues/43) we can move this out of ArchiveFactory. --- .../java/org/gbif/dwca/io/ArchiveFactory.java | 3 +- .../org/gbif/dwca/io/ArchiveFactoryTest.java | 47 +++++++++++++++++++ src/test/resources/invalid/empty/.keep | 0 .../extension-core-id-missing/media.txt | 0 .../extension-core-id-missing/meta.xml | 20 ++++++++ .../extension-encoding-missing/media.txt | 0 .../extension-encoding-missing/meta.xml | 20 ++++++++ .../invalid/extension-id-missing/media.txt | 0 .../invalid/extension-id-missing/meta.xml | 19 ++++++++ .../extension-location-missing/media.txt | 0 .../extension-location-missing/meta.xml | 19 ++++++++ .../invalid/extension-missing/meta.xml | 20 ++++++++ .../meta-file-encoding-missing/meta.xml | 11 +++++ .../meta-file-encoding-missing/taxa.txt | 0 .../meta-file-location-missing/meta.xml | 10 ++++ .../meta-file-location-missing/taxa.txt | 0 src/test/resources/invalid/meta-only/meta.xml | 10 ++++ 17 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/invalid/empty/.keep create mode 100644 src/test/resources/invalid/extension-core-id-missing/media.txt create mode 100644 src/test/resources/invalid/extension-core-id-missing/meta.xml create mode 100644 src/test/resources/invalid/extension-encoding-missing/media.txt create mode 100644 src/test/resources/invalid/extension-encoding-missing/meta.xml create mode 100644 src/test/resources/invalid/extension-id-missing/media.txt create mode 100644 src/test/resources/invalid/extension-id-missing/meta.xml create mode 100644 src/test/resources/invalid/extension-location-missing/media.txt create mode 100644 src/test/resources/invalid/extension-location-missing/meta.xml create mode 100644 src/test/resources/invalid/extension-missing/meta.xml create mode 100644 src/test/resources/invalid/meta-file-encoding-missing/meta.xml create mode 100644 src/test/resources/invalid/meta-file-encoding-missing/taxa.txt create mode 100644 src/test/resources/invalid/meta-file-location-missing/meta.xml create mode 100644 src/test/resources/invalid/meta-file-location-missing/taxa.txt create mode 100644 src/test/resources/invalid/meta-only/meta.xml diff --git a/src/main/java/org/gbif/dwca/io/ArchiveFactory.java b/src/main/java/org/gbif/dwca/io/ArchiveFactory.java index f710ab05e..880ba6a91 100644 --- a/src/main/java/org/gbif/dwca/io/ArchiveFactory.java +++ b/src/main/java/org/gbif/dwca/io/ArchiveFactory.java @@ -25,9 +25,8 @@ /** * Factory used to build {@link Archive} object from a DarwinCore Archive file. - * - * @author mdoering * + * Replaced by DwcFiles, except that this also does some validation of the archive structure. */ public class ArchiveFactory { diff --git a/src/test/java/org/gbif/dwca/io/ArchiveFactoryTest.java b/src/test/java/org/gbif/dwca/io/ArchiveFactoryTest.java index 160f7b397..bed6d19d7 100644 --- a/src/test/java/org/gbif/dwca/io/ArchiveFactoryTest.java +++ b/src/test/java/org/gbif/dwca/io/ArchiveFactoryTest.java @@ -542,4 +542,51 @@ public void testOpenArchiveForGenericCore() throws IOException, UnsupportedArchi assertEquals(DwcLayout.FILE_ROOT, arch.getDwcLayout()); } + /** + * Basic validation of archives, that the declared files exist and have basic, valid structure. + */ + @Test + public void testInvalidArchives() throws IOException { + // Simple archive problems + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/empty")); + fail("Empty archive should not be opened."); + } catch (UnsupportedArchiveException e) {} + + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/meta-file-encoding-missing")); + fail("Archive with missing file encoding in meta.xml should not be opened."); + } catch (UnsupportedArchiveException e) {} + + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/meta-file-location-missing")); + fail("Archive with missing file location in meta.xml should not be opened."); + } catch (UnsupportedArchiveException e) {} + + // Extension archive problems + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/extension-missing")); + fail("Archive with missing extension file should not be opened."); + } catch (UnsupportedArchiveException e) {} + + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/extension-encoding-missing")); + fail("Archive with missing extension file encoding in meta.xml should not be opened."); + } catch (UnsupportedArchiveException e) {} + + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/extension-location-missing")); + fail("Archive with missing extension file location in meta.xml should not be opened."); + } catch (UnsupportedArchiveException e) {} + + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/extension-core-id-missing")); + fail("Archive with extension lacking coreid in meta.xml should not be opened."); + } catch (UnsupportedArchiveException e) {} + + try { + Archive arch = ArchiveFactory.openArchive(FileUtils.getClasspathFile("invalid/extension-id-missing")); + fail("Archive with extension and core missing id in meta.xml should not be opened."); + } catch (UnsupportedArchiveException e) {} + } } diff --git a/src/test/resources/invalid/empty/.keep b/src/test/resources/invalid/empty/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/invalid/extension-core-id-missing/media.txt b/src/test/resources/invalid/extension-core-id-missing/media.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/invalid/extension-core-id-missing/meta.xml b/src/test/resources/invalid/extension-core-id-missing/meta.xml new file mode 100644 index 000000000..e7560624d --- /dev/null +++ b/src/test/resources/invalid/extension-core-id-missing/meta.xml @@ -0,0 +1,20 @@ + + + + + taxa.txt + + + + + + + + + media.txt + + + + + + diff --git a/src/test/resources/invalid/extension-encoding-missing/media.txt b/src/test/resources/invalid/extension-encoding-missing/media.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/invalid/extension-encoding-missing/meta.xml b/src/test/resources/invalid/extension-encoding-missing/meta.xml new file mode 100644 index 000000000..7a24cedbc --- /dev/null +++ b/src/test/resources/invalid/extension-encoding-missing/meta.xml @@ -0,0 +1,20 @@ + + + + + taxa.txt + + + + + + + + + media.txt + + + + + + diff --git a/src/test/resources/invalid/extension-id-missing/media.txt b/src/test/resources/invalid/extension-id-missing/media.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/invalid/extension-id-missing/meta.xml b/src/test/resources/invalid/extension-id-missing/meta.xml new file mode 100644 index 000000000..9f01650c4 --- /dev/null +++ b/src/test/resources/invalid/extension-id-missing/meta.xml @@ -0,0 +1,19 @@ + + + + + taxa.txt + + + + + + + + media.txt + + + + + + diff --git a/src/test/resources/invalid/extension-location-missing/media.txt b/src/test/resources/invalid/extension-location-missing/media.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/invalid/extension-location-missing/meta.xml b/src/test/resources/invalid/extension-location-missing/meta.xml new file mode 100644 index 000000000..975ea5154 --- /dev/null +++ b/src/test/resources/invalid/extension-location-missing/meta.xml @@ -0,0 +1,19 @@ + + + + + taxa.txt + + + + + + + + + + + + + + diff --git a/src/test/resources/invalid/extension-missing/meta.xml b/src/test/resources/invalid/extension-missing/meta.xml new file mode 100644 index 000000000..e7560624d --- /dev/null +++ b/src/test/resources/invalid/extension-missing/meta.xml @@ -0,0 +1,20 @@ + + + + + taxa.txt + + + + + + + + + media.txt + + + + + + diff --git a/src/test/resources/invalid/meta-file-encoding-missing/meta.xml b/src/test/resources/invalid/meta-file-encoding-missing/meta.xml new file mode 100644 index 000000000..1e73bfa53 --- /dev/null +++ b/src/test/resources/invalid/meta-file-encoding-missing/meta.xml @@ -0,0 +1,11 @@ + + + + + taxa.txt + + + + + + diff --git a/src/test/resources/invalid/meta-file-encoding-missing/taxa.txt b/src/test/resources/invalid/meta-file-encoding-missing/taxa.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/invalid/meta-file-location-missing/meta.xml b/src/test/resources/invalid/meta-file-location-missing/meta.xml new file mode 100644 index 000000000..d3d23cc64 --- /dev/null +++ b/src/test/resources/invalid/meta-file-location-missing/meta.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/test/resources/invalid/meta-file-location-missing/taxa.txt b/src/test/resources/invalid/meta-file-location-missing/taxa.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/invalid/meta-only/meta.xml b/src/test/resources/invalid/meta-only/meta.xml new file mode 100644 index 000000000..e2c6f8c42 --- /dev/null +++ b/src/test/resources/invalid/meta-only/meta.xml @@ -0,0 +1,10 @@ + + + + + taxa.txt + + + + +