Changeset 16642 in josm


Ignore:
Timestamp:
2020-06-14T19:35:06+02:00 (4 weeks ago)
Author:
simon04
Message:

see #18258 - OsmReaderTest: test all options combinations

File:
1 edited

Legend:

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

    r16641 r16642  
    4040    public JOSMTestRules test = new JOSMTestRules();
    4141
     42    private static Options[][] options() {
     43        return new Options[][]{
     44                new Options[]{},
     45                new Options[]{Options.CONVERT_UNKNOWN_TO_TAGS},
     46                new Options[]{Options.SAVE_ORIGINAL_ID},
     47                new Options[]{Options.CONVERT_UNKNOWN_TO_TAGS, Options.SAVE_ORIGINAL_ID},
     48        };
     49    }
     50
    4251    private static final class PostProcessorStub implements OsmServerReadPostprocessor {
    4352        boolean called;
     
    7281    }
    7382
    74     private static void testUnknown(String osm) throws Exception {
     83    private static void testUnknown(String osm, Options[] options) throws Exception {
    7584        try (InputStream in = new ByteArrayInputStream(
    7685                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    77             assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE).allPrimitives().isEmpty());
    78         }
    79         testUnknown(osm, true);
    80         testUnknown(osm, false);
    81     }
    82 
    83     private static void testUnknown(String osm, boolean parseUnknownAttributes) throws Exception {
    84         try (InputStream in = new ByteArrayInputStream(
    85                 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    86             assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS).allPrimitives()
    87                     .isEmpty());
    88         }
    89         testUnknown(osm, parseUnknownAttributes, true);
    90         testUnknown(osm, parseUnknownAttributes, true);
    91     }
    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, Options.CONVERT_UNKNOWN_TO_TAGS, Options.SAVE_ORIGINAL_ID)
    98                     .allPrimitives().isEmpty());
     86            assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, options).allPrimitives().isEmpty());
    9987        }
    10088    }
     
    10694    @Test
    10795    public void testUnknownRoot() throws Exception {
    108         testUnknown("<nonosm/>");
     96        for (Options[] options : options()) {
     97            testUnknown("<nonosm/>", options);
     98        }
    10999    }
    110100
     
    115105    @Test
    116106    public void testUnknownMeta() throws Exception {
    117         testUnknown("<osm version='0.6'><meta osm_base='2017-03-29T19:04:03Z'/></osm>");
     107        for (Options[] options : options()) {
     108            testUnknown("<osm version='0.6'><meta osm_base='2017-03-29T19:04:03Z'/></osm>", options);
     109        }
    118110    }
    119111
     
    124116    @Test
    125117    public void testUnknownNote() throws Exception {
    126         testUnknown("<osm version='0.6'><note>The data included in this document is from www.openstreetmap.org.</note></osm>");
     118        for (Options[] options : options()) {
     119            testUnknown("<osm version='0.6'><note>The data included in this document is from www.openstreetmap.org.</note></osm>", options);
     120        }
    127121    }
    128122
     
    133127    @Test
    134128    public void testUnknownTag() throws Exception {
    135         testUnknown("<osm version='0.6'><foo>bar</foo></osm>");
    136         testUnknown("<osm version='0.6'><foo><bar/></foo></osm>");
     129        for (Options[] options : options()) {
     130            testUnknown("<osm version='0.6'><foo>bar</foo></osm>", options);
     131            testUnknown("<osm version='0.6'><foo><bar/></foo></osm>", options);
     132        }
    137133    }
    138134
     
    143139     * @throws Exception if any error occurs
    144140     */
    145     private static DataSet testValidData(String osm) throws Exception {
    146         try (InputStream in = new ByteArrayInputStream(
    147                 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    148             return OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
    149         }
    150     }
    151 
    152     /**
    153      * Test valid data.
    154      * @param osm OSM data without XML prefix
    155      * @param options The options to use to parse the data
    156      * @return parsed data set
    157      * @throws Exception if any error occurs
    158      */
    159     private static DataSet testValidData(String osm, Options... options) throws Exception {
     141    private static DataSet testValidData(String osm, Options[] options) throws Exception {
    160142        try (InputStream in = new ByteArrayInputStream(
    161143                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     
    178160            assertEquals(expectedError, e.getMessage());
    179161        }
    180         testInvalidData(osm, expectedError, true);
    181         testInvalidData(osm, expectedError, false);
    182     }
    183 
    184     /**
    185      * Test invalid data.
    186      *
    187      * @param osm                    OSM data without XML prefix
    188      * @param expectedError          expected error message
    189      * @param parseUnknownAttributes if true, attempt to parse unknown xml
    190      *                               attributes
    191      * @throws Exception if any error occurs
    192      */
    193     private static void testInvalidData(String osm, String expectedError, boolean parseUnknownAttributes)
    194             throws Exception {
    195         try (InputStream in = new ByteArrayInputStream(
    196                 ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
    197             OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE, Options.CONVERT_UNKNOWN_TO_TAGS);
    198             fail("should throw exception");
    199         } catch (IllegalDataException e) {
    200             assertEquals(expectedError, e.getMessage());
    201         }
    202162    }
    203163
     
    336296    public void testGdprChangeset() throws Exception {
    337297        String gdprChangeset = "<osm version='0.6'><node id='1' version='1' changeset='0'/></osm>";
    338         testValidData(gdprChangeset);
    339         testValidData(gdprChangeset, Options.CONVERT_UNKNOWN_TO_TAGS);
    340         testValidData(gdprChangeset, (Options) null);
    341         testValidData(gdprChangeset, Options.SAVE_ORIGINAL_ID);
    342         testValidData(gdprChangeset, Options.values());
     298        for (Options[] options : options()) {
     299            testValidData(gdprChangeset, options);
     300        }
    343301    }
    344302
     
    420378                "<remark>runtime error: Query ran out of memory in \"query\" at line 5.</remark>\r\n" +
    421379                "</osm>";
    422         for (DataSet ds : Arrays.asList(testValidData(query), testValidData(query, Options.CONVERT_UNKNOWN_TO_TAGS), testValidData(query, (Options) null),
    423                 testValidData(query, Options.SAVE_ORIGINAL_ID), testValidData(query, Options.values()))) {
     380        for (Options[] options : options()) {
     381            DataSet ds = testValidData(query, options);
    424382            assertEquals("runtime error: Query ran out of memory in \"query\" at line 5.", ds.getRemark());
    425383        }
     
    434392        String testData = "<osm version=\"0.6\" generator=\"fake generator\">"
    435393                + "<node id='1' version='1' visible='true' changeset='82' randomkey='randomvalue'></node>" + "</osm>";
    436         DataSet ds = testValidData(testData);
    437         assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
    438         assertEquals(1, ds.getNodes().iterator().next().getUniqueId());
    439 
    440         ds = testValidData(testData, Options.CONVERT_UNKNOWN_TO_TAGS);
    441         Node firstNode = ds.getNodes().iterator().next();
    442         assertEquals(1, firstNode.getKeys().size());
    443         assertEquals("randomvalue", firstNode.get("randomkey"));
    444         assertEquals(1, ds.getNodes().iterator().next().getUniqueId());
    445 
    446 
    447         ds = testValidData(testData, (Options) null);
    448         assertEquals(0, ds.getNodes().iterator().next().getKeys().size());
    449         assertEquals(1, ds.getNodes().iterator().next().getUniqueId());
     394        for (Options[] options : options()) {
     395            DataSet ds = testValidData(testData, options);
     396            Node firstNode = ds.getNodes().iterator().next();
     397            if (Arrays.asList(options).contains(Options.CONVERT_UNKNOWN_TO_TAGS)) {
     398                assertEquals("randomvalue", firstNode.get("randomkey"));
     399            } else {
     400                assertNull(firstNode.get("randomkey"));
     401            }
     402            if (Arrays.asList(options).contains(Options.SAVE_ORIGINAL_ID)) {
     403                assertEquals("1", firstNode.get("current_id"));
     404            } else {
     405                assertNull(firstNode.get("current_id"));
     406            }
     407        }
    450408    }
    451409}
Note: See TracChangeset for help on using the changeset viewer.