-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
taylor.smock
committed
Sep 5, 2024
1 parent
d825ea6
commit 8803b53
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/unit/org/openstreetmap/josm/testutils/annotations/Plugins.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.testutils.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.openstreetmap.josm.io.AbstractReader; | ||
import org.openstreetmap.josm.io.OsmServerReadPostprocessor; | ||
import org.openstreetmap.josm.plugins.PluginHandler; | ||
import org.openstreetmap.josm.plugins.PluginInformation; | ||
import org.openstreetmap.josm.tools.Destroyable; | ||
|
||
/** | ||
* Cleanup plugins if they've been loaded | ||
*/ | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(Plugins.PluginExtension.class) | ||
public @interface Plugins { | ||
/** | ||
* The extension to clean up after plugin installs | ||
*/ | ||
class PluginExtension implements AfterEachCallback { | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
// We want to clean up as much as possible using "standard" methods | ||
for (PluginInformation plugin : PluginHandler.getPlugins()) { | ||
Object root = PluginHandler.getPlugin(plugin.name); | ||
if (root instanceof Destroyable) { | ||
((Destroyable) root).destroy(); | ||
PluginHandler.removePlugins(Collections.singletonList(plugin)); | ||
} | ||
} | ||
final Field pluginListField = PluginHandler.class.getDeclaredField("pluginList"); | ||
final Field classLoadersField = PluginHandler.class.getDeclaredField("classLoaders"); | ||
final Field postprocessorsField = AbstractReader.class.getDeclaredField("postprocessors"); | ||
org.openstreetmap.josm.tools.ReflectionUtils.setObjectsAccessible(classLoadersField, postprocessorsField, | ||
pluginListField); | ||
((List<?>) pluginListField.get(null)).clear(); | ||
((Map<?, ?>) classLoadersField.get(null)).clear(); | ||
// Needed due to SDS | ||
final Object postprocessors = postprocessorsField.get(null); | ||
if (postprocessors instanceof Collection) { | ||
for (OsmServerReadPostprocessor pp : new ArrayList<>((Collection<OsmServerReadPostprocessor>) postprocessors)) { | ||
AbstractReader.deregisterPostprocessor(pp); | ||
} | ||
} | ||
} | ||
} | ||
} |