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

Last change on this file since 7937 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

  • Property svn:eol-style set to native
File size: 4.8 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 * @return path to test data root directory
25 */
26 public static String getTestDataRoot() {
27 String testDataRoot = System.getProperty("josm.test.data");
28 if (testDataRoot == null || testDataRoot.isEmpty()) {
29 testDataRoot = "test/data";
30 System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
31 }
32 return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
33 }
34
35 /**
36 * Gets path to test data directory for given ticket id.
37 * @param ticketid Ticket numeric identifier
38 * @return path to test data directory for given ticket id
39 */
40 public static String getRegressionDataDir(int ticketid) {
41 return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
42 }
43
44 /**
45 * Gets path to given file in test data directory for given ticket id.
46 * @param ticketid Ticket numeric identifier
47 * @param filename File name
48 * @return path to given file in test data directory for given ticket id
49 */
50 public static String getRegressionDataFile(int ticketid, String filename) {
51 return getRegressionDataDir(ticketid) + '/' + filename;
52 }
53
54 @Test
55 public void testCreatePrimitive() throws Exception {
56 final OsmPrimitive p = OsmUtils.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 OsmUtils.createPrimitive("noway name=Foo");
66 }
67
68 /**
69 * Checks that the given Comparator respects its contract on the given table.
70 * @param comparator The comparator to test
71 * @param array The array sorted for test purpose
72 */
73 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
74 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
75 // Check each compare possibility
76 for (int i=0; i<array.length; i++) {
77 T r1 = array[i];
78 for (int j=i; j<array.length; j++) {
79 T r2 = array[j];
80 int a = comparator.compare(r1, r2);
81 int b = comparator.compare(r2, r1);
82 if (i==j || a==b) {
83 if (a != 0 || b != 0) {
84 fail(getFailMessage(r1, r2, a, b));
85 }
86 } else {
87 if (a != -b) {
88 fail(getFailMessage(r1, r2, a, b));
89 }
90 }
91 for (int k=j; k<array.length; k++) {
92 T r3 = array[k];
93 int c = comparator.compare(r1, r3);
94 int d = comparator.compare(r2, r3);
95 if (a > 0 && d > 0) {
96 if (c <= 0) {
97 fail(getFailMessage(r1, r2, r3, a, b, c, d));
98 }
99 } else if (a == 0 && d == 0) {
100 if (c != 0) {
101 fail(getFailMessage(r1, r2, r3, a, b, c, d));
102 }
103 } else if (a < 0 && d < 0) {
104 if (c >= 0) {
105 fail(getFailMessage(r1, r2, r3, a, b, c, d));
106 }
107 }
108 }
109 }
110 }
111 // Sort relation array
112 Arrays.sort(array, comparator);
113 }
114
115 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
116 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
117 .append(o2).append("\ngave: ").append(a).append("/").append(b)
118 .toString();
119 }
120
121 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
122 return new StringBuilder(getFailMessage(o1, o2, a, b))
123 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
124 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
125 .toString();
126 }
127}
Note: See TracBrowser for help on using the repository browser.