Ticket #22250: 22250.2.patch

File 22250.2.patch, 17.3 KB (added by taylor.smock, 3 years ago)

Add unit tests for <error> tags

  • src/org/openstreetmap/josm/io/OsmReader.java

    diff --git a/src/org/openstreetmap/josm/io/OsmReader.java b/src/org/openstreetmap/josm/io/OsmReader.java
    index 43bd83c8b6..632e70c74a 100644
    a b public class OsmReader extends AbstractReader {  
    201201        }
    202202    }
    203203
     204    private void parseError() throws XMLStreamException {
     205        while (parser.hasNext()) {
     206            int event = parser.next();
     207            if (event == XMLStreamConstants.CHARACTERS) {
     208                throwException(parser.getText());
     209            } else if (event == XMLStreamConstants.END_ELEMENT) {
     210                return;
     211            } else {
     212                throwException("Unknown error element type");
     213            }
     214        }
     215    }
     216
    204217    private void parseRemark() throws XMLStreamException {
    205218        while (parser.hasNext()) {
    206219            int event = parser.next();
    public class OsmReader extends AbstractReader {  
    393406        if (printWarning && ("note".equals(element) || "meta".equals(element))) {
    394407            // we know that Overpass API returns those elements
    395408            Logging.debug(tr("Undefined element ''{0}'' found in input stream. Skipping.", element));
     409        } else if ("error".equals(element)) {
     410            parseError();
    396411        } else if (printWarning) {
    397412            Logging.info(tr("Undefined element ''{0}'' found in input stream. Skipping.", element));
    398413        }
  • test/unit/org/openstreetmap/josm/io/OsmReaderTest.java

    diff --git a/test/unit/org/openstreetmap/josm/io/OsmReaderTest.java b/test/unit/org/openstreetmap/josm/io/OsmReaderTest.java
    index 5c52e2708d..9aa912aef6 100644
    a b package org.openstreetmap.josm.io;  
    44import static org.hamcrest.CoreMatchers.anyOf;
    55import static org.hamcrest.CoreMatchers.is;
    66import static org.hamcrest.MatcherAssert.assertThat;
     7import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
    78import static org.junit.jupiter.api.Assertions.assertEquals;
    89import static org.junit.jupiter.api.Assertions.assertFalse;
    910import static org.junit.jupiter.api.Assertions.assertNull;
     11import static org.junit.jupiter.api.Assertions.assertThrows;
    1012import static org.junit.jupiter.api.Assertions.assertTrue;
    1113import static org.junit.jupiter.api.Assertions.fail;
    1214
    import java.nio.file.Files;  
    1719import java.nio.file.Paths;
    1820import java.util.Arrays;
    1921
     22import org.junit.jupiter.params.ParameterizedTest;
     23import org.junit.jupiter.params.provider.ValueSource;
    2024import org.openstreetmap.josm.TestUtils;
    2125import org.openstreetmap.josm.data.osm.DataSet;
    2226import org.openstreetmap.josm.data.osm.Node;
    class OsmReaderTest {  
    135139    private static DataSet testValidData(String osm, Options[] options) throws Exception {
    136140        try (InputStream in = new ByteArrayInputStream(
    137141                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    138             return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, options);
     142            return assertDoesNotThrow(() -> OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, options));
    139143        }
    140144    }
    141145
    142146    /**
    143147     * Test invalid data.
    144148     * @param osm OSM data without XML prefix
    145      * @param expectedError expected error message
    146      * @throws Exception if any error occurs
     149     * @return The exception
    147150     */
    148     private static void testInvalidData(String osm, String expectedError) throws Exception {
     151    private static IllegalDataException testInvalidData(String osm) throws Exception {
    149152        try (InputStream in = new ByteArrayInputStream(
    150153                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    151             OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
    152             fail("should throw exception");
    153         } catch (IllegalDataException e) {
    154             assertEquals(expectedError, e.getMessage());
     154            return assertThrows(IllegalDataException.class, () -> OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE));
    155155        }
    156156    }
    157157
    class OsmReaderTest {  
    161161     */
    162162    @Test
    163163    void testInvalidUid() throws Exception {
    164         testInvalidData("<osm version='0.6'><node id='1' uid='nan'/></osm>",
    165                 "Illegal value for attribute 'uid'. Got 'nan'. (at line 1, column 82). 82 bytes have been read");
     164        assertEquals("Illegal value for attribute 'uid'. Got 'nan'. (at line 1, column 82). 82 bytes have been read",
     165                testInvalidData("<osm version='0.6'><node id='1' uid='nan'/></osm>").getMessage());
    166166    }
    167167
    168168    /**
    class OsmReaderTest {  
    171171     */
    172172    @Test
    173173    void testMissingId() throws Exception {
    174         testInvalidData("<osm version='0.6'><node/></osm>",
    175                 "Missing required attribute 'id'. (at line 1, column 65). 64 bytes have been read");
     174        assertEquals("Missing required attribute 'id'. (at line 1, column 65). 64 bytes have been read",
     175                testInvalidData("<osm version='0.6'><node/></osm>").getMessage());
    176176    }
    177177
    178178    /**
    class OsmReaderTest {  
    181181     */
    182182    @Test
    183183    void testMissingRef() throws Exception {
    184         testInvalidData("<osm version='0.6'><way id='1' version='1'><nd/></way></osm>",
    185                 "Missing mandatory attribute 'ref' on <nd> of way 1. (at line 1, column 87). 88 bytes have been read");
    186         testInvalidData("<osm version='0.6'><relation id='1' version='1'><member/></relation></osm>",
    187                 "Missing attribute 'ref' on member in relation 1. (at line 1, column 96). 101 bytes have been read");
     184        assertEquals("Missing mandatory attribute 'ref' on <nd> of way 1. (at line 1, column 87). 88 bytes have been read",
     185                testInvalidData("<osm version='0.6'><way id='1' version='1'><nd/></way></osm>").getMessage());
     186        assertEquals("Missing attribute 'ref' on member in relation 1. (at line 1, column 96). 101 bytes have been read",
     187                testInvalidData("<osm version='0.6'><relation id='1' version='1'><member/></relation></osm>").getMessage());
    188188    }
    189189
    190190    /**
    class OsmReaderTest {  
    193193     */
    194194    @Test
    195195    void testIllegalRef() throws Exception {
    196         testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='0'/></way></osm>",
    197                 "Illegal value of attribute 'ref' of element <nd>. Got 0. (at line 1, column 95). 96 bytes have been read");
    198         testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='nan'/></way></osm>",
    199                 "Illegal long value for attribute 'ref'. Got 'nan'. (at line 1, column 97). 98 bytes have been read");
    200 
    201         testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='0'/></relation></osm>",
    202                 "Incomplete <member> specification with ref=0 (at line 1, column 116). 121 bytes have been read");
    203         testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='nan'/></relation></osm>",
    204                 "Illegal value for attribute 'ref' on member in relation 1. Got nan (at line 1, column 118). 123 bytes have been read");
     196        assertEquals("Illegal value of attribute 'ref' of element <nd>. Got 0. (at line 1, column 95). 96 bytes have been read",
     197                testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='0'/></way></osm>").getMessage());
     198        assertEquals("Illegal long value for attribute 'ref'. Got 'nan'. (at line 1, column 97). 98 bytes have been read",
     199                testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='nan'/></way></osm>").getMessage());
     200
     201        assertEquals("Incomplete <member> specification with ref=0 (at line 1, column 116). 121 bytes have been read",
     202                testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='0'/></relation></osm>").getMessage());
     203        assertEquals("Illegal value for attribute 'ref' on member in relation 1. Got nan (at line 1, column 118). 123 bytes have been read",
     204                testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='nan'/></relation></osm>").getMessage());
    205205    }
    206206
    207207    /**
    class OsmReaderTest {  
    210210     */
    211211    @Test
    212212    void testMissingType() throws Exception {
    213         testInvalidData("<osm version='0.6'><relation id='1' version='1'><member ref='1'/></relation></osm>",
    214                 "Missing attribute 'type' on member 1 in relation 1. (at line 1, column 104). 109 bytes have been read");
     213        assertEquals("Missing attribute 'type' on member 1 in relation 1. (at line 1, column 104). 109 bytes have been read",
     214                testInvalidData("<osm version='0.6'><relation id='1' version='1'><member ref='1'/></relation></osm>").getMessage());
    215215    }
    216216
    217217    /**
    class OsmReaderTest {  
    220220     */
    221221    @Test
    222222    void testIllegalType() throws Exception {
    223         testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='foo' ref='1'/></relation></osm>",
    224                 "Illegal value for attribute 'type' on member 1 in relation 1. Got foo. (at line 1, column 115). 120 bytes have been read");
     223        assertEquals("Illegal value for attribute 'type' on member 1 in relation 1. Got foo. (at line 1, column 115). 120 bytes have been read",
     224                testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='foo' ref='1'/></relation></osm>").getMessage());
    225225    }
    226226
    227227    /**
    class OsmReaderTest {  
    230230     */
    231231    @Test
    232232    void testMissingKeyValue() throws Exception {
    233         testInvalidData("<osm version='0.6'><node id='1' version='1'><tag/></node></osm>",
    234                 "Missing key or value attribute in tag. (at line 1, column 89). 89 bytes have been read");
    235         testInvalidData("<osm version='0.6'><node id='1' version='1'><tag k='foo'/></node></osm>",
    236                 "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
    237         testInvalidData("<osm version='0.6'><node id='1' version='1'><tag v='bar'/></node></osm>",
    238                 "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
     233        assertEquals("Missing key or value attribute in tag. (at line 1, column 89). 89 bytes have been read",
     234                testInvalidData("<osm version='0.6'><node id='1' version='1'><tag/></node></osm>").getMessage());
     235        assertEquals("Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read",
     236                testInvalidData("<osm version='0.6'><node id='1' version='1'><tag k='foo'/></node></osm>").getMessage());
     237        assertEquals("Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read",
     238                testInvalidData("<osm version='0.6'><node id='1' version='1'><tag v='bar'/></node></osm>").getMessage());
    239239    }
    240240
    241241    /**
    class OsmReaderTest {  
    244244     */
    245245    @Test
    246246    void testMissingVersion() throws Exception {
    247         testInvalidData("<osm/>",
    248                 "Missing mandatory attribute 'version'. (at line 1, column 45). 44 bytes have been read");
    249         testInvalidData("<osm version='0.6'><node id='1'/></osm>",
    250                 "Missing attribute 'version' on OSM primitive with ID 1. (at line 1, column 72). 72 bytes have been read");
     247        assertEquals("Missing mandatory attribute 'version'. (at line 1, column 45). 44 bytes have been read",
     248                testInvalidData("<osm/>").getMessage());
     249        assertEquals("Missing attribute 'version' on OSM primitive with ID 1. (at line 1, column 72). 72 bytes have been read",
     250                testInvalidData("<osm version='0.6'><node id='1'/></osm>").getMessage());
    251251    }
    252252
    253253    /**
    class OsmReaderTest {  
    256256     */
    257257    @Test
    258258    void testUnsupportedVersion() throws Exception {
    259         testInvalidData("<osm version='0.1'/>",
    260                 "Unsupported version: 0.1 (at line 1, column 59). 58 bytes have been read");
     259        assertEquals("Unsupported version: 0.1 (at line 1, column 59). 58 bytes have been read",
     260                testInvalidData("<osm version='0.1'/>").getMessage());
    261261    }
    262262
    263263    /**
    class OsmReaderTest {  
    266266     */
    267267    @Test
    268268    void testIllegalVersion() throws Exception {
    269         testInvalidData("<osm version='0.6'><node id='1' version='nan'/></osm>",
    270                 "Illegal value for attribute 'version' on OSM primitive with ID 1. Got nan. (at line 1, column 86). 86 bytes have been read");
     269        assertEquals("Illegal value for attribute 'version' on OSM primitive with ID 1. Got nan. (at line 1, column 86). 86 bytes have been read",
     270                testInvalidData("<osm version='0.6'><node id='1' version='nan'/></osm>").getMessage());
    271271    }
    272272
    273273    /**
    class OsmReaderTest {  
    276276     */
    277277    @Test
    278278    void testIllegalChangeset() throws Exception {
    279         testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='nan'/></osm>",
    280                 "Illegal value for attribute 'changeset'. Got nan. (at line 1, column 100). 100 bytes have been read");
    281         testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='-1'/></osm>",
    282                 "Illegal value for attribute 'changeset'. Got -1. (at line 1, column 99). 99 bytes have been read");
     279        assertEquals("Illegal value for attribute 'changeset'. Got nan. (at line 1, column 100). 100 bytes have been read",
     280                testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='nan'/></osm>").getMessage());
     281        assertEquals("Illegal value for attribute 'changeset'. Got -1. (at line 1, column 99). 99 bytes have been read",
     282                testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='-1'/></osm>").getMessage());
    283283    }
    284284
    285285    /**
    class OsmReaderTest {  
    300300     */
    301301    @Test
    302302    void testInvalidBounds() throws Exception {
    303         testInvalidData("<osm version='0.6'><bounds/></osm>",
    304                 "Missing mandatory attributes on element 'bounds'. " +
    305                 "Got minlon='null',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 67). 72 bytes have been read");
    306         testInvalidData("<osm version='0.6'><bounds minlon='0'/></osm>",
    307                 "Missing mandatory attributes on element 'bounds'. " +
    308                 "Got minlon='0',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 78). 83 bytes have been read");
    309         testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0'/></osm>",
    310                 "Missing mandatory attributes on element 'bounds'. " +
    311                 "Got minlon='0',minlat='0',maxlon='null',maxlat='null', origin='null'. (at line 1, column 89). 94 bytes have been read");
    312         testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0' maxlon='1'/></osm>",
    313                 "Missing mandatory attributes on element 'bounds'. " +
    314                 "Got minlon='0',minlat='0',maxlon='1',maxlat='null', origin='null'. (at line 1, column 100). 105 bytes have been read");
     303        assertEquals("Missing mandatory attributes on element 'bounds'. " +
     304                "Got minlon='null',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 67). 72 bytes have been read",
     305                testInvalidData("<osm version='0.6'><bounds/></osm>").getMessage());
     306        assertEquals("Missing mandatory attributes on element 'bounds'. " +
     307                "Got minlon='0',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 78). 83 bytes have been read",
     308                testInvalidData("<osm version='0.6'><bounds minlon='0'/></osm>").getMessage());
     309        assertEquals("Missing mandatory attributes on element 'bounds'. " +
     310                "Got minlon='0',minlat='0',maxlon='null',maxlat='null', origin='null'. (at line 1, column 89). 94 bytes have been read",
     311                testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0'/></osm>").getMessage());
     312        assertEquals("Missing mandatory attributes on element 'bounds'. " +
     313                "Got minlon='0',minlat='0',maxlon='1',maxlat='null', origin='null'. (at line 1, column 100). 105 bytes have been read",
     314                testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0' maxlon='1'/></osm>").getMessage());
    315315    }
    316316
    317317    /**
    class OsmReaderTest {  
    404404            }
    405405        }
    406406    }
     407
     408    @ParameterizedTest
     409    @ValueSource(strings = {
     410            "<osm version=\"0.6\"><error>Mismatch in tags key and value size</error></osm>",
     411            "<osm version=\"0.6\"><node id=\"1\" visible=\"true\" version=\"1\"><error>Mismatch in tags key and value size</error></node></osm>",
     412            "<osm version=\"0.6\"><way id=\"1\" visible=\"true\" version=\"1\"><error>Mismatch in tags key and value size</error></way></osm>",
     413            "<osm version=\"0.6\"><way id=\"1\" visible=\"true\" version=\"1\"><error>Mismatch in tags key and value size</error></way></osm>",
     414            "<osm version=\"0.6\"><relation id=\"1\" visible=\"true\" version=\"1\"><error>Mismatch in tags key and value size</error></relation></osm>"
     415    })
     416    void testError(String testData) throws Exception {
     417        IllegalDataException illegalDataException = testInvalidData(testData);
     418        assertTrue(illegalDataException.getMessage().contains("Mismatch in tags key and value size"));
     419    }
    407420}