RuntimeModuleMerger { Collection<BQModule> toDIModules(Collection<BQModuleMetadata> bqModulesMetadata) { ModuleGraph moduleGraph = new ModuleGraph(bqModulesMetadata.size()); Map<Class<? extends BQModule>, BQModuleMetadata> moduleByClass = new HashMap<>(); bqModulesMetadata.forEach(metadata -> { Class<? extends BQModule> moduleClass = metadata.getModule().getClass(); moduleByClass.putIfAbsent(moduleClass, metadata); moduleGraph.add(metadata); if(metadata.getOverrides().isEmpty()) { bootLogger.trace(() -> traceMessage(metadata, null)); } }); bqModulesMetadata.forEach(metadata -> metadata.getOverrides() .forEach(override -> { BQModuleMetadata overrideModule = moduleByClass.get(override); moduleGraph.add(metadata, overrideModule); bootLogger.trace(() -> traceMessage(metadata, overrideModule)); })); List<BQModule> modules = new ArrayList<>(moduleByClass.size()); moduleGraph.topSort().forEach(moduleClass -> modules.add(moduleClass.getModule())); return modules; } RuntimeModuleMerger(BootLogger bootLogger);   }
@Test public void testGetModules_Empty() { assertTrue(new RuntimeModuleMerger(bootLogger).toDIModules(Collections.emptyList()).isEmpty()); }
@Test public void testGetModules_One() { Collection<BQModuleMetadata> bqModules = Arrays.asList(mockBqModules.get(2)); Collection<BQModule> modules = new RuntimeModuleMerger(bootLogger).toDIModules(bqModules); assertEquals(1, modules.size()); assertTrue(modules.contains(testModules.get(2))); }
@Test public void testGetModules_Two() { Collection<BQModuleMetadata> bqModules = Arrays.asList(mockBqModules.get(2), mockBqModules.get(1)); Collection<BQModule> modules = new RuntimeModuleMerger(bootLogger).toDIModules(bqModules); assertEquals(2, modules.size()); assertTrue(modules.contains(testModules.get(1))); assertTrue(modules.contains(testModules.get(2))); }
@Test public void testGetModules_Three_Dupes() { Collection<BQModuleMetadata> bqModules = Arrays.asList(mockBqModules.get(2), mockBqModules.get(1), mockBqModules.get(2)); Collection<BQModule> modules = new RuntimeModuleMerger(bootLogger).toDIModules(bqModules); assertEquals(2, modules.size()); assertTrue(modules.contains(testModules.get(1))); assertTrue(modules.contains(testModules.get(2))); }
@Test public void testGetModules_Overrides() { mockBqModules.set(0, createBQModule(testModules.get(0), M3.class)); Collection<BQModuleMetadata> bqModules = Arrays.asList(mockBqModules.get(0), mockBqModules.get(3)); Collection<BQModule> modules = new RuntimeModuleMerger(bootLogger).toDIModules(bqModules); assertEquals(2, modules.size()); }
@Test public void testGetModules_Overrides_Chain() { mockBqModules.set(0, createBQModule(testModules.get(0), M3.class)); mockBqModules.set(3, createBQModule(testModules.get(3), M4.class)); Collection<BQModuleMetadata> bqModules = Arrays.asList(mockBqModules.get(4), mockBqModules.get(0), mockBqModules.get(1), mockBqModules.get(3)); Collection<BQModule> modules = new RuntimeModuleMerger(bootLogger).toDIModules(bqModules); assertEquals(4, modules.size()); assertTrue(modules.contains(testModules.get(4))); assertTrue(modules.contains(testModules.get(1))); assertTrue(modules.contains(testModules.get(0))); assertTrue(modules.contains(testModules.get(3))); }
@Test public void testGetModules_OverrideCycle() { mockBqModules.set(0, createBQModule(testModules.get(0), M3.class)); mockBqModules.set(3, createBQModule(testModules.get(3), M0.class)); Collection<BQModuleMetadata> bqModules = Arrays.asList(mockBqModules.get(0), mockBqModules.get(3)); assertThrows(RuntimeException.class, () -> new RuntimeModuleMerger(bootLogger).toDIModules(bqModules)); }
@Test public void testGetModules_OverrideIndirectCycle() { mockBqModules.set(0, createBQModule(testModules.get(0), M3.class)); mockBqModules.set(3, createBQModule(testModules.get(3), M4.class)); mockBqModules.set(4, createBQModule(testModules.get(4), M0.class)); Collection<BQModuleMetadata> bqModules = Arrays.asList(mockBqModules.get(0), mockBqModules.get(4), mockBqModules.get(3)); assertThrows(RuntimeException.class, () -> new RuntimeModuleMerger(bootLogger).toDIModules(bqModules)); }