Changeset 15470 in josm for trunk/test/unit


Ignore:
Timestamp:
2019-10-22T23:54:14+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #18249 - Allow unknown xml attributes to be added as tags (patch by taylor.smock)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/io/OsmReaderTest.java

    r15034 r15470  
    1313import java.nio.file.Files;
    1414import java.nio.file.Paths;
     15import java.util.Arrays;
    1516
    1617import org.junit.Rule;
     
    1819import org.openstreetmap.josm.TestUtils;
    1920import org.openstreetmap.josm.data.osm.DataSet;
     21import org.openstreetmap.josm.data.osm.Node;
    2022import org.openstreetmap.josm.data.osm.Way;
    2123import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     
    7476            assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE).allPrimitives().isEmpty());
    7577        }
     78        testUnknown(osm, true);
     79        testUnknown(osm, false);
     80    }
     81
     82    private static void testUnknown(String osm, boolean parseUnknownAttributes) throws Exception {
     83        try (InputStream in = new ByteArrayInputStream(
     84                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     85            assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes).allPrimitives()
     86                    .isEmpty());
     87        }
    7688    }
    7789
     
    127139
    128140    /**
     141     * Test valid data.
     142     * @param osm OSM data without XML prefix
     143     * @param parseUnknownAttributes if true, attempt to parse unknown xml attributes
     144     * @return parsed data set
     145     * @throws Exception if any error occurs
     146     */
     147    private static DataSet testValidData(String osm, boolean parseUnknownAttributes) throws Exception {
     148        try (InputStream in = new ByteArrayInputStream(
     149                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     150            return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);
     151        }
     152    }
     153
     154    /**
    129155     * Test invalid data.
    130156     * @param osm OSM data without XML prefix
     
    136162                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    137163            OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
     164            fail("should throw exception");
     165        } catch (IllegalDataException e) {
     166            assertEquals(expectedError, e.getMessage());
     167        }
     168        testInvalidData(osm, expectedError, true);
     169        testInvalidData(osm, expectedError, false);
     170    }
     171
     172    /**
     173     * Test invalid data.
     174     *
     175     * @param osm                    OSM data without XML prefix
     176     * @param expectedError          expected error message
     177     * @param parseUnknownAttributes if true, attempt to parse unknown xml
     178     *                               attributes
     179     * @throws Exception if any error occurs
     180     */
     181    private static void testInvalidData(String osm, String expectedError, boolean parseUnknownAttributes)
     182            throws Exception {
     183        try (InputStream in = new ByteArrayInputStream(
     184                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     185            OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes);
    138186            fail("should throw exception");
    139187        } catch (IllegalDataException e) {
     
    275323    @Test
    276324    public void testGdprChangeset() throws Exception {
    277         testValidData("<osm version='0.6'><node id='1' version='1' changeset='0'/></osm>");
     325        String gdprChangeset = "<osm version='0.6'><node id='1' version='1' changeset='0'/></osm>";
     326        testValidData(gdprChangeset);
     327        testValidData(gdprChangeset, true);
     328        testValidData(gdprChangeset, false);
    278329    }
    279330
     
    350401    @Test
    351402    public void testRemark() throws Exception {
    352         DataSet ds = testValidData(
    353                 "<osm version=\"0.6\" generator=\"Overpass API 0.7.55.4 3079d8ea\">\r\n" +
     403        String query = "<osm version=\"0.6\" generator=\"Overpass API 0.7.55.4 3079d8ea\">\r\n" +
    354404                "<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>\r\n" +
    355405                "<meta osm_base=\"2018-08-30T12:46:02Z\" areas=\"2018-08-30T12:40:02Z\"/>\r\n" +
    356406                "<remark>runtime error: Query ran out of memory in \"query\" at line 5.</remark>\r\n" +
    357                 "</osm>");
    358         assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
     407                "</osm>";
     408        for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, true), testValidData(query, false))) {
     409            assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
     410        }
     411    }
     412
     413    /**
     414     * Test reading a file with unknown attributes in osm primitives
     415     * @throws Exception if any error occurs
     416     */
     417    @Test
     418    public void testUnknownAttributeTags() throws Exception {
     419        String testData = "<osm version=\"0.6\" generator=\"fake generator\">"
     420                + "<node id='1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>";
     421        DataSet ds = testValidData(testData);
     422        assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
     423
     424        ds = testValidData(testData, true);
     425        Node firstNode = ds.getNodes().iterator().next();
     426        assertEquals(1, firstNode.getKeys().size());
     427        assertEquals("randomvalue", firstNode.get("randomkey"));
     428
     429        ds = testValidData(testData, false);
     430        assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
    359431    }
    360432}
Note: See TracChangeset for help on using the changeset viewer.