Skip to content

Commit 8803b53

Browse files
author
taylor.smock
committed
Add @Plugins annotation to clean up after plugins have been loaded in tests
This should help reduce the issues related to plugins not being unloaded after PluginHandlerTestIT when tests are run locally with a UI -- the primary cause of problems is the SDS plugin though. git-svn-id: https://josm.openstreetmap.de/svn/trunk@19209 0c6e7542-c601-0410-84e7-c038aed88b3b
1 parent d825ea6 commit 8803b53

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

test/unit/org/openstreetmap/josm/gui/MainApplicationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.openstreetmap.josm.testutils.annotations.HTTPS;
5858
import org.openstreetmap.josm.testutils.annotations.Main;
5959
import org.openstreetmap.josm.testutils.annotations.OsmApi;
60+
import org.openstreetmap.josm.testutils.annotations.Plugins;
6061
import org.openstreetmap.josm.testutils.annotations.Projection;
6162
import org.openstreetmap.josm.tools.Logging;
6263
import org.openstreetmap.josm.tools.PlatformManager;
@@ -170,6 +171,7 @@ void testParamType() {
170171
* Test of {@link MainApplication#updateAndLoadEarlyPlugins} and {@link MainApplication#loadLatePlugins} methods.
171172
* @throws PluginListParseException if an error occurs
172173
*/
174+
@Plugins
173175
@Test
174176
void testUpdateAndLoadPlugins() throws PluginListParseException {
175177
final String old = System.getProperty("josm.plugins");

test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
4343
import org.openstreetmap.josm.testutils.annotations.HTTPS;
4444
import org.openstreetmap.josm.testutils.annotations.Main;
45+
import org.openstreetmap.josm.testutils.annotations.Plugins;
4546
import org.openstreetmap.josm.testutils.annotations.Projection;
4647
import org.openstreetmap.josm.testutils.annotations.Territories;
48+
import org.openstreetmap.josm.testutils.annotations.ThreadSync;
4749
import org.openstreetmap.josm.tools.Destroyable;
4850
import org.openstreetmap.josm.tools.Logging;
4951
import org.openstreetmap.josm.tools.Utils;
@@ -57,6 +59,8 @@
5759
@Projection
5860
@Territories
5961
@Timeout(value = 10, unit = TimeUnit.MINUTES)
62+
@ThreadSync
63+
@Plugins
6064
public class PluginHandlerTestIT {
6165

6266
private static final List<String> errorsToIgnore = new ArrayList<>();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// License: GPL. For details, see LICENSE file.
2+
package org.openstreetmap.josm.testutils.annotations;
3+
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
import java.lang.reflect.Field;
9+
import java.util.ArrayList;
10+
import java.util.Collection;
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import org.junit.jupiter.api.extension.AfterEachCallback;
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
import org.junit.jupiter.api.extension.ExtensionContext;
18+
import org.openstreetmap.josm.io.AbstractReader;
19+
import org.openstreetmap.josm.io.OsmServerReadPostprocessor;
20+
import org.openstreetmap.josm.plugins.PluginHandler;
21+
import org.openstreetmap.josm.plugins.PluginInformation;
22+
import org.openstreetmap.josm.tools.Destroyable;
23+
24+
/**
25+
* Cleanup plugins if they've been loaded
26+
*/
27+
@Target({ElementType.TYPE, ElementType.METHOD})
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@ExtendWith(Plugins.PluginExtension.class)
30+
public @interface Plugins {
31+
/**
32+
* The extension to clean up after plugin installs
33+
*/
34+
class PluginExtension implements AfterEachCallback {
35+
36+
@Override
37+
public void afterEach(ExtensionContext context) throws Exception {
38+
// We want to clean up as much as possible using "standard" methods
39+
for (PluginInformation plugin : PluginHandler.getPlugins()) {
40+
Object root = PluginHandler.getPlugin(plugin.name);
41+
if (root instanceof Destroyable) {
42+
((Destroyable) root).destroy();
43+
PluginHandler.removePlugins(Collections.singletonList(plugin));
44+
}
45+
}
46+
final Field pluginListField = PluginHandler.class.getDeclaredField("pluginList");
47+
final Field classLoadersField = PluginHandler.class.getDeclaredField("classLoaders");
48+
final Field postprocessorsField = AbstractReader.class.getDeclaredField("postprocessors");
49+
org.openstreetmap.josm.tools.ReflectionUtils.setObjectsAccessible(classLoadersField, postprocessorsField,
50+
pluginListField);
51+
((List<?>) pluginListField.get(null)).clear();
52+
((Map<?, ?>) classLoadersField.get(null)).clear();
53+
// Needed due to SDS
54+
final Object postprocessors = postprocessorsField.get(null);
55+
if (postprocessors instanceof Collection) {
56+
for (OsmServerReadPostprocessor pp : new ArrayList<>((Collection<OsmServerReadPostprocessor>) postprocessors)) {
57+
AbstractReader.deregisterPostprocessor(pp);
58+
}
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)