| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package relcontext.relationfix;
|
|---|
| 3 |
|
|---|
| 4 | import static org.junit.jupiter.api.Assertions.assertAll;
|
|---|
| 5 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|---|
| 6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
|---|
| 7 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|---|
| 8 | import static org.junit.jupiter.api.Assertions.assertNull;
|
|---|
| 9 | import static org.junit.jupiter.api.Assertions.assertSame;
|
|---|
| 10 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
|---|
| 11 |
|
|---|
| 12 | import java.util.stream.Stream;
|
|---|
| 13 |
|
|---|
| 14 | import org.junit.jupiter.api.Test;
|
|---|
| 15 | import org.junit.jupiter.api.TestInstance;
|
|---|
| 16 | import org.junit.jupiter.params.ParameterizedTest;
|
|---|
| 17 | import org.junit.jupiter.params.provider.MethodSource;
|
|---|
| 18 | import org.openstreetmap.josm.TestUtils;
|
|---|
| 19 | import org.openstreetmap.josm.command.Command;
|
|---|
| 20 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 26 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * A class for checking common issues with the {@link RelationFixer} subclasses
|
|---|
| 30 | */
|
|---|
| 31 | @TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|---|
| 32 | interface RelationFixerTest {
|
|---|
| 33 | /**
|
|---|
| 34 | * Get the instance to use for checking
|
|---|
| 35 | * @return The fixer instance
|
|---|
| 36 | */
|
|---|
| 37 | RelationFixer getInstance();
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * A relation that should be fixed by the fixer
|
|---|
| 41 | * @return The bad relation
|
|---|
| 42 | */
|
|---|
| 43 | Stream<Relation> getBadRelations();
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * A relation that should not be fixed by the fixer
|
|---|
| 47 | * @return The good relation
|
|---|
| 48 | */
|
|---|
| 49 | Stream<Relation> getGoodRelations();
|
|---|
| 50 |
|
|---|
| 51 | default Stream<Relation> getRelations() {
|
|---|
| 52 | return Stream.concat(getBadRelations(), getGoodRelations());
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | @ParameterizedTest
|
|---|
| 56 | @MethodSource("getRelations")
|
|---|
| 57 | default void testIsFixerApplicable(Relation relation) {
|
|---|
| 58 | final RelationFixer fixer = getInstance();
|
|---|
| 59 | assertTrue(fixer.isFixerApplicable(relation));
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | @Test
|
|---|
| 63 | default void testIsFixerApplicableEmptyRelation() {
|
|---|
| 64 | assertFalse(getInstance().isFixerApplicable(new Relation()));
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | @ParameterizedTest
|
|---|
| 68 | @MethodSource("getGoodRelations")
|
|---|
| 69 | default void testGoodRelationMatches(Relation goodRelation) {
|
|---|
| 70 | final RelationFixer fixer = getInstance();
|
|---|
| 71 | assertTrue(fixer.isFixerApplicable(goodRelation));
|
|---|
| 72 | assertFalse(fixer.isFixerApplicable(new Relation()));
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | @ParameterizedTest
|
|---|
| 76 | @MethodSource("getGoodRelations")
|
|---|
| 77 | default void testIsRelationGoodGoodRelation(Relation goodRelation) {
|
|---|
| 78 | assertTrue(getInstance().isRelationGood(goodRelation));
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | @ParameterizedTest
|
|---|
| 82 | @MethodSource("getBadRelations")
|
|---|
| 83 | default void testIsRelationGoodBadRelation(Relation badRelation) {
|
|---|
| 84 | assertFalse(getInstance().isRelationGood(badRelation));
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | @MethodSource("getBadRelations")
|
|---|
| 88 | @ParameterizedTest
|
|---|
| 89 | @Projection
|
|---|
| 90 | default void testFixBadRelation(Relation badRelation) {
|
|---|
| 91 | final DataSet ds = badRelation.getDataSet();
|
|---|
| 92 | final RelationFixer fixer = getInstance();
|
|---|
| 93 | final Command command = fixer.fixRelation(badRelation);
|
|---|
| 94 | assertNotNull(command);
|
|---|
| 95 | assertDoesNotThrow(command::executeCommand);
|
|---|
| 96 | final Relation relation = ds.getRelations().stream().filter(fixer::isFixerApplicable).findFirst().orElseThrow(AssertionError::new);
|
|---|
| 97 | assertAll(relation.getMemberPrimitives().stream()
|
|---|
| 98 | .map(member -> () -> assertSame(ds, member.getDataSet(), member + " does not have the same dataset as " + relation)));
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | @Projection
|
|---|
| 102 | @ParameterizedTest
|
|---|
| 103 | @MethodSource("getGoodRelations")
|
|---|
| 104 | default void testGoodRelationNotFixed(Relation goodRelation) {
|
|---|
| 105 | assertTrue(getInstance().isRelationGood(goodRelation));
|
|---|
| 106 | final DataSet ds = goodRelation.getDataSet();
|
|---|
| 107 | final RelationFixer fixer = getInstance();
|
|---|
| 108 | final Command command = fixer.fixRelation(goodRelation);
|
|---|
| 109 | assertNull(command);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | @Projection
|
|---|
| 113 | @ParameterizedTest
|
|---|
| 114 | @MethodSource("getGoodRelations")
|
|---|
| 115 | default void testAddBadMember(Relation goodRelation) {
|
|---|
| 116 | // regression test for #23715
|
|---|
| 117 | final DataSet ds = goodRelation.getDataSet();
|
|---|
| 118 | assertTrue(getInstance().isRelationGood(goodRelation));
|
|---|
| 119 | Node n1 = new Node(new LatLon(2.0, 2.0));
|
|---|
| 120 | Node n2 = new Node(new LatLon(2.0, 3.0));
|
|---|
| 121 | Way w1 = TestUtils.newWay("", n1, n2);
|
|---|
| 122 | ds.addPrimitiveRecursive(w1);
|
|---|
| 123 | goodRelation.addMember(new RelationMember("", w1));
|
|---|
| 124 | final RelationFixer fixer = getInstance();
|
|---|
| 125 | final Command command = fixer.fixRelation(goodRelation);
|
|---|
| 126 | assertNull(command);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | }
|
|---|