source: josm/trunk/test/unit/org/openstreetmap/TestUtils.java@ 6927

Last change on this file since 6927 was 6881, checked in by Don-vip, 11 years ago

javadoc/code style/minor refactorization

File size: 2.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap;
3
4import static org.hamcrest.CoreMatchers.is;
5import static org.junit.Assert.assertThat;
6import static org.junit.Assert.assertTrue;
7
8import java.util.Map;
9
10import org.junit.Test;
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Relation;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.tools.TextTagParser;
17
18public 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}
Note: See TracBrowser for help on using the repository browser.