Ignore:
Timestamp:
2018-07-26T22:01:31+02:00 (6 years ago)
Author:
Don-vip
Message:

see #16010 - use JMockit to enable more extensive test coverage (patch by ris, modified)

see https://github.com/openstreetmap/josm/pull/24/commits for details

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/TestUtils.java

    r13742 r14052  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertArrayEquals;
     6import static org.junit.Assert.assertTrue;
    57import static org.junit.Assert.fail;
    68
     
    911import java.awt.Graphics2D;
    1012import java.io.File;
     13import java.io.FileInputStream;
    1114import java.io.IOException;
    1215import java.io.InputStream;
     
    2326import java.util.Comparator;
    2427import java.util.Objects;
     28import java.util.concurrent.ExecutionException;
     29import java.util.concurrent.ThreadPoolExecutor;
    2530import java.util.stream.Stream;
    2631
     
    3439import org.openstreetmap.josm.data.osm.RelationMember;
    3540import org.openstreetmap.josm.data.osm.Way;
     41import org.openstreetmap.josm.gui.MainApplication;
    3642import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
    3743import org.openstreetmap.josm.gui.progress.CancelHandler;
    3844import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3945import org.openstreetmap.josm.gui.progress.ProgressTaskId;
     46import org.openstreetmap.josm.gui.util.GuiHelper;
    4047import org.openstreetmap.josm.io.Compression;
    4148import org.openstreetmap.josm.testutils.FakeGraphics;
     
    4552import com.github.tomakehurst.wiremock.WireMockServer;
    4653import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
     54
     55import com.google.common.io.ByteStreams;
    4756
    4857import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    429438        return getHTTPDate(Instant.ofEpochMilli(time));
    430439    }
     440
     441    /**
     442     * Throws AssertionError if contents of both files are not equal
     443     * @param fileA File A
     444     * @param fileB File B
     445     */
     446    public static void assertFileContentsEqual(final File fileA, final File fileB) {
     447        assertTrue(fileA.exists());
     448        assertTrue(fileA.canRead());
     449        assertTrue(fileB.exists());
     450        assertTrue(fileB.canRead());
     451        try {
     452            try (
     453                FileInputStream streamA = new FileInputStream(fileA);
     454                FileInputStream streamB = new FileInputStream(fileB);
     455            ) {
     456                assertArrayEquals(
     457                    ByteStreams.toByteArray(streamA),
     458                    ByteStreams.toByteArray(streamB)
     459                );
     460            }
     461        } catch (IOException e) {
     462            fail(e.toString());
     463        }
     464    }
     465
     466    /**
     467     * Waits until any asynchronous operations launched by the test on the EDT or worker threads have
     468     * (almost certainly) completed.
     469     */
     470    public static void syncEDTAndWorkerThreads() {
     471        boolean workerQueueEmpty = false;
     472        while (!workerQueueEmpty) {
     473            try {
     474                // once our own task(s) have made it to the front of their respective queue(s),
     475                // they're both executing at the same time and we know there aren't any outstanding
     476                // worker tasks, then presumably the only way there could be incomplete operations
     477                // is if the EDT had launched a deferred task to run on itself or perhaps set up a
     478                // swing timer - neither are particularly common patterns in JOSM (?)
     479                //
     480                // there shouldn't be a risk of creating a deadlock in doing this as there shouldn't
     481                // (...couldn't?) be EDT operations waiting on the results of a worker task.
     482                workerQueueEmpty = MainApplication.worker.submit(
     483                    () -> GuiHelper.runInEDTAndWaitAndReturn(
     484                        () -> ((ThreadPoolExecutor) MainApplication.worker).getQueue().isEmpty()
     485                    )
     486                ).get();
     487            } catch (InterruptedException | ExecutionException e) {
     488                // inconclusive - retry...
     489                workerQueueEmpty = false;
     490            }
     491        }
     492    }
    431493}
Note: See TracChangeset for help on using the changeset viewer.