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

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

fix #10206 - Check MapCSS validator assertions for local rules if new advanced option validator.check_assert_local_rules is enabled in preferences

File size: 4.0 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;
11
12import org.junit.Test;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.OsmUtils;
15import org.openstreetmap.josm.data.osm.Way;
16
17/**
18 * Various utils, useful for unit tests.
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 @Test
35 public void testCreatePrimitive() throws Exception {
36 final OsmPrimitive p = OsmUtils.createPrimitive("way name=Foo railway=rail");
37 assertTrue(p instanceof Way);
38 assertThat(p.keySet().size(), is(2));
39 assertThat(p.get("name"), is("Foo"));
40 assertThat(p.get("railway"), is("rail"));
41 }
42
43 @Test(expected = IllegalArgumentException.class)
44 public void testCreatePrimitiveFail() throws Exception {
45 OsmUtils.createPrimitive("noway name=Foo");
46 }
47
48 /**
49 * Checks that the given Comparator respects its contract on the given table.
50 * @param comparator The comparator to test
51 * @param array The array sorted for test purpose
52 */
53 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
54 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
55 // Check each compare possibility
56 for (int i=0; i<array.length; i++) {
57 T r1 = array[i];
58 for (int j=i; j<array.length; j++) {
59 T r2 = array[j];
60 int a = comparator.compare(r1, r2);
61 int b = comparator.compare(r2, r1);
62 if (i==j || a==b) {
63 if (a != 0 || b != 0) {
64 fail(getFailMessage(r1, r2, a, b));
65 }
66 } else {
67 if (a != -b) {
68 fail(getFailMessage(r1, r2, a, b));
69 }
70 }
71 for (int k=j; k<array.length; k++) {
72 T r3 = array[k];
73 int c = comparator.compare(r1, r3);
74 int d = comparator.compare(r2, r3);
75 if (a > 0 && d > 0) {
76 if (c <= 0) {
77 fail(getFailMessage(r1, r2, r3, a, b, c, d));
78 }
79 } else if (a == 0 && d == 0) {
80 if (c != 0) {
81 fail(getFailMessage(r1, r2, r3, a, b, c, d));
82 }
83 } else if (a < 0 && d < 0) {
84 if (c >= 0) {
85 fail(getFailMessage(r1, r2, r3, a, b, c, d));
86 }
87 }
88 }
89 }
90 }
91 // Sort relation array
92 Arrays.sort(array, comparator);
93 }
94
95 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
96 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
97 .append(o2).append("\ngave: ").append(a).append("/").append(b)
98 .toString();
99 }
100
101 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
102 return new StringBuilder(getFailMessage(o1, o2, a, b))
103 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
104 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
105 .toString();
106 }
107}
Note: See TracBrowser for help on using the repository browser.