Changeset 12138 in josm for trunk/test


Ignore:
Timestamp:
2017-05-13T16:07:14+02:00 (7 years ago)
Author:
Don-vip
Message:

improve unit tests of OsmReader

Location:
trunk/test
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/test/data/empty.osm

    r12135 r12138  
    11<?xml version='1.0' encoding='UTF-8'?>
    2 <osm version='0.3' generator='JOSM'>
     2<osm version='0.6' upload='never' generator='JOSM'>
    33</osm>
  • trunk/test/unit/org/openstreetmap/josm/io/OsmReaderTest.java

    r12137 r12138  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
    56import static org.junit.Assert.assertNull;
    67import static org.junit.Assert.assertTrue;
    78import static org.junit.Assert.fail;
    89
     10import java.io.ByteArrayInputStream;
     11import java.io.FileInputStream;
    912import java.io.InputStream;
     13import java.nio.charset.StandardCharsets;
    1014
    1115import org.junit.Test;
    1216import org.openstreetmap.josm.TestUtils;
     17import org.openstreetmap.josm.data.osm.DataSet;
    1318import org.openstreetmap.josm.data.osm.Way;
    1419import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1521
    1622/**
     
    1824 */
    1925public class OsmReaderTest {
     26
     27    private static final class PostProcessorStub implements OsmServerReadPostprocessor {
     28        boolean called;
     29
     30        @Override
     31        public void postprocessDataSet(DataSet ds, ProgressMonitor progress) {
     32            called = true;
     33        }
     34    }
     35
     36    /**
     37     * Unit test of {@link OsmReader#registerPostprocessor} / {@link OsmReader#deregisterPostprocessor}.
     38     * @throws Exception if any error occurs
     39     */
     40    @Test
     41    public void testPostProcessors() throws Exception {
     42        PostProcessorStub registered = new PostProcessorStub();
     43        PostProcessorStub unregistered = new PostProcessorStub();
     44
     45        OsmReader.registerPostprocessor(registered);
     46
     47        OsmReader.registerPostprocessor(unregistered);
     48        OsmReader.deregisterPostprocessor(unregistered);
     49
     50        try (InputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "empty.osm")) {
     51            OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
     52            assertTrue(registered.called);
     53            assertFalse(unregistered.called);
     54        } finally {
     55            OsmReader.deregisterPostprocessor(registered);
     56        }
     57    }
     58
     59    private static void testUnknown(String osm) throws Exception {
     60        try (InputStream in = new ByteArrayInputStream(
     61                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     62            assertTrue(OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE).allPrimitives().isEmpty());
     63        }
     64    }
     65
     66    /**
     67     * Unit test of {@link OsmReader#parseUnknown} - root case.
     68     * @throws Exception if any error occurs
     69     */
     70    @Test
     71    public void testUnknownRoot() throws Exception {
     72        testUnknown("<nonosm/>");
     73    }
     74
     75    /**
     76     * Unit test of {@link OsmReader#parseUnknown} - meta case from Overpass API.
     77     * @throws Exception if any error occurs
     78     */
     79    @Test
     80    public void testUnknownMeta() throws Exception {
     81        testUnknown("<osm version='0.6'><meta osm_base='2017-03-29T19:04:03Z'/></osm>");
     82    }
     83
     84    /**
     85     * Unit test of {@link OsmReader#parseUnknown} - note case from Overpass API.
     86     * @throws Exception if any error occurs
     87     */
     88    @Test
     89    public void testUnknownNote() throws Exception {
     90        testUnknown("<osm version='0.6'><note>The data included in this document is from www.openstreetmap.org.</note></osm>");
     91    }
     92
     93    /**
     94     * Unit test of {@link OsmReader#parseUnknown} - other cases.
     95     * @throws Exception if any error occurs
     96     */
     97    @Test
     98    public void testUnknownTag() throws Exception {
     99        testUnknown("<osm version='0.6'><foo>bar</foo></osm>");
     100        testUnknown("<osm version='0.6'><foo><bar/></foo></osm>");
     101    }
     102
     103    /**
     104     * Test invalid data.
     105     * @param osm OSM data without XML prefix
     106     * @param expectedError expected error message
     107     * @throws Exception if any error occurs
     108     */
     109    private static void testInvalidData(String osm, String expectedError) throws Exception {
     110        try (InputStream in = new ByteArrayInputStream(
     111                ("<?xml version='1.0' encoding='UTF-8'?>" + osm).getBytes(StandardCharsets.UTF_8))) {
     112            OsmReader.parseDataSet(in, NullProgressMonitor.INSTANCE);
     113            fail("should throw exception");
     114        } catch (IllegalDataException e) {
     115            assertEquals(expectedError, e.getMessage());
     116        }
     117    }
     118
     119    /**
     120     * Test invalid UID.
     121     * @throws Exception if any error occurs
     122     */
     123    @Test
     124    public void testInvalidUid() throws Exception {
     125        testInvalidData("<osm version='0.6'><node id='1' uid='nan'/></osm>",
     126                "Illegal value for attribute 'uid'. Got 'nan'. (at line 1, column 82). 82 bytes have been read");
     127    }
     128
     129    /**
     130     * Test missing ID.
     131     * @throws Exception if any error occurs
     132     */
     133    @Test
     134    public void testMissingId() throws Exception {
     135        testInvalidData("<osm version='0.6'><node/></osm>",
     136                "Missing required attribute 'id'. (at line 1, column 65). 64 bytes have been read");
     137    }
     138
     139    /**
     140     * Test missing ref.
     141     * @throws Exception if any error occurs
     142     */
     143    @Test
     144    public void testMissingRef() throws Exception {
     145        testInvalidData("<osm version='0.6'><way id='1' version='1'><nd/></way></osm>",
     146                "Missing mandatory attribute 'ref' on <nd> of way 1. (at line 1, column 87). 88 bytes have been read");
     147        testInvalidData("<osm version='0.6'><relation id='1' version='1'><member/></relation></osm>",
     148                "Missing attribute 'ref' on member in relation 1. (at line 1, column 96). 101 bytes have been read");
     149    }
     150
     151    /**
     152     * Test illegal ref.
     153     * @throws Exception if any error occurs
     154     */
     155    @Test
     156    public void testIllegalRef() throws Exception {
     157        testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='0'/></way></osm>",
     158                "Illegal value of attribute 'ref' of element <nd>. Got 0. (at line 1, column 95). 96 bytes have been read");
     159        testInvalidData("<osm version='0.6'><way id='1' version='1'><nd ref='nan'/></way></osm>",
     160                "Illegal long value for attribute 'ref'. Got 'nan'. (at line 1, column 97). 98 bytes have been read");
     161
     162        testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='0'/></relation></osm>",
     163                "Incomplete <member> specification with ref=0 (at line 1, column 116). 121 bytes have been read");
     164        testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='node' ref='nan'/></relation></osm>",
     165                "Illegal value for attribute 'ref' on member in relation 1. Got nan (at line 1, column 118). 123 bytes have been read");
     166    }
     167
     168    /**
     169     * Test missing member type.
     170     * @throws Exception if any error occurs
     171     */
     172    @Test
     173    public void testMissingType() throws Exception {
     174        testInvalidData("<osm version='0.6'><relation id='1' version='1'><member ref='1'/></relation></osm>",
     175                "Missing attribute 'type' on member 1 in relation 1. (at line 1, column 104). 109 bytes have been read");
     176    }
     177
     178    /**
     179     * Test illegal member type.
     180     * @throws Exception if any error occurs
     181     */
     182    @Test
     183    public void testIllegalType() throws Exception {
     184        testInvalidData("<osm version='0.6'><relation id='1' version='1'><member type='foo' ref='1'/></relation></osm>",
     185                "Illegal value for attribute 'type' on member 1 in relation 1. Got foo. (at line 1, column 115). 120 bytes have been read");
     186    }
     187
     188    /**
     189     * Test missing key/value.
     190     * @throws Exception if any error occurs
     191     */
     192    @Test
     193    public void testMissingKeyValue() throws Exception {
     194        testInvalidData("<osm version='0.6'><node id='1' version='1'><tag/></node></osm>",
     195                "Missing key or value attribute in tag. (at line 1, column 89). 89 bytes have been read");
     196        testInvalidData("<osm version='0.6'><node id='1' version='1'><tag k='foo'/></node></osm>",
     197                "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
     198        testInvalidData("<osm version='0.6'><node id='1' version='1'><tag v='bar'/></node></osm>",
     199                "Missing key or value attribute in tag. (at line 1, column 97). 97 bytes have been read");
     200    }
     201
     202    /**
     203     * Test missing version.
     204     * @throws Exception if any error occurs
     205     */
     206    @Test
     207    public void testMissingVersion() throws Exception {
     208        testInvalidData("<osm/>",
     209                "Missing mandatory attribute 'version'. (at line 1, column 45). 44 bytes have been read");
     210        testInvalidData("<osm version='0.6'><node id='1'/></osm>",
     211                "Missing attribute 'version' on OSM primitive with ID 1. (at line 1, column 72). 72 bytes have been read");
     212    }
     213
     214    /**
     215     * Test unsupported version.
     216     * @throws Exception if any error occurs
     217     */
     218    @Test
     219    public void testUnsupportedVersion() throws Exception {
     220        testInvalidData("<osm version='0.1'/>",
     221                "Unsupported version: 0.1 (at line 1, column 59). 58 bytes have been read");
     222    }
     223
     224    /**
     225     * Test illegal version.
     226     * @throws Exception if any error occurs
     227     */
     228    @Test
     229    public void testIllegalVersion() throws Exception {
     230        testInvalidData("<osm version='0.6'><node id='1' version='nan'/></osm>",
     231                "Illegal value for attribute 'version' on OSM primitive with ID 1. Got nan. (at line 1, column 86). 86 bytes have been read");
     232    }
     233
     234    /**
     235     * Test illegal changeset.
     236     * @throws Exception if any error occurs
     237     */
     238    @Test
     239    public void testIllegalChangeset() throws Exception {
     240        testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='nan'/></osm>",
     241                "Illegal value for attribute 'changeset'. Got nan. (at line 1, column 100). 100 bytes have been read");
     242        testInvalidData("<osm version='0.6'><node id='1' version='1' changeset='0'/></osm>",
     243                "Illegal value for attribute 'changeset'. Got 0. (at line 1, column 98). 98 bytes have been read");
     244    }
     245
     246    /**
     247     * Test invalid bounds.
     248     * @throws Exception if any error occurs
     249     */
     250    @Test
     251    public void testInvalidBounds() throws Exception {
     252        testInvalidData("<osm version='0.6'><bounds/></osm>",
     253                "Missing mandatory attributes on element 'bounds'. " +
     254                "Got minlon='null',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 67). 72 bytes have been read");
     255        testInvalidData("<osm version='0.6'><bounds minlon='0'/></osm>",
     256                "Missing mandatory attributes on element 'bounds'. " +
     257                "Got minlon='0',minlat='null',maxlon='null',maxlat='null', origin='null'. (at line 1, column 78). 83 bytes have been read");
     258        testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0'/></osm>",
     259                "Missing mandatory attributes on element 'bounds'. " +
     260                "Got minlon='0',minlat='0',maxlon='null',maxlat='null', origin='null'. (at line 1, column 89). 94 bytes have been read");
     261        testInvalidData("<osm version='0.6'><bounds minlon='0' minlat='0' maxlon='1'/></osm>",
     262                "Missing mandatory attributes on element 'bounds'. " +
     263                "Got minlon='0',minlat='0',maxlon='1',maxlat='null', origin='null'. (at line 1, column 100). 105 bytes have been read");
     264    }
    20265
    21266    /**
Note: See TracChangeset for help on using the changeset viewer.