Ticket #18258: 18258.1.patch
| File 18258.1.patch, 9.8 KB (added by , 6 years ago) |
|---|
-
src/org/openstreetmap/josm/io/OsmReader.java
42 42 protected XMLStreamReader parser; 43 43 44 44 protected boolean convertUnknownToTags; 45 protected boolean saveOriginalId; 45 46 46 47 private static final Set<String> COMMON_XML_ATTRIBUTES = new TreeSet<>(); 47 48 … … 64 65 * @see #parseDataSet(InputStream, ProgressMonitor) 65 66 */ 66 67 protected OsmReader() { 67 this(false );68 this(false, false); 68 69 } 69 70 70 71 /** … … 75 76 * @since 15470 76 77 */ 77 78 protected OsmReader(boolean convertUnknownToTags) { 79 this(convertUnknownToTags, false); 78 80 // Restricts visibility 81 } 82 /** 83 * constructor (for private and subclasses use only) 84 * @param convertUnknownToTags if true, keep unknown xml attributes as tags 85 * @param saveOriginalId if true, save the original id in a "current_id" tag 86 * 87 * @see #parseDataSet(InputStream, ProgressMonitor) 88 * @since xxx 89 */ 90 protected OsmReader(boolean convertUnknownToTags, boolean saveOriginalId) { 91 // Restricts visibility 79 92 this.convertUnknownToTags = convertUnknownToTags; 80 93 } 81 94 … … 425 438 parseAction(current, parser.getAttributeValue(null, "action")); 426 439 parseChangeset(current, parser.getAttributeValue(null, "changeset")); 427 440 441 if (saveOriginalId) { 442 parseTag(current, "current_id", Long.toString(getLong("id"))); 443 } 428 444 if (convertUnknownToTags) { 429 445 for (int i = 0; i < parser.getAttributeCount(); i++) { 430 446 if (!COMMON_XML_ATTRIBUTES.contains(parser.getAttributeLocalName(i))) { … … 496 512 * @throws IllegalArgumentException if source is null 497 513 */ 498 514 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException { 499 return parseDataSet(source, progressMonitor, false );515 return parseDataSet(source, progressMonitor, false, false); 500 516 } 501 517 502 518 /** … … 513 529 */ 514 530 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags) 515 531 throws IllegalDataException { 516 return new OsmReader(convertUnknownToTags).doParseDataSet(source, progressMonitor);532 return parseDataSet(source, progressMonitor, convertUnknownToTags, false); 517 533 } 534 535 /** 536 * Parse the given input source and return the dataset. 537 * 538 * @param source the source input stream. Must not be null. 539 * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed 540 * @param convertUnknownToTags true if unknown xml attributes should be kept as tags 541 * @param saveOriginalId if true, keep the original id (as a tag, "current_id") 542 * 543 * @return the dataset with the parsed data 544 * @throws IllegalDataException if an error was found while parsing the data from the source 545 * @throws IllegalArgumentException if source is null 546 * @since xxx 547 */ 548 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags, boolean saveOriginalId) 549 throws IllegalDataException { 550 return new OsmReader(convertUnknownToTags, saveOriginalId).doParseDataSet(source, progressMonitor); 551 } 518 552 } -
test/unit/org/openstreetmap/josm/io/OsmReaderTest.java
6 6 import static org.junit.Assert.assertNull; 7 7 import static org.junit.Assert.assertTrue; 8 8 import static org.junit.Assert.fail; 9 import static org.junit.jupiter.api.Assertions.assertNotEquals; 9 10 10 11 import java.io.ByteArrayInputStream; 11 12 import java.io.InputStream; … … 85 86 assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes).allPrimitives() 86 87 .isEmpty()); 87 88 } 89 testUnknown(osm, parseUnknownAttributes, true); 90 testUnknown(osm, parseUnknownAttributes, true); 88 91 } 89 92 93 private static void testUnknown(String osm, boolean parseUnknownAttributes, boolean keepOriginalId) 94 throws Exception { 95 try (InputStream in = new ByteArrayInputStream( 96 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) { 97 assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes, keepOriginalId) 98 .allPrimitives().isEmpty()); 99 } 100 } 101 90 102 /** 91 103 * Unit test of {@link OsmReader#parseUnknown} - root case. 92 104 * @throws Exception if any error occurs … … 152 164 } 153 165 154 166 /** 167 * Test valid data. 168 * 169 * @param osm OSM data without XML prefix 170 * @param parseUnknownAttributes if true, attempt to parse unknown xml 171 * attributes 172 * @param keepOriginalId if true, keep the original id of the object (if 173 * a negative id) 174 * @return parsed data set 175 * @throws Exception if any error occurs 176 */ 177 private static DataSet testValidData(String osm, boolean parseUnknownAttributes, boolean keepOriginalId) 178 throws Exception { 179 try (InputStream in = new ByteArrayInputStream( 180 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) { 181 return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes, keepOriginalId); 182 } 183 } 184 /** 155 185 * Test invalid data. 156 186 * @param osm OSM data without XML prefix 157 187 * @param expectedError expected error message … … 326 356 testValidData(gdprChangeset); 327 357 testValidData(gdprChangeset, true); 328 358 testValidData(gdprChangeset, false); 359 testValidData(gdprChangeset, false, true); 360 testValidData(gdprChangeset, true, false); 361 testValidData(gdprChangeset, false, false); 362 testValidData(gdprChangeset, true, true); 329 363 } 330 364 331 365 /** … … 405 439 "<meta osm_base=\"2018-08-30T12:46:02Z\" areas=\"2018-08-30T12:40:02Z\"/>\r\n" + 406 440 "<remark>runtime error: Query ran out of memory in \"query\" at line 5.</remark>\r\n" + 407 441 "</osm>"; 408 for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, true), testValidData(query, false))) { 442 for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, true), testValidData(query, false), 443 testValidData(query, true, false), testValidData(query, false, false), testValidData(query, true, true), 444 testValidData(query, false, true))) { 409 445 assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark()); 410 446 } 411 447 } … … 420 456 + "<node id='1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>"; 421 457 DataSet ds = testValidData(testData); 422 458 assertEquals(0, ds.getNodes().iterator().next().getKeys().size()); 459 assertEquals(1, ds.getNodes().iterator().next().getUniqueId()); 423 460 424 ds = testValidData(testData, true); 425 Node firstNode = ds.getNodes().iterator().next(); 426 assertEquals(1, firstNode.getKeys().size()); 427 assertEquals("randomvalue", firstNode.get("randomkey")); 461 for (DataSet ds1 : Arrays.asList(testValidData(testData, true), testValidData(testData, true, false), 462 testValidData(testData, true, true))) { 463 ds = ds1; 464 Node firstNode = ds.getNodes().iterator().next(); 465 assertEquals(1, firstNode.getKeys().size()); 466 assertEquals("randomvalue", firstNode.get("randomkey")); 467 assertEquals(1, ds.getNodes().iterator().next().getUniqueId()); 468 } 428 469 429 ds = testValidData(testData, false); 430 assertEquals(0, ds.getNodes().iterator().next().getKeys().size()); 470 for (DataSet ds1 : Arrays.asList(testValidData(testData, false), testValidData(testData, false, false), 471 testValidData(testData, false, true))) { 472 ds = ds1; 473 assertEquals(0, ds.getNodes().iterator().next().getKeys().size()); 474 assertEquals(1, ds.getNodes().iterator().next().getUniqueId()); 475 } 431 476 } 477 478 /** 479 * Test reading a file with negative ids in osm primitives 480 * 481 * @throws Exception if any error occurs 482 */ 483 @Test 484 public void testNegativeIds() throws Exception { 485 String testData = "<osm version=\"0.6\" generator=\"fake generator\">" 486 + "<node id='-1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>"; 487 DataSet ds = testValidData(testData); 488 assertNotEquals(-1L, ds.getNodes().iterator().next().getUniqueId()); 489 490 for (DataSet ds1 : Arrays.asList(testValidData(testData, true), testValidData(testData, true, false), 491 testValidData(testData, true, false))) { 492 ds = ds1; 493 assertNotEquals(-1L, ds.getNodes().iterator().next().getUniqueId()); 494 } 495 496 for (DataSet ds1 : Arrays.asList(testValidData(testData, true, true), testValidData(testData, false, true))) { 497 ds = ds1; 498 assertEquals(-1L, ds.getNodes().iterator().next().getUniqueId()); // if we set the id to the original id 499 assertEquals("-1", ds.getNodes().iterator().next().get("current_id")); // if we just add a tag 500 } 501 } 432 502 }
