1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.fail;
|
---|
6 |
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Container;
|
---|
9 | import java.awt.Graphics2D;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.lang.reflect.Field;
|
---|
14 | import java.lang.reflect.Method;
|
---|
15 | import java.security.AccessController;
|
---|
16 | import java.security.PrivilegedAction;
|
---|
17 | import java.time.Instant;
|
---|
18 | import java.time.ZoneOffset;
|
---|
19 | import java.time.format.DateTimeFormatter;
|
---|
20 | import java.time.temporal.Temporal;
|
---|
21 | import java.util.Arrays;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.Comparator;
|
---|
24 | import java.util.Objects;
|
---|
25 | import java.util.stream.Stream;
|
---|
26 |
|
---|
27 | import org.junit.Assume;
|
---|
28 | import org.openstreetmap.josm.command.Command;
|
---|
29 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
30 | import org.openstreetmap.josm.data.osm.Node;
|
---|
31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
33 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
34 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
35 | import org.openstreetmap.josm.data.osm.Way;
|
---|
36 | import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
|
---|
37 | import org.openstreetmap.josm.gui.progress.CancelHandler;
|
---|
38 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
39 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
40 | import org.openstreetmap.josm.io.Compression;
|
---|
41 | import org.openstreetmap.josm.testutils.FakeGraphics;
|
---|
42 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
43 | import org.openstreetmap.josm.tools.Utils;
|
---|
44 |
|
---|
45 | import com.github.tomakehurst.wiremock.WireMockServer;
|
---|
46 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
|
---|
47 |
|
---|
48 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Various utils, useful for unit tests.
|
---|
52 | */
|
---|
53 | public final class TestUtils {
|
---|
54 |
|
---|
55 | private TestUtils() {
|
---|
56 | // Hide constructor for utility classes
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Returns the path to test data root directory.
|
---|
61 | * @return path to test data root directory
|
---|
62 | */
|
---|
63 | public static String getTestDataRoot() {
|
---|
64 | String testDataRoot = System.getProperty("josm.test.data");
|
---|
65 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
---|
66 | testDataRoot = "test/data";
|
---|
67 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
---|
68 | }
|
---|
69 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Gets path to test data directory for given ticket id.
|
---|
74 | * @param ticketid Ticket numeric identifier
|
---|
75 | * @return path to test data directory for given ticket id
|
---|
76 | */
|
---|
77 | public static String getRegressionDataDir(int ticketid) {
|
---|
78 | return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Gets path to given file in test data directory for given ticket id.
|
---|
83 | * @param ticketid Ticket numeric identifier
|
---|
84 | * @param filename File name
|
---|
85 | * @return path to given file in test data directory for given ticket id
|
---|
86 | */
|
---|
87 | public static String getRegressionDataFile(int ticketid, String filename) {
|
---|
88 | return getRegressionDataDir(ticketid) + '/' + filename;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Gets input stream to given file in test data directory for given ticket id.
|
---|
93 | * @param ticketid Ticket numeric identifier
|
---|
94 | * @param filename File name
|
---|
95 | * @return path to given file in test data directory for given ticket id
|
---|
96 | * @throws IOException if any I/O error occurs
|
---|
97 | */
|
---|
98 | public static InputStream getRegressionDataStream(int ticketid, String filename) throws IOException {
|
---|
99 | return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid), filename));
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Checks that the given Comparator respects its contract on the given table.
|
---|
104 | * @param <T> type of elements
|
---|
105 | * @param comparator The comparator to test
|
---|
106 | * @param array The array sorted for test purpose
|
---|
107 | */
|
---|
108 | @SuppressFBWarnings(value = "RV_NEGATING_RESULT_OF_COMPARETO")
|
---|
109 | public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
|
---|
110 | System.out.println("Validating Comparable contract on array of "+array.length+" elements");
|
---|
111 | // Check each compare possibility
|
---|
112 | for (int i = 0; i < array.length; i++) {
|
---|
113 | T r1 = array[i];
|
---|
114 | for (int j = i; j < array.length; j++) {
|
---|
115 | T r2 = array[j];
|
---|
116 | int a = comparator.compare(r1, r2);
|
---|
117 | int b = comparator.compare(r2, r1);
|
---|
118 | if (i == j || a == b) {
|
---|
119 | if (a != 0 || b != 0) {
|
---|
120 | fail(getFailMessage(r1, r2, a, b));
|
---|
121 | }
|
---|
122 | } else {
|
---|
123 | if (a != -b) {
|
---|
124 | fail(getFailMessage(r1, r2, a, b));
|
---|
125 | }
|
---|
126 | }
|
---|
127 | for (int k = j; k < array.length; k++) {
|
---|
128 | T r3 = array[k];
|
---|
129 | int c = comparator.compare(r1, r3);
|
---|
130 | int d = comparator.compare(r2, r3);
|
---|
131 | if (a > 0 && d > 0) {
|
---|
132 | if (c <= 0) {
|
---|
133 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
134 | }
|
---|
135 | } else if (a == 0 && d == 0) {
|
---|
136 | if (c != 0) {
|
---|
137 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
138 | }
|
---|
139 | } else if (a < 0 && d < 0) {
|
---|
140 | if (c >= 0) {
|
---|
141 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | // Sort relation array
|
---|
148 | Arrays.sort(array, comparator);
|
---|
149 | }
|
---|
150 |
|
---|
151 | private static <T> String getFailMessage(T o1, T o2, int a, int b) {
|
---|
152 | return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
|
---|
153 | .append(o2).append("\ngave: ").append(a).append("/").append(b)
|
---|
154 | .toString();
|
---|
155 | }
|
---|
156 |
|
---|
157 | private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
|
---|
158 | return new StringBuilder(getFailMessage(o1, o2, a, b))
|
---|
159 | .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
|
---|
160 | .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
|
---|
161 | .toString();
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Returns a private field value.
|
---|
166 | * @param obj object
|
---|
167 | * @param fieldName private field name
|
---|
168 | * @return private field value
|
---|
169 | * @throws ReflectiveOperationException if a reflection operation error occurs
|
---|
170 | */
|
---|
171 | public static Object getPrivateField(Object obj, String fieldName) throws ReflectiveOperationException {
|
---|
172 | Field f = obj.getClass().getDeclaredField(fieldName);
|
---|
173 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
---|
174 | f.setAccessible(true);
|
---|
175 | return null;
|
---|
176 | });
|
---|
177 | return f.get(obj);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Returns a private static field value.
|
---|
182 | * @param cls object class
|
---|
183 | * @param fieldName private field name
|
---|
184 | * @return private field value
|
---|
185 | * @throws ReflectiveOperationException if a reflection operation error occurs
|
---|
186 | */
|
---|
187 | public static Object getPrivateStaticField(Class<?> cls, String fieldName) throws ReflectiveOperationException {
|
---|
188 | Field f = cls.getDeclaredField(fieldName);
|
---|
189 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
---|
190 | f.setAccessible(true);
|
---|
191 | return null;
|
---|
192 | });
|
---|
193 | return f.get(null);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Sets a private static field value.
|
---|
198 | * @param cls object class
|
---|
199 | * @param fieldName private field name
|
---|
200 | * @param value replacement value
|
---|
201 | * @throws ReflectiveOperationException if a reflection operation error occurs
|
---|
202 | */
|
---|
203 | public static void setPrivateStaticField(Class<?> cls, String fieldName, final Object value) throws ReflectiveOperationException {
|
---|
204 | Field f = cls.getDeclaredField(fieldName);
|
---|
205 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
---|
206 | f.setAccessible(true);
|
---|
207 | return null;
|
---|
208 | });
|
---|
209 | f.set(null, value);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
|
---|
214 | * but does not show the progress.
|
---|
215 | * @return a progress monitor
|
---|
216 | */
|
---|
217 | public static ProgressMonitor newTestProgressMonitor() {
|
---|
218 | return new AbstractProgressMonitor(new CancelHandler()) {
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | protected void doBeginTask() {
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | protected void doFinishTask() {
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Override
|
---|
229 | protected void doSetIntermediate(boolean value) {
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | protected void doSetTitle(String title) {
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | protected void doSetCustomText(String title) {
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | protected void updateProgress(double value) {
|
---|
242 | }
|
---|
243 |
|
---|
244 | @Override
|
---|
245 | public void setProgressTaskId(ProgressTaskId taskId) {
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public ProgressTaskId getProgressTaskId() {
|
---|
250 | return null;
|
---|
251 | }
|
---|
252 |
|
---|
253 | @Override
|
---|
254 | public Component getWindowParent() {
|
---|
255 | return null;
|
---|
256 | }
|
---|
257 | };
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Returns an instance of {@link Graphics2D}.
|
---|
262 | * @return a mockup graphics instance
|
---|
263 | */
|
---|
264 | public static Graphics2D newGraphics() {
|
---|
265 | return new FakeGraphics();
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Makes sure the given primitive belongs to a data set.
|
---|
270 | * @param <T> OSM primitive type
|
---|
271 | * @param osm OSM primitive
|
---|
272 | * @return OSM primitive, attached to a new {@code DataSet}
|
---|
273 | */
|
---|
274 | public static <T extends OsmPrimitive> T addFakeDataSet(T osm) {
|
---|
275 | new DataSet(osm);
|
---|
276 | return osm;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Creates a new node with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)})
|
---|
281 | *
|
---|
282 | * @param tags the tags to set
|
---|
283 | * @return a new node
|
---|
284 | */
|
---|
285 | public static Node newNode(String tags) {
|
---|
286 | return (Node) OsmUtils.createPrimitive("node " + tags);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Creates a new way with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the nodes added
|
---|
291 | *
|
---|
292 | * @param tags the tags to set
|
---|
293 | * @param nodes the nodes to add
|
---|
294 | * @return a new way
|
---|
295 | */
|
---|
296 | public static Way newWay(String tags, Node... nodes) {
|
---|
297 | final Way way = (Way) OsmUtils.createPrimitive("way " + tags);
|
---|
298 | for (Node node : nodes) {
|
---|
299 | way.addNode(node);
|
---|
300 | }
|
---|
301 | return way;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Creates a new relation with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the members added
|
---|
306 | *
|
---|
307 | * @param tags the tags to set
|
---|
308 | * @param members the members to add
|
---|
309 | * @return a new relation
|
---|
310 | */
|
---|
311 | public static Relation newRelation(String tags, RelationMember... members) {
|
---|
312 | final Relation relation = (Relation) OsmUtils.createPrimitive("relation " + tags);
|
---|
313 | for (RelationMember member : members) {
|
---|
314 | relation.addMember(member);
|
---|
315 | }
|
---|
316 | return relation;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Creates a new empty command.
|
---|
321 | * @param ds data set
|
---|
322 | * @return a new empty command
|
---|
323 | */
|
---|
324 | public static Command newCommand(DataSet ds) {
|
---|
325 | return new Command(ds) {
|
---|
326 | @Override
|
---|
327 | public String getDescriptionText() {
|
---|
328 | return "";
|
---|
329 | }
|
---|
330 |
|
---|
331 | @Override
|
---|
332 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
333 | Collection<OsmPrimitive> added) {
|
---|
334 | // Do nothing
|
---|
335 | }
|
---|
336 | };
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Ensures 100% code coverage for enums.
|
---|
341 | * @param enumClass enum class to cover
|
---|
342 | */
|
---|
343 | public static void superficialEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
|
---|
344 | try {
|
---|
345 | Method values = enumClass.getMethod("values");
|
---|
346 | Method valueOf = enumClass.getMethod("valueOf", String.class);
|
---|
347 | Utils.setObjectsAccessible(values, valueOf);
|
---|
348 | for (Object o : (Object[]) values.invoke(null)) {
|
---|
349 | assertEquals(o, valueOf.invoke(null, ((Enum<?>) o).name()));
|
---|
350 | }
|
---|
351 | } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
|
---|
352 | throw new JosmRuntimeException(e);
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * Get a descendant component by name.
|
---|
358 | * @param root The root component to start searching from.
|
---|
359 | * @param name The component name
|
---|
360 | * @return The component with that name or null if it does not exist.
|
---|
361 | * @since 12045
|
---|
362 | */
|
---|
363 | public static Component getComponentByName(Component root, String name) {
|
---|
364 | if (name.equals(root.getName())) {
|
---|
365 | return root;
|
---|
366 | } else if (root instanceof Container) {
|
---|
367 | Container container = (Container) root;
|
---|
368 | return Stream.of(container.getComponents())
|
---|
369 | .map(child -> getComponentByName(child, name))
|
---|
370 | .filter(Objects::nonNull)
|
---|
371 | .findFirst().orElse(null);
|
---|
372 | } else {
|
---|
373 | return null;
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Use to assume that EqualsVerifier is working with the current JVM.
|
---|
379 | */
|
---|
380 | public static void assumeWorkingEqualsVerifier() {
|
---|
381 | try {
|
---|
382 | // Workaround to https://github.com/jqno/equalsverifier/issues/177
|
---|
383 | // Inspired by https://issues.apache.org/jira/browse/SOLR-11606
|
---|
384 | nl.jqno.equalsverifier.internal.lib.bytebuddy.ClassFileVersion.ofThisVm();
|
---|
385 | } catch (IllegalArgumentException e) {
|
---|
386 | Assume.assumeNoException(e);
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Return WireMock server serving files under ticker directory
|
---|
392 | * @param ticketId Ticket numeric identifier
|
---|
393 | * @return WireMock HTTP server on dynamic port
|
---|
394 | */
|
---|
395 | public static WireMockServer getWireMockServer(int ticketId) {
|
---|
396 | return new WireMockServer(
|
---|
397 | WireMockConfiguration.options()
|
---|
398 | .dynamicPort()
|
---|
399 | .usingFilesUnderDirectory(getRegressionDataDir(ticketId))
|
---|
400 | );
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Return WireMock server serving files under ticker directory
|
---|
405 | * @return WireMock HTTP server on dynamic port
|
---|
406 | */
|
---|
407 | public static WireMockServer getWireMockServer() {
|
---|
408 | return new WireMockServer(
|
---|
409 | WireMockConfiguration.options()
|
---|
410 | .dynamicPort()
|
---|
411 | );
|
---|
412 | }
|
---|
413 | /**
|
---|
414 | * Renders Temporal to RFC 1123 Date Time
|
---|
415 | * @param time
|
---|
416 | * @return string representation according to RFC1123 of time
|
---|
417 | */
|
---|
418 | public static String getHTTPDate(Temporal time) {
|
---|
419 | return DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC).format(time);
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Renders java time stamp to RFC 1123 Date Time
|
---|
424 | * @param time
|
---|
425 | * @return string representation according to RFC1123 of time
|
---|
426 | */
|
---|
427 | public static String getHTTPDate(long time) {
|
---|
428 | return getHTTPDate(Instant.ofEpochMilli(time));
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | }
|
---|