diff --git a/test/functional/org/openstreetmap/josm/io/OsmServerBackreferenceReaderTest.java b/test/functional/org/openstreetmap/josm/io/OsmServerBackreferenceReaderTest.java index 15cca85efe5..c78c814185f 100644 --- a/test/functional/org/openstreetmap/josm/io/OsmServerBackreferenceReaderTest.java +++ b/test/functional/org/openstreetmap/josm/io/OsmServerBackreferenceReaderTest.java @@ -11,11 +11,11 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.text.MessageFormat; import java.util.HashSet; import java.util.Locale; @@ -25,7 +25,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.openstreetmap.josm.JOSMFixture; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.APIDataSet; import org.openstreetmap.josm.data.coor.LatLon; @@ -42,6 +41,8 @@ import org.openstreetmap.josm.data.projection.Projections; import org.openstreetmap.josm.gui.progress.NullProgressMonitor; import org.openstreetmap.josm.spi.preferences.Config; +import org.openstreetmap.josm.testutils.annotations.TestUser; +import org.openstreetmap.josm.tools.JosmRuntimeException; import org.openstreetmap.josm.tools.Logging; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -51,6 +52,8 @@ * @since 1806 */ @SuppressFBWarnings(value = "CRLF_INJECTION_LOGS") +@org.openstreetmap.josm.testutils.annotations.OsmApi(org.openstreetmap.josm.testutils.annotations.OsmApi.APIType.DEV) +@TestUser class OsmServerBackreferenceReaderTest { private static final Logger logger = Logger.getLogger(OsmServerBackreferenceReader.class.getName()); @@ -75,7 +78,7 @@ protected static Relation lookupRelation(DataSet ds, int i) { if (("relation-" + i).equals(r.get("name"))) return r; } fail("Cannot find relation "+i); - return null; + throw new JosmRuntimeException("Cannot reach this point due to fail() above"); } protected static void populateTestDataSetWithNodes(DataSet ds) { @@ -165,8 +168,6 @@ public static void setUpBeforeClass() throws OsmTransferException, CyclicUploadD } logger.info("initializing ..."); - JOSMFixture.createFunctionalTestFixture().init(); - Config.getPref().put("osm-server.auth-method", "basic"); // don't use atomic upload, the test API server can't cope with large diff uploads @@ -196,7 +197,7 @@ public static void setUpBeforeClass() throws OsmTransferException, CyclicUploadD try ( PrintWriter pw = new PrintWriter( - new OutputStreamWriter(new FileOutputStream(dataSetCacheOutputFile), StandardCharsets.UTF_8) + new OutputStreamWriter(Files.newOutputStream(dataSetCacheOutputFile.toPath()), StandardCharsets.UTF_8) )) { logger.info(MessageFormat.format("caching test data set in ''{0}'' ...", dataSetCacheOutputFile.toString())); try (OsmWriter w = new OsmWriter(pw, false, testDataSet.getVersion())) { diff --git a/test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSourceFilterTest.java b/test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSourceFilterPerformanceTest.java similarity index 99% rename from test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSourceFilterTest.java rename to test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSourceFilterPerformanceTest.java index c0ab9c9dcd8..c213a38c169 100644 --- a/test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSourceFilterTest.java +++ b/test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSourceFilterPerformanceTest.java @@ -18,7 +18,7 @@ */ @Projection @Timeout(value = 15, unit = TimeUnit.MINUTES) -class MapCSSStyleSourceFilterTest { +class MapCSSStyleSourceFilterPerformanceTest { private static final int TEST_RULE_COUNT = 10000; diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java index a7beae44cfe..923a6fdc69a 100644 --- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java +++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java @@ -5,26 +5,17 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import org.junit.jupiter.api.extension.RegisterExtension; -import org.openstreetmap.josm.testutils.JOSMTestRules; import org.openstreetmap.josm.testutils.annotations.BasicWiremock; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import com.github.tomakehurst.wiremock.WireMockServer; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Superclass of {@link DownloadGpsTaskTest}, {@link DownloadOsmTaskTest} and {@link DownloadNotesTaskTest}. */ +@BasicWiremock +@HTTPS public abstract class AbstractDownloadTaskTestParent { - - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - JOSMTestRules test = new JOSMTestRules().https(); - /** * HTTP mock. */ diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTaskTest.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTaskTest.java index 6dd3b19c05b..336af88644a 100644 --- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTaskTest.java +++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTaskTest.java @@ -9,12 +9,10 @@ import org.junit.jupiter.api.Test; import org.openstreetmap.josm.data.gpx.GpxData; -import org.openstreetmap.josm.testutils.annotations.BasicWiremock; /** * Unit tests for class {@link DownloadGpsTask}. */ -@BasicWiremock class DownloadGpsTaskTest extends AbstractDownloadTaskTestParent { /** diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadNotesTaskTest.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadNotesTaskTest.java index 929fd3fa1a9..4b3ae55b2d0 100644 --- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadNotesTaskTest.java +++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadNotesTaskTest.java @@ -9,12 +9,10 @@ import org.junit.jupiter.api.Test; import org.openstreetmap.josm.data.osm.NoteData; -import org.openstreetmap.josm.testutils.annotations.BasicWiremock; /** * Unit tests for class {@link DownloadNotesTask}. */ -@BasicWiremock class DownloadNotesTaskTest extends AbstractDownloadTaskTestParent { /** diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskTest.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskTest.java index d5c514c34ac..fc6c88119ec 100644 --- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskTest.java +++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskTest.java @@ -9,12 +9,10 @@ import org.junit.jupiter.api.Test; import org.openstreetmap.josm.data.osm.DataSet; -import org.openstreetmap.josm.testutils.annotations.BasicWiremock; /** * Unit tests for class {@link DownloadOsmTask}. */ -@BasicWiremock class DownloadOsmTaskTest extends AbstractDownloadTaskTestParent { /** diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java index 1407387e925..758fdf0430c 100644 --- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java +++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java @@ -12,36 +12,21 @@ import java.util.Collections; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.Preferences; import org.openstreetmap.josm.gui.progress.NullProgressMonitor; import org.openstreetmap.josm.plugins.PluginDownloadTask; import org.openstreetmap.josm.plugins.PluginInformation; -import org.openstreetmap.josm.testutils.JOSMTestRules; -import org.openstreetmap.josm.testutils.annotations.BasicPreferences; -import org.openstreetmap.josm.testutils.annotations.BasicWiremock; +import org.openstreetmap.josm.testutils.annotations.AssumeRevision; import org.openstreetmap.josm.tools.Utils; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Unit tests for class {@link PluginDownloadTask}. */ -@BasicWiremock -@BasicPreferences +@AssumeRevision("Revision: 8000\n") class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent { protected String pluginPath; - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules testRule = new JOSMTestRules().https().assumeRevision( - "Revision: 8000\n" - ); - @Override protected String getRemoteContentType() { return "application/java-archive"; diff --git a/test/unit/org/openstreetmap/josm/data/oauth/SignpostAdaptersTest.java b/test/unit/org/openstreetmap/josm/data/oauth/SignpostAdaptersTest.java index 0bfb7f6a152..5c1375a37e7 100644 --- a/test/unit/org/openstreetmap/josm/data/oauth/SignpostAdaptersTest.java +++ b/test/unit/org/openstreetmap/josm/data/oauth/SignpostAdaptersTest.java @@ -11,28 +11,20 @@ import java.net.URL; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.data.oauth.SignpostAdapters.HttpRequest; import org.openstreetmap.josm.data.oauth.SignpostAdapters.HttpResponse; import org.openstreetmap.josm.data.oauth.SignpostAdapters.OAuthConsumer; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.tools.HttpClient; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import net.trajano.commons.testing.UtilityClassTestUtil; /** * Unit tests for class {@link SignpostAdapters}. */ +@HTTPS class SignpostAdaptersTest { - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().https(); - private static HttpClient newClient() throws MalformedURLException { return HttpClient.create(new URL("https://www.openstreetmap.org")); } diff --git a/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java b/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java index e3fe5cb5dbd..84943267eb7 100644 --- a/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java +++ b/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java @@ -45,9 +45,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.Test; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.tools.Logging; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -57,15 +56,8 @@ * * @version $Revision: 1723861 $ */ +@HTTPS class DomainValidatorTestIT { - - /** - * Setup rule - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().https(); - /** * Download and process local copy of http://data.iana.org/TLD/tlds-alpha-by-domain.txt * Check if the internal TLD table is up to date diff --git a/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java b/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java index f513ba78c6a..8ee0f7927cc 100644 --- a/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java +++ b/test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java @@ -34,12 +34,10 @@ import javax.swing.UIManager; import javax.swing.plaf.metal.MetalLookAndFeel; -import mockit.Mock; -import mockit.MockUp; import org.awaitility.Awaitility; import org.awaitility.Durations; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.Timeout; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.actions.JosmAction; import org.openstreetmap.josm.actions.OpenLocationAction; @@ -56,26 +54,28 @@ import org.openstreetmap.josm.plugins.PluginListParseException; import org.openstreetmap.josm.plugins.PluginListParser; import org.openstreetmap.josm.spi.preferences.Config; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.HTTPS; +import org.openstreetmap.josm.testutils.annotations.Main; +import org.openstreetmap.josm.testutils.annotations.OsmApi; +import org.openstreetmap.josm.testutils.annotations.Projection; import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.PlatformManager; import org.openstreetmap.josm.tools.Shortcut; +import org.openstreetmap.josm.tools.bugreport.BugReportQueue; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import org.openstreetmap.josm.tools.bugreport.BugReportQueue; +import mockit.Mock; +import mockit.MockUp; /** * Unit tests of {@link MainApplication} class. */ +@HTTPS +@Main +@OsmApi(OsmApi.APIType.DEV) +@Projection +@Timeout(20) public class MainApplicationTest { - - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().main().projection().https().devAPI().timeout(20000); - /** * Make sure {@link MainApplication#contentPanePrivate} is initialized. */ @@ -248,7 +248,7 @@ private static PluginInformation newPluginInformation(String plugin) throws Plug @Test void testPostConstructorProcessCmdLineEmpty() { // Check the method accepts no arguments - MainApplication.postConstructorProcessCmdLine(new ProgramArguments()); + assertDoesNotThrow(() -> MainApplication.postConstructorProcessCmdLine(new ProgramArguments())); } private static void doTestPostConstructorProcessCmdLine(String download, String downloadGps, boolean gpx) { diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java index f8c8b23c2c1..bba287653ae 100644 --- a/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java +++ b/test/unit/org/openstreetmap/josm/gui/dialogs/MinimapDialogTest.java @@ -27,15 +27,11 @@ import javax.swing.JMenuItem; import javax.swing.JPopupMenu; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.awaitility.Awaitility; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.Bounds; import org.openstreetmap.josm.data.DataSource; -import org.openstreetmap.josm.data.SystemOfMeasurement; import org.openstreetmap.josm.data.imagery.ImageryInfo; import org.openstreetmap.josm.data.imagery.ImageryLayerInfo; import org.openstreetmap.josm.data.osm.DataSet; @@ -51,31 +47,25 @@ import org.openstreetmap.josm.gui.util.GuiHelper; import org.openstreetmap.josm.spi.preferences.Config; import org.openstreetmap.josm.testutils.ImagePatternMatching; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.FakeImagery; +import org.openstreetmap.josm.testutils.annotations.Main; +import org.openstreetmap.josm.testutils.annotations.MeasurementSystem; +import org.openstreetmap.josm.testutils.annotations.Projection; /** * Unit tests of {@link MinimapDialog} class. */ -public class MinimapDialogTest { - - /** - * Setup tests - */ - @Rule - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules josmTestRules = new JOSMTestRules().main().projection().fakeImagery(); - - @Before - public void beforeAll() { - // Needed since testShowDownloadedAreaLayerSwitching expects the measurement to be imperial - SystemOfMeasurement.setSystemOfMeasurement(SystemOfMeasurement.IMPERIAL); - } - +@FakeImagery +@Main +@Projection +// Needed since testShowDownloadedAreaLayerSwitching expects the measurement to be imperial +@MeasurementSystem("Imperial") +class MinimapDialogTest { /** * Unit test of {@link MinimapDialog} class. */ @Test - public void testMinimapDialog() { + void testMinimapDialog() { MinimapDialog dlg = new MinimapDialog(); dlg.showDialog(); assertTrue(dlg.isVisible()); @@ -205,7 +195,7 @@ protected void paintSlippyMap() { * Tests to switch imagery source. */ @Test - public void testSourceSwitching() { + void testSourceSwitching() { // relevant prefs starting out empty, should choose the first source and have shown download area enabled // (not that there's a data layer for it to use) @@ -251,7 +241,7 @@ public void testSourceSwitching() { * Tests that the apparently-selected TileSource survives the tile sources being refreshed. */ @Test - public void testRefreshSourcesRetainsSelection() { + void testRefreshSourcesRetainsSelection() { // relevant prefs starting out empty, should choose the first source and have shown download area enabled // (not that there's a data layer for it to use) @@ -288,7 +278,7 @@ public void testRefreshSourcesRetainsSelection() { * selected in the source menu even after the tile sources have been refreshed. */ @Test - public void testRemovedSourceStillSelected() { + void testRemovedSourceStillSelected() { // relevant prefs starting out empty, should choose the first source and have shown download area enabled // (not that there's a data layer for it to use) @@ -320,7 +310,7 @@ public void testRemovedSourceStillSelected() { * Tests the tile source list includes sources only present in the LayerManager */ @Test - public void testTileSourcesFromCurrentLayers() { + void testTileSourcesFromCurrentLayers() { // relevant prefs starting out empty, should choose the first (ImageryLayerInfo) source and have shown download area enabled // (not that there's a data layer for it to use) @@ -453,7 +443,7 @@ public void testTileSourcesFromCurrentLayers() { * Tests minimap obeys a saved "mapstyle" preference on startup. */ @Test - public void testSourcePrefObeyed() { + void testSourcePrefObeyed() { Config.getPref().put("slippy_map_chooser.mapstyle", "Green Tiles"); this.setUpMiniMap(); @@ -479,7 +469,7 @@ public void testSourcePrefObeyed() { * Tests minimap handles an unrecognized "mapstyle" preference on startup */ @Test - public void testSourcePrefInvalid() { + void testSourcePrefInvalid() { Config.getPref().put("slippy_map_chooser.mapstyle", "Hooloovoo Tiles"); this.setUpMiniMap(); @@ -500,7 +490,7 @@ public void testSourcePrefInvalid() { * test viewport marker rectangle matches the mapView's aspect ratio */ @Test - public void testViewportAspectRatio() { + void testViewportAspectRatio() { // Add a test layer to the layer manager to get the MapFrame & MapView MainApplication.getLayerManager().addLayer(new TestLayer()); @@ -639,7 +629,7 @@ protected JCheckBoxMenuItem getShowDownloadedAreaMenuItem() { * test downloaded area is shown shaded */ @Test - public void testShowDownloadedArea() { + void testShowDownloadedArea() { Config.getPref().put("slippy_map_chooser.mapstyle", "Green Tiles"); Config.getPref().putBoolean("slippy_map_chooser.show_downloaded_area", false); @@ -797,7 +787,7 @@ public void testShowDownloadedArea() { * test display of downloaded area follows active layer switching */ @Test - public void testShowDownloadedAreaLayerSwitching() { + void testShowDownloadedAreaLayerSwitching() { Config.getPref().put("slippy_map_chooser.mapstyle", "Green Tiles"); Config.getPref().putBoolean("slippy_map_chooser.show_downloaded_area", true); diff --git a/test/unit/org/openstreetmap/josm/gui/dialogs/layer/CycleLayerActionTest.java b/test/unit/org/openstreetmap/josm/gui/dialogs/layer/CycleLayerActionTest.java index d0ad37a9a34..b2b6c0100c0 100644 --- a/test/unit/org/openstreetmap/josm/gui/dialogs/layer/CycleLayerActionTest.java +++ b/test/unit/org/openstreetmap/josm/gui/dialogs/layer/CycleLayerActionTest.java @@ -4,9 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.openstreetmap.josm.tools.I18n.tr; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.openstreetmap.josm.data.imagery.ImageryInfo; import org.openstreetmap.josm.data.imagery.ImageryLayerInfo; import org.openstreetmap.josm.data.osm.DataSet; @@ -14,21 +13,19 @@ import org.openstreetmap.josm.gui.layer.ImageryLayer; import org.openstreetmap.josm.gui.layer.MainLayerManager; import org.openstreetmap.josm.gui.layer.OsmDataLayer; -import org.openstreetmap.josm.testutils.JOSMTestRules; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openstreetmap.josm.testutils.annotations.FakeImagery; +import org.openstreetmap.josm.testutils.annotations.Main; +import org.openstreetmap.josm.testutils.annotations.Projection; /** * Test class for {@link CycleLayerDownAction} * * @author Taylor Smock */ -public class CycleLayerActionTest { - /** Layers need a projection */ - @Rule - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().main().preferences().projection().fakeImagery(); - +@FakeImagery +@Main +@Projection +class CycleLayerActionTest { private CycleLayerDownAction cycleDown; private CycleLayerUpAction cycleUp; private MainLayerManager manager; @@ -36,8 +33,8 @@ public class CycleLayerActionTest { /** * Set up common items (make layers, etc.) */ - @Before - public void setUp() { + @BeforeEach + void setUp() { cycleDown = new CycleLayerDownAction(); cycleUp = new CycleLayerUpAction(); manager = MainApplication.getLayerManager(); @@ -50,7 +47,7 @@ public void setUp() { * Test going down from the bottom */ @Test - public void testDownBottom() { + void testDownBottom() { manager.setActiveLayer(manager.getLayers().get(0)); cycleDown.actionPerformed(null); assertEquals(manager.getLayers().size() - 1, manager.getLayers().indexOf(manager.getActiveLayer())); @@ -60,7 +57,7 @@ public void testDownBottom() { * Check going up from the top */ @Test - public void testUpTop() { + void testUpTop() { manager.setActiveLayer(manager.getLayers().get(manager.getLayers().size() - 1)); cycleUp.actionPerformed(null); assertEquals(0, manager.getLayers().indexOf(manager.getActiveLayer())); @@ -70,7 +67,7 @@ public void testUpTop() { * Check going down */ @Test - public void testDown() { + void testDown() { manager.setActiveLayer(manager.getLayers().get(3)); cycleDown.actionPerformed(null); assertEquals(2, manager.getLayers().indexOf(manager.getActiveLayer())); @@ -80,7 +77,7 @@ public void testDown() { * Check going up */ @Test - public void testUp() { + void testUp() { manager.setActiveLayer(manager.getLayers().get(3)); cycleUp.actionPerformed(null); assertEquals(4, manager.getLayers().indexOf(manager.getActiveLayer())); @@ -90,7 +87,7 @@ public void testUp() { * Test no layers */ @Test - public void testNoLayers() { + void testNoLayers() { manager.getLayers().forEach(manager::removeLayer); cycleUp.actionPerformed(null); cycleDown.actionPerformed(null); @@ -101,7 +98,7 @@ public void testNoLayers() { * Test with an aerial imagery layer */ @Test - public void testWithAerialImagery() { + void testWithAerialImagery() { final ImageryInfo magentaTilesInfo = ImageryLayerInfo.instance.getLayers().stream() .filter(i -> i.getName().equals("Magenta Tiles")).findAny().get(); ImageryLayer imageryLayer = ImageryLayer.create(magentaTilesInfo); diff --git a/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java b/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java index 0eb9f795bbb..65d22bc4ca9 100644 --- a/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java +++ b/test/unit/org/openstreetmap/josm/gui/help/HelpBrowserTest.java @@ -9,15 +9,14 @@ import javax.swing.JOptionPane; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.TestUtils; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.FullPreferences; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; import org.openstreetmap.josm.tools.LanguageInfo.LocaleType; import org.openstreetmap.josm.tools.PlatformHook; import org.openstreetmap.josm.tools.PlatformManager; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import mockit.Expectations; import mockit.Injectable; import mockit.Mocked; @@ -25,19 +24,14 @@ /** * Unit tests of {@link HelpBrowser} class. */ +@FullPreferences +@HTTPS class HelpBrowserTest { static final String URL_1 = "https://josm.openstreetmap.de/wiki/Help"; static final String URL_2 = "https://josm.openstreetmap.de/wiki/Introduction"; static final String URL_3 = "https://josm.openstreetmap.de/javadoc"; - /** - * Setup tests - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - static JOSMTestRules test = new JOSMTestRules().preferences().https(); - static IHelpBrowser newHelpBrowser() { return new IHelpBrowser() { diff --git a/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java b/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java index 7f42a7cf1b5..980c9339fb0 100644 --- a/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java +++ b/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java @@ -22,7 +22,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.gpx.GpxData; import org.openstreetmap.josm.data.gpx.GpxTrack; @@ -35,24 +34,21 @@ import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.widgets.HtmlPanel; import org.openstreetmap.josm.io.GpxReaderTest; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.I18n; +import org.openstreetmap.josm.testutils.annotations.Main; +import org.openstreetmap.josm.testutils.annotations.MeasurementSystem; +import org.openstreetmap.josm.testutils.annotations.Projection; import org.openstreetmap.josm.tools.date.DateUtils; import org.xml.sax.SAXException; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Unit tests of {@link GpxLayer} class. */ +@I18n +@Main +@MeasurementSystem +@Projection public class GpxLayerTest { - - /** - * Setup tests - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().main().projection().i18n().metricSystem(); - /** * Setup test. */ diff --git a/test/unit/org/openstreetmap/josm/gui/layer/MainLayerManagerTest.java b/test/unit/org/openstreetmap/josm/gui/layer/MainLayerManagerTest.java index f1c905e6d83..af5807195e1 100644 --- a/test/unit/org/openstreetmap/josm/gui/layer/MainLayerManagerTest.java +++ b/test/unit/org/openstreetmap/josm/gui/layer/MainLayerManagerTest.java @@ -12,10 +12,8 @@ import java.util.logging.Handler; import java.util.logging.LogRecord; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.openstreetmap.josm.JOSMFixture; import org.openstreetmap.josm.data.osm.DataSet; import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent; import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener; @@ -61,7 +59,7 @@ public LayerPositionStrategy getDefaultLayerPosition() { protected static class LoggingHandler extends Handler { - private List records = new ArrayList<>(); + private final List records = new ArrayList<>(); @Override public void publish(LogRecord record) { @@ -80,11 +78,6 @@ public List getRecords() { } - @BeforeAll - public static void setUpClass() { - JOSMFixture.createUnitTestFixture().init(); - } - @Override @BeforeEach public void setUp() { @@ -152,8 +145,8 @@ void testAddActiveLayerChangeListener() { CapturingActiveLayerChangeListener listener2 = new CapturingActiveLayerChangeListener(); layerManagerWithActive.addAndFireActiveLayerChangeListener(listener2); - assertSame(listener2.lastEvent.getPreviousActiveLayer(), null); - assertSame(listener2.lastEvent.getPreviousDataLayer(), null); + assertNull(listener2.lastEvent.getPreviousActiveLayer()); + assertNull(listener2.lastEvent.getPreviousDataLayer()); layerManagerWithActive.setActiveLayer(layer1); assertSame(listener2.lastEvent.getPreviousActiveLayer(), layer2); diff --git a/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java b/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java index 5b69c60ce5a..b8fec9a16bb 100644 --- a/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java +++ b/test/unit/org/openstreetmap/josm/gui/layer/gpx/DownloadWmsAlongTrackActionTest.java @@ -10,38 +10,34 @@ import java.util.Collections; import org.awaitility.Awaitility; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.actions.MergeLayerActionTest.MergeLayerExtendedDialogMocker; import org.openstreetmap.josm.data.gpx.GpxData; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.layer.GpxLayerTest; import org.openstreetmap.josm.gui.layer.TMSLayer; import org.openstreetmap.josm.gui.layer.gpx.DownloadWmsAlongTrackAction.PrecacheWmsTask; -import org.openstreetmap.josm.TestUtils; -import org.openstreetmap.josm.testutils.JOSMTestRules; -import org.openstreetmap.josm.testutils.TileSourceRule; +import org.openstreetmap.josm.testutils.annotations.FakeImagery; +import org.openstreetmap.josm.testutils.annotations.Main; +import org.openstreetmap.josm.testutils.annotations.Projection; import org.openstreetmap.josm.testutils.mockers.JOptionPaneSimpleMocker; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Unit tests of {@link DownloadWmsAlongTrackAction} class. */ -public class DownloadWmsAlongTrackActionTest { - - /** - * Setup test. - */ - @Rule - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().main().projection().fakeImagery().timeout(20000); +@FakeImagery +@Main +@Projection +@Timeout(20) +class DownloadWmsAlongTrackActionTest { /** * Test action without layer. */ @Test - public void testNoLayer() { + void testNoLayer() { TestUtils.assumeWorkingJMockit(); final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( Collections.singletonMap("There are no imagery layers.", 0) @@ -60,15 +56,13 @@ public void testNoLayer() { * @throws Exception if an error occurs */ @Test - public void testTMSLayer() throws Exception { + void testTMSLayer(FakeImagery.FakeImageryWireMockExtension fakeImageryWireMockExtension) throws Exception { TestUtils.assumeWorkingJMockit(); final MergeLayerExtendedDialogMocker edMocker = new MergeLayerExtendedDialogMocker(); edMocker.getMockResultMap().put("Please select the imagery layer.", "Download"); - final TileSourceRule tileSourceRule = this.test.getTileSourceRule(); - final TMSLayer layer = new TMSLayer( - tileSourceRule.getSourcesList().get(0).getImageryInfo(tileSourceRule.port()) + fakeImageryWireMockExtension.getSourcesList().get(0).getImageryInfo(fakeImageryWireMockExtension.getRuntimeInfo().getHttpPort()) ); try { MainApplication.getLayerManager().addLayer(layer); diff --git a/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java b/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java index 21ce309d495..f3f6d151b29 100644 --- a/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java +++ b/test/unit/org/openstreetmap/josm/gui/layer/markerlayer/WebMarkerTest.java @@ -6,16 +6,14 @@ import java.net.URL; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.coor.LatLon; import org.openstreetmap.josm.data.gpx.GpxData; import org.openstreetmap.josm.data.gpx.WayPoint; -import org.openstreetmap.josm.testutils.JOSMTestRules; -import org.openstreetmap.josm.tools.PlatformManager; +import org.openstreetmap.josm.testutils.annotations.FullPreferences; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.tools.PlatformHook; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openstreetmap.josm.tools.PlatformManager; import mockit.Expectations; import mockit.Injectable; @@ -24,15 +22,9 @@ /** * Unit tests of {@link WebMarker} class. */ +@FullPreferences +@HTTPS class WebMarkerTest { - - /** - * Setup tests - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().preferences().https(); - /** * Unit test of {@link WebMarker#WebMarker}. * @param mockPlatformHook platform hook mock diff --git a/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTestIT.java b/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTestIT.java index 6db613fbe76..a6fe8021086 100644 --- a/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTestIT.java +++ b/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTestIT.java @@ -3,24 +3,16 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser; -import org.openstreetmap.josm.testutils.JOSMTestRules; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openstreetmap.josm.testutils.annotations.HTTPS; +import org.openstreetmap.josm.testutils.annotations.Projection; /** * Integration tests of {@link MapCSSParser}. */ +@HTTPS +@Projection class MapCSSParserTestIT { - - /** - * Setup rule - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().https().projection(); - /** * Checks Kothic stylesheets */ diff --git a/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java b/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java index da43f982bc4..a3cbf1cbe80 100644 --- a/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java +++ b/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; import org.junit.jupiter.params.ParameterizedTest; @@ -65,31 +65,28 @@ import org.openstreetmap.josm.data.projection.Projections; import org.openstreetmap.josm.io.imagery.ApiKeyProvider; import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.HTTPS; +import org.openstreetmap.josm.testutils.annotations.I18n; +import org.openstreetmap.josm.testutils.annotations.ProjectionNadGrids; import org.openstreetmap.josm.tools.HttpClient; import org.openstreetmap.josm.tools.HttpClient.Response; import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Utils; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Integration tests of {@link ImageryPreference} class. */ +@HTTPS +@I18n +@org.openstreetmap.josm.testutils.annotations.Projection +@ProjectionNadGrids +@Timeout(value = 40, unit = TimeUnit.MINUTES) public class ImageryPreferenceTestIT { private static final String ERROR_SEP = " -> "; private static final LatLon GREENWICH = new LatLon(51.47810, -0.00170); private static final int DEFAULT_ZOOM = 12; - /** - * Setup rule - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - static JOSMTestRules test = new JOSMTestRules().https().i18n().preferences().projection().projectionNadGrids() - .timeout((int) TimeUnit.MINUTES.toMillis(40)); - /** Entry to test */ private final Map>> errors = synchronizedMap(new TreeMap<>()); private final Map>> ignoredErrors = synchronizedMap(new TreeMap<>()); diff --git a/test/unit/org/openstreetmap/josm/gui/preferences/map/MapPaintPreferenceTestIT.java b/test/unit/org/openstreetmap/josm/gui/preferences/map/MapPaintPreferenceTestIT.java index e3e668c0a7a..e5587cc2d66 100644 --- a/test/unit/org/openstreetmap/josm/gui/preferences/map/MapPaintPreferenceTestIT.java +++ b/test/unit/org/openstreetmap/josm/gui/preferences/map/MapPaintPreferenceTestIT.java @@ -8,9 +8,10 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.Timeout; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.openstreetmap.josm.TestUtils; @@ -21,26 +22,18 @@ import org.openstreetmap.josm.gui.mappaint.StyleSource; import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction; import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.AssignmentInstruction; -import org.openstreetmap.josm.gui.preferences.AbstractExtendedSourceEntryTestCase; import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule; import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.gui.preferences.AbstractExtendedSourceEntryTestCase; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.tools.ImageProvider; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Integration tests of {@link MapPaintPreference} class. */ +@HTTPS +@Timeout(value = 15, unit = TimeUnit.MINUTES) class MapPaintPreferenceTestIT extends AbstractExtendedSourceEntryTestCase { - - /** - * Setup rule - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public static JOSMTestRules test = new JOSMTestRules().https().timeout(15000*60).parameters(); - /** * Setup test * @throws IOException in case of I/O error @@ -65,11 +58,10 @@ public static List data() throws Exception { * @param displayName displayed name * @param url URL * @param source source entry to test - * @throws Exception in case of error */ @ParameterizedTest(name = "{0} - {1}") @MethodSource("data") - void testStyleValidity(String displayName, String url, ExtendedSourceEntry source) throws Exception { + void testStyleValidity(String displayName, String url, ExtendedSourceEntry source) { assumeFalse(isIgnoredSubstring(source, source.url)); StyleSource style = MapPaintStyles.addStyle(source); if (style instanceof MapCSSStyleSource) { diff --git a/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java b/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java index 3e714bd3c88..5621029c780 100644 --- a/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java +++ b/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java @@ -14,9 +14,10 @@ import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.Timeout; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.openstreetmap.josm.TestUtils; @@ -27,27 +28,19 @@ import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetsTest; import org.openstreetmap.josm.gui.tagging.presets.items.Link; import org.openstreetmap.josm.spi.preferences.Config; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.tools.HttpClient; import org.openstreetmap.josm.tools.HttpClient.Response; import org.openstreetmap.josm.tools.ImageProvider; import org.openstreetmap.josm.tools.Logging; import org.xml.sax.SAXException; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Integration tests of {@link TaggingPresetPreference} class. */ +@HTTPS +@Timeout(value = 20, unit = TimeUnit.MINUTES) class TaggingPresetPreferenceTestIT extends AbstractExtendedSourceEntryTestCase { - - /** - * Setup rule - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public static JOSMTestRules test = new JOSMTestRules().https().timeout(10000*120).parameters(); - /** * Setup test * @throws IOException in case of I/O error @@ -77,11 +70,10 @@ public static List data() throws Exception { * @param displayName displayed name * @param url URL * @param source source entry to test - * @throws Exception in case of error */ @ParameterizedTest(name = "{0} - {1}") @MethodSource("data") - void testPresetsValidity(String displayName, String url, ExtendedSourceEntry source) throws Exception { + void testPresetsValidity(String displayName, String url, ExtendedSourceEntry source) { assumeFalse(isIgnoredSubstring(source, source.url)); List ignoredErrors = new ArrayList<>(); Set errors = new HashSet<>(); diff --git a/test/unit/org/openstreetmap/josm/gui/preferences/validator/ValidatorTagCheckerRulesPreferenceTestIT.java b/test/unit/org/openstreetmap/josm/gui/preferences/validator/ValidatorTagCheckerRulesPreferenceTestIT.java index 677cc110499..0be0991356a 100644 --- a/test/unit/org/openstreetmap/josm/gui/preferences/validator/ValidatorTagCheckerRulesPreferenceTestIT.java +++ b/test/unit/org/openstreetmap/josm/gui/preferences/validator/ValidatorTagCheckerRulesPreferenceTestIT.java @@ -13,7 +13,7 @@ import java.util.Set; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; import org.junit.jupiter.params.ParameterizedTest; @@ -23,22 +23,14 @@ import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker; import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.ParseResult; import org.openstreetmap.josm.gui.preferences.AbstractExtendedSourceEntryTestCase; -import org.openstreetmap.josm.testutils.JOSMTestRules; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openstreetmap.josm.testutils.annotations.HTTPS; /** * Integration tests of {@link ValidatorTagCheckerRulesPreference} class. */ +@HTTPS +@Timeout(20) class ValidatorTagCheckerRulesPreferenceTestIT extends AbstractExtendedSourceEntryTestCase { - - /** - * Setup rule - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - static JOSMTestRules test = new JOSMTestRules().https().timeout(20_000); - /** * Setup test * @throws IOException in case of I/O error diff --git a/test/unit/org/openstreetmap/josm/io/CertificateAmendmentTestIT.java b/test/unit/org/openstreetmap/josm/io/CertificateAmendmentTestIT.java index 7b73e5d5cd0..baaac538c4e 100644 --- a/test/unit/org/openstreetmap/josm/io/CertificateAmendmentTestIT.java +++ b/test/unit/org/openstreetmap/josm/io/CertificateAmendmentTestIT.java @@ -12,26 +12,18 @@ import javax.net.ssl.SSLHandshakeException; -import org.junit.ClassRule; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.openstreetmap.josm.TestUtils; -import org.openstreetmap.josm.testutils.JOSMTestRules; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openstreetmap.josm.testutils.annotations.HTTPS; /** * Integration tests of {@link CertificateAmendment} class. */ +@HTTPS +@Timeout(20) class CertificateAmendmentTestIT { - - /** - * Setup rule - */ - @ClassRule - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public static JOSMTestRules test = new JOSMTestRules().https().preferences().timeout(20000); - private static final List errorsToIgnore = new ArrayList<>(); /** diff --git a/test/unit/org/openstreetmap/josm/io/NetworkManagerTest.java b/test/unit/org/openstreetmap/josm/io/NetworkManagerTest.java index 22ddf1a6706..ef5248799f7 100644 --- a/test/unit/org/openstreetmap/josm/io/NetworkManagerTest.java +++ b/test/unit/org/openstreetmap/josm/io/NetworkManagerTest.java @@ -9,24 +9,20 @@ import java.net.URL; import java.util.Map; -import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.Test; -import org.openstreetmap.josm.testutils.JOSMTestRules; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openstreetmap.josm.testutils.annotations.HTTPS; +import org.openstreetmap.josm.testutils.annotations.Main; +import org.openstreetmap.josm.testutils.annotations.OsmApi; +import org.openstreetmap.josm.testutils.annotations.Projection; /** * Unit tests of {@link NetworkManager} class. */ +@HTTPS +@Main +@OsmApi(OsmApi.APIType.DEV) +@Projection class NetworkManagerTest { - - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().https().devAPI().main().projection(); - /** * Unit test of {@link NetworkManager#addNetworkError}, * {@link NetworkManager#getNetworkErrors} and diff --git a/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java b/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java index 583ab5f3933..ff41eb694ec 100644 --- a/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java +++ b/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java @@ -13,27 +13,20 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.spi.preferences.Config; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.AssertionsInEDT; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.tools.Utils; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Unit tests for Remote Control */ +@AssertionsInEDT +@HTTPS class RemoteControlTest { private String httpBase; - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().preferences().https().assertionsInEDT(); - /** * Starts Remote control before testing requests. * @throws GeneralSecurityException if a security error occurs @@ -64,7 +57,7 @@ void testHttpListOfCommands() throws Exception { private void testListOfCommands(String url) throws IOException, ReflectiveOperationException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.connect(); - assertEquals(connection.getResponseCode(), HttpURLConnection.HTTP_BAD_REQUEST); + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, connection.getResponseCode()); try (InputStream is = connection.getErrorStream()) { String responseBody = new String(Utils.readBytesFromStream(is), StandardCharsets.UTF_8); assert responseBody.contains(RequestProcessor.getUsageAsHtml()); diff --git a/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java b/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java index 539595bac09..b76b7dfb2f3 100644 --- a/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java +++ b/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java @@ -28,7 +28,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; -import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.platform.commons.util.ReflectionUtils; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.data.Preferences; @@ -40,8 +39,8 @@ import org.openstreetmap.josm.gui.layer.OsmDataLayer; import org.openstreetmap.josm.gui.progress.NullProgressMonitor; import org.openstreetmap.josm.spi.preferences.Config; -import org.openstreetmap.josm.testutils.JOSMTestRules; import org.openstreetmap.josm.testutils.annotations.BasicPreferences; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.testutils.annotations.Main; import org.openstreetmap.josm.testutils.annotations.Projection; import org.openstreetmap.josm.testutils.annotations.Territories; @@ -49,12 +48,11 @@ import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Utils; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Integration tests of {@link PluginHandler} class. */ @BasicPreferences +@HTTPS @Main @Projection @Territories @@ -62,12 +60,6 @@ public class PluginHandlerTestIT { private static final List errorsToIgnore = new ArrayList<>(); - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public static JOSMTestRules test = new JOSMTestRules().https(); /** * Setup test diff --git a/test/unit/org/openstreetmap/josm/spi/lifecycle/LifecycleTest.java b/test/unit/org/openstreetmap/josm/spi/lifecycle/LifecycleTest.java index 9c4fd038755..41225b98382 100644 --- a/test/unit/org/openstreetmap/josm/spi/lifecycle/LifecycleTest.java +++ b/test/unit/org/openstreetmap/josm/spi/lifecycle/LifecycleTest.java @@ -4,24 +4,20 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.Test; -import org.openstreetmap.josm.testutils.JOSMTestRules; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.openstreetmap.josm.testutils.annotations.HTTPS; +import org.openstreetmap.josm.testutils.annotations.Main; +import org.openstreetmap.josm.testutils.annotations.OsmApi; +import org.openstreetmap.josm.testutils.annotations.Projection; /** * Unit tests of {@link Lifecycle} class. */ +@HTTPS +@Main +@OsmApi(OsmApi.APIType.DEV) +@Projection class LifecycleTest { - - /** - * Setup test. - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().https().devAPI().main().projection(); - private static class InitStatusListenerStub implements InitStatusListener { boolean updated; diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java index f4b794a3617..33727e383a6 100644 --- a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java +++ b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java @@ -57,13 +57,13 @@ import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard; import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreferenceTestIT; import org.openstreetmap.josm.gui.util.GuiHelper; -import org.openstreetmap.josm.io.CertificateAmendment; import org.openstreetmap.josm.io.OsmApi; import org.openstreetmap.josm.io.OsmApiInitializationException; import org.openstreetmap.josm.io.OsmConnection; import org.openstreetmap.josm.io.OsmTransferCanceledException; import org.openstreetmap.josm.spi.preferences.Config; import org.openstreetmap.josm.spi.preferences.Setting; +import org.openstreetmap.josm.testutils.annotations.HTTPS; import org.openstreetmap.josm.testutils.annotations.JosmDefaults; import org.openstreetmap.josm.testutils.annotations.MapPaintStyles; import org.openstreetmap.josm.testutils.annotations.TaggingPresets; @@ -135,6 +135,7 @@ public JOSMTestRules noTimeout() { * Set a timeout for all tests in this class. Local method timeouts may only reduce this timeout. * @param millis The timeout duration in milliseconds. * @return this instance, for easy chaining + * @see org.junit.jupiter.api.Timeout */ public JOSMTestRules timeout(int millis) { timeout = isDebugMode() ? -1 : millis; @@ -144,6 +145,8 @@ public JOSMTestRules timeout(int millis) { /** * Enable the use of default preferences. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.BasicPreferences + * @see org.openstreetmap.josm.testutils.annotations.FullPreferences */ public JOSMTestRules preferences() { josmHome(); @@ -163,6 +166,7 @@ private JOSMTestRules josmHome() { /** * Enables the i18n module for this test in english. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.I18n */ public JOSMTestRules i18n() { return i18n("en"); @@ -172,6 +176,7 @@ public JOSMTestRules i18n() { * Enables the i18n module for this test. * @param language The language to use. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.I18n */ public JOSMTestRules i18n(String language) { i18n = language; @@ -182,6 +187,7 @@ public JOSMTestRules i18n(String language) { * Mock this test's assumed JOSM version (as reported by {@link Version}). * @param revisionProperties mock contents of JOSM's {@code REVISION} properties file * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.AssumeRevision */ public JOSMTestRules assumeRevision(final String revisionProperties) { this.assumeRevisionString = revisionProperties; @@ -191,6 +197,7 @@ public JOSMTestRules assumeRevision(final String revisionProperties) { /** * Enable the dev.openstreetmap.org API for this test. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.OsmApi.APIType#DEV */ public JOSMTestRules devAPI() { preferences(); @@ -201,6 +208,7 @@ public JOSMTestRules devAPI() { /** * Use the {@link FakeOsmApi} for testing. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.OsmApi.APIType#FAKE */ public JOSMTestRules fakeAPI() { useAPI = APIType.FAKE; @@ -210,6 +218,7 @@ public JOSMTestRules fakeAPI() { /** * Set up default projection (Mercator) * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.Projection */ public JOSMTestRules projection() { useProjection = true; @@ -219,6 +228,7 @@ public JOSMTestRules projection() { /** * Set up loading of NTV2 grit shift files to support projections that need them. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.ProjectionNadGrids */ public JOSMTestRules projectionNadGrids() { useProjectionNadGrids = true; @@ -228,6 +238,7 @@ public JOSMTestRules projectionNadGrids() { /** * Set up HTTPS certificates * @return this instance, for easy chaining + * @see HTTPS */ public JOSMTestRules https() { useHttps = true; @@ -246,6 +257,7 @@ public JOSMTestRules commands() { /** * Allow the memory manager to contain items after execution of the test cases. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.MemoryManagerLeaks */ public JOSMTestRules memoryManagerLeaks() { allowMemoryManagerLeaks = true; @@ -255,6 +267,7 @@ public JOSMTestRules memoryManagerLeaks() { /** * Use map styles in this test. * @return this instance, for easy chaining + * @see MapPaintStyles * @since 11777 */ public JOSMTestRules mapStyles() { @@ -266,6 +279,7 @@ public JOSMTestRules mapStyles() { /** * Use presets in this test. * @return this instance, for easy chaining + * @see TaggingPresets * @since 12568 */ public JOSMTestRules presets() { @@ -277,6 +291,7 @@ public JOSMTestRules presets() { /** * Use boundaries dataset in this test. * @return this instance, for easy chaining + * @see Territories * @since 12545 */ public JOSMTestRules territories() { @@ -299,6 +314,7 @@ public JOSMTestRules rlTraffic() { /** * Force metric measurement system. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.MeasurementSystem * @since 15400 */ public JOSMTestRules metricSystem() { @@ -309,6 +325,7 @@ public JOSMTestRules metricSystem() { /** * Re-raise AssertionErrors thrown in the EDT where they would have normally been swallowed. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.AssertionsInEDT */ public JOSMTestRules assertionsInEDT() { return this.assertionsInEDT(EDTAssertionMocker::new); @@ -319,6 +336,7 @@ public JOSMTestRules assertionsInEDT() { * @param edtAssertionMockingRunnable Runnable for initializing this functionality * * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.AssertionsInEDT */ public JOSMTestRules assertionsInEDT(final Runnable edtAssertionMockingRunnable) { this.edtAssertionMockingRunnable = edtAssertionMockingRunnable; @@ -329,6 +347,7 @@ public JOSMTestRules assertionsInEDT(final Runnable edtAssertionMockingRunnable) * Replace imagery sources with a default set of mock tile sources * * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.FakeImagery */ public JOSMTestRules fakeImagery() { return this.fakeImagery( @@ -349,6 +368,7 @@ public JOSMTestRules fakeImagery() { * @param tileSourceRule Tile source rule * * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.FakeImagery */ public JOSMTestRules fakeImagery(TileSourceRule tileSourceRule) { this.preferences(); @@ -360,6 +380,7 @@ public JOSMTestRules fakeImagery(TileSourceRule tileSourceRule) { * Use the {@code Main#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel}, * global variables in this test. * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.Main * @since 12557 */ public JOSMTestRules main() { @@ -378,6 +399,7 @@ public JOSMTestRules main() { * of {@link org.openstreetmap.josm.gui.NavigatableComponent}, null to skip. * * @return this instance, for easy chaining + * @see org.openstreetmap.josm.testutils.annotations.AssertionsInEDT */ public JOSMTestRules main( final Runnable mapViewStateMockingRunnable, @@ -560,7 +582,7 @@ protected void before() throws InitializationError, ReflectiveOperationException if (useHttps) { try { - CertificateAmendment.addMissingCertificates(); + new HTTPS.HTTPSExtension().beforeEach(null); } catch (IOException | GeneralSecurityException ex) { throw new JosmRuntimeException(ex); } diff --git a/test/unit/org/openstreetmap/josm/testutils/annotations/BasicWiremock.java b/test/unit/org/openstreetmap/josm/testutils/annotations/BasicWiremock.java index 2abe8efff94..3e677613098 100644 --- a/test/unit/org/openstreetmap/josm/testutils/annotations/BasicWiremock.java +++ b/test/unit/org/openstreetmap/josm/testutils/annotations/BasicWiremock.java @@ -7,6 +7,7 @@ import java.lang.annotation.Documented; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -50,6 +51,7 @@ * @see OsmApiExtension (this sets the Osm Api to the wiremock URL) * @since 18106 */ +@Inherited @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) diff --git a/test/unit/org/openstreetmap/josm/testutils/annotations/FakeImagery.java b/test/unit/org/openstreetmap/josm/testutils/annotations/FakeImagery.java new file mode 100644 index 00000000000..1af565661ee --- /dev/null +++ b/test/unit/org/openstreetmap/josm/testutils/annotations/FakeImagery.java @@ -0,0 +1,240 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.testutils.annotations; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static org.openstreetmap.josm.TestUtils.getPrivateStaticField; + +import java.awt.Color; +import java.lang.annotation.Documented; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.openstreetmap.josm.data.imagery.ImageryInfo; +import org.openstreetmap.josm.data.imagery.ImageryLayerInfo; +import org.openstreetmap.josm.gui.bbox.JosmMapViewer; +import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser; +import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.TileSourceRule; +import org.openstreetmap.josm.tools.Logging; + +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; + +/** + * Replace imagery sources with a default set of mock tile sources. + * @author Taylor Smock + * @since 18893 + * @see JOSMTestRules#fakeImagery() + */ +@Inherited +@Documented +@Retention(RUNTIME) +@Target(TYPE) +@BasicPreferences +@ExtendWith(FakeImagery.FakeImageryExtension.class) +public @interface FakeImagery { + /** + * This is a stop-gap for WireMock #1981. + * We just wrap everything. + */ + class FakeImageryExtension implements ParameterResolver, + BeforeEachCallback, + BeforeAllCallback, + AfterEachCallback, + AfterAllCallback { + + @Override + public void afterAll(ExtensionContext extensionContext) throws Exception { + getActualExtension(extensionContext).afterAll(extensionContext); + } + + @Override + public void afterEach(ExtensionContext extensionContext) throws Exception { + final FakeImageryWireMockExtension extension = getActualExtension(extensionContext); + extension.afterEach(extensionContext); + extension.onAfterEach(extensionContext, getWireMockRuntimeInfo(extensionContext)); + } + + @Override + public void beforeAll(ExtensionContext extensionContext) throws Exception { + getActualExtension(extensionContext).beforeAll(extensionContext); + } + + @Override + public void beforeEach(ExtensionContext extensionContext) throws Exception { + final FakeImageryWireMockExtension extension = getActualExtension(extensionContext); + extension.beforeEach(extensionContext); + extension.onBeforeEach(extensionContext, getWireMockRuntimeInfo(extensionContext)); + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + if (parameterContext.getParameter().getType().equals(FakeImageryWireMockExtension.class)) { + return true; + } + return getActualExtension(extensionContext).supportsParameter(parameterContext, extensionContext); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + if (parameterContext.getParameter().getType().equals(FakeImageryWireMockExtension.class)) { + return getActualExtension(extensionContext); + } + return getActualExtension(extensionContext).resolveParameter(parameterContext, extensionContext); + } + + private static FakeImageryWireMockExtension getActualExtension(ExtensionContext extensionContext) { + return FakeImageryWireMockExtension.getStore(extensionContext) + .getOrComputeIfAbsent(FakeImageryWireMockExtension.class, ignored -> new FakeImageryWireMockExtension(), + FakeImageryWireMockExtension.class); + } + + private static WireMockRuntimeInfo getWireMockRuntimeInfo(ExtensionContext extensionContext) { + return FakeImageryWireMockExtension.getStore(extensionContext) + .getOrComputeIfAbsent(WireMockRuntimeInfo.class, ignored -> getActualExtension(extensionContext).getRuntimeInfo(), + WireMockRuntimeInfo.class); + + } + } + + class FakeImageryWireMockExtension extends WireMockExtension { + + private final boolean clearLayerList; + private final boolean clearSlippyMapSources; + private final boolean registerInLayerList; + private final List sources; + + static ExtensionContext.Store getStore(ExtensionContext extensionContext) { + return extensionContext.getStore(ExtensionContext.Namespace.create(FakeImageryWireMockExtension.class)); + } + + /** + * See {@link FakeImageryWireMockExtension#FakeImageryWireMockExtension(boolean, boolean, boolean, TileSourceRule.ConstSource...)}. + * This provides tile sources for that are white, black, magenta, or green. + */ + FakeImageryWireMockExtension() { + this( + true, + true, + true, + new TileSourceRule.ColorSource(Color.WHITE, "White Tiles", 256), + new TileSourceRule.ColorSource(Color.BLACK, "Black Tiles", 256), + new TileSourceRule.ColorSource(Color.MAGENTA, "Magenta Tiles", 256), + new TileSourceRule.ColorSource(Color.GREEN, "Green Tiles", 256) + ); + } + + /** + * Construct a FakeImageryWireMockExtension for use with a JUnit test. + *

+ * This is hidden for now, since all internal used {@link JOSMTestRules#fakeImagery()} instead of + * {@link JOSMTestRules#fakeImagery(TileSourceRule)}. Before making this public, we'll probably want to move + * {@link TileSourceRule.ConstSource} and it's subclasses around. + *

+ * The three boolean parameters control whether to perform various steps registering the tile sources with parts + * of JOSM's internals as part of the setup process. It is advised to only enable any of these if it can be ensured + * that this rule will have its setup routine executed *after* the relevant parts of JOSM have been set up, e.g. + * when handled by {@link FakeImagery}. + * + * @param clearLayerList whether to clear ImageryLayerInfo's layer list of any pre-existing entries + * @param clearSlippyMapSources whether to clear SlippyMapBBoxChooser's stubborn fallback Mapnik TileSource + * @param registerInLayerList whether to add sources to ImageryLayerInfo's layer list + * @param sources tile sources to serve from this mock server + */ + private FakeImageryWireMockExtension(boolean clearLayerList, boolean clearSlippyMapSources, boolean registerInLayerList, + TileSourceRule.ConstSource... sources) { + super(WireMockExtension.extensionOptions()); + this.clearLayerList = clearLayerList; + this.clearSlippyMapSources = clearSlippyMapSources; + this.registerInLayerList = registerInLayerList; + this.sources = Collections.unmodifiableList(Arrays.asList(sources.clone())); + } + + /** + * Get the tile sources served by this {@link FakeImageryWireMockExtension}. + * + * @return an unmodifiable list of the tile sources served by this {@link FakeImageryWireMockExtension} + */ + public List getSourcesList() { + return this.sources; + } + + protected void onBeforeEach(ExtensionContext extensionContext, WireMockRuntimeInfo wireMockRuntimeInfo) { + super.onBeforeEach(wireMockRuntimeInfo); + final ExtensionContext.Store store = getStore(extensionContext); + registerLayers(store, wireMockRuntimeInfo); + for (TileSourceRule.ConstSource source : this.sources) { + this.stubFor(source.getMappingBuilder().willReturn(source.getResponseDefinitionBuilder())); + } + } + + protected void onAfterEach(ExtensionContext extensionContext, WireMockRuntimeInfo wireMockRuntimeInfo) { + super.onAfterEach(wireMockRuntimeInfo); + final ExtensionContext.Store store = getStore(extensionContext); + unregisterLayers(store); + } + + private void registerLayers(ExtensionContext.Store store, WireMockRuntimeInfo wireMockRuntimeInfo) { + if (this.clearSlippyMapSources) { + try { + @SuppressWarnings("unchecked") + List slippyMapProviders = + (List) getPrivateStaticField( + SlippyMapBBoxChooser.class, + "providers" + ); + // pop this off the beginning of the list, keep for later + SlippyMapBBoxChooser.TileSourceProvider slippyMapDefaultProvider = slippyMapProviders.remove(0); + store.put("slippyMapProviders", slippyMapProviders); + store.put("slippyMapDefaultProvider", slippyMapDefaultProvider); + } catch (ReflectiveOperationException e) { + Logging.warn("Failed to remove default SlippyMapBBoxChooser TileSourceProvider"); + } + } + + if (this.clearLayerList) { + store.put("originalImageryInfoList", ImageryLayerInfo.instance.getLayers()); + ImageryLayerInfo.instance.clear(); + } + if (this.registerInLayerList) { + for (TileSourceRule.ConstSource source : this.sources) { + ImageryLayerInfo.addLayer(source.getImageryInfo(wireMockRuntimeInfo.getHttpPort())); + } + } + } + + private void unregisterLayers(ExtensionContext.Store store) { + @SuppressWarnings("unchecked") + final List slippyMapProviders = + (List) store.get("slippyMapProviders", List.class); + SlippyMapBBoxChooser.TileSourceProvider slippyMapDefaultProvider = + store.get("slippyMapDefaultProvider", JosmMapViewer.TileSourceProvider.class); + @SuppressWarnings("unchecked") + List originalImageryInfoList = (List) store.get("originalImageryInfoList", List.class); + // clean up to original state + if (slippyMapDefaultProvider != null && slippyMapProviders != null) { + slippyMapProviders.add(0, slippyMapDefaultProvider); + } + if (originalImageryInfoList != null) { + ImageryLayerInfo.instance.clear(); + ImageryLayerInfo.addLayers(originalImageryInfoList); + } + } + } +} diff --git a/test/unit/org/openstreetmap/josm/testutils/annotations/HTTPS.java b/test/unit/org/openstreetmap/josm/testutils/annotations/HTTPS.java new file mode 100644 index 00000000000..0b646081320 --- /dev/null +++ b/test/unit/org/openstreetmap/josm/testutils/annotations/HTTPS.java @@ -0,0 +1,43 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.testutils.annotations; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.io.IOException; +import java.lang.annotation.Documented; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import java.security.GeneralSecurityException; + +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.openstreetmap.josm.io.CertificateAmendment; +import org.openstreetmap.josm.testutils.JOSMTestRules; + +/** + * Set up the HTTPS certificates + * @author Taylor Smock + * @since 18893 + * @see JOSMTestRules#https() + */ +@Inherited +@Documented +@Retention(RUNTIME) +@Target(TYPE) +@BasicPreferences +@ExtendWith(HTTPS.HTTPSExtension.class) +public @interface HTTPS { + class HTTPSExtension implements BeforeEachCallback { + private static boolean initialized; + @Override + public void beforeEach(ExtensionContext extensionContext) throws IOException, GeneralSecurityException { + if (!initialized) { + CertificateAmendment.addMissingCertificates(); + initialized = true; + } + } + } +} diff --git a/test/unit/org/openstreetmap/josm/testutils/annotations/JosmDefaults.java b/test/unit/org/openstreetmap/josm/testutils/annotations/JosmDefaults.java index d285deccc45..4146eb450f6 100644 --- a/test/unit/org/openstreetmap/josm/testutils/annotations/JosmDefaults.java +++ b/test/unit/org/openstreetmap/josm/testutils/annotations/JosmDefaults.java @@ -64,7 +64,7 @@ public void beforeEach(ExtensionContext context) throws Exception { @Override public void afterEach(ExtensionContext context) throws Exception { - MemoryManagerTest.resetState(false); + MemoryManagerTest.resetState(AnnotationUtils.findFirstParentAnnotation(context, MemoryManagerLeaks.class).isPresent()); Window[] windows = Window.getWindows(); if (windows.length != 0) { diff --git a/test/unit/org/openstreetmap/josm/testutils/annotations/MeasurementSystem.java b/test/unit/org/openstreetmap/josm/testutils/annotations/MeasurementSystem.java new file mode 100644 index 00000000000..8f01fd3c3ba --- /dev/null +++ b/test/unit/org/openstreetmap/josm/testutils/annotations/MeasurementSystem.java @@ -0,0 +1,53 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.testutils.annotations; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.openstreetmap.josm.data.SystemOfMeasurement; +import org.openstreetmap.josm.testutils.JOSMTestRules; + +/** + * Set up the system of measurement + * @author Taylor Smock + * @since 18893 + * @see JOSMTestRules#metricSystem() + */ +@Inherited +@Documented +@Retention(RUNTIME) +@Target(TYPE) +@BasicPreferences +@ExtendWith(MeasurementSystem.SystemOfMeasurementExtension.class) +public @interface MeasurementSystem { + /** + * The measurement system to use. See {@link SystemOfMeasurement#ALL_SYSTEMS} for all currently implemented measurement systems. + * Currently known measurement systems are: + *

    + *
  • Chinese
  • + *
  • Imperial
  • + *
  • Metric
  • + *
  • Nautical Mile
  • + *
+ * @return The measurement system name. + */ + String value() default "Metric"; + + class SystemOfMeasurementExtension implements BeforeEachCallback { + @Override + public void beforeEach(ExtensionContext extensionContext) throws Exception { + final String system = AnnotationUtils.findFirstParentAnnotation(extensionContext, MeasurementSystem.class) + .map(MeasurementSystem::value) + .orElse(SystemOfMeasurement.METRIC.getName()); + SystemOfMeasurement.setSystemOfMeasurement(SystemOfMeasurement.ALL_SYSTEMS.get(system)); + } + } +} diff --git a/test/unit/org/openstreetmap/josm/testutils/annotations/MemoryManagerLeaks.java b/test/unit/org/openstreetmap/josm/testutils/annotations/MemoryManagerLeaks.java new file mode 100644 index 00000000000..cddda690b05 --- /dev/null +++ b/test/unit/org/openstreetmap/josm/testutils/annotations/MemoryManagerLeaks.java @@ -0,0 +1,27 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.testutils.annotations; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.openstreetmap.josm.testutils.JOSMTestRules; + +/** + * Allow the memory manager to contain items after execution of the test cases. + * @author Taylor Smock + * @since 18893 + * @see JOSMTestRules#memoryManagerLeaks() + */ +@Inherited +@Documented +@Retention(RUNTIME) +@Target({TYPE, METHOD}) +@BasicPreferences +public @interface MemoryManagerLeaks { +} diff --git a/test/unit/org/openstreetmap/josm/tools/MemoryManagerTest.java b/test/unit/org/openstreetmap/josm/tools/MemoryManagerTest.java index c47b23a2ba3..2bcad3d56c7 100644 --- a/test/unit/org/openstreetmap/josm/tools/MemoryManagerTest.java +++ b/test/unit/org/openstreetmap/josm/tools/MemoryManagerTest.java @@ -1,35 +1,26 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.tools; -import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.util.List; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.MemoryManagerLeaks; import org.openstreetmap.josm.tools.MemoryManager.MemoryHandle; import org.openstreetmap.josm.tools.MemoryManager.NotEnoughMemoryException; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - /** * Tests the {@link MemoryManager} class. * @author Michael Zangl */ +@MemoryManagerLeaks public class MemoryManagerTest { - /** - * Base test environment - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().memoryManagerLeaks(); - /** * Test {@link MemoryManager#allocateMemory(String, long, java.util.function.Supplier)} * @throws NotEnoughMemoryException if there is not enough memory diff --git a/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java b/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java index a77f57e97df..16d19e4f050 100644 --- a/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java +++ b/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java @@ -1,6 +1,7 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.tools; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -16,27 +17,18 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.spi.preferences.Config; -import org.openstreetmap.josm.testutils.JOSMTestRules; +import org.openstreetmap.josm.testutils.annotations.HTTPS; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import mockit.Expectations; import mockit.Mocked; /** * Unit tests of {@link PlatformHookWindows} class. */ +@HTTPS class PlatformHookWindowsTest { - - /** - * Setup tests - */ - @RegisterExtension - @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") - public JOSMTestRules test = new JOSMTestRules().preferences().https(); - static PlatformHookWindows hook; /** @@ -52,7 +44,9 @@ public static void setUp() { */ @Test void testStartupHook() { - hook.startupHook((a, b, c, d) -> System.out.println("java callback"), u -> System.out.println("webstart callback")); + final PlatformHook.JavaExpirationCallback javaCallback = (a, b, c, d) -> System.out.println("java callback"); + final PlatformHook.WebStartMigrationCallback webstartCallback = u -> System.out.println("webstart callback"); + assertDoesNotThrow(() -> hook.startupHook(javaCallback, webstartCallback)); } /** @@ -78,7 +72,7 @@ void testGetRootKeystore() throws Exception { */ @Test void testAfterPrefStartupHook() { - hook.afterPrefStartupHook(); + assertDoesNotThrow(hook::afterPrefStartupHook); } /** @@ -95,7 +89,7 @@ void testOpenUrlSuccess(@Mocked final Desktop mockDesktop) throws IOException { mockDesktop.browse(withNotNull()); times = 1; }}; - hook.openUrl(Config.getUrls().getJOSMWebsite()); + assertDoesNotThrow(() -> hook.openUrl(Config.getUrls().getJOSMWebsite())); } /** @@ -121,7 +115,7 @@ void testOpenUrlFallback(@Mocked final Desktop mockDesktop, @Mocked Runtime anyR anyRuntime.exec((String[]) withNotNull()); result = null; times = 0; }}; - hook.openUrl(Config.getUrls().getJOSMWebsite()); + assertDoesNotThrow(() -> hook.openUrl(Config.getUrls().getJOSMWebsite())); } /** @@ -195,6 +189,6 @@ void testGetOSDescription() { */ @Test void testInitSystemShortcuts() { - hook.initSystemShortcuts(); + assertDoesNotThrow(hook::initSystemShortcuts); } }