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