Skip to content

Commit

Permalink
Tests for existing basic validation done when using ArchiveFactory.
Browse files Browse the repository at this point in the history
Then (regarding #43) we can move this
out of ArchiveFactory.
  • Loading branch information
MattBlissett committed Mar 19, 2018
1 parent c6c8f68 commit 1271de4
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/gbif/dwca/io/ArchiveFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
47 changes: 47 additions & 0 deletions src/test/java/org/gbif/dwca/io/ArchiveFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
}
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions src/test/resources/invalid/extension-core-id-missing/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
<location>taxa.txt</location>
</files>
<id index="0"/>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
<field index="2" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
</core>

<extension encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.gbif.org/terms/1.0/Multimedia">
<files>
<location>media.txt</location>
</files>
<coreid index="0"/>
<field index="1" term="http://purl.org/dc/terms/identifier"/>
<field default="StillImage" term="http://purl.org/dc/terms/type"/>
</extension>
</archive>
Empty file.
20 changes: 20 additions & 0 deletions src/test/resources/invalid/extension-encoding-missing/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
<location>taxa.txt</location>
</files>
<id index="0"/>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
<field index="2" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
</core>

<extension fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.gbif.org/terms/1.0/Multimedia">
<files>
<location>media.txt</location>
</files>
<coreid index="0"/>
<field index="1" term="http://purl.org/dc/terms/identifier"/>
<field default="StillImage" term="http://purl.org/dc/terms/type"/>
</extension>
</archive>
Empty file.
19 changes: 19 additions & 0 deletions src/test/resources/invalid/extension-id-missing/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
<location>taxa.txt</location>
</files>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
<field index="2" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
</core>

<extension encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.gbif.org/terms/1.0/Multimedia">
<files>
<location>media.txt</location>
</files>
<coreid index="0"/>
<field index="1" term="http://purl.org/dc/terms/identifier"/>
<field default="StillImage" term="http://purl.org/dc/terms/type"/>
</extension>
</archive>
Empty file.
19 changes: 19 additions & 0 deletions src/test/resources/invalid/extension-location-missing/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
<location>taxa.txt</location>
</files>
<id index="0"/>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
<field index="2" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
</core>

<extension encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.gbif.org/terms/1.0/Multimedia">
<files>
</files>
<coreid index="0"/>
<field index="1" term="http://purl.org/dc/terms/identifier"/>
<field default="StillImage" term="http://purl.org/dc/terms/type"/>
</extension>
</archive>
20 changes: 20 additions & 0 deletions src/test/resources/invalid/extension-missing/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
<location>taxa.txt</location>
</files>
<id index="0"/>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
<field index="2" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
</core>

<extension encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.gbif.org/terms/1.0/Multimedia">
<files>
<location>media.txt</location>
</files>
<coreid index="0"/>
<field index="1" term="http://purl.org/dc/terms/identifier"/>
<field default="StillImage" term="http://purl.org/dc/terms/type"/>
</extension>
</archive>
11 changes: 11 additions & 0 deletions src/test/resources/invalid/meta-file-encoding-missing/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
<location>taxa.txt</location>
</files>
<id index="0"/>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
<field index="2" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
</core>
</archive>
Empty file.
10 changes: 10 additions & 0 deletions src/test/resources/invalid/meta-file-location-missing/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
</files>
<id index="0"/>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
<field index="2" term="http://rs.tdwg.org/dwc/terms/taxonRank"/>
</core>
</archive>
Empty file.
10 changes: 10 additions & 0 deletions src/test/resources/invalid/meta-only/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='utf-8'?>
<archive xmlns="http://rs.tdwg.org/dwc/text/">
<core encoding="UTF-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy='' ignoreHeaderLines="1" rowType="http://rs.tdwg.org/dwc/terms/Taxon">
<files>
<location>taxa.txt</location>
</files>
<id index="0"/>
<field index="1" term="http://rs.tdwg.org/dwc/terms/scientificName"/>
</core>
</archive>

0 comments on commit 1271de4

Please sign in to comment.