001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.dwc; 015 016import org.gbif.dwc.terms.DwcTerm; 017import org.gbif.dwc.terms.Term; 018import org.gbif.utils.file.FileUtils; 019 020import java.io.File; 021import java.util.Arrays; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.junit.jupiter.api.Test; 026 027import static org.junit.jupiter.api.Assertions.assertEquals; 028import static org.junit.jupiter.api.Assertions.assertNotNull; 029import static org.junit.jupiter.api.Assertions.assertThrows; 030 031public class DwcaStreamWriterTest { 032 033 @Test 034 public void writeEmpty() throws Exception { 035 File dwca = FileUtils.createTempDir(); 036 DwcaStreamWriter dwcaWriter = null; 037 try { 038 dwcaWriter = new DwcaStreamWriter(dwca, DwcTerm.Taxon, DwcTerm.taxonID, true); 039 } finally { 040 assertNotNull(dwcaWriter); 041 assertThrows(IllegalStateException.class, dwcaWriter::close); 042 org.apache.commons.io.FileUtils.deleteQuietly(dwca); 043 } 044 } 045 046 @Test 047 public void write() throws Exception { 048 File dwca = FileUtils.createTempDir(); 049 Map<Term, Integer> mapping = new HashMap<>(); 050 mapping.put(DwcTerm.taxonID, 0); 051 mapping.put(DwcTerm.scientificName, 1); 052 mapping.put(DwcTerm.taxonRank, 2); 053 054 try (DwcaStreamWriter dwcaWriter = new DwcaStreamWriter(dwca, DwcTerm.Taxon, DwcTerm.taxonID, true)){ 055 String m = "<eml/>"; 056 dwcaWriter.setMetadata(m, "eml.xml"); 057 dwcaWriter.write(DwcTerm.Taxon, 0, mapping, Arrays.asList( 058 new String[] { "tax-1", "Abies Mill.", "genus" }, 059 new String[] { "tax-2", "Abies alba Mill.", "species" }, 060 new String[] { "tax-3", "Piceae abies L.", "species" }, 061 new String[] { "tax-4", "Piceae abies subsp. helvetica L.", "subspecies" }) 062 ); 063 064 Archive arch = DwcFiles.fromLocation(dwca.toPath()); 065 assertEquals("eml.xml", arch.getMetadataLocation()); 066 assertEquals("<eml/>", arch.getMetadata()); 067 068 } finally { 069 org.apache.commons.io.FileUtils.deleteQuietly(dwca); 070 } 071 } 072}