1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.pair.properties;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.util.Observable;
|
---|
9 | import java.util.Observer;
|
---|
10 |
|
---|
11 | import org.junit.Before;
|
---|
12 | import org.junit.BeforeClass;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.JOSMFixture;
|
---|
15 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType;
|
---|
23 |
|
---|
24 | public class PropertiesMergeModelTest {
|
---|
25 |
|
---|
26 | private abstract static class TestObserver implements Observer {
|
---|
27 | public int numInvocations;
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public void update(Observable o, Object arg) {
|
---|
31 | numInvocations++;
|
---|
32 | doTest();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public abstract void doTest();
|
---|
36 |
|
---|
37 | public void assertNumInvocations(int count) {
|
---|
38 | assertEquals(count, numInvocations);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | PropertiesMergeModel model;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Setup test.
|
---|
46 | */
|
---|
47 | @BeforeClass
|
---|
48 | public static void init() {
|
---|
49 | JOSMFixture.createUnitTestFixture().init();
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Setup test.
|
---|
54 | */
|
---|
55 | @Before
|
---|
56 | public void setUp() {
|
---|
57 | model = new PropertiesMergeModel();
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void populate(OsmPrimitive my, OsmPrimitive their) {
|
---|
61 | model.populate(new Conflict<>(my, their));
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Test
|
---|
65 | public void populate() {
|
---|
66 | DataSet d1 = new DataSet();
|
---|
67 | DataSet d2 = new DataSet();
|
---|
68 | Node n1 = new Node(1);
|
---|
69 | Node n2 = new Node(1);
|
---|
70 | d1.addPrimitive(n1);
|
---|
71 | d2.addPrimitive(n2);
|
---|
72 | populate(n1, n2);
|
---|
73 |
|
---|
74 | Way w1 = new Way(1);
|
---|
75 | Way w2 = new Way(1);
|
---|
76 | d1.addPrimitive(w1);
|
---|
77 | d2.addPrimitive(w2);
|
---|
78 | populate(w2, w2);
|
---|
79 |
|
---|
80 | Relation r1 = new Relation(1);
|
---|
81 | Relation r2 = new Relation(1);
|
---|
82 | d1.addPrimitive(r1);
|
---|
83 | d2.addPrimitive(r2);
|
---|
84 | populate(r1, r2);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Test
|
---|
88 | public void decidingAboutCoords() {
|
---|
89 | DataSet d1 = new DataSet();
|
---|
90 | DataSet d2 = new DataSet();
|
---|
91 |
|
---|
92 | Node n1 = new Node(1);
|
---|
93 | Node n2 = new Node(1);
|
---|
94 | d1.addPrimitive(n1);
|
---|
95 | d2.addPrimitive(n2);
|
---|
96 | populate(n1, n2);
|
---|
97 | assertFalse(model.hasCoordConflict());
|
---|
98 |
|
---|
99 | n1.setCoor(new LatLon(1, 1));
|
---|
100 | populate(n1, n2);
|
---|
101 | assertTrue(model.hasCoordConflict());
|
---|
102 |
|
---|
103 | n1.cloneFrom(new Node(1));
|
---|
104 | n2.setCoor(new LatLon(2, 2));
|
---|
105 | populate(n1, n2);
|
---|
106 | assertTrue(model.hasCoordConflict());
|
---|
107 |
|
---|
108 | n1.setCoor(new LatLon(1, 1));
|
---|
109 | n2.setCoor(new LatLon(2, 2));
|
---|
110 | populate(n1, n2);
|
---|
111 | assertTrue(model.hasCoordConflict());
|
---|
112 |
|
---|
113 | // decide KEEP_MINE and ensure notification via Observable
|
---|
114 | //
|
---|
115 | TestObserver observerTest;
|
---|
116 | model.addObserver(
|
---|
117 | observerTest = new TestObserver() {
|
---|
118 | @Override
|
---|
119 | public void doTest() {
|
---|
120 | assertTrue(model.isCoordMergeDecision(MergeDecisionType.KEEP_MINE));
|
---|
121 | }
|
---|
122 | }
|
---|
123 | );
|
---|
124 | model.decideCoordsConflict(MergeDecisionType.KEEP_MINE);
|
---|
125 | assertTrue(model.isCoordMergeDecision(MergeDecisionType.KEEP_MINE));
|
---|
126 | observerTest.assertNumInvocations(1);
|
---|
127 |
|
---|
128 | // decide KEEP_THEIR and ensure notification via Observable
|
---|
129 | //
|
---|
130 | model.deleteObserver(observerTest);
|
---|
131 | model.addObserver(
|
---|
132 | observerTest = new TestObserver() {
|
---|
133 | @Override
|
---|
134 | public void doTest() {
|
---|
135 | assertTrue(model.isCoordMergeDecision(MergeDecisionType.KEEP_THEIR));
|
---|
136 | }
|
---|
137 | }
|
---|
138 | );
|
---|
139 | model.decideCoordsConflict(MergeDecisionType.KEEP_THEIR);
|
---|
140 | assertTrue(model.isCoordMergeDecision(MergeDecisionType.KEEP_THEIR));
|
---|
141 | observerTest.assertNumInvocations(1);
|
---|
142 | model.deleteObserver(observerTest);
|
---|
143 | }
|
---|
144 | }
|
---|