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

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

unit tests code refactoring/cleanup

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