Commit 507be26c authored by Matija Obreza's avatar Matija Obreza
Browse files

Extracted DatabaseTest for TaxonomyCheckerTest

parent bf1d17f4
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.gringlobal.taxonomy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import org.genesys2.gringlobal.taxonomy.component.InMemoryTaxonomyDatabase;
import org.genesys2.gringlobal.taxonomy.component.TaxonomyException;
import org.genesys2.gringlobal.taxonomy.component.TaxonomyReader;
import org.genesys2.gringlobal.taxonomy.model.GenusRow;
import org.genesys2.gringlobal.taxonomy.model.SpeciesRow;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import com.opencsv.CSVReader;
public abstract class DatabaseTest {
protected static InMemoryTaxonomyDatabase DATABASE = null;
@BeforeClass
public static void loadDatabase() throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException, TaxonomyException {
File file = new File("../taxonomy_data", "TAXONOMY_GENUS.txt");
if (!file.exists()) {
return;
}
// read TAXONOMY_GENUS.txt
try (CSVReader reader = TaxonomyReader.openCsvReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 1)) {
DATABASE = new InMemoryTaxonomyDatabase();
GenusRow genusRow = null;
while ((genusRow = TaxonomyReader.toGenus(reader.readNext())) != null) {
DATABASE.registerGenus(genusRow.getGenusId(), genusRow.getGenusName());
}
}
file = new File("../taxonomy_data", "TAXONOMY_SPECIES.txt");
if (!file.exists()) {
return;
}
// read TAXONOMY_SPECIES.txt
try (CSVReader reader = TaxonomyReader.openCsvReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 1)) {
SpeciesRow speciesRow = null;
while ((speciesRow = TaxonomyReader.toSpecies(reader.readNext())) != null) {
DATABASE.registerSpecies(speciesRow);
}
}
}
@AfterClass
public static void unloadDatabase() {
DATABASE = null;
}
}
\ No newline at end of file
......@@ -19,60 +19,10 @@ import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import org.genesys2.gringlobal.taxonomy.component.InMemoryTaxonomyDatabase;
import org.genesys2.gringlobal.taxonomy.component.TaxonomyException;
import org.genesys2.gringlobal.taxonomy.component.TaxonomyReader;
import org.genesys2.gringlobal.taxonomy.model.GenusRow;
import org.genesys2.gringlobal.taxonomy.model.SpeciesRow;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.opencsv.CSVReader;
public class InMemoryDatabaseTest {
private static InMemoryTaxonomyDatabase DATABASE = null;
@BeforeClass
public static void loadDatabase() throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException, TaxonomyException {
File file = new File("../taxonomy_data", "TAXONOMY_GENUS.txt");
if (!file.exists()) {
return;
}
// read TAXONOMY_GENUS.txt
try (CSVReader reader = TaxonomyReader.openCsvReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 1)) {
DATABASE = new InMemoryTaxonomyDatabase();
GenusRow genusRow = null;
while ((genusRow = TaxonomyReader.toGenus(reader.readNext())) != null) {
DATABASE.registerGenus(genusRow.getGenusId(), genusRow.getGenusName());
}
}
file = new File("../taxonomy_data", "TAXONOMY_SPECIES.txt");
if (!file.exists()) {
return;
}
// read TAXONOMY_SPECIES.txt
try (CSVReader reader = TaxonomyReader.openCsvReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 1)) {
SpeciesRow speciesRow = null;
while ((speciesRow = TaxonomyReader.toSpecies(reader.readNext())) != null) {
DATABASE.registerSpecies(speciesRow);
}
}
}
@AfterClass
public static void unloadDatabase() {
DATABASE = null;
}
public class InMemoryDatabaseTest extends DatabaseTest {
@Test
public void testVigna() {
......
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.gringlobal.taxonomy;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.genesys2.gringlobal.taxonomy.component.TaxonomyChecker;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class TaxonomyCheckerTest extends DatabaseTest {
protected static TaxonomyChecker CHECKER = null;
@BeforeClass
public static void initializeChecker() {
if (DATABASE != null) {
CHECKER = new TaxonomyChecker();
CHECKER.setTaxonomyDatabase(DATABASE);
}
}
@AfterClass
public static void unloadChecker() {
CHECKER = null;
}
@Test
public void testCucumisMeloConomon() {
if (CHECKER == null) {
return;
}
// for (SpeciesRow speciesRow : DATABASE.listSpecies("Cucumis", "melo", 100)) {
// System.err.println(speciesRow);
// }
//
// List<String> suggestions = CHECKER.suggestSubtaxa("Cucumis", "melo", "var. conomon", 5);
// for (String subspecies : suggestions) {
// System.err.println(subspecies);
// }
assertThat(DATABASE.containsSubtaxa("Cucumis", "melo", "var. conomon"), equalTo(true));
assertThat("Checker must not return a suggestion for valid subtaxa", CHECKER.suggestSubtaxa("Cucumis", "melo", "var. conomon", 1), hasSize(0));
assertThat(CHECKER.suggestSubtaxa("Cucumis", "melo", "var. conoman", 1), contains("var. conomon"));
// TODO
// assertThat("Checker must prefer the quadrinomial name when provided", CHECKER.suggestSubtaxa("Cucumis", "melo", "subsp. agrestis var. conomon", 1), contains("subsp.
// agrestis var. conomon"));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment