1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.fail;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Comparator;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
|
---|
14 | import org.openstreetmap.josm.gui.progress.CancelHandler;
|
---|
15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
17 | import org.openstreetmap.josm.io.Compression;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Various utils, useful for unit tests.
|
---|
21 | */
|
---|
22 | public final class TestUtils {
|
---|
23 |
|
---|
24 | private TestUtils() {
|
---|
25 | // Hide constructor for utility classes
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Returns the path to test data root directory.
|
---|
30 | * @return path to test data root directory
|
---|
31 | */
|
---|
32 | public static String getTestDataRoot() {
|
---|
33 | String testDataRoot = System.getProperty("josm.test.data");
|
---|
34 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
---|
35 | testDataRoot = "test/data";
|
---|
36 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
---|
37 | }
|
---|
38 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Gets path to test data directory for given ticket id.
|
---|
43 | * @param ticketid Ticket numeric identifier
|
---|
44 | * @return path to test data directory for given ticket id
|
---|
45 | */
|
---|
46 | public static String getRegressionDataDir(int ticketid) {
|
---|
47 | return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Gets path to given file in test data directory for given ticket id.
|
---|
52 | * @param ticketid Ticket numeric identifier
|
---|
53 | * @param filename File name
|
---|
54 | * @return path to given file in test data directory for given ticket id
|
---|
55 | */
|
---|
56 | public static String getRegressionDataFile(int ticketid, String filename) {
|
---|
57 | return getRegressionDataDir(ticketid) + '/' + filename;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Gets input stream to given file in test data directory for given ticket id.
|
---|
62 | * @param ticketid Ticket numeric identifier
|
---|
63 | * @param filename File name
|
---|
64 | * @return path to given file in test data directory for given ticket id
|
---|
65 | * @throws IOException if any I/O error occurs
|
---|
66 | */
|
---|
67 | public static InputStream getRegressionDataStream(int ticketid, String filename) throws IOException {
|
---|
68 | return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid) + '/' + filename));
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Checks that the given Comparator respects its contract on the given table.
|
---|
73 | * @param <T> type of elements
|
---|
74 | * @param comparator The comparator to test
|
---|
75 | * @param array The array sorted for test purpose
|
---|
76 | */
|
---|
77 | public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
|
---|
78 | System.out.println("Validating Comparable contract on array of "+array.length+" elements");
|
---|
79 | // Check each compare possibility
|
---|
80 | for (int i = 0; i < array.length; i++) {
|
---|
81 | T r1 = array[i];
|
---|
82 | for (int j = i; j < array.length; j++) {
|
---|
83 | T r2 = array[j];
|
---|
84 | int a = comparator.compare(r1, r2);
|
---|
85 | int b = comparator.compare(r2, r1);
|
---|
86 | if (i == j || a == b) {
|
---|
87 | if (a != 0 || b != 0) {
|
---|
88 | fail(getFailMessage(r1, r2, a, b));
|
---|
89 | }
|
---|
90 | } else {
|
---|
91 | if (a != -b) {
|
---|
92 | fail(getFailMessage(r1, r2, a, b));
|
---|
93 | }
|
---|
94 | }
|
---|
95 | for (int k = j; k < array.length; k++) {
|
---|
96 | T r3 = array[k];
|
---|
97 | int c = comparator.compare(r1, r3);
|
---|
98 | int d = comparator.compare(r2, r3);
|
---|
99 | 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 | } else if (a < 0 && d < 0) {
|
---|
108 | if (c >= 0) {
|
---|
109 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | // Sort relation array
|
---|
116 | Arrays.sort(array, comparator);
|
---|
117 | }
|
---|
118 |
|
---|
119 | private static <T> String getFailMessage(T o1, T o2, int a, int b) {
|
---|
120 | return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
|
---|
121 | .append(o2).append("\ngave: ").append(a).append("/").append(b)
|
---|
122 | .toString();
|
---|
123 | }
|
---|
124 |
|
---|
125 | private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
|
---|
126 | return new StringBuilder(getFailMessage(o1, o2, a, b))
|
---|
127 | .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
|
---|
128 | .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
|
---|
129 | .toString();
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Returns the Java version as an int value.
|
---|
134 | * @return the Java version as an int value (7, 8, 9, etc.)
|
---|
135 | */
|
---|
136 | public static int getJavaVersion() {
|
---|
137 | String version = System.getProperty("java.version");
|
---|
138 | if (version.startsWith("1.")) {
|
---|
139 | version = version.substring(2);
|
---|
140 | }
|
---|
141 | // Allow these formats:
|
---|
142 | // 1.7.0_91
|
---|
143 | // 1.8.0_72-ea
|
---|
144 | // 9-ea
|
---|
145 | // 9
|
---|
146 | // 9.0.1
|
---|
147 | int dotPos = version.indexOf('.');
|
---|
148 | int dashPos = version.indexOf('-');
|
---|
149 | return Integer.parseInt(version.substring(0,
|
---|
150 | dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
|
---|
155 | * but does not show the progress.
|
---|
156 | * @return a progress monitor
|
---|
157 | */
|
---|
158 | public static ProgressMonitor newTestProgressMonitor() {
|
---|
159 | return new AbstractProgressMonitor(new CancelHandler()) {
|
---|
160 |
|
---|
161 | @Override
|
---|
162 | protected void doBeginTask() {
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | protected void doFinishTask() {
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | protected void doSetIntermediate(boolean value) {
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | protected void doSetTitle(String title) {
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | protected void doSetCustomText(String title) {
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | protected void updateProgress(double value) {
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public void setProgressTaskId(ProgressTaskId taskId) {
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public ProgressTaskId getProgressTaskId() {
|
---|
191 | return null;
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public Component getWindowParent() {
|
---|
196 | return null;
|
---|
197 | }
|
---|
198 |
|
---|
199 | };
|
---|
200 | }
|
---|
201 | }
|
---|