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

Last change on this file since 8509 was 8257, checked in by simon04, 9 years ago

fix #11370 - "area" is not supported in validator asserts

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.junit.Assert.fail;
5
6import java.util.Arrays;
7import java.util.Comparator;
8
9/**
10 * Various utils, useful for unit tests.
11 */
12public class TestUtils {
13
14 /**
15 * Returns the path to test data root directory.
16 * @return path to test data root directory
17 */
18 public static String getTestDataRoot() {
19 String testDataRoot = System.getProperty("josm.test.data");
20 if (testDataRoot == null || testDataRoot.isEmpty()) {
21 testDataRoot = "test/data";
22 System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
23 }
24 return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
25 }
26
27 /**
28 * Gets path to test data directory for given ticket id.
29 * @param ticketid Ticket numeric identifier
30 * @return path to test data directory for given ticket id
31 */
32 public static String getRegressionDataDir(int ticketid) {
33 return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
34 }
35
36 /**
37 * Gets path to given file in test data directory for given ticket id.
38 * @param ticketid Ticket numeric identifier
39 * @param filename File name
40 * @return path to given file in test data directory for given ticket id
41 */
42 public static String getRegressionDataFile(int ticketid, String filename) {
43 return getRegressionDataDir(ticketid) + '/' + filename;
44 }
45
46 /**
47 * Checks that the given Comparator respects its contract on the given table.
48 * @param comparator The comparator to test
49 * @param array The array sorted for test purpose
50 */
51 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
52 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
53 // Check each compare possibility
54 for (int i=0; i<array.length; i++) {
55 T r1 = array[i];
56 for (int j=i; j<array.length; j++) {
57 T r2 = array[j];
58 int a = comparator.compare(r1, r2);
59 int b = comparator.compare(r2, r1);
60 if (i==j || a==b) {
61 if (a != 0 || b != 0) {
62 fail(getFailMessage(r1, r2, a, b));
63 }
64 } else {
65 if (a != -b) {
66 fail(getFailMessage(r1, r2, a, b));
67 }
68 }
69 for (int k=j; k<array.length; k++) {
70 T r3 = array[k];
71 int c = comparator.compare(r1, r3);
72 int d = comparator.compare(r2, r3);
73 if (a > 0 && d > 0) {
74 if (c <= 0) {
75 fail(getFailMessage(r1, r2, r3, a, b, c, d));
76 }
77 } else if (a == 0 && d == 0) {
78 if (c != 0) {
79 fail(getFailMessage(r1, r2, r3, a, b, c, d));
80 }
81 } else if (a < 0 && d < 0) {
82 if (c >= 0) {
83 fail(getFailMessage(r1, r2, r3, a, b, c, d));
84 }
85 }
86 }
87 }
88 }
89 // Sort relation array
90 Arrays.sort(array, comparator);
91 }
92
93 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
94 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
95 .append(o2).append("\ngave: ").append(a).append("/").append(b)
96 .toString();
97 }
98
99 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
100 return new StringBuilder(getFailMessage(o1, o2, a, b))
101 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
102 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
103 .toString();
104 }
105}
Note: See TracBrowser for help on using the repository browser.