Tests: Better test isolation

Signed-off-by: Taylor Smock <tsmock@fb.com>
pull/1/head
Taylor Smock 2021-09-07 16:26:46 -06:00
rodzic a9c29cc8ae
commit a23e94be0d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
39 zmienionych plików z 594 dodań i 124 usunięć

Wyświetl plik

@ -38,6 +38,10 @@ tasks.withType(JavaCompile).configureEach {
}
}
rootProject.tasks.named("jar") {
duplicatesStrategy = 'include'
}
archivesBaseName = "mapwithai"
def gitlabGroup = "gokaart"
def gitlabRepositoryName = "JOSM_MapWithAI"

Wyświetl plik

@ -29,8 +29,10 @@ import org.openstreetmap.josm.tools.Territories;
import org.openstreetmap.josm.tools.Utils;
public class DataAvailability {
/** The default server URL */
public static final String DEFAULT_SERVER_URL = "https://gokaart.gitlab.io/JOSM_MapWithAI/json/sources.json";
/** This points to a list of default sources that can be used with MapWithAI */
private static String defaultServerUrl = "https://gokaart.gitlab.io/JOSM_MapWithAI/json/sources.json";
private static String defaultServerUrl = DEFAULT_SERVER_URL;
/** A map of tag -&gt; message of possible data types */
static final Map<String, String> POSSIBLE_DATA_POINTS = new TreeMap<>();

Wyświetl plik

@ -93,7 +93,7 @@ public class MapWithAILayerInfo {
synchronized (finished) {
while (!finished.get()) {
try {
finished.wait();
finished.wait(1000);
} catch (InterruptedException e) {
Logging.error(e);
Thread.currentThread().interrupt();
@ -290,6 +290,8 @@ public class MapWithAILayerInfo {
}
loadSource(source);
}
} catch (Exception e) {
Logging.error(e);
} finally {
if (preferences != null) {
// saveOnPut is pretty much always true

Wyświetl plik

@ -9,16 +9,22 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.extension.RegisterExtension;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.plugins.mapwithai.backend.DataAvailability;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIType;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
/**
* Integration test for {@link MapWithAISourceReader}
*
* @author Taylor Smock
*/
@NoExceptions
@BasicPreferences
class MapWithAISourceReaderTestIT {
@RegisterExtension
@ -27,6 +33,7 @@ class MapWithAISourceReaderTestIT {
@Test
void testDefaultSourceIT() throws IOException {
DataAvailability.setReleaseUrl(DataAvailability.DEFAULT_SERVER_URL);
try (MapWithAISourceReader source = new MapWithAISourceReader(DataAvailability.getReleaseUrl())) {
List<MapWithAIInfo> infoList = source.parse();
assertFalse(infoList.isEmpty(), "There should be viable sources");

Wyświetl plik

@ -13,6 +13,7 @@ import java.util.Map;
import javax.swing.JMenu;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.AfterEach;
@ -25,25 +26,27 @@ import org.openstreetmap.josm.plugins.PluginInformation;
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.MapWithAIPreferences;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
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.mockers.WindowMocker;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for {@link MapWithAIPlugin}
*
* @author Taylor Smock
*/
@MapWithAISources
@NoExceptions
@BasicPreferences
@Wiremock
@MapWithAISources
class MapWithAIPluginTest {
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
JOSMTestRules test = new MapWithAITestRules().wiremock().main().projection();
JOSMTestRules test = new MapWithAITestRules().main().projection();
PluginInformation info;
MapWithAIPlugin plugin;

Wyświetl plik

@ -22,6 +22,7 @@ import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
@ -30,11 +31,12 @@ import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
*
* @author Taylor Smock
*/
@MapWithAISources
@NoExceptions
@BasicPreferences
@MapWithAISources
class AddMapWithAILayerActionTest {
@RegisterExtension
JOSMTestRules rule = new MapWithAITestRules().wiremock().projection();
JOSMTestRules rule = new MapWithAITestRules().projection();
@Test
void testAddMapWithAILayerActionTest() {

Wyświetl plik

@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.Test;
@ -20,9 +21,8 @@ import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
/**
@ -30,12 +30,13 @@ import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
*
* @author Taylor Smock
*/
@MapWithAISources
@NoExceptions
@BasicPreferences
@MapWithAISources
class DownloadListenerTest {
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
JOSMTestRules rule = new MapWithAITestRules().wiremock().projection();
JOSMTestRules rule = new MapWithAITestRules().projection();
@Test
void testDataSourceChange() {

Wyświetl plik

@ -11,6 +11,7 @@ import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
@ -18,11 +19,11 @@ import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
@NoExceptions
@MapWithAISources
@BasicPreferences
class DownloadMapWithAITaskTest {

Wyświetl plik

@ -14,7 +14,9 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.TestUtils;
@ -29,31 +31,23 @@ import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Territories;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.Geometry;
import com.github.tomakehurst.wiremock.WireMockServer;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for {@link GetDataRunnable}
*
* @author Taylor Smock
*/
@NoExceptions
@MapWithAISources
@Territories
class GetDataRunnableTest {
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
JOSMTestRules rule = new MapWithAITestRules().wiremock().projection().fakeAPI().territories();
static String getDefaultMapWithAIAPIForTest(WireMockServer wireMock, String url) {
return getDefaultMapWithAIAPIForTest(wireMock, url, "https://www.mapwith.ai");
}
static String getDefaultMapWithAIAPIForTest(WireMockServer wireMock, String url, String wireMockReplace) {
return url.replace(wireMockReplace, wireMock.baseUrl());
}
JOSMTestRules rule = new MapWithAITestRules().projection().fakeAPI();
@Test
void testAddMissingElement() {
@ -99,7 +93,7 @@ class GetDataRunnableTest {
assertEquals(2, ds.getWays().parallelStream().filter(way -> !way.isDeleted()).count());
Node tNode = new Node(way1.lastNode(), true);
Node tNode = new Node(Objects.requireNonNull(way1.lastNode()), true);
ds.addPrimitive(tNode);
way2.addNode(tNode);
@ -107,6 +101,10 @@ class GetDataRunnableTest {
assertEquals(1, ds.getWays().parallelStream().filter(way -> !way.isDeleted()).count());
}
/**
* Non-regression test for GitLab #46: Data Integrity Issue (Node already
* deleted)
*/
@Test
void testRegressionTicket46() {
DataSet ds = new DataSet();

Wyświetl plik

@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.awaitility.Durations;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -24,18 +25,23 @@ import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.tools.Territories;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@MapWithAISources
/**
* Test class for {@link MapWithAIAction}
*
* @author Taylor Smock
*/
@NoExceptions
@BasicPreferences
@MapWithAISources
class MapWithAIActionTest {
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
JOSMTestRules test = new MapWithAITestRules().wiremock().main().projection().territories().timeout(100000);
JOSMTestRules test = new MapWithAITestRules().main().projection().territories().timeout(100000);
private MapWithAIAction action;

Wyświetl plik

@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.awaitility.Durations;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -18,23 +19,24 @@ import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.Territories;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for testing availability
*
* @author Taylor Smock
*/
@Wiremock
@MapWithAISources
@org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Territories
class MapWithAIAvailabilityTest {
private DataAvailability instance;
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
JOSMTestRules test = new MapWithAITestRules().wiremock().projection().territories();
JOSMTestRules test = new MapWithAITestRules().projection();
@BeforeEach
void setUp() {

Wyświetl plik

@ -14,6 +14,7 @@ import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.TestUtils;
@ -31,26 +32,28 @@ import org.openstreetmap.josm.gui.layer.GpxLayer;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.tools.Logging;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for {@link MapWithAIDataUtils}
*
* @author Taylor Smock
*/
@MapWithAISources
@NoExceptions
@BasicPreferences
@Wiremock
@MapWithAISources
public class MapWithAIDataUtilsTest {
/** This is the default MapWithAI URL */
private static final String DEFAULT_MAPWITHAI_API = "https://www.mapwith.ai/maps/ml_roads?conflate_with_osm=true&theme=ml_road_vector&collaborator=josm&token=ASb3N5o9HbX8QWn8G_NtHIRQaYv3nuG2r7_f3vnGld3KhZNCxg57IsaQyssIaEw5rfRNsPpMwg4TsnrSJtIJms5m&hash=ASawRla3rBcwEjY4HIY&bbox={bbox}";
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
JOSMTestRules test = new MapWithAITestRules().wiremock().main().projection().fakeAPI().territories();
JOSMTestRules test = new MapWithAITestRules().main().projection().fakeAPI().territories();
/**
* This gets data from MapWithAI. This test may fail if someone adds the data to

Wyświetl plik

@ -23,6 +23,7 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.awaitility.Durations;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@ -54,8 +55,6 @@ import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.tools.Territories;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for {@link MapWithAILayer}
*

Wyświetl plik

@ -13,6 +13,7 @@ import java.util.stream.Stream;
import javax.swing.text.JTextComponent;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -26,8 +27,6 @@ import org.openstreetmap.josm.gui.MapStatus;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.commands.MapWithAIAddCommand;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
/**

Wyświetl plik

@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openstreetmap.josm.tools.I18n.tr;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.awaitility.Durations;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -21,17 +22,17 @@ import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.tools.Utils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for {@link MapWithAIRemoteControl}
*
* @author Taylor Smock
*/
@NoExceptions
@BasicPreferences
@MapWithAISources
class MapWithAIRemoteControlTest {

Wyświetl plik

@ -14,6 +14,7 @@ import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -35,8 +36,6 @@ import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAIPluginMock;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
/**

Wyświetl plik

@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -20,8 +21,6 @@ import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Command;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@Command
class AddNodeToWayCommandTest {
private Node toAdd;

Wyświetl plik

@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Collection;
import java.util.Collections;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.TestUtils;
@ -17,10 +18,10 @@ import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@NoExceptions
class ConnectedCommandTest {
/**
* Setup test.

Wyświetl plik

@ -11,6 +11,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.TestUtils;
@ -28,8 +29,6 @@ import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Tests for {@link CreateConnections}
*

Wyświetl plik

@ -16,6 +16,8 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import mockit.Mock;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.Assertions;
@ -36,21 +38,18 @@ import org.openstreetmap.josm.data.validation.tests.SharpAngles;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.BleedTest;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Command;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MissingConnectionTagsMocker;
import org.openstreetmap.josm.plugins.mapwithai.testutils.PleaseWaitDialogMocker;
import org.openstreetmap.josm.plugins.mapwithai.testutils.SwingUtilitiesMocker;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.BleedTest;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Command;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.testutils.mockers.WindowMocker;
import org.openstreetmap.josm.tools.Logging;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import mockit.Mock;
/**
* Test class for {@link MapWithAIAddCommand}
*

Wyświetl plik

@ -20,6 +20,7 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.TestUtils;
@ -32,8 +33,6 @@ import org.openstreetmap.josm.plugins.mapwithai.backend.GetDataRunnable;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.Pair;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* @author Taylor Smock
*/

Wyświetl plik

@ -11,6 +11,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.TestUtils;
@ -24,8 +25,6 @@ import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Command;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@Command
class MovePrimitiveDataSetCommandTest {
@RegisterExtension

Wyświetl plik

@ -9,6 +9,7 @@ import static org.openstreetmap.josm.tools.I18n.tr;
import java.util.Arrays;
import java.util.Collection;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -24,8 +25,6 @@ import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.commands.MergeAddressBuildings;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
@org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Command

Wyświetl plik

@ -12,6 +12,7 @@ import java.util.Map;
import javax.swing.JOptionPane;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -26,13 +27,18 @@ import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.commands.cleanup.MissingConnectionTags;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MissingConnectionTagsMocker;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.WoundedTest;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.testutils.mockers.WindowMocker;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for {@link MissingConnectionTags}
*
* @author Taylor Smock
*/
@NoExceptions
@BasicPreferences
@org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Command
class MissingConnectionTagsTest {

Wyświetl plik

@ -27,6 +27,8 @@ import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.data.validation.Severity;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
import org.openstreetmap.josm.spi.preferences.Config;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
@ -37,7 +39,9 @@ import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
* @author Taylor Smock
* @since xxx
*/
@NoExceptions
@BasicPreferences
@MapWithAISources
class RoutingIslandsTestTest {
/**
* Setup test.

Wyświetl plik

@ -23,6 +23,7 @@ import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.mapwithai.actions.AddMapWithAILayerAction;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.tools.Logging;
@ -34,10 +35,11 @@ import org.openstreetmap.josm.tools.Logging;
*
*/
@BasicPreferences
@Wiremock
@MapWithAISources
class MapWithAIMenuTest {
@RegisterExtension
static JOSMTestRules rule = new MapWithAITestRules().wiremock().territories().projection().main();
static JOSMTestRules rule = new MapWithAITestRules().territories().projection().main();
private static MapWithAIMenu menu;
@BeforeAll

Wyświetl plik

@ -10,13 +10,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JCheckBox;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtilsTest;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
@BasicPreferences

Wyświetl plik

@ -11,15 +11,15 @@ import java.util.List;
import javax.swing.SpinnerNumberModel;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIPreferenceHelper;
import org.openstreetmap.josm.plugins.mapwithai.testutils.ImageProviderMocker;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
/**
@ -28,10 +28,11 @@ import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
* @author Taylor Smock
*/
@BasicPreferences
@MapWithAISources
class MapWithAIPreferencesTest {
@RegisterExtension
@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
JOSMTestRules test = new JOSMTestRules().main().timeout(100_000);
JOSMTestRules test = new JOSMTestRules().main();
private MapWithAIPreferences preferences;

Wyświetl plik

@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Field;
import java.util.Arrays;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.gui.widgets.JosmTextArea;
@ -19,8 +20,6 @@ import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAIS
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.mockers.WindowMocker;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Test class for {@link AddMapWithAIPanel}
*

Wyświetl plik

@ -17,6 +17,7 @@ import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.testutils.JOSMTestRules;
/**
@ -24,10 +25,11 @@ import org.openstreetmap.josm.testutils.JOSMTestRules;
*
* @author Taylor Smock
*/
@Wiremock
@MapWithAISources
class MapWithAIDefaultLayerTableModelTest {
@RegisterExtension
JOSMTestRules rule = new MapWithAITestRules().wiremock().projection();
JOSMTestRules rule = new MapWithAITestRules().projection();
@Test
void testGetRow() {

Wyświetl plik

@ -22,6 +22,7 @@ import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInf
import org.openstreetmap.josm.plugins.mapwithai.gui.preferences.mapwithai.MapWithAIProvidersPanel.AreaListener;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.ListenerList;
@ -30,10 +31,11 @@ import org.openstreetmap.josm.tools.ListenerList;
*
* @author Taylor Smock
*/
@Wiremock
@MapWithAISources
class MapWithAIProvidersPanelTest {
@RegisterExtension
JOSMTestRules rule = new MapWithAITestRules().wiremock().projection();
JOSMTestRules rule = new MapWithAITestRules().projection();
private MapWithAIProvidersPanel mapwithaiProvidersPanel;

Wyświetl plik

@ -9,6 +9,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -16,10 +17,18 @@ import org.junit.jupiter.api.extension.RegisterExtension;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIType;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
@Wiremock
class ESRISourceReaderTest {
@BasicWiremock
public WireMockServer wireMockServer;
@RegisterExtension
MapWithAITestRules rule = (MapWithAITestRules) new MapWithAITestRules().wiremock().projection();
JOSMTestRules rule = new MapWithAITestRules().projection();
@BeforeEach
void setUp() {
@ -42,7 +51,7 @@ class ESRISourceReaderTest {
// TODO wiremock
MapWithAIInfo info = new MapWithAIInfo("TEST", "test_url", "bdf6c800b3ae453b9db239e03d7c1727");
info.setSourceType(MapWithAIType.ESRI);
String tUrl = rule.getWireMock().baseUrl() + "/sharing/rest";
String tUrl = wireMockServer.url("/sharing/rest");
for (String url : Arrays.asList(tUrl, tUrl + "/")) {
info.setUrl(url);
final ESRISourceReader reader = new ESRISourceReader(info);

Wyświetl plik

@ -15,8 +15,15 @@ import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import mockit.integration.TestRunnerDecorator;
import org.junit.runners.model.InitializationError;
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.gui.util.GuiHelper;
import org.openstreetmap.josm.io.OsmApi;
@ -31,16 +38,6 @@ import org.openstreetmap.josm.spi.preferences.Config;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.Logging;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import mockit.integration.TestRunnerDecorator;
public class MapWithAITestRules extends JOSMTestRules {
private boolean wiremock;
@ -63,6 +60,13 @@ public class MapWithAITestRules extends JOSMTestRules {
return this;
}
/**
* @deprecated Use
* {@link org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock}
* instead
* @return this, for easy chaining
*/
@Deprecated
public MapWithAITestRules wiremock() {
this.wiremock = true;
territories();
@ -191,15 +195,6 @@ public class MapWithAITestRules extends JOSMTestRules {
return null;
}
/**
* Get the wiremock instance
*
* @return The WireMock
*/
public WireMockServer getWireMock() {
return wireMock;
}
/**
* Replace URL's with the wiremock URL
*

Wyświetl plik

@ -1,34 +1,23 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.testutils.annotations;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
/**
* An annotation to use {@link MapWithAILayerInfo}
*
* @author Taylor Smock
*/
@ExtendWith(MapWithAISources.MapWithAILayerInfoExtension.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Wiremock
@Territories
@ExtendWith(Wiremock.MapWithAILayerInfoExtension.class)
public @interface MapWithAISources {
class MapWithAILayerInfoExtension implements AfterAllCallback, BeforeAllCallback {
@Override
public void afterAll(ExtensionContext context) {
MapWithAILayerInfo.getInstance().clear();
}
@Override
public void beforeAll(ExtensionContext context) {
AtomicBoolean finished = new AtomicBoolean();
MapWithAILayerInfo.getInstance().load(false, () -> finished.set(true));
Awaitility.await().atMost(Durations.TEN_SECONDS).until(finished::get);
}
}
}

Wyświetl plik

@ -0,0 +1,66 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.testutils.annotations;
import static org.junit.jupiter.api.Assertions.assertAll;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openstreetmap.josm.gui.MainApplication;
/**
* Ensure that no exceptions occur in different threads
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.TYPE, ElementType.METHOD })
@ExtendWith(NoExceptions.NoExceptionsExtension.class)
public @interface NoExceptions {
class NoExceptionsExtension implements AfterAllCallback, BeforeAllCallback {
@Override
public void afterAll(ExtensionContext context) {
final AtomicBoolean atomicBoolean = new AtomicBoolean();
MainApplication.worker.submit(() -> atomicBoolean.set(true));
Awaitility.await().atMost(Durations.TEN_SECONDS).until(atomicBoolean::get);
final ExtensionContext.Store store = context
.getStore(ExtensionContext.Namespace.create(NoExceptions.class));
Thread.setDefaultUncaughtExceptionHandler(
store.get(Thread.UncaughtExceptionHandler.class, Thread.UncaughtExceptionHandler.class));
final NoExceptionsUncaughtExceptionHandler handler = store.get(NoExceptions.class,
NoExceptionsUncaughtExceptionHandler.class);
if (!handler.exceptionCollection.isEmpty()) {
assertAll(handler.exceptionCollection.stream().map(Assertions::fail));
}
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
final ExtensionContext.Store store = context
.getStore(ExtensionContext.Namespace.create(NoExceptions.class));
store.put(Thread.UncaughtExceptionHandler.class, Thread.getDefaultUncaughtExceptionHandler());
final NoExceptionsUncaughtExceptionHandler handler = new NoExceptionsUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(handler);
store.put(NoExceptions.class, handler);
}
static class NoExceptionsUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
final List<Throwable> exceptionCollection = new ArrayList<>();
@Override
public void uncaughtException(Thread t, Throwable e) {
this.exceptionCollection.add(e);
}
}
}
}

Wyświetl plik

@ -0,0 +1,73 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.testutils.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Optional;
import org.junit.jupiter.api.extension.AfterAllCallback;
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.platform.commons.support.AnnotationSupport;
import org.openstreetmap.josm.data.projection.ProjectionRegistry;
import org.openstreetmap.josm.data.projection.Projections;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.AnnotationUtils;
/**
* Use projections in tests (Mercator). Prefer the JOSM implementation in the
* future.
*
* @author Taylor Smock
* @see JOSMTestRules#projection()
*
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@ExtendWith(Projection.ProjectionExtension.class)
public @interface Projection {
/**
* The value to use for the projection. Defaults to EPSG:3857 (Mercator).
*
* @return The value to use to get the projection from
* {@link Projections#getProjectionByCode}.
*/
String projectionCode() default "EPSG:3857";
/**
* Use projections in tests. Use {@link Projection} preferentially.
*
* @author Taylor Smock
*
*/
class ProjectionExtension implements BeforeEachCallback, BeforeAllCallback, AfterAllCallback {
@Override
public void afterAll(ExtensionContext context) throws Exception {
ProjectionRegistry.clearProjectionChangeListeners();
AnnotationUtils.resetStaticClass(ProjectionRegistry.class);
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
// Needed in order to run prior to Territories
beforeEach(context);
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Optional<Projection> annotation = AnnotationSupport.findAnnotation(context.getElement(), Projection.class);
if (annotation.isPresent()) {
ProjectionRegistry.setProjection(Projections.getProjectionByCode(annotation.get().projectionCode()));
} else {
ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
}
}
}
}

Wyświetl plik

@ -0,0 +1,87 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.testutils.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Optional;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.support.AnnotationSupport;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
/**
* Use boundaries dataset in this test. FIXME: Use the JOSM version
*
* @see JOSMTestRules#territories()
* @author Taylor Smock
* @since xxx
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@BasicPreferences // Needed for nodes
@Projection // Needed for getEastNorth
@ExtendWith(Territories.TerritoriesExtension.class)
public @interface Territories {
/**
* Initialization states. Please note that the highest initialization state
* holds.
*
* @author Taylor Smock
*/
enum Initialize {
/** Don't initialize */
NONE,
/** Initialize only internal data */
INTERNAL,
/** Initialize internal and external data */
ALL
}
/**
* The way to initialize Territories
*
* @return The value to use
*/
Initialize value() default Initialize.INTERNAL;
/**
* Initialize boundaries prior to use
*
* @author Taylor Smock
*
*/
class TerritoriesExtension implements BeforeAllCallback, AfterAllCallback {
@Override
public void afterAll(ExtensionContext context) throws Exception {
synchronized (TerritoriesExtension.class) {
// TODO
// org.openstreetmap.josm.tools.Territories.uninitialize();
}
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
Optional<Territories> annotation = AnnotationSupport.findAnnotation(context.getElement(),
Territories.class);
if (annotation.isPresent()) {
Initialize current = annotation.get().value();
// Avoid potential race conditions if tests are parallelized
synchronized (TerritoriesExtension.class) {
if (current == Initialize.INTERNAL) {
org.openstreetmap.josm.tools.Territories.initializeInternalData();
} else if (current == Initialize.ALL) {
org.openstreetmap.josm.tools.Territories.initialize();
}
}
}
}
}
}

Wyświetl plik

@ -0,0 +1,207 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.testutils.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openstreetmap.josm.plugins.mapwithai.backend.DataAvailability;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIConflationCategory;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
import org.openstreetmap.josm.plugins.mapwithai.tools.MapPaintUtils;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
import org.openstreetmap.josm.testutils.annotations.HTTP;
/**
* Test annotation to ensure that wiremock is used
*
* @author Taylor Smock
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.TYPE, ElementType.METHOD })
@BasicPreferences
@HTTP
@ExtendWith(Wiremock.DataAvailabilityExtension.class)
@ExtendWith(Wiremock.MapPaintUtilsExtension.class)
@ExtendWith(Wiremock.MapWithAIConflationCategoryExtension.class)
@BasicWiremock(value = "test/resources/wiremock", responseTransformers = Wiremock.WireMockUrlTransformer.class)
public @interface Wiremock {
/**
* Set to {@code false} to turn off wiremock (use only in integration tests)
*
* @return {@code true} indicates that Wiremock must be used
*/
boolean value() default true;
/**
* Replace URL's with the wiremock URL
*
* @author Taylor Smock
*/
class WireMockUrlTransformer extends ResponseTransformer {
private final ExtensionContext context;
public WireMockUrlTransformer(ExtensionContext context) {
this.context = context;
}
@Override
public String getName() {
return "Convert urls in responses to wiremock url";
}
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
if (!request.getUrl().endsWith("/capabilities")
&& !response.getHeaders().getContentTypeHeader().mimeTypePart().contains("application/zip")) {
String origBody = response.getBodyAsString();
String newBody = origBody.replaceAll("https?:\\/\\/.*?\\/",
WiremockExtension.getWiremock(context).baseUrl() + "/");
return Response.Builder.like(response).but().body(newBody).build();
}
return response;
}
}
/**
* This is the base wiremock extension class
*/
class WiremockExtension extends BasicWiremock.WireMockExtension {
/**
* Get the default wiremock server
*
* @param context The context to search
* @return The wiremock server
*/
static WireMockServer getWiremock(ExtensionContext context) {
ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(BasicWiremock.class);
return context.getStore(namespace).get(WireMockServer.class, WireMockServer.class);
}
}
/**
* Extension for {@link MapPaintUtils}
*/
class MapPaintUtilsExtension extends WiremockExtension {
@Override
public void afterAll(ExtensionContext context) throws Exception {
try {
super.afterAll(context);
} finally {
MapPaintUtils.removeMapWithAIPaintStyles();
MapPaintUtils.setPaintStyleUrl("https://invalid.url/josmfile?page=Styles/MapWithAI&zip=1");
}
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
super.beforeAll(context);
MapPaintUtils.setPaintStyleUrl(replaceUrl(getWiremock(context), MapPaintUtils.getPaintStyleUrl()));
}
}
/**
* Extension for {@link MapWithAILayerInfo}
*/
class MapWithAILayerInfoExtension extends WiremockExtension {
private static Collection<String> getSourceSites(ExtensionContext context) {
ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(MapWithAILayerInfoExtension.class);
return (Collection<String>) context.getStore(namespace).get(MapWithAILayerInfo.class, Collection.class);
}
@Override
public void afterAll(ExtensionContext context) throws Exception {
try {
super.afterAll(context);
} finally {
MapWithAILayerInfo.setImageryLayersSites(
Collections.singleton("https://invalid.url/JOSM_MapWithAI/json/sources.json"));
resetMapWithAILayerInfo();
}
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
super.beforeAll(context);
MapWithAILayerInfo.setImageryLayersSites(
MapWithAILayerInfo.getImageryLayersSites().stream().map(t -> replaceUrl(getWiremock(context), t))
.filter(Objects::nonNull).collect(Collectors.toList()));
AtomicBoolean finished = new AtomicBoolean();
MapWithAILayerInfo.getInstance().clear();
MapWithAILayerInfo.getInstance().load(false, () -> finished.set(true));
Awaitility.await().atMost(Durations.TEN_SECONDS).until(finished::get);
}
private void resetMapWithAILayerInfo() {
// This should probably only be run if territories is initialized
// TODO update if @Territories becomes an annotation
synchronized (MapWithAILayerInfo.class) {
MapWithAILayerInfo.getInstance().clear();
MapWithAILayerInfo.getInstance().getDefaultLayers().stream().filter(MapWithAIInfo::isDefaultEntry)
.forEach(MapWithAILayerInfo.getInstance()::add);
MapWithAILayerInfo.getInstance().save();
}
}
}
/**
* Extension for {@link DataAvailability}
*/
class DataAvailabilityExtension extends WiremockExtension {
@Override
public void afterAll(ExtensionContext context) throws Exception {
try {
super.afterAll(context);
} finally {
DataAvailability.setReleaseUrl("https://invalid.url/JOSM_MapWithAI/json/sources.json");
}
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
super.beforeAll(context);
DataAvailability.setReleaseUrl(replaceUrl(getWiremock(context), DataAvailability.getReleaseUrl()));
}
}
/**
* Extension for {@link MapWithAIConflationCategory}
*/
class MapWithAIConflationCategoryExtension extends WiremockExtension {
@Override
public void afterAll(ExtensionContext context) throws Exception {
try {
super.afterAll(context);
} finally {
MapWithAIConflationCategory.resetConflationJsonLocation();
}
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
super.beforeAll(context);
MapWithAIConflationCategory.setConflationJsonLocation(
replaceUrl(getWiremock(context), MapWithAIConflationCategory.getConflationJsonLocation()));
}
}
}

Wyświetl plik

@ -20,10 +20,15 @@ import org.openstreetmap.josm.gui.mappaint.StyleSource;
import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
import org.openstreetmap.josm.io.CachedFile;
import org.openstreetmap.josm.plugins.mapwithai.testutils.MapWithAITestRules;
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.Wiremock;
import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
@BasicPreferences
@Wiremock
class MapPaintUtilsTest {
@RegisterExtension
MapWithAITestRules rule = (MapWithAITestRules) new MapWithAITestRules().wiremock().projection();
JOSMTestRules rule = new MapWithAITestRules().projection();
@Test
void testAddPaintStyle() {