1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.is;
|
---|
5 | import static org.junit.Assert.assertThat;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.tools.TextTagParser;
|
---|
17 |
|
---|
18 | public class TestUtils {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Returns the path to test data root directory.
|
---|
22 | */
|
---|
23 | public static String getTestDataRoot() {
|
---|
24 | String testDataRoot = System.getProperty("josm.test.data");
|
---|
25 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
---|
26 | testDataRoot = "test/data";
|
---|
27 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
---|
28 | }
|
---|
29 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
---|
30 | }
|
---|
31 |
|
---|
32 | public static OsmPrimitive createPrimitive(String assertion) {
|
---|
33 | if (Main.pref == null) {
|
---|
34 | Main.initApplicationPreferences();
|
---|
35 | }
|
---|
36 | final String[] x = assertion.split("\\s+", 2);
|
---|
37 | final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
|
---|
38 | ? new Node()
|
---|
39 | : "w".equals(x[0]) || "way".equals(x[0])
|
---|
40 | ? new Way()
|
---|
41 | : "r".equals(x[0]) || "relation".equals(x[0])
|
---|
42 | ? new Relation()
|
---|
43 | : null;
|
---|
44 | if (p == null) {
|
---|
45 | throw new IllegalArgumentException("Expecting n/node/w/way/r/relation, but got " + x[0]);
|
---|
46 | }
|
---|
47 | for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
|
---|
48 | p.put(i.getKey(), i.getValue());
|
---|
49 | }
|
---|
50 | return p;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Test
|
---|
54 | public void testCreatePrimitive() throws Exception {
|
---|
55 | final OsmPrimitive p = createPrimitive("way name=Foo railway=rail");
|
---|
56 | assertTrue(p instanceof Way);
|
---|
57 | assertThat(p.keySet().size(), is(2));
|
---|
58 | assertThat(p.get("name"), is("Foo"));
|
---|
59 | assertThat(p.get("railway"), is("rail"));
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Test(expected = IllegalArgumentException.class)
|
---|
63 | public void testCreatePrimitiveFail() throws Exception {
|
---|
64 | TestUtils.createPrimitive("noway name=Foo");
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|