Skip to content

Commit

Permalink
Update tests to use JOSM JUnit 5 @annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Nov 6, 2023
1 parent af1b1eb commit c45cf46
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@
import java.util.Arrays;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.plugins.conflation.OsmFeature;
import org.openstreetmap.josm.plugins.jts.JTSConverter;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.testutils.annotations.Projection;

import com.vividsolutions.jump.feature.Feature;
import com.vividsolutions.jump.feature.FeatureCollection;
import com.vividsolutions.jump.feature.FeatureDataset;
import com.vividsolutions.jump.feature.FeatureSchema;
import com.vividsolutions.jump.feature.IndexedFeatureCollection;

/**
* Test class for {@link WeightedMatcher}
* @author Taylor Smock
*/
@BasicPreferences
@Projection
class WeightedMatcherTest {
@RegisterExtension
JOSMTestRules josmTestRules = new JOSMTestRules().projection();
/**
* Non-regression test for JOSM #21788
* This occurred when two {@link org.openstreetmap.josm.plugins.conflation.OsmFeature} objects had {@link Comparable}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright 2012 by Josh Doe and others.
package org.openstreetmap.josm.plugins.conflation;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -17,10 +17,9 @@

import javax.xml.stream.XMLStreamException;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openstreetmap.josm.actions.ExpertToggleAction;
import org.openstreetmap.josm.command.Command;
import org.openstreetmap.josm.data.Preferences;
Expand All @@ -37,38 +36,33 @@
import org.openstreetmap.josm.plugins.conflation.config.MatchingPanel;
import org.openstreetmap.josm.plugins.conflation.config.MergingPanel;
import org.openstreetmap.josm.spi.preferences.Config;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.I18n;

/**
* FIXME: this class is not functional yet
*/
public class ConflationPluginTest {

@Rule
public JOSMTestRules rules = new JOSMTestRules().preferences();

class ConflationPluginTest {
static String[] testList = new String[]{"test1-buildings"};

@Test
@Ignore("FIXME: restore test when @{link #compare} method will be fixed")
public void testConflationPlugin() throws FileNotFoundException, XMLStreamException, IOException, IllegalDataException {
@Disabled("FIXME: restore test when @{link #compare} method will be fixed")
void testConflationPlugin() throws XMLStreamException, IOException, IllegalDataException {
for (String testName: testList) {
assertTrue(testName, testCase(testName));
assertTrue(testCase(testName), testName);
}
}

static boolean testCase(String name) throws FileNotFoundException, XMLStreamException, IOException, IllegalDataException {
static boolean testCase(String name) throws XMLStreamException, IOException, IllegalDataException {
SimpleMatchSettings settings = openTestCase(name);
OsmDataLayer expectedResult = openTestResult(name);
SimpleMatchList matchesList = new SimpleMatchList();
matchesList.addAll(MatchesComputation.generateMatches(settings, NullProgressMonitor.INSTANCE));
Collection<OsmPrimitive> referenceOnly = settings.referenceSelection.stream()
.filter((p) -> matchesList.getMatchByReference(p) == null)
.collect(Collectors.toList());;
.collect(Collectors.toList());
Collection<OsmPrimitive> subjectOnly = settings.subjectSelection.stream()
.filter((p) -> matchesList.getMatchBySubject(p) == null)
.collect(Collectors.toList());;
.collect(Collectors.toList());
while (matchesList.size() > 0) {
SimpleMatch match = matchesList.get(matchesList.size()-1);
new ConflateMatchCommand(match, matchesList, settings).executeCommand();
Expand Down Expand Up @@ -100,33 +94,33 @@ static boolean compare(OsmDataLayer layer1, OsmDataLayer layer2) {

/**
* Open a test case and return the corresponding settings.
*
* <p>
* A test case a directory "test/data/<b>name></b>/".
*
* This directory should contains a files "reference.osm", a file "subject.osm"
* <p>
* This directory should contain a file "reference.osm", a file "subject.osm"
* or a file "reference-subject.osm" if the same layer should be used
* for both reference and subject.
*
* <p>
* The directory should also contain a file named "result.xml" for
* the expected result of the conflation.
*
* <p>
* The directory could contain a file named "preferences.xml" to specify
* specific settings.
*/
static SimpleMatchSettings openTestCase(String name)
throws FileNotFoundException, XMLStreamException, IOException, IllegalDataException {
throws XMLStreamException, IOException, IllegalDataException {
SimpleMatchSettings settings = new SimpleMatchSettings();
File path = new File(new File("test", "data"), name);
assertTrue(path.isDirectory());
OsmDataLayer reference = importOsmFile(new File(path, "reference.osm"));
OsmDataLayer subject;
if (reference == null) {
reference = importOsmFile(new File(path, "reference-subject.osm"));
assertNotNull("referece.osm or reference-subject.osm file not found in directory " + path, reference);
assertNotNull(reference, "reference.osm or reference-subject.osm file not found in directory " + path);
subject = reference;
} else {
subject = importOsmFile(new File(path, "subject.osm"));
assertNotNull("subject.osm file not found in directory " + path, subject);
assertNotNull(subject, "subject.osm file not found in directory " + path);
}
Preferences pref = openPreferencesXML(new File(path, "preferences.xml"));
settings.subjectLayer = subject;
Expand All @@ -136,7 +130,7 @@ static SimpleMatchSettings openTestCase(String name)
settings.referenceSelection = reference.data.allPrimitives().stream().filter(
(p) -> "yes".equals(p.get("reference"))).collect(Collectors.toList());
settings.subjectSelection = subject.data.allPrimitives().stream().filter(
(p) -> "yes".equals(p.get("subject"))).collect(Collectors.toList());;
(p) -> "yes".equals(p.get("subject"))).collect(Collectors.toList());
new MatchingPanel(null, pref, () -> { }).fillSettings(settings);
new MergingPanel(null, pref).fillSettings(settings);
return settings;
Expand All @@ -147,30 +141,29 @@ static OsmDataLayer openTestResult(String name) throws IllegalDataException {
return importOsmFile(new File(path, "result.osm"));
}

public static Preferences openPreferencesXML(File file) throws FileNotFoundException, XMLStreamException, IOException {
static Preferences openPreferencesXML(File file) throws XMLStreamException, IOException {
Preferences pref = new Preferences();
if (file.exists()) {
pref.fromXML(new BufferedReader(new FileReader(file)));
}
return pref;
}

public static OsmDataLayer importOsmFile(File file) throws IllegalDataException {
static OsmDataLayer importOsmFile(File file) throws IllegalDataException {
OsmImporter importer = new OsmImporter();
try {
InputStream in = new FileInputStream(file);
OsmImporterData oid = importer.loadLayer(in, file, file.getName(), NullProgressMonitor.INSTANCE);
OsmDataLayer layer = oid.getLayer();
return layer;
return oid.getLayer();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}

static boolean isInitialized;
@BeforeClass
public static void setUpBeforeClass() {
@BeforeAll
static void setUpBeforeClass() {
if (!isInitialized) {
//System.setProperty("josm.home", "test/data/preferences");
Preferences.main().enableSaveOnPut(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.plugins.jts.JTSConverter;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.testutils.annotations.Projection;

import com.vividsolutions.jump.feature.AttributeType;
import com.vividsolutions.jump.feature.FeatureSchema;
Expand All @@ -24,9 +22,8 @@
* @author Taylor Smock
*/
@BasicPreferences
@Projection
class OsmFeatureTest {
@RegisterExtension
JOSMTestRules josmTestRules = new JOSMTestRules().projection();
/**
* This checks that two osm features (points) do not match if they have the same geometry but different ids.
* This is a partial non-regression test for JOSM #21788. The root of the issue is that TreeMap uses the Comparable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.conflation;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.Collections;

import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.TagCollection;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;

/**
* Unit tests of {@link SimpleMatch}
*/
public class SimpleMatchTest {

@Rule
public JOSMTestRules rules = new JOSMTestRules().preferences();

@BasicPreferences
class SimpleMatchTest {
@Test
public void testGetMergingTagCollectionOverwrite() {
void testGetMergingTagCollectionOverwrite() {
SimpleMatchSettings settings = new SimpleMatchSettings();
settings.isReplacingGeometry = true;
settings.mergeTags = new SimpleMatchSettings.All<>();
settings.overwriteTags = Arrays.asList("addr:housenumber");
settings.overwriteTags = Collections.singletonList("addr:housenumber");
OsmPrimitive n1 = new Node();
OsmPrimitive n2 = new Node();
SimpleMatch match = new SimpleMatch(n1, n2, 0.5, 10.0);
Expand All @@ -34,7 +30,7 @@ public void testGetMergingTagCollectionOverwrite() {
n1.put("addr:street", "Street One");
n2.put("addr:street", "Street One Two");
TagCollection tagCollection = match.getMergingTagCollection(settings);
assertTrue(tagCollection.getNumTagsFor("addr:street") == 2);
assertTrue(tagCollection.getNumTagsFor("addr:housenumber") == 1);
assertEquals(2, tagCollection.getNumTagsFor("addr:street"));
assertEquals(1, tagCollection.getNumTagsFor("addr:housenumber"));
}
}

0 comments on commit c45cf46

Please sign in to comment.