<commit_before>/*
 * Copyright (c) 2016, <NAME>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.github.ansell.dwca;

import static org.junit.Assert.*;

import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

/**
 * Tests for {@link DarwinCoreArchiveChecker}.
 * 
 * @author <NAME> <EMAIL>
 */
public class DarwinCoreArchiveCheckerTest {

	@Rule
	public ExpectedException thrown = ExpectedException.none();

	@Rule
	public TemporaryFolder tempDir = new TemporaryFolder();

	private Path testFile;

	private Path testTempDir;

	private Path testMetadataXml;

	@Before
	public void setUp() throws Exception {
		testTempDir = tempDir.newFolder("dwca-check-temp").toPath();
		testFile = tempDir.newFolder("dwca-check-input").toPath().resolve("dwca-test.zip");
		try (OutputStream out = Files.newOutputStream(testFile, StandardOpenOption.CREATE);
				ZipOutputStream zipOut = new ZipOutputStream(out, StandardCharsets.UTF_8);) {
			ZipEntry metadataXml = new ZipEntry(DarwinCoreArchiveChecker.METADATA_XML);
			zipOut.putNextEntry(metadataXml);
			IOUtils.copy(this.getClass().getResourceAsStream("/com/github/ansell/dwca/metadata.xml"), zipOut);
			zipOut.closeEntry();
			ZipEntry specimensCsv = new ZipEntry("specimens.csv");
			zipOut.putNextEntry(specimensCsv);
			IOUtils.copy(this.getClass().getResourceAsStream("/com/github/ansell/dwca/metadata.xml"), zipOut);
			zipOut.flush();
			zipOut.closeEntry();
			zipOut.flush();
			out.flush();
		}
		testMetadataXml = tempDir.newFolder("dwca-check-unittest").toPath()
				.resolve(DarwinCoreArchiveChecker.METADATA_XML);
		try (Writer out = Files.newBufferedWriter(testMetadataXml)) {
			IOUtils.copy(this.getClass().getResourceAsStream("/com/github/ansell/dwca/metadata.xml"), out);
		}
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#main(java.lang.String[])}
	 * .
	 */
	@Test
	public final void testMainHelp() throws Exception {
		DarwinCoreArchiveChecker.main("--help");
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#main(java.lang.String[])}
	 * .
	 */
	@Test
	public final void testMain() throws Exception {
		DarwinCoreArchiveChecker.main("--input", testFile.toAbsolutePath().toString());
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#checkZip(java.nio.file.Path, java.nio.file.Path)}
	 * .
	 */
	@Test
	public final void testCheckZip() throws Exception {
		Path metadataPath = DarwinCoreArchiveChecker.checkZip(testFile, testTempDir);
		assertNotNull(metadataPath);
		assertTrue(Files.exists(metadataPath));
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#parseMetadataXml(java.nio.file.Path)}
	 * .
	 */
	@Test
	public final void testParseMetadataXmlCoreOnly() throws Exception {
		DarwinCoreArchiveDocument testDocument = DarwinCoreArchiveChecker.parseMetadataXml(testMetadataXml);
		assertNotNull(testDocument);
		assertNotNull(testDocument.getCore());
		assertEquals("http://rs.tdwg.org/dwc/xsd/simpledarwincore/SimpleDarwinRecord",
				testDocument.getCore().getRowType());
		assertEquals(1, testDocument.getCore().getIgnoreHeaderLines());
		assertEquals(0, testDocument.getExtensions().size());
		assertNotNull(testDocument.getCore().getFiles());
		assertEquals(1, testDocument.getCore().getFiles().getLocations().size());
		assertEquals("./specimens.csv", testDocument.getCore().getFiles().getLocations().get(0));
		assertEquals(4, testDocument.getCore().getFields().size());
		for (DarwinCoreField field : testDocument.getCore().getFields()) {
			assertTrue(field.getTerm().trim().length() > 0);
		}
	}

}<commit_msg>Add test for metadata with extensions<commit_after>/*
 * Copyright (c) 2016, <NAME>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.github.ansell.dwca;

import static org.junit.Assert.*;

import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

/**
 * Tests for {@link DarwinCoreArchiveChecker}.
 * 
 * @author <NAME> <EMAIL>
 */
public class DarwinCoreArchiveCheckerTest {

	@Rule
	public ExpectedException thrown = ExpectedException.none();

	@Rule
	public TemporaryFolder tempDir = new TemporaryFolder();

	private Path testFile;

	private Path testTempDir;

	private Path testMetadataXml;

	private Path testMetadataXmlWithExtension;

	@Before
	public void setUp() throws Exception {
		testTempDir = tempDir.newFolder("dwca-check-temp").toPath();
		testFile = tempDir.newFolder("dwca-check-input").toPath().resolve("dwca-test.zip");
		try (OutputStream out = Files.newOutputStream(testFile, StandardOpenOption.CREATE);
				ZipOutputStream zipOut = new ZipOutputStream(out, StandardCharsets.UTF_8);) {
			ZipEntry metadataXml = new ZipEntry(DarwinCoreArchiveChecker.METADATA_XML);
			zipOut.putNextEntry(metadataXml);
			IOUtils.copy(this.getClass().getResourceAsStream("/com/github/ansell/dwca/metadata.xml"), zipOut);
			zipOut.closeEntry();
			ZipEntry specimensCsv = new ZipEntry("specimens.csv");
			zipOut.putNextEntry(specimensCsv);
			IOUtils.copy(this.getClass().getResourceAsStream("/com/github/ansell/dwca/specimens.csv"), zipOut);
			zipOut.flush();
			zipOut.closeEntry();
			zipOut.flush();
			out.flush();
		}
		testMetadataXml = tempDir.newFolder("dwca-check-unittest").toPath()
				.resolve(DarwinCoreArchiveChecker.METADATA_XML);
		try (Writer out = Files.newBufferedWriter(testMetadataXml)) {
			IOUtils.copy(this.getClass().getResourceAsStream("/com/github/ansell/dwca/metadata.xml"), out);
		}
		testMetadataXmlWithExtension = tempDir.newFolder("dwca-check-unittest-with-extensions").toPath()
				.resolve(DarwinCoreArchiveChecker.METADATA_XML);
		try (Writer out = Files.newBufferedWriter(testMetadataXmlWithExtension)) {
			IOUtils.copy(this.getClass().getResourceAsStream("/com/github/ansell/dwca/extensionMetadata.xml"), out);
		}
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#main(java.lang.String[])}
	 * .
	 */
	@Test
	public final void testMainHelp() throws Exception {
		DarwinCoreArchiveChecker.main("--help");
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#main(java.lang.String[])}
	 * .
	 */
	@Test
	public final void testMain() throws Exception {
		DarwinCoreArchiveChecker.main("--input", testFile.toAbsolutePath().toString());
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#checkZip(java.nio.file.Path, java.nio.file.Path)}
	 * .
	 */
	@Test
	public final void testCheckZip() throws Exception {
		Path metadataPath = DarwinCoreArchiveChecker.checkZip(testFile, testTempDir);
		assertNotNull(metadataPath);
		assertTrue(Files.exists(metadataPath));
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#parseMetadataXml(java.nio.file.Path)}
	 * .
	 */
	@Test
	public final void testParseMetadataXmlCoreOnly() throws Exception {
		DarwinCoreArchiveDocument testDocument = DarwinCoreArchiveChecker.parseMetadataXml(testMetadataXml);
		assertNotNull(testDocument);
		assertNotNull(testDocument.getCore());
		assertEquals("http://rs.tdwg.org/dwc/xsd/simpledarwincore/SimpleDarwinRecord",
				testDocument.getCore().getRowType());
		assertEquals(1, testDocument.getCore().getIgnoreHeaderLines());
		assertEquals(0, testDocument.getExtensions().size());
		assertNotNull(testDocument.getCore().getFiles());
		assertEquals(1, testDocument.getCore().getFiles().getLocations().size());
		assertEquals("./specimens.csv", testDocument.getCore().getFiles().getLocations().get(0));
		assertEquals(4, testDocument.getCore().getFields().size());
		for (DarwinCoreField field : testDocument.getCore().getFields()) {
			assertTrue(field.getTerm().trim().length() > 0);
		}
	}

	/**
	 * Test method for
	 * {@link com.github.ansell.dwca.DarwinCoreArchiveChecker#parseMetadataXml(java.nio.file.Path)}
	 * .
	 */
	@Test
	public final void testParseMetadataXmlWithExtensions() throws Exception {
		DarwinCoreArchiveDocument testDocument = DarwinCoreArchiveChecker
				.parseMetadataXml(testMetadataXmlWithExtension);
		assertNotNull(testDocument);
		assertNotNull(testDocument.getCore());
		assertEquals("http://rs.tdwg.org/dwc/terms/Taxon",
				testDocument.getCore().getRowType());
		assertEquals(1, testDocument.getCore().getIgnoreHeaderLines());
		assertEquals(StandardCharsets.UTF_8, testDocument.getCore().getEncoding());
		assertEquals("\n", testDocument.getCore().getLinesTerminatedBy());
		assertEquals("\t", testDocument.getCore().getFieldsTerminatedBy());
		assertEquals(2, testDocument.getExtensions().size());
		assertNotNull(testDocument.getCore().getFiles());
		assertEquals(1, testDocument.getCore().getFiles().getLocations().size());
		assertEquals("whales.txt", testDocument.getCore().getFiles().getLocations().get(0));
		assertEquals(6, testDocument.getCore().getFields().size());
		for (DarwinCoreField field : testDocument.getCore().getFields()) {
			assertTrue(field.getTerm().trim().length() > 0);
		}
		
	}
}
{'fraction_non_alphanumeric': 0.12201053911633564, 'fraction_numerical': 0.0023510336441021486, 'mean_word_length': 5.217741935483871, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '680187', 'n_tokens_mistral': 3937, 'n_tokens_neox': 3571, 'n_words': 828}