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

Last change on this file since 7109 was 7109, checked in by Don-vip, 10 years ago

see #8671 - some code refactoring done in an unsuccessful attempt to reproduce error, but worth keeping

File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.hamcrest.CoreMatchers.is;
5import static org.junit.Assert.assertThat;
6import static org.junit.Assert.assertTrue;
7import static org.junit.Assert.fail;
8
9import java.util.Arrays;
10import java.util.Comparator;
11import java.util.Map;
12
13import org.junit.Test;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.tools.TextTagParser;
19
20public class TestUtils {
21
22 /**
23 * Returns the path to test data root directory.
24 */
25 public static String getTestDataRoot() {
26 String testDataRoot = System.getProperty("josm.test.data");
27 if (testDataRoot == null || testDataRoot.isEmpty()) {
28 testDataRoot = "test/data";
29 System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
30 }
31 return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
32 }
33
34 public static OsmPrimitive createPrimitive(String assertion) {
35 if (Main.pref == null) {
36 Main.initApplicationPreferences();
37 }
38 final String[] x = assertion.split("\\s+", 2);
39 final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
40 ? new Node()
41 : "w".equals(x[0]) || "way".equals(x[0])
42 ? new Way()
43 : "r".equals(x[0]) || "relation".equals(x[0])
44 ? new Relation()
45 : null;
46 if (p == null) {
47 throw new IllegalArgumentException("Expecting n/node/w/way/r/relation, but got " + x[0]);
48 }
49 for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
50 p.put(i.getKey(), i.getValue());
51 }
52 return p;
53 }
54
55 @Test
56 public void testCreatePrimitive() throws Exception {
57 final OsmPrimitive p = createPrimitive("way name=Foo railway=rail");
58 assertTrue(p instanceof Way);
59 assertThat(p.keySet().size(), is(2));
60 assertThat(p.get("name"), is("Foo"));
61 assertThat(p.get("railway"), is("rail"));
62 }
63
64 @Test(expected = IllegalArgumentException.class)
65 public void testCreatePrimitiveFail() throws Exception {
66 TestUtils.createPrimitive("noway name=Foo");
67 }
68
69 /**
70 * Checks that the given Comparator respects its contract on the given table.
71 * @param comparator The comparator to test
72 * @param array The array sorted for test purpose
73 */
74 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
75 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
76 // Check each compare possibility
77 for (int i=0; i<array.length; i++) {
78 T r1 = array[i];
79 for (int j=i; j<array.length; j++) {
80 T r2 = array[j];
81 int a = comparator.compare(r1, r2);
82 int b = comparator.compare(r2, r1);
83 if (i==j || a==b) {
84 if (a != 0 || b != 0) {
85 fail(getFailMessage(r1, r2, a, b));
86 }
87 } else {
88 if (a != -b) {
89 fail(getFailMessage(r1, r2, a, b));
90 }
91 }
92 for (int k=j; k<array.length; k++) {
93 T r3 = array[k];
94 int c = comparator.compare(r1, r3);
95 int d = comparator.compare(r2, r3);
96 if (a > 0 && d > 0) {
97 if (c <= 0) {
98 fail(getFailMessage(r1, r2, r3, a, b, c, d));
99 }
100 } else if (a == 0 && d == 0) {
101 if (c != 0) {
102 fail(getFailMessage(r1, r2, r3, a, b, c, d));
103 }
104 } else if (a < 0 && d < 0) {
105 if (c >= 0) {
106 fail(getFailMessage(r1, r2, r3, a, b, c, d));
107 }
108 }
109 }
110 }
111 }
112 // Sort relation array
113 Arrays.sort(array, comparator);
114 }
115
116 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
117 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
118 .append(o2).append("\ngave: ").append(a).append("/").append(b)
119 .toString();
120 }
121
122 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
123 return new StringBuilder(getFailMessage(o1, o2, a, b))
124 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
125 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
126 .toString();
127 }
128}
Note: See TracBrowser for help on using the repository browser.