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

Last change on this file since 9385 was 9246, checked in by Don-vip, 8 years ago

javadoc update

  • 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.junit.Assert.fail;
5
6import java.util.Arrays;
7import java.util.Comparator;
8
9/**
10 * Various utils, useful for unit tests.
11 */
12public final class TestUtils {
13
14 private TestUtils() {
15 // Hide constructor for utility classes
16 }
17
18 /**
19 * Returns the path to test data root directory.
20 * @return path to test data root directory
21 */
22 public static String getTestDataRoot() {
23 String testDataRoot = System.getProperty("josm.test.data");
24 if (testDataRoot == null || testDataRoot.isEmpty()) {
25 testDataRoot = "test/data";
26 System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
27 }
28 return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
29 }
30
31 /**
32 * Gets path to test data directory for given ticket id.
33 * @param ticketid Ticket numeric identifier
34 * @return path to test data directory for given ticket id
35 */
36 public static String getRegressionDataDir(int ticketid) {
37 return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
38 }
39
40 /**
41 * Gets path to given file in test data directory for given ticket id.
42 * @param ticketid Ticket numeric identifier
43 * @param filename File name
44 * @return path to given file in test data directory for given ticket id
45 */
46 public static String getRegressionDataFile(int ticketid, String filename) {
47 return getRegressionDataDir(ticketid) + '/' + filename;
48 }
49
50 /**
51 * Checks that the given Comparator respects its contract on the given table.
52 * @param <T> type of elements
53 * @param comparator The comparator to test
54 * @param array The array sorted for test purpose
55 */
56 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
57 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
58 // Check each compare possibility
59 for (int i = 0; i < array.length; i++) {
60 T r1 = array[i];
61 for (int j = i; j < array.length; j++) {
62 T r2 = array[j];
63 int a = comparator.compare(r1, r2);
64 int b = comparator.compare(r2, r1);
65 if (i == j || a == b) {
66 if (a != 0 || b != 0) {
67 fail(getFailMessage(r1, r2, a, b));
68 }
69 } else {
70 if (a != -b) {
71 fail(getFailMessage(r1, r2, a, b));
72 }
73 }
74 for (int k = j; k < array.length; k++) {
75 T r3 = array[k];
76 int c = comparator.compare(r1, r3);
77 int d = comparator.compare(r2, r3);
78 if (a > 0 && d > 0) {
79 if (c <= 0) {
80 fail(getFailMessage(r1, r2, r3, a, b, c, d));
81 }
82 } else if (a == 0 && d == 0) {
83 if (c != 0) {
84 fail(getFailMessage(r1, r2, r3, a, b, c, d));
85 }
86 } else if (a < 0 && d < 0) {
87 if (c >= 0) {
88 fail(getFailMessage(r1, r2, r3, a, b, c, d));
89 }
90 }
91 }
92 }
93 }
94 // Sort relation array
95 Arrays.sort(array, comparator);
96 }
97
98 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
99 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
100 .append(o2).append("\ngave: ").append(a).append("/").append(b)
101 .toString();
102 }
103
104 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
105 return new StringBuilder(getFailMessage(o1, o2, a, b))
106 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
107 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
108 .toString();
109 }
110
111 /**
112 * Returns the Java version as an int value.
113 * @return the Java version as an int value (7, 8, 9, etc.)
114 */
115 public static int getJavaVersion() {
116 String version = System.getProperty("java.version");
117 if (version.startsWith("1.")) {
118 version = version.substring(2);
119 }
120 // Allow these formats:
121 // 1.7.0_91
122 // 1.8.0_72-ea
123 // 9-ea
124 // 9
125 // 9.0.1
126 int dotPos = version.indexOf('.');
127 int dashPos = version.indexOf('-');
128 return Integer.parseInt(version.substring(0,
129 dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
130 }
131}
Note: See TracBrowser for help on using the repository browser.