Skip to content

Commit e8c6e96

Browse files
committed
Remove unnecessary test rules
Signed-off-by: Taylor Smock <[email protected]>
1 parent e796f49 commit e8c6e96

File tree

10 files changed

+146
-189
lines changed

10 files changed

+146
-189
lines changed

src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnable.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ public DataSet compute() {
219219
if (!monitor.isCanceled()) {
220220
if (bounds.size() == MAX_NUMBER_OF_BBOXES_TO_PROCESS) {
221221
final var temporaryDataSet = getDataReal(bounds.get(0), monitor);
222-
synchronized (this.dataSet) {
223-
dataSet.mergeFrom(temporaryDataSet);
224-
}
222+
this.dataSet.update(() -> dataSet.mergeFrom(temporaryDataSet));
225223
} else {
226224
final Collection<GetDataRunnable> tasks = bounds.stream()
227225
.map(bound -> new GetDataRunnable(bound, dataSet, monitor.createSubTaskMonitor(0, true)))
@@ -250,10 +248,10 @@ public DataSet compute() {
250248
* @param info The information used to download the data
251249
*/
252250
public static void cleanup(DataSet dataSet, Bounds bounds, MapWithAIInfo info) {
253-
realCleanup(dataSet, bounds, info);
251+
dataSet.update(() -> realCleanup(dataSet, bounds, info));
254252
}
255253

256-
private static synchronized void realCleanup(DataSet dataSet, Bounds bounds, MapWithAIInfo info) {
254+
private static void realCleanup(DataSet dataSet, Bounds bounds, MapWithAIInfo info) {
257255
final Bounds boundsToUse;
258256
if (bounds == null && !dataSet.getDataSourceBounds().isEmpty()) {
259257
boundsToUse = new Bounds(dataSet.getDataSourceBounds().get(0));

src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAILayerInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ public static MapWithAILayerInfo getInstance() {
9898
}
9999
// Avoid a deadlock in the EDT.
100100
if (!finished.get() && !SwingUtilities.isEventDispatchThread()) {
101+
var count = 0;
101102
synchronized (MapWithAILayerInfo.class) {
102-
while (!finished.get()) {
103+
while (!finished.get() && count < 120) {
104+
count++;
103105
try {
104106
MapWithAILayerInfo.class.wait(1000);
105107
} catch (InterruptedException e) {

src/test/unit/org/openstreetmap/josm/plugins/mapwithai/actions/AddMapWithAILayerActionTest.java

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@
44
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
55
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
66
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
78
import static org.junit.jupiter.api.Assertions.assertNotNull;
89
import static org.junit.jupiter.api.Assertions.assertNull;
910
import static org.junit.jupiter.api.Assertions.assertTrue;
1011

11-
import javax.imageio.ImageIO;
12-
import javax.swing.Action;
13-
import javax.swing.ImageIcon;
14-
1512
import java.awt.image.BufferedImage;
1613
import java.io.ByteArrayOutputStream;
1714
import java.io.IOException;
1815
import java.util.ArrayList;
1916
import java.util.HashMap;
2017
import java.util.List;
2118
import java.util.Map;
22-
import java.util.concurrent.ExecutionException;
2319
import java.util.logging.Handler;
2420
import java.util.logging.LogRecord;
2521
import java.util.stream.Collectors;
2622

27-
import org.awaitility.Awaitility;
28-
import org.awaitility.Durations;
23+
import javax.imageio.ImageIO;
24+
import javax.swing.Action;
25+
import javax.swing.ImageIcon;
26+
27+
import org.junit.jupiter.api.BeforeEach;
2928
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.RegisterExtension;
3030
import org.openstreetmap.josm.data.Bounds;
3131
import org.openstreetmap.josm.data.DataSource;
3232
import org.openstreetmap.josm.data.osm.DataSet;
3333
import org.openstreetmap.josm.gui.MainApplication;
3434
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
35-
import org.openstreetmap.josm.gui.util.GuiHelper;
3635
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
3736
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAILayer;
3837
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
@@ -41,17 +40,19 @@
4140
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.MapWithAISources;
4241
import org.openstreetmap.josm.plugins.mapwithai.testutils.annotations.NoExceptions;
4342
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
43+
import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
4444
import org.openstreetmap.josm.testutils.annotations.Projection;
45+
import org.openstreetmap.josm.testutils.annotations.ThreadSync;
4546
import org.openstreetmap.josm.tools.ImageProvider;
4647
import org.openstreetmap.josm.tools.Logging;
4748

4849
import com.github.tomakehurst.wiremock.WireMockServer;
4950
import com.github.tomakehurst.wiremock.client.WireMock;
50-
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
5151
import com.github.tomakehurst.wiremock.matching.AnythingPattern;
5252
import com.github.tomakehurst.wiremock.matching.EqualToPattern;
5353
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
5454
import com.github.tomakehurst.wiremock.matching.UrlPathPattern;
55+
import jakarta.json.Json;
5556

5657
/**
5758
* Test class for {@link AddMapWithAILayerAction}
@@ -63,11 +64,47 @@
6364
@MapWithAISources
6465
@Projection
6566
class AddMapWithAILayerActionTest {
66-
@Test
67-
void testAddMapWithAILayerActionTest() {
68-
MapWithAIInfo info = MapWithAILayerInfo.getInstance().getLayers().stream()
67+
68+
private static class ThreadSyncMWAI extends ThreadSync.ThreadSyncExtension {
69+
public ThreadSyncMWAI() {
70+
this.registerForkJoinPool(MapWithAIDataUtils.getForkJoinPool());
71+
}
72+
}
73+
74+
@RegisterExtension
75+
static ThreadSyncMWAI threadSync = new ThreadSyncMWAI();
76+
77+
@BasicWiremock
78+
WireMockServer wireMockServer;
79+
80+
private static MapWithAIInfo info;
81+
private static MapWithAIInfo backupInfo;
82+
83+
@BeforeEach
84+
void setup() {
85+
final Map<String, StringValuePattern> parameterMap = new HashMap<>();
86+
final AnythingPattern anythingPattern = new AnythingPattern();
87+
parameterMap.put("geometryType", anythingPattern);
88+
parameterMap.put("geometry", anythingPattern);
89+
parameterMap.put("inSR", new EqualToPattern("4326"));
90+
parameterMap.put("f", new EqualToPattern("geojson"));
91+
parameterMap.put("outfields", new EqualToPattern("*"));
92+
parameterMap.put("result_type", new EqualToPattern("road_building_vector_xml"));
93+
parameterMap.put("resultOffset", anythingPattern);
94+
wireMockServer.stubFor(
95+
WireMock.get(new UrlPathPattern(new EqualToPattern("/query"), false)).withQueryParams(parameterMap)
96+
.willReturn(WireMock.aResponse()
97+
.withBody(Json.createObjectBuilder().add("type", "FeatureCollection")
98+
.add("features", Json.createArrayBuilder().build()).build().toString()))
99+
.atPriority(Integer.MIN_VALUE));
100+
info = MapWithAILayerInfo.getInstance().getLayers().stream()
69101
.filter(i -> i.getName().equalsIgnoreCase("MapWithAI")).findAny().orElse(null);
70102
assertNotNull(info);
103+
info = new MapWithAIInfo(info);
104+
}
105+
106+
@Test
107+
void testAddMapWithAILayerActionTest() {
71108
AddMapWithAILayerAction action = new AddMapWithAILayerAction(info);
72109
assertDoesNotThrow(() -> action.actionPerformed(null));
73110
OsmDataLayer osmLayer = new OsmDataLayer(new DataSet(), "TEST DATA", null);
@@ -78,7 +115,7 @@ void testAddMapWithAILayerActionTest() {
78115
assertNull(MapWithAIDataUtils.getLayer(false));
79116
action.updateEnabledState();
80117
action.actionPerformed(null);
81-
Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> MapWithAIDataUtils.getLayer(false) != null);
118+
threadSync.threadSync();
82119
assertNotNull(MapWithAIDataUtils.getLayer(false));
83120

84121
MainApplication.getLayerManager().removeLayer(MapWithAIDataUtils.getLayer(false));
@@ -90,7 +127,7 @@ void testAddMapWithAILayerActionTest() {
90127

91128
action.updateEnabledState();
92129
action.actionPerformed(null);
93-
Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> !layer.getDataSet().isEmpty());
130+
threadSync.threadSync();
94131
assertFalse(layer.getDataSet().isEmpty());
95132

96133
MainApplication.getLayerManager().removeLayer(MapWithAIDataUtils.getLayer(false));
@@ -100,69 +137,40 @@ void testAddMapWithAILayerActionTest() {
100137
mapwithaiLayer.getDataSet()
101138
.addDataSource(new DataSource(new Bounds(39.095376, -108.4495519, 39.0987811, -108.4422314), ""));
102139
action.actionPerformed(null);
103-
Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> !mapwithaiLayer.getDataSet().isEmpty());
140+
threadSync.threadSync();
104141
assertFalse(mapwithaiLayer.getDataSet().isEmpty());
105142
}
106143

107144
@Test
108-
void testRemoteIcon() throws IOException, ExecutionException, InterruptedException {
145+
void testRemoteIcon() throws IOException {
109146
final ImageIcon blankImage = ImageProvider.createBlankIcon(ImageProvider.ImageSizes.LARGEICON);
110147
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
111148
// BufferedImage is what the current implementation uses. Otherwise, we will
112149
// have to copy it into a BufferedImage.
113-
assertTrue(blankImage.getImage() instanceof BufferedImage);
114-
final BufferedImage bi = (BufferedImage) blankImage.getImage();
150+
final BufferedImage bi = assertInstanceOf(BufferedImage.class, blankImage.getImage());
115151
ImageIO.write(bi, "png", byteArrayOutputStream);
116152
byte[] originalImage = byteArrayOutputStream.toByteArray();
117-
final WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort());
118-
try {
119-
wireMockServer.start();
120-
wireMockServer.addStubMapping(wireMockServer
121-
.stubFor(WireMock.get("/icon").willReturn(WireMock.aResponse().withBody(originalImage))));
122-
final MapWithAIInfo info = MapWithAILayerInfo.getInstance().getLayers().stream()
123-
.filter(i -> i.getName().equalsIgnoreCase("MapWithAI")).findAny().orElse(null);
124-
assertNotNull(info);
125-
final MapWithAIInfo remoteInfo = new MapWithAIInfo(info);
126-
remoteInfo.setIcon(wireMockServer.baseUrl() + "/icon");
127-
final AddMapWithAILayerAction action = new AddMapWithAILayerAction(remoteInfo);
128-
GuiHelper.runInEDTAndWait(() -> {
129-
/* Sync EDT */});
130-
MainApplication.worker.submit(() -> {
131-
/* Sync worker thread */}).get();
132-
final Object image = action.getValue(Action.LARGE_ICON_KEY);
133-
assertTrue(image instanceof ImageIcon);
134-
final ImageIcon attachedIcon = (ImageIcon) image;
135-
assertTrue(attachedIcon.getImage() instanceof BufferedImage);
136-
byteArrayOutputStream.reset();
137-
ImageIO.write((BufferedImage) attachedIcon.getImage(), "png", byteArrayOutputStream);
138-
final byte[] downloadedImage = byteArrayOutputStream.toByteArray();
139-
assertArrayEquals(originalImage, downloadedImage);
140-
} finally {
141-
wireMockServer.stop();
142-
}
153+
wireMockServer.addStubMapping(
154+
wireMockServer.stubFor(WireMock.get("/icon").willReturn(WireMock.aResponse().withBody(originalImage))));
155+
final MapWithAIInfo remoteInfo = new MapWithAIInfo(info);
156+
remoteInfo.setIcon(wireMockServer.baseUrl() + "/icon");
157+
final AddMapWithAILayerAction action = new AddMapWithAILayerAction(remoteInfo);
158+
threadSync.threadSync();
159+
final Object image = action.getValue(Action.LARGE_ICON_KEY);
160+
final ImageIcon attachedIcon = assertInstanceOf(ImageIcon.class, image);
161+
final BufferedImage bufferedImage = assertInstanceOf(BufferedImage.class, attachedIcon.getImage());
162+
byteArrayOutputStream.reset();
163+
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
164+
final byte[] downloadedImage = byteArrayOutputStream.toByteArray();
165+
assertArrayEquals(originalImage, downloadedImage);
143166
}
144167

145168
@Test
146-
void testNonRegression22683() throws ExecutionException, InterruptedException {
147-
final MapWithAIInfo info = MapWithAILayerInfo.getInstance().getLayers().stream()
148-
.filter(i -> i.getName().equalsIgnoreCase("MapWithAI")).findAny().orElse(null);
149-
assertNotNull(info);
169+
void testNonRegression22683() {
150170
final OsmDataLayer layer = new OsmDataLayer(new DataSet(), "testNonRegression22683", null);
151171
layer.getDataSet().addDataSource(new DataSource(new Bounds(0, 0, 0.001, 0.001), "Area 1"));
152172
layer.getDataSet().addDataSource(new DataSource(new Bounds(-0.001, -0.001, 0, 0), "Area 2"));
153173
MainApplication.getLayerManager().addLayer(layer);
154-
final WireMockServer server = new WireMockServer(WireMockConfiguration.options().dynamicPort());
155-
final Map<String, StringValuePattern> parameterMap = new HashMap<>();
156-
final AnythingPattern anythingPattern = new AnythingPattern();
157-
parameterMap.put("geometryType", anythingPattern);
158-
parameterMap.put("geometry", anythingPattern);
159-
parameterMap.put("inSR", new EqualToPattern("4326"));
160-
parameterMap.put("f", new EqualToPattern("geojson"));
161-
parameterMap.put("outfields", new EqualToPattern("*"));
162-
parameterMap.put("result_type", new EqualToPattern("road_building_vector_xml"));
163-
parameterMap.put("resultOffset", anythingPattern);
164-
server.stubFor(WireMock.get(new UrlPathPattern(new EqualToPattern("/query"), false))
165-
.withQueryParams(parameterMap).willReturn(WireMock.aResponse().withBody("{\"test\":0}")));
166174
final List<LogRecord> logs = new ArrayList<>();
167175
Handler testHandler = new Handler() {
168176
@Override
@@ -182,22 +190,16 @@ public void close() {
182190
};
183191
Logging.getLogger().addHandler(testHandler);
184192
try {
185-
server.start();
186-
info.setUrl(server.baseUrl());
193+
info.setUrl(wireMockServer.baseUrl());
187194
info.setSourceType(MapWithAIType.ESRI_FEATURE_SERVER);
188195
final AddMapWithAILayerAction action = new AddMapWithAILayerAction(info);
189196
Logging.clearLastErrorAndWarnings();
190197
assertDoesNotThrow(() -> action.actionPerformed(null));
191-
GuiHelper.runInEDTAndWait(() -> {
192-
/* Sync thread */ });
193-
MainApplication.worker.submit(() -> {
194-
/* Sync thread */ }).get();
198+
threadSync.threadSync();
195199
final List<LogRecord> ides = logs.stream()
196-
.filter(record -> record.getThrown() instanceof IllegalArgumentException)
197-
.collect(Collectors.toList());
200+
.filter(record -> record.getThrown() instanceof IllegalArgumentException).toList();
198201
assertTrue(ides.isEmpty(), ides.stream().map(LogRecord::getMessage).collect(Collectors.joining("\n")));
199202
} finally {
200-
server.stop();
201203
Logging.getLogger().removeHandler(testHandler);
202204
}
203205
}

src/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAILayerTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
import static org.awaitility.Awaitility.await;
55
import static org.junit.jupiter.api.Assertions.assertEquals;
66
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
78
import static org.junit.jupiter.api.Assertions.assertNotNull;
89
import static org.junit.jupiter.api.Assertions.assertNull;
910
import static org.junit.jupiter.api.Assertions.assertSame;
1011
import static org.junit.jupiter.api.Assertions.assertTrue;
1112
import static org.openstreetmap.josm.tools.I18n.tr;
1213

13-
import javax.swing.Action;
14-
import javax.swing.JLabel;
15-
import javax.swing.JPanel;
16-
import javax.swing.SwingUtilities;
17-
1814
import java.awt.Component;
1915
import java.lang.reflect.InvocationTargetException;
2016
import java.text.MessageFormat;
@@ -23,6 +19,11 @@
2319
import java.util.List;
2420
import java.util.stream.Stream;
2521

22+
import javax.swing.Action;
23+
import javax.swing.JLabel;
24+
import javax.swing.JPanel;
25+
import javax.swing.SwingUtilities;
26+
2627
import org.awaitility.Durations;
2728
import org.junit.jupiter.api.BeforeAll;
2829
import org.junit.jupiter.api.BeforeEach;
@@ -53,6 +54,7 @@
5354
import org.openstreetmap.josm.testutils.annotations.Main;
5455
import org.openstreetmap.josm.testutils.annotations.OsmApi;
5556
import org.openstreetmap.josm.testutils.annotations.Projection;
57+
import org.openstreetmap.josm.testutils.annotations.ResetUniquePrimitiveIdCounters;
5658
import org.openstreetmap.josm.testutils.annotations.Territories;
5759

5860
/**
@@ -124,9 +126,9 @@ void testSourceDeduplication() {
124126
@Test
125127
void testGetInfoComponent() {
126128
final Object tObject = layer.getInfoComponent();
127-
assertTrue(tObject instanceof JPanel, "The info component should be a JPanel instead of a string");
129+
JPanel jPanel = assertInstanceOf(JPanel.class, tObject,
130+
"The info component should be a JPanel instead of a string");
128131

129-
JPanel jPanel = (JPanel) tObject;
130132
final Component[] startComponents = jPanel.getComponents();
131133
for (final Component comp : startComponents) {
132134
final JLabel label = (JLabel) comp;
@@ -173,6 +175,7 @@ void testGetLayer() {
173175
}
174176
}
175177

178+
@ResetUniquePrimitiveIdCounters
176179
@Test
177180
void testSelection() throws InvocationTargetException, InterruptedException {
178181
MapWithAILayer mapWithAILayer = MapWithAIDataUtils.getLayer(true);
@@ -186,10 +189,10 @@ void testSelection() throws InvocationTargetException, InterruptedException {
186189
SwingUtilities.invokeAndWait(() -> ds.setSelected(ds.allNonDeletedCompletePrimitives()));
187190
assertEquals(1, ds.getSelected().size());
188191
OsmPrimitive prim = ds.getSelected().iterator().next();
189-
assertTrue(prim instanceof Way);
190-
SwingUtilities.invokeAndWait(() -> ds.setSelected(((Way) prim).getNodes()));
191-
assertEquals(((Way) prim).getNodes().size(), ds.getSelected().size());
192-
assertTrue(((Way) prim).getNodes().stream().allMatch(ds::isSelected));
192+
final Way way = assertInstanceOf(Way.class, prim);
193+
SwingUtilities.invokeAndWait(() -> ds.setSelected(way.getNodes()));
194+
assertEquals(way.getNodes().size(), ds.getSelected().size());
195+
assertTrue(way.getNodes().stream().allMatch(ds::isSelected));
193196
}
194197

195198
@Test

src/test/unit/org/openstreetmap/josm/plugins/mapwithai/commands/MovePrimitiveDataSetCommandTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ void testMovePrimitives() {
4949
move.fillModifiedData(modified, deleted, added);
5050
final Collection<? extends OsmPrimitive> participatingPrimitives = move.getParticipatingPrimitives();
5151
assertAll(() -> assertEquals(0, deleted.size()), () -> assertEquals(0, added.size()), // the JOSM Add command
52-
// doesn't add to this
53-
// list
52+
// doesn't add to this
53+
// list
5454
() -> assertEquals(0, modified.size()), () -> assertEquals(1, from.allNonDeletedPrimitives().size()),
5555
() -> assertEquals(3, to.allNonDeletedPrimitives().size()),
5656
() -> assertNotNull(to.getPrimitiveById(way1)), () -> assertTrue(way1.isDeleted()),
@@ -86,8 +86,8 @@ void testMovePrimitivesAdditionalData() {
8686
move.fillModifiedData(modified, deleted, added);
8787
final List<OsmPrimitive> participatingPrimitives = new ArrayList<>(move.getParticipatingPrimitives());
8888
assertAll(() -> assertEquals(0, deleted.size()), () -> assertEquals(0, added.size()), // the JOSM Add command
89-
// doesn't add to this
90-
// list
89+
// doesn't add to this
90+
// list
9191
() -> assertEquals(0, modified.size()), () -> assertEquals(1, from.allNonDeletedPrimitives().size()),
9292
() -> assertEquals(3, to.allNonDeletedPrimitives().size()),
9393
() -> assertNotNull(to.getPrimitiveById(way1)),

src/test/unit/org/openstreetmap/josm/plugins/mapwithai/data/validation/tests/RoutingIslandsTestTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ void testRoundabouts() {
157157
/**
158158
* Test method for {@link RoutingIslandsTest#checkForUnconnectedWays}.
159159
*/
160+
@BasicPreferences
160161
@Test
161162
void testCheckForUnconnectedWaysIncoming() {
162163
RoutingIslandsTest.checkForUnconnectedWays(Collections.emptySet(), Collections.emptySet(), null);

0 commit comments

Comments
 (0)