Changeset 18636 in josm for trunk/test
- Timestamp:
- 2023-01-17T14:41:59+01:00 (2 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java
r18267 r18636 4 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 5 import static org.junit.jupiter.api.Assertions.assertFalse; 6 import static org.junit.jupiter.api.Assertions.assertNotNull; 6 7 import static org.junit.jupiter.api.Assertions.assertTrue; 7 8 … … 17 18 import java.util.Map; 18 19 import java.util.Map.Entry; 20 import java.util.Objects; 19 21 import java.util.Set; 20 22 import java.util.function.Consumer; 23 import java.util.logging.Handler; 24 import java.util.logging.LogRecord; 21 25 import java.util.stream.Collectors; 22 26 … … 24 28 import org.junit.jupiter.api.Test; 25 29 import org.junit.jupiter.api.extension.RegisterExtension; 30 import org.junit.platform.commons.util.ReflectionUtils; 26 31 import org.openstreetmap.josm.TestUtils; 27 32 import org.openstreetmap.josm.data.Preferences; … … 74 79 Map<String, Throwable> loadingExceptions = PluginHandler.pluginLoadingExceptions.entrySet().stream() 75 80 .filter(e -> !(Utils.getRootCause(e.getValue()) instanceof HeadlessException)) 76 .collect(Collectors.toMap( e -> e.getKey(), e -> Utils.getRootCause(e.getValue())));81 .collect(Collectors.toMap(Map.Entry::getKey, e -> Utils.getRootCause(e.getValue()))); 77 82 78 83 List<PluginInformation> loadedPlugins = PluginHandler.getPlugins(); … … 93 98 } 94 99 100 Map<String, String> testCodeHashCollisions = checkForHashCollisions(); 101 95 102 Map<String, Throwable> noRestartExceptions = new HashMap<>(); 96 103 testCompletelyRestartlessPlugins(loadedPlugins, noRestartExceptions); … … 100 107 debugPrint(layerExceptions); 101 108 debugPrint(noRestartExceptions); 109 debugPrint(testCodeHashCollisions); 102 110 103 111 invalidManifestEntries = filterKnownErrors(invalidManifestEntries); … … 105 113 layerExceptions = filterKnownErrors(layerExceptions); 106 114 noRestartExceptions = filterKnownErrors(noRestartExceptions); 115 testCodeHashCollisions = filterKnownErrors(testCodeHashCollisions); 107 116 108 117 String msg = errMsg("invalidManifestEntries", invalidManifestEntries) + '\n' + 109 118 errMsg("loadingExceptions", loadingExceptions) + '\n' + 110 119 errMsg("layerExceptions", layerExceptions) + '\n' + 111 errMsg("noRestartExceptions", noRestartExceptions); 120 errMsg("noRestartExceptions", noRestartExceptions) + '\n' + 121 errMsg("testCodeHashCollisions", testCodeHashCollisions); 112 122 assertTrue(invalidManifestEntries.isEmpty() 113 123 && loadingExceptions.isEmpty() 114 124 && layerExceptions.isEmpty() 115 && noRestartExceptions.isEmpty(), msg); 125 && noRestartExceptions.isEmpty() 126 && testCodeHashCollisions.isEmpty(), msg); 116 127 } 117 128 … … 122 133 private static void testCompletelyRestartlessPlugins(List<PluginInformation> loadedPlugins, 123 134 Map<String, Throwable> noRestartExceptions) { 135 final List<LogRecord> records = new ArrayList<>(); 136 Handler tempHandler = new Handler() { 137 @Override 138 public void publish(LogRecord record) { 139 records.add(record); 140 } 141 142 @Override 143 public void flush() { /* Do nothing */ } 144 145 @Override 146 public void close() throws SecurityException { /* Do nothing */ } 147 }; 148 Logging.getLogger().addHandler(tempHandler); 124 149 try { 125 150 List<PluginInformation> restartable = loadedPlugins.parallelStream() … … 142 167 root.printStackTrace(); 143 168 noRestartExceptions.put(findFaultyPlugin(loadedPlugins, root), root); 144 } 169 records.removeIf(record -> Objects.equals(Utils.getRootCause(record.getThrown()), root)); 170 } catch (AssertionError assertionError) { 171 noRestartExceptions.put("Plugin load/unload failed", assertionError); 172 } finally { 173 Logging.getLogger().removeHandler(tempHandler); 174 for (LogRecord record : records) { 175 if (record.getThrown() != null) { 176 Throwable root = Utils.getRootCause(record.getThrown()); 177 root.printStackTrace(); 178 noRestartExceptions.put(findFaultyPlugin(loadedPlugins, root), root); 179 } 180 } 181 } 182 } 183 184 private static Map<String, String> checkForHashCollisions() { 185 Map<Integer, List<String>> codes = new HashMap<>(); 186 for (Class<?> clazz : ReflectionUtils.findAllClassesInPackage("org.openstreetmap", 187 org.openstreetmap.josm.data.validation.Test.class::isAssignableFrom, s -> true)) { 188 if (org.openstreetmap.josm.data.validation.Test.class.isAssignableFrom(clazz) 189 && !Objects.equals(org.openstreetmap.josm.data.validation.Test.class, clazz)) { 190 // clazz.getName().hashCode() is how the base error codes are calculated since xxx 191 // We want to avoid cases where the hashcode is too close, so we want to 192 // ensure that there is at least 1m available codes after the hashCode. 193 // This is needed since some plugins pick some really large number, and count up from there. 194 int hashCeil = (int) Math.ceil(clazz.getName().hashCode() / 1_000_000d); 195 int hashFloor = (int) Math.floor(clazz.getName().hashCode() / 1_000_000d); 196 codes.computeIfAbsent(hashCeil, k -> new ArrayList<>()).add(clazz.getName()); 197 codes.computeIfAbsent(hashFloor, k -> new ArrayList<>()).add(clazz.getName()); 198 } 199 } 200 return codes.entrySet().stream().filter(entry -> entry.getValue().size() > 1).collect( 201 Collectors.toMap(entry -> entry.getKey().toString(), entry -> String.join(", ", entry.getValue()))); 145 202 } 146 203 … … 154 211 System.out.println(invalidManifestEntries.entrySet() 155 212 .stream() 156 .map( e ->convertEntryToString(e))213 .map(PluginHandlerTestIT::convertEntryToString) 157 214 .collect(Collectors.joining(", "))); 158 215 } … … 242 299 try { 243 300 ClassLoader cl = PluginHandler.getPluginClassLoader(p.getName()); 301 assertNotNull(cl); 244 302 String pluginPackage = cl.loadClass(p.className).getPackage().getName(); 245 303 for (StackTraceElement e : root.getStackTrace()) {
Note:
See TracChangeset
for help on using the changeset viewer.