Changeset 14044 in josm for trunk/test
- Timestamp:
- 2018-07-22T22:29:41+02:00 (6 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/io/DiffResultProcessorTest.java
r14043 r14044 2 2 package org.openstreetmap.josm.io; 3 3 4 import static groovy.test.GroovyAssert.shouldFail 5 import static org.junit.Assert.* 6 7 import org.junit.Test 8 import org.openstreetmap.josm.data.coor.LatLon 9 import org.openstreetmap.josm.data.osm.Changeset 10 import org.openstreetmap.josm.data.osm.Node 11 import org.openstreetmap.josm.data.osm.OsmPrimitiveType 12 import org.openstreetmap.josm.data.osm.Relation 13 import org.openstreetmap.josm.data.osm.SimplePrimitiveId 14 import org.openstreetmap.josm.data.osm.Way 15 import org.openstreetmap.josm.gui.progress.NullProgressMonitor 16 import org.openstreetmap.josm.tools.XmlParsingException 17 18 class DiffResultProcessorTest { 19 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertNotNull; 6 import static org.junit.Assert.fail; 7 8 import java.util.Arrays; 9 import java.util.Collections; 10 import java.util.Set; 11 12 import org.junit.Rule; 13 import org.junit.Test; 14 import org.openstreetmap.josm.data.coor.LatLon; 15 import org.openstreetmap.josm.data.osm.Changeset; 16 import org.openstreetmap.josm.data.osm.Node; 17 import org.openstreetmap.josm.data.osm.OsmPrimitive; 18 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 19 import org.openstreetmap.josm.data.osm.Relation; 20 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 21 import org.openstreetmap.josm.data.osm.Way; 22 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 23 import org.openstreetmap.josm.io.DiffResultProcessor.DiffResultEntry; 24 import org.openstreetmap.josm.testutils.JOSMTestRules; 25 import org.openstreetmap.josm.tools.Logging; 26 import org.openstreetmap.josm.tools.XmlParsingException; 27 28 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 29 30 /** 31 * Unit tests of {@link DiffResultProcessor} 32 */ 33 public class DiffResultProcessorTest { 34 35 /** 36 * Setup rule 37 */ 38 @Rule 39 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 40 public JOSMTestRules test = new JOSMTestRules(); 41 42 private static void shouldFail(String s) { 43 try { 44 new DiffResultProcessor(null).parse(s, NullProgressMonitor.INSTANCE); 45 fail("should throw exception"); 46 } catch (IllegalArgumentException | XmlParsingException e) { 47 Logging.trace(e); 48 } 49 } 50 51 /** 52 * Unit test of {@link DiffResultProcessor#DiffResultProcessor}. 53 */ 20 54 @Test 21 55 public void testConstructor() { 22 Node n = new Node(1) 56 Node n = new Node(1); 23 57 // these calls should not fail 24 58 // 25 new DiffResultProcessor(null) 26 new DiffResultProcessor([]) 27 new DiffResultProcessor([n]) 28 } 29 59 new DiffResultProcessor(null); 60 new DiffResultProcessor(Collections.emptyList()); 61 new DiffResultProcessor(Collections.singleton(n)); 62 } 63 64 /** 65 * Test invalid cases. 66 */ 30 67 @Test 31 68 public void testParse_NOK_Cases() { 32 def DiffResultProcessor processor = new DiffResultProcessor([]) 33 34 shouldFail(IllegalArgumentException) { 35 processor.parse null, NullProgressMonitor.INSTANCE 36 } 37 38 shouldFail(XmlParsingException) { 39 processor.parse "", NullProgressMonitor.INSTANCE 40 } 41 42 shouldFail(XmlParsingException) { 43 processor.parse "<x></x>", NullProgressMonitor.INSTANCE 44 } 45 } 46 47 @Test 48 public void testParse_OK_Cases() { 49 def DiffResultProcessor processor = new DiffResultProcessor([]) 50 String doc = """\ 51 <diffResult version="0.6" generator="Test Data"> 52 <node old_id="-1" new_id="1" new_version="1"/> 53 <way old_id="-2" new_id="2" new_version="1"/> 54 <relation old_id="-3" new_id="3" new_version="1"/> 55 </diffResult> 56 """ 57 58 processor.parse doc, null 59 assert processor.@diffResults.size() == 3 60 SimplePrimitiveId id = new SimplePrimitiveId(-1, OsmPrimitiveType.NODE) 61 def entry = processor.@diffResults[id] 62 assert entry != null 63 assert entry.newId == 1 64 assert entry.newVersion == 1 65 66 id = new SimplePrimitiveId(-2, OsmPrimitiveType.WAY) 67 entry = processor.@diffResults[id] 68 assert entry != null 69 assert entry.newId == 2 70 assert entry.newVersion == 1 71 72 id = new SimplePrimitiveId(-3, OsmPrimitiveType.RELATION) 73 entry = processor.@diffResults[id] 74 assert entry != null 75 assert entry.newId == 3 76 assert entry.newVersion == 1 77 } 78 79 @Test 80 public void testPostProcess_Invocation_Variants() { 81 def DiffResultProcessor processor = new DiffResultProcessor([]) 82 String doc = """\ 83 <diffResult version="0.6" generator="Test Data"> 84 <node old_id="-1" new_id="1" new_version="1"/> 85 <way old_id="-2" new_id="2" new_version="1"/> 86 <relation old_id="-3" new_id="3" new_version="1"/> 87 </diffResult> 88 """ 89 90 processor.parse doc, null 69 shouldFail(null); 70 shouldFail(""); 71 shouldFail("<x></x>"); 72 } 73 74 /** 75 * Test valid cases. 76 * @throws XmlParsingException never 77 */ 78 @Test 79 public void testParse_OK_Cases() throws XmlParsingException { 80 DiffResultProcessor processor = new DiffResultProcessor(null); 81 String doc = 82 "<diffResult version=\"0.6\" generator=\"Test Data\">\n" + 83 " <node old_id=\"-1\" new_id=\"1\" new_version=\"1\"/>\n" + 84 " <way old_id=\"-2\" new_id=\"2\" new_version=\"1\"/>\n" + 85 " <relation old_id=\"-3\" new_id=\"3\" new_version=\"1\"/>\n" + 86 "</diffResult>"; 87 88 processor.parse(doc, null); 89 assertEquals(3, processor.getDiffResults().size()); 90 SimplePrimitiveId id = new SimplePrimitiveId(-1, OsmPrimitiveType.NODE); 91 DiffResultEntry entry = processor.getDiffResults().get(id); 92 assertNotNull(entry); 93 assertEquals(1, entry.newId); 94 assertEquals(1, entry.newVersion); 95 96 id = new SimplePrimitiveId(-2, OsmPrimitiveType.WAY); 97 entry = processor.getDiffResults().get(id); 98 assertNotNull(entry); 99 assertEquals(2, entry.newId); 100 assertEquals(1, entry.newVersion); 101 102 id = new SimplePrimitiveId(-3, OsmPrimitiveType.RELATION); 103 entry = processor.getDiffResults().get(id); 104 assertNotNull(entry); 105 assertEquals(3, entry.newId); 106 assertEquals(1, entry.newVersion); 107 } 108 109 /** 110 * Test {@link DiffResultProcessor#postProcess} 111 * @throws XmlParsingException never 112 */ 113 @Test 114 public void testPostProcess_Invocation_Variants() throws XmlParsingException { 115 DiffResultProcessor processor = new DiffResultProcessor(null); 116 String doc = 117 "<diffResult version=\"0.6\" generator=\"Test Data\">\n" + 118 " <node old_id=\"-1\" new_id=\"1\" new_version=\"1\"/>\n" + 119 " <way old_id=\"-2\" new_id=\"2\" new_version=\"1\"/>\n" + 120 " <relation old_id=\"-3\" new_id=\"3\" new_version=\"1\"/>\n" + 121 "</diffResult>"; 122 123 processor.parse(doc, null); 91 124 92 125 // should all be ok 93 126 // 94 processor.postProcess null, null 95 processor.postProcess null, NullProgressMonitor.INSTANCE 96 processor.postProcess new Changeset(1), null 97 processor.postProcess new Changeset(1), NullProgressMonitor.INSTANCE 98 } 99 100 @Test 101 public void testPostProcess_OK() { 102 103 Node n = new Node() 104 Way w = new Way() 105 Relation r = new Relation() 106 107 String doc = """\ 108 <diffResult version="0.6" generator="Test Data"> 109 <node old_id="${n.uniqueId}" new_id="1" new_version="10"/> 110 <way old_id="${w.uniqueId}" new_id="2" new_version="11"/> 111 <relation old_id="${r.uniqueId}" new_id="3" new_version="12"/> 112 </diffResult> 113 """ 114 115 def DiffResultProcessor processor = new DiffResultProcessor([n,w,r]) 116 processor.parse doc, null 117 def processed = processor.postProcess(new Changeset(5), null) 118 assert processed.size() == 3 119 n = processed.find {it.uniqueId == 1} 120 assert n.changesetId == 5 121 assert n.version == 10 122 123 w = processed.find {it.uniqueId == 2} 124 assert w.changesetId == 5 125 assert w.version == 11 126 127 r = processed.find {it.uniqueId == 3} 128 assert r.changesetId == 5 129 assert r.version == 12 130 } 131 132 @Test 133 public void testPostProcess_ForCreatedElement() { 134 135 Node n = new Node() 136 String doc = """\ 137 <diffResult version="0.6" generator="Test Data"> 138 <node old_id="${n.uniqueId}" new_id="1" new_version="1"/> 139 </diffResult> 140 """ 141 142 def DiffResultProcessor processor = new DiffResultProcessor([n]) 143 processor.parse doc, null 144 def processed = processor.postProcess(new Changeset(5), null) 145 assert processed.size() == 1 146 n = processed.find {it.uniqueId == 1} 147 assert n.changesetId == 5 148 assert n.version == 1 149 } 150 151 @Test 152 public void testPostProcess_ForModifiedElement() { 153 154 Node n = new Node(1) 155 n.coor = new LatLon(1,1) 156 n.setOsmId 1, 2 157 n.modified = true 158 String doc = """\ 159 <diffResult version="0.6" generator="Test Data"> 160 <node old_id="${n.uniqueId}" new_id="${n.uniqueId}" new_version="3"/> 161 </diffResult> 162 """ 163 164 def DiffResultProcessor processor = new DiffResultProcessor([n]) 165 processor.parse doc, null 166 def processed = processor.postProcess(new Changeset(5), null) 167 assert processed.size() == 1 168 n = processed.find {it.uniqueId == 1} 169 assert n.changesetId == 5 170 assert n.version == 3 171 } 172 173 @Test 174 public void testPostProcess_ForDeletedElement() { 175 176 Node n = new Node(1) 177 n.coor = new LatLon(1,1) 178 n.setOsmId 1, 2 179 n.deleted = true 180 String doc = """\ 181 <diffResult version="0.6" generator="Test Data"> 182 <node old_id="${n.uniqueId}"/> 183 </diffResult> 184 """ 185 186 def DiffResultProcessor processor = new DiffResultProcessor([n]) 187 processor.parse doc, null 188 def processed = processor.postProcess(new Changeset(5), null) 189 assert processed.size() == 1 190 n = processed.find {it.uniqueId == 1} 191 assert n.changesetId == 5 192 assert n.version == 2 127 processor.postProcess(null, null); 128 processor.postProcess(null, NullProgressMonitor.INSTANCE); 129 processor.postProcess(new Changeset(1), null); 130 processor.postProcess(new Changeset(1), NullProgressMonitor.INSTANCE); 131 } 132 133 /** 134 * Test {@link DiffResultProcessor#postProcess} 135 * @throws XmlParsingException never 136 */ 137 @Test 138 public void testPostProcess_OK() throws XmlParsingException { 139 140 Node n = new Node(); 141 Way w = new Way(); 142 Relation r = new Relation(); 143 144 String doc = 145 "<diffResult version=\"0.6\" generator=\"Test Data\">\n" + 146 " <node old_id=\""+n.getUniqueId()+"\" new_id=\"1\" new_version=\"10\"/>\n" + 147 " <way old_id=\""+w.getUniqueId()+"\" new_id=\"2\" new_version=\"11\"/>\n" + 148 " <relation old_id=\""+r.getUniqueId()+"\" new_id=\"3\" new_version=\"12\"/>\n" + 149 "</diffResult>"; 150 151 DiffResultProcessor processor = new DiffResultProcessor(Arrays.asList(n, w, r)); 152 processor.parse(doc, null); 153 Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null); 154 assertEquals(3, processed.size()); 155 n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get(); 156 assertEquals(5, n.getChangesetId()); 157 assertEquals(10, n.getVersion()); 158 159 w = (Way) processed.stream().filter(x -> x.getUniqueId() == 2).findFirst().get(); 160 assertEquals(5, w.getChangesetId()); 161 assertEquals(11, w.getVersion()); 162 163 r = (Relation) processed.stream().filter(x -> x.getUniqueId() == 3).findFirst().get(); 164 assertEquals(5, r.getChangesetId()); 165 assertEquals(12, r.getVersion()); 166 } 167 168 /** 169 * Test {@link DiffResultProcessor#postProcess} 170 * @throws XmlParsingException never 171 */ 172 @Test 173 public void testPostProcess_ForCreatedElement() throws XmlParsingException { 174 175 Node n = new Node(); 176 String doc = 177 "<diffResult version=\"0.6\" generator=\"Test Data\">\n" + 178 " <node old_id=\""+n.getUniqueId()+"\" new_id=\"1\" new_version=\"1\"/>\n" + 179 "</diffResult>"; 180 181 DiffResultProcessor processor = new DiffResultProcessor(Collections.singleton(n)); 182 processor.parse(doc, null); 183 Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null); 184 assertEquals(1, processed.size()); 185 n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get(); 186 assertEquals(5, n.getChangesetId()); 187 assertEquals(1, n.getVersion()); 188 } 189 190 /** 191 * Test {@link DiffResultProcessor#postProcess} 192 * @throws XmlParsingException never 193 */ 194 @Test 195 public void testPostProcess_ForModifiedElement() throws XmlParsingException { 196 197 Node n = new Node(1); 198 n.setCoor(new LatLon(1, 1)); 199 n.setOsmId(1, 2); 200 n.setModified(true); 201 String doc = 202 "<diffResult version=\"0.6\" generator=\"Test Data\">\n" + 203 " <node old_id=\""+n.getUniqueId()+"\" new_id=\""+n.getUniqueId()+"\" new_version=\"3\"/>\n" + 204 "</diffResult>"; 205 206 DiffResultProcessor processor = new DiffResultProcessor(Collections.singleton(n)); 207 processor.parse(doc, null); 208 Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null); 209 assertEquals(1, processed.size()); 210 n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get(); 211 assertEquals(5, n.getChangesetId()); 212 assertEquals(3, n.getVersion()); 213 } 214 215 /** 216 * Test {@link DiffResultProcessor#postProcess} 217 * @throws XmlParsingException never 218 */ 219 @Test 220 public void testPostProcess_ForDeletedElement() throws XmlParsingException { 221 222 Node n = new Node(1); 223 n.setCoor(new LatLon(1, 1)); 224 n.setOsmId(1, 2); 225 n.setDeleted(true); 226 String doc = 227 "<diffResult version=\"0.6\" generator=\"Test Data\">\n" + 228 " <node old_id=\""+n.getUniqueId()+"\"/>\n" + 229 "</diffResult>"; 230 231 DiffResultProcessor processor = new DiffResultProcessor(Collections.singleton(n)); 232 processor.parse(doc, null); 233 Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null); 234 assertEquals(1, processed.size()); 235 n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get(); 236 assertEquals(5, n.getChangesetId()); 237 assertEquals(2, n.getVersion()); 193 238 } 194 239 }
Note:
See TracChangeset
for help on using the changeset viewer.