Ticket #18258: 18258.1.patch

File 18258.1.patch, 9.8 KB (added by taylor.smock, 6 years ago)

Add generic unit tests, with a test that will fail until a strategy is decided upon (at which point it will need to be modified)

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

     
    4242    protected XMLStreamReader parser;
    4343
    4444    protected boolean convertUnknownToTags;
     45    protected boolean saveOriginalId;
    4546
    4647    private static final Set<String> COMMON_XML_ATTRIBUTES = new TreeSet<>();
    4748
     
    6465     * @see #parseDataSet(InputStream, ProgressMonitor)
    6566     */
    6667    protected OsmReader() {
    67         this(false);
     68        this(false, false);
    6869    }
    6970
    7071    /**
     
    7576     * @since 15470
    7677     */
    7778    protected OsmReader(boolean convertUnknownToTags) {
     79        this(convertUnknownToTags, false);
    7880        // 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
    7992        this.convertUnknownToTags = convertUnknownToTags;
    8093    }
    8194
     
    425438            parseAction(current, parser.getAttributeValue(null, "action"));
    426439            parseChangeset(current, parser.getAttributeValue(null, "changeset"));
    427440
     441            if (saveOriginalId) {
     442                parseTag(current, "current_id", Long.toString(getLong("id")));
     443            }
    428444            if (convertUnknownToTags) {
    429445                for (int i = 0; i < parser.getAttributeCount(); i++) {
    430446                    if (!COMMON_XML_ATTRIBUTES.contains(parser.getAttributeLocalName(i))) {
     
    496512     * @throws IllegalArgumentException if source is null
    497513     */
    498514    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
    499         return parseDataSet(source, progressMonitor, false);
     515        return parseDataSet(source, progressMonitor, false, false);
    500516    }
    501517
    502518    /**
     
    513529     */
    514530    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor, boolean convertUnknownToTags)
    515531            throws IllegalDataException {
    516         return new OsmReader(convertUnknownToTags).doParseDataSet(source, progressMonitor);
     532        return parseDataSet(source, progressMonitor, convertUnknownToTags, false);
    517533    }
     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    }
    518552}
  • test/unit/org/openstreetmap/josm/io/OsmReaderTest.java

     
    66import static org.junit.Assert.assertNull;
    77import static org.junit.Assert.assertTrue;
    88import static org.junit.Assert.fail;
     9import static org.junit.jupiter.api.Assertions.assertNotEquals;
    910
    1011import java.io.ByteArrayInputStream;
    1112import java.io.InputStream;
     
    8586            assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, parseUnknownAttributes).allPrimitives()
    8687                    .isEmpty());
    8788        }
     89        testUnknown(osm, parseUnknownAttributes, true);
     90        testUnknown(osm, parseUnknownAttributes, true);
    8891    }
    8992
     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
    90102    /**
    91103     * Unit test of {@link OsmReader#parseUnknown} - root case.
    92104     * @throws Exception if any error occurs
     
    152164    }
    153165
    154166    /**
     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    /**
    155185     * Test invalid data.
    156186     * @param osm OSM data without XML prefix
    157187     * @param expectedError expected error message
     
    326356        testValidData(gdprChangeset);
    327357        testValidData(gdprChangeset, true);
    328358        testValidData(gdprChangeset, false);
     359        testValidData(gdprChangeset, false, true);
     360        testValidData(gdprChangeset, true, false);
     361        testValidData(gdprChangeset, false, false);
     362        testValidData(gdprChangeset, true, true);
    329363    }
    330364
    331365    /**
     
    405439                "<meta osm_base=\"2018-08-30T12:46:02Z\" areas=\"2018-08-30T12:40:02Z\"/>\r\n" +
    406440                "<remark>runtime error: Query ran out of memory in \"query\" at line 5.</remark>\r\n" +
    407441                "</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))) {
    409445            assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
    410446        }
    411447    }
     
    420456                + "<node id='1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>";
    421457        DataSet ds = testValidData(testData);
    422458        assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
     459        assertEquals(1, ds.getNodes().iterator().next().getUniqueId());
    423460
    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        }
    428469
    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        }
    431476    }
     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    }
    432502}