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

Last change on this file since 6736 was 6601, checked in by simon04, 11 years ago

see #9414 fix #9485 - MapCSSTagChecker: add support for set class_name instruction and .class_name condition

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