public FakePluginInstaller add(String pluginKey, File jarFile, long lastUpdatedAt) { pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginKey, lastUpdatedAt, PluginInfo.create(jarFile))); return this; }
@Override public PluginInfo getPluginInfo(String key) { ScannerPlugin info = pluginsByKeys.get(key); Preconditions.checkState(info != null, "Plugin [%s] does not exist", key); return info.getInfo(); }
private void logPlugins() { if (pluginsByKeys.isEmpty()) { LOG.debug("No plugins loaded"); } else { LOG.debug("Plugins:"); for (ScannerPlugin p : pluginsByKeys.values()) { LOG.debug(" * {} {} ({})", p.getName(), p.getVersion(), p.getKey()); } } }
@Test public void download_installed_plugins() throws IOException { WsTestUtil.mockReader(wsClient, "api/plugins/installed", new InputStreamReader(getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json"))); enqueueDownload("scmgit", "abc"); enqueueDownload("java", "def"); when(pluginPredicate.apply(any())).thenReturn(true); Map<String, ScannerPlugin> result = underTest.installRemotes(); assertThat(result.keySet()).containsExactlyInAnyOrder("scmgit", "java"); ScannerPlugin gitPlugin = result.get("scmgit"); assertThat(gitPlugin.getKey()).isEqualTo("scmgit"); assertThat(gitPlugin.getInfo().getNonNullJarFile()).exists().isFile(); assertThat(gitPlugin.getUpdatedAt()).isEqualTo(100L); ScannerPlugin javaPlugin = result.get("java"); assertThat(javaPlugin.getKey()).isEqualTo("java"); assertThat(javaPlugin.getInfo().getNonNullJarFile()).exists().isFile(); assertThat(javaPlugin.getUpdatedAt()).isEqualTo(200L); }
builder.putPluginsByKey(pluginEntry.getKey(), ScannerReport.Metadata.Plugin.newBuilder() .setKey(pluginEntry.getKey()) .setUpdatedAt(pluginEntry.getValue().getUpdatedAt()).build());
builder.putPluginsByKey(pluginEntry.getKey(), ScannerReport.Metadata.Plugin.newBuilder() .setKey(pluginEntry.getKey()) .setUpdatedAt(pluginEntry.getValue().getUpdatedAt()).build());
private void logPlugins() { if (pluginsByKeys.isEmpty()) { LOG.debug("No plugins loaded"); } else { LOG.debug("Plugins:"); for (ScannerPlugin p : pluginsByKeys.values()) { LOG.debug(" * {} {} ({})", p.getName(), p.getVersion(), p.getKey()); } } }
@Override public void start() { pluginsByKeys = new HashMap<>(installer.installRemotes()); pluginInstancesByKeys = new HashMap<>( loader.load(pluginsByKeys.values().stream() .map(ScannerPlugin::getInfo) .collect(toMap(PluginInfo::getKey, Function.identity())))); // this part is only used by medium tests for (Object[] localPlugin : installer.installLocals()) { String pluginKey = (String) localPlugin[0]; PluginInfo pluginInfo = new PluginInfo(pluginKey); pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginInfo.getKey(), (long) localPlugin[2], pluginInfo)); pluginInstancesByKeys.put(pluginKey, (Plugin) localPlugin[1]); } keysByClassLoader = new HashMap<>(); for (Map.Entry<String, Plugin> e : pluginInstancesByKeys.entrySet()) { keysByClassLoader.put(e.getValue().getClass().getClassLoader(), e.getKey()); } logPlugins(); }
@Override public PluginInfo getPluginInfo(String key) { ScannerPlugin info = pluginsByKeys.get(key); Preconditions.checkState(info != null, "Plugin [%s] does not exist", key); return info.getInfo(); }
private Loaded loadPlugins(Map<String, ScannerPlugin> result) { for (InstalledPlugin plugin : listInstalledPlugins()) { if (pluginPredicate.apply(plugin.key)) { Optional<File> jarFile = pluginFiles.get(plugin); if (!jarFile.isPresent()) { return new Loaded(false, plugin.key); } PluginInfo info = PluginInfo.create(jarFile.get()); result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, info)); } } return new Loaded(true, null); }
@Test public void install_and_load_plugins() { PluginInfo info = new PluginInfo("squid"); ImmutableMap<String, ScannerPlugin> plugins = ImmutableMap.of("squid", new ScannerPlugin("squid", 1L, info)); Plugin instance = mock(Plugin.class); when(loader.load(anyMap())).thenReturn(ImmutableMap.of("squid", instance)); when(installer.installRemotes()).thenReturn(plugins); underTest.start(); assertThat(underTest.getPluginInfos()).containsOnly(info); assertThat(underTest.getPluginsByKey()).isEqualTo(plugins); assertThat(underTest.getPluginInfo("squid")).isSameAs(info); assertThat(underTest.getPluginInstance("squid")).isSameAs(instance); underTest.stop(); verify(loader).unload(anyCollection()); }
@Test public void write_metadata() throws Exception { Date date = new Date(); when(qProfiles.findAll()).thenReturn(asList(new QProfile("q1", "Q1", "java", date))); when(pluginRepository.getPluginsByKey()).thenReturn(ImmutableMap.of( "java", new ScannerPlugin("java", 12345L, null), "php", new ScannerPlugin("php", 45678L, null))); File outputDir = temp.newFolder(); ScannerReportWriter writer = new ScannerReportWriter(outputDir); underTest.publish(writer); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getAnalysisDate()).isEqualTo(1234567L); assertThat(metadata.getProjectKey()).isEqualTo("root"); assertThat(metadata.getModulesProjectRelativePathByKeyMap()).containsOnly(entry("module", "modulePath"), entry("root", "")); assertThat(metadata.getQprofilesPerLanguageMap()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.QProfile.newBuilder() .setKey("q1") .setName("Q1") .setLanguage("java") .setRulesUpdatedAt(date.getTime()) .build())); assertThat(metadata.getPluginsByKey()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() .setKey("java") .setUpdatedAt(12345) .build()), entry("php", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() .setKey("php") .setUpdatedAt(45678) .build())); }
public FakePluginInstaller add(String pluginKey, File jarFile, long lastUpdatedAt) { pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginKey, lastUpdatedAt, PluginInfo.create(jarFile))); return this; }
@Override public void start() { pluginsByKeys = new HashMap<>(installer.installRemotes()); pluginInstancesByKeys = new HashMap<>( loader.load(pluginsByKeys.values().stream() .map(ScannerPlugin::getInfo) .collect(toMap(PluginInfo::getKey, Function.identity())))); // this part is only used by medium tests for (Object[] localPlugin : installer.installLocals()) { String pluginKey = (String) localPlugin[0]; PluginInfo pluginInfo = new PluginInfo(pluginKey); pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginInfo.getKey(), (long) localPlugin[2], pluginInfo)); pluginInstancesByKeys.put(pluginKey, (Plugin) localPlugin[1]); } keysByClassLoader = new HashMap<>(); for (Map.Entry<String, Plugin> e : pluginInstancesByKeys.entrySet()) { keysByClassLoader.put(e.getValue().getClass().getClassLoader(), e.getKey()); } logPlugins(); }
private Loaded loadPlugins(Map<String, ScannerPlugin> result) { for (InstalledPlugin plugin : listInstalledPlugins()) { if (pluginPredicate.apply(plugin.key)) { Optional<File> jarFile = pluginFiles.get(plugin); if (!jarFile.isPresent()) { return new Loaded(false, plugin.key); } PluginInfo info = PluginInfo.create(jarFile.get()); result.put(info.getKey(), new ScannerPlugin(plugin.key, plugin.updatedAt, info)); } } return new Loaded(true, null); }