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