Stagemonitor { static void initializePluginsInOrder(Collection<String> disabledPlugins, Iterable<StagemonitorPlugin> plugins) { Set<Class<? extends StagemonitorPlugin>> alreadyInitialized = new HashSet<Class<? extends StagemonitorPlugin>>(); Set<StagemonitorPlugin> notYetInitialized = getPluginsToInit(disabledPlugins, plugins); while (!notYetInitialized.isEmpty()) { int countNotYetInitialized = notYetInitialized.size(); for (Iterator<StagemonitorPlugin> iterator = notYetInitialized.iterator(); iterator.hasNext(); ) { StagemonitorPlugin stagemonitorPlugin = iterator.next(); { final List<Class<? extends StagemonitorPlugin>> dependencies = stagemonitorPlugin.dependsOn(); if (dependencies.isEmpty() || alreadyInitialized.containsAll(dependencies)) { initializePlugin(stagemonitorPlugin); iterator.remove(); alreadyInitialized.add(stagemonitorPlugin.getClass()); } } } if (countNotYetInitialized == notYetInitialized.size()) { throw new IllegalStateException("Cyclic dependencies detected: " + notYetInitialized); } } } private  Stagemonitor(); static synchronized void init(); static synchronized void shutDown(); @Deprecated static MetricRegistry getMetricRegistry(); static Metric2Registry getMetric2Registry(); static HealthCheckRegistry getHealthCheckRegistry(); static ConfigurationRegistry getConfiguration(); static T getPlugin(Class<T> plugin); @Deprecated static T getConfiguration(Class<T> plugin); static MeasurementSession getMeasurementSession(); static boolean isStarted(); static List<String> getPathsOfWidgetTabPlugins(); static List<String> getPathsOfWidgetMetricTabPlugins(); @Deprecated static void reset(); @Deprecated static void reset(MeasurementSession measurementSession);  }
@Test public void testInitPluginsCyclicDependency() { final List<StagemonitorPlugin> plugins = Arrays.asList(new PluginNoDependency(), new PluginCyclicA(), new PluginCyclicB()); assertThatThrownBy(() -> Stagemonitor.initializePluginsInOrder(Collections.emptyList(), plugins)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("PluginCyclicA") .hasMessageContaining("PluginCyclicB"); }
@Test public void testInitPlugins_NoDependency() { final PluginNoDependency pluginNoDependency = new PluginNoDependency(); Stagemonitor.initializePluginsInOrder(Collections.emptyList(), Collections.singletonList(pluginNoDependency)); assertThat(pluginNoDependency.isInitialized()).isTrue(); }
@Test public void testInitPlugins_SimpleDependency() { final PluginNoDependency pluginNoDependency = new PluginNoDependency(); final PluginSimpleDependency pluginSimpleDependency = new PluginSimpleDependency(); Stagemonitor.initializePluginsInOrder(Collections.emptyList(), Arrays.asList(pluginNoDependency, pluginSimpleDependency)); assertThat(pluginNoDependency.isInitialized()).isTrue(); assertThat(pluginSimpleDependency.isInitialized()).isTrue(); }
@Test public void testInitPlugins_MultipleDependencies() { final List<StagemonitorPlugin> plugins = Arrays.asList(new PluginMultipleDependencies(), new PluginSimpleDependency2(), new PluginNoDependency(), new PluginSimpleDependency()); Stagemonitor.initializePluginsInOrder(Collections.emptyList(), plugins); for (StagemonitorPlugin plugin : plugins) { assertThat(plugin.isInitialized()).describedAs("{} is not initialized", plugin.getClass().getSimpleName()).isTrue(); } }