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

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

see #11924 - add robustness for short versions "9" and "9-ea"

  • Property svn:eol-style set to native
File size: 4.7 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 comparator The comparator to test
53 * @param array The array sorted for test purpose
54 */
55 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
56 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
57 // Check each compare possibility
58 for (int i = 0; i < array.length; i++) {
59 T r1 = array[i];
60 for (int j = i; j < array.length; j++) {
61 T r2 = array[j];
62 int a = comparator.compare(r1, r2);
63 int b = comparator.compare(r2, r1);
64 if (i == j || a == b) {
65 if (a != 0 || b != 0) {
66 fail(getFailMessage(r1, r2, a, b));
67 }
68 } else {
69 if (a != -b) {
70 fail(getFailMessage(r1, r2, a, b));
71 }
72 }
73 for (int k = j; k < array.length; k++) {
74 T r3 = array[k];
75 int c = comparator.compare(r1, r3);
76 int d = comparator.compare(r2, r3);
77 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 } else if (a < 0 && d < 0) {
86 if (c >= 0) {
87 fail(getFailMessage(r1, r2, r3, a, b, c, d));
88 }
89 }
90 }
91 }
92 }
93 // Sort relation array
94 Arrays.sort(array, comparator);
95 }
96
97 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
98 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
99 .append(o2).append("\ngave: ").append(a).append("/").append(b)
100 .toString();
101 }
102
103 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
104 return new StringBuilder(getFailMessage(o1, o2, a, b))
105 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
106 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
107 .toString();
108 }
109
110 /**
111 * Returns the Java version as an int value.
112 * @return the Java version as an int value (7, 8, 9, etc.)
113 */
114 public static int getJavaVersion() {
115 String version = System.getProperty("java.version");
116 if (version.startsWith("1.")) {
117 version = version.substring(2);
118 }
119 // Allow these formats:
120 // 1.7.0_91
121 // 1.8.0_72-ea
122 // 9-ea
123 // 9
124 // 9.0.1
125 int dotPos = version.indexOf('.');
126 int dashPos = version.indexOf('-');
127 return Integer.parseInt(version.substring(0,
128 dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
129 }
130}
Note: See TracBrowser for help on using the repository browser.