public StructurizrAnnotationsComponentFinderStrategy() { super(new FirstImplementationOfInterfaceSupportingTypesStrategy()); }
@Override public Set<Class<?>> findSupportingTypes(Component component) { Set<Class<?>> set = new HashSet<>(); try { Class componentType = getTypeRepository().loadClass(component.getType().getType()); if (componentType.isInterface()) { Class type = TypeUtils.findFirstImplementationOfInterface(componentType, getTypeRepository().getAllTypes()); if (type != null) { set.add(type); } } } catch (ClassNotFoundException e) { log.warn("Could not load type " + component.getType().getType()); } return set; }
@Test public void test_findComponents_CorrectlyFindsSupportingTypes_WhenTheReferencedTypesInSamePackageSupportingTypesStrategyIsUsed() throws Exception { ComponentFinder componentFinder = new ComponentFinder( webApplication, "com.structurizr.analysis.reflections.supportingTypes.myapp", new StructurizrAnnotationsComponentFinderStrategy( new FirstImplementationOfInterfaceSupportingTypesStrategy(), new ReferencedTypesInSamePackageSupportingTypesStrategy() ) ); componentFinder.findComponents(); assertEquals(2, webApplication.getComponents().size()); Component myController = webApplication.getComponentWithName("MyController"); Component myRepository = webApplication.getComponentWithName("MyRepository"); assertEquals(1, myController.getRelationships().size()); assertNotNull(myController.getRelationships().stream().filter(r -> r.getDestination() == myRepository).findFirst().get()); assertEquals(1, myController.getCode().size()); assertEquals(3, myRepository.getCode().size()); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepository", CodeElementRole.Primary); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepositoryImpl", CodeElementRole.Supporting); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepositoryRowMapper", CodeElementRole.Supporting); }
@Test public void test_findComponents_CorrectlyFindsSupportingTypes_WhenTheReferencedTypesStrategyIsUsedAndIndirectlyReferencedTypesShouldBeExcluded() throws Exception { ComponentFinder componentFinder = new ComponentFinder( webApplication, "com.structurizr.analysis.reflections.supportingTypes.myapp", new StructurizrAnnotationsComponentFinderStrategy( new FirstImplementationOfInterfaceSupportingTypesStrategy(), new ReferencedTypesSupportingTypesStrategy(false) ) ); componentFinder.findComponents(); assertEquals(2, webApplication.getComponents().size()); Component myController = webApplication.getComponentWithName("MyController"); Component myRepository = webApplication.getComponentWithName("MyRepository"); assertEquals(1, myController.getRelationships().size()); assertNotNull(myController.getRelationships().stream().filter(r -> r.getDestination() == myRepository).findFirst().get()); assertEquals(2, myController.getCode().size()); assertCodeElementInComponent(myController, "com.structurizr.analysis.reflections.supportingTypes.myapp.web.MyController", CodeElementRole.Primary); assertCodeElementInComponent(myController, "com.structurizr.analysis.reflections.supportingTypes.myapp.AbstractComponent", CodeElementRole.Supporting); assertEquals(4, myRepository.getCode().size()); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepository", CodeElementRole.Primary); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.AbstractComponent", CodeElementRole.Supporting); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepositoryImpl", CodeElementRole.Supporting); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepositoryRowMapper", CodeElementRole.Supporting); }
@Test public void test_findComponents_CorrectlyFindsSupportingTypes_WhenTheReferencedTypesStrategyIsUsedAndIndirectlyReferencedTypesShouldBeIncluded() throws Exception { ComponentFinder componentFinder = new ComponentFinder( webApplication, "com.structurizr.analysis.reflections.supportingTypes.myapp", new StructurizrAnnotationsComponentFinderStrategy( new FirstImplementationOfInterfaceSupportingTypesStrategy(), new ReferencedTypesSupportingTypesStrategy() ) ); componentFinder.findComponents(); assertEquals(2, webApplication.getComponents().size()); Component myController = webApplication.getComponentWithName("MyController"); Component myRepository = webApplication.getComponentWithName("MyRepository"); assertEquals(1, myController.getRelationships().size()); assertNotNull(myController.getRelationships().stream().filter(r -> r.getDestination() == myRepository).findFirst().get()); assertEquals(2, myController.getCode().size()); assertCodeElementInComponent(myController, "com.structurizr.analysis.reflections.supportingTypes.myapp.web.MyController", CodeElementRole.Primary); assertCodeElementInComponent(myController, "com.structurizr.analysis.reflections.supportingTypes.myapp.AbstractComponent", CodeElementRole.Supporting); assertEquals(5, myRepository.getCode().size()); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepository", CodeElementRole.Primary); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.AbstractComponent", CodeElementRole.Supporting); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepositoryImpl", CodeElementRole.Supporting); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.data.MyRepositoryRowMapper", CodeElementRole.Supporting); assertCodeElementInComponent(myRepository, "com.structurizr.analysis.reflections.supportingTypes.myapp.util.RowMapperHelper", CodeElementRole.Supporting); }