Changeset 15508 in josm for trunk/test


Ignore:
Timestamp:
2019-11-04T22:19:54+01:00 (4 years ago)
Author:
Don-vip
Message:

fix #18277 - Allow plugins to implement Destroyable if they want to allow restartless updates/removals (patch by taylor.smock)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java

    r15242 r15508  
    88import java.awt.GraphicsEnvironment;
    99import java.awt.HeadlessException;
     10import java.io.IOException;
     11import java.util.ArrayList;
    1012import java.util.Arrays;
    1113import java.util.Collection;
     
    1416import java.util.List;
    1517import java.util.Map;
     18import java.util.Map.Entry;
    1619import java.util.Set;
    1720import java.util.function.Consumer;
     
    1922
    2023import org.apache.commons.lang3.exception.ExceptionUtils;
    21 import org.junit.Rule;
     24import org.junit.BeforeClass;
     25import org.junit.ClassRule;
    2226import org.junit.Test;
     27import org.openstreetmap.josm.TestUtils;
    2328import org.openstreetmap.josm.data.Preferences;
    2429import org.openstreetmap.josm.data.gpx.GpxData;
     
    3136import org.openstreetmap.josm.spi.preferences.Config;
    3237import org.openstreetmap.josm.testutils.JOSMTestRules;
     38import org.openstreetmap.josm.tools.Destroyable;
    3339import org.openstreetmap.josm.tools.Utils;
    3440
     
    4046public class PluginHandlerTestIT {
    4147
     48    private static List<String> errorsToIgnore = new ArrayList<>();
    4249    /**
    4350     * Setup test.
    4451     */
    45     @Rule
     52    @ClassRule
    4653    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    47     public JOSMTestRules test = new JOSMTestRules().main().projection().preferences().https().timeout(10*60*1000);
     54    public static JOSMTestRules test = new JOSMTestRules().main().projection().preferences().https()
     55            .timeout(10 * 60 * 1000);
     56
     57    /**
     58     * Setup test
     59     *
     60     * @throws IOException in case of I/O error
     61     */
     62    @BeforeClass
     63    public static void beforeClass() throws IOException {
     64        errorsToIgnore.addAll(TestUtils.getIgnoredErrorMessages(PluginHandlerTestIT.class));
     65    }
    4866
    4967    /**
     
    7593        }
    7694
     95        Map<String, Throwable> noRestartExceptions = new HashMap<>();
     96        testCompletelyRestartlessPlugins(loadedPlugins, noRestartExceptions);
     97
    7798        debugPrint(invalidManifestEntries);
    7899        debugPrint(loadingExceptions);
    79100        debugPrint(layerExceptions);
     101        debugPrint(noRestartExceptions);
     102
     103        invalidManifestEntries = filterKnownErrors(invalidManifestEntries);
     104        loadingExceptions = filterKnownErrors(loadingExceptions);
     105        layerExceptions = filterKnownErrors(layerExceptions);
     106        noRestartExceptions = filterKnownErrors(noRestartExceptions);
     107
    80108        String msg = Arrays.toString(invalidManifestEntries.entrySet().toArray()) + '\n' +
    81109                     Arrays.toString(loadingExceptions.entrySet().toArray()) + '\n' +
    82                      Arrays.toString(layerExceptions.entrySet().toArray());
     110                Arrays.toString(layerExceptions.entrySet().toArray()) + '\n'
     111                + Arrays.toString(noRestartExceptions.entrySet().toArray());
    83112        assertTrue(msg, invalidManifestEntries.isEmpty() && loadingExceptions.isEmpty() && layerExceptions.isEmpty());
     113    }
     114
     115    private static void testCompletelyRestartlessPlugins(List<PluginInformation> loadedPlugins,
     116            Map<String, Throwable> noRestartExceptions) {
     117        try {
     118            List<PluginInformation> restartable = loadedPlugins.parallelStream()
     119                    .filter(info -> PluginHandler.getPlugin(info.name) instanceof Destroyable)
     120                    .collect(Collectors.toList());
     121            // ensure good plugin behavior with regards to Destroyable (i.e., they can be
     122            // removed and readded)
     123            for (int i = 0; i < 2; i++) {
     124                assertFalse(PluginHandler.removePlugins(restartable));
     125                assertTrue(restartable.stream().noneMatch(info -> PluginHandler.getPlugins().contains(info)));
     126                loadPlugins(restartable);
     127            }
     128
     129            assertTrue(PluginHandler.removePlugins(loadedPlugins));
     130            assertTrue(restartable.parallelStream().noneMatch(info -> PluginHandler.getPlugins().contains(info)));
     131        } catch (Exception | LinkageError t) {
     132            Throwable root = ExceptionUtils.getRootCause(t);
     133            root.printStackTrace();
     134            noRestartExceptions.put(findFaultyPlugin(loadedPlugins, root), root);
     135        }
     136    }
     137
     138    private static <T> Map<String, T> filterKnownErrors(Map<String, T> errorMap) {
     139        return errorMap.entrySet().parallelStream()
     140                .filter(entry -> !errorsToIgnore.contains(convertEntryToString(entry)))
     141                .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
    84142    }
    85143
     
    87145        System.out.println(invalidManifestEntries.entrySet()
    88146                .stream()
    89                 .map(e -> e.getKey() + "=\"" + e.getValue() + "\"")
     147                .map(e -> convertEntryToString(e))
    90148                .collect(Collectors.joining(", ")));
     149    }
     150
     151    private static String convertEntryToString(Entry<String, ?> entry) {
     152        return entry.getKey() + "=\"" + entry.getValue() + "\"";
    91153    }
    92154
     
    134196        downloadPlugins(plugins);
    135197
     198        loadPlugins(plugins);
     199    }
     200
     201    static void loadPlugins(List<PluginInformation> plugins) {
    136202        // Load early plugins
    137203        PluginHandler.loadEarlyPlugins(null, plugins, null);
Note: See TracChangeset for help on using the changeset viewer.