@Test public void test_findComponents_UsesTheConfiguredDuplicateComponentFinderStrategy() throws Exception { try { SpringComponentFinderStrategy springComponentFinderStrategy = new SpringComponentFinderStrategy(); springComponentFinderStrategy.setDuplicateComponentStrategy(new DuplicateComponentStrategy() { @Override public Component duplicateComponentFound(Component component, String name, String type, String description, String technology) { return null; } }); ComponentFinder componentFinder = new ComponentFinder( webApplication, "com.structurizr.analysis.myapp", springComponentFinderStrategy ); componentFinder.findComponents(); componentFinder.findComponents(); } catch (DuplicateComponentException dce) { fail(); } }
protected Set<Component> findClassesWithAnnotation(Class<? extends Annotation> type, String technology, boolean includePublicTypesOnly) { Set<Component> components = new HashSet<>(); Set<Class<?>> componentTypes = findTypesAnnotatedWith(type); for (Class<?> componentType : componentTypes) { if (!includePublicTypesOnly || Modifier.isPublic(componentType.getModifiers())) { final Container container = getComponentFinder().getContainer(); Component newComponent = addComponent( container, componentType.getSimpleName(), componentType.getCanonicalName(), "", technology); if (newComponent != null) { components.add(newComponent); } } } return components; }
/** * Gets the type repository used to analyse java classes. * * @return the type supplied type repository, or a default implementation */ public TypeRepository getTypeRepository() { if (typeRepository == null) { typeRepository = new DefaultTypeRepository(getPackageNames(), getExclusions(), getUrlClassLoader()); } return typeRepository; }
ComponentFinder componentFinder = new ComponentFinder( webApplication, "org.springframework.samples.petclinic", new SourceCodeComponentFinderStrategy(new File(sourceRoot, "/src/main/java/"), 150)); componentFinder.exclude(".*Formatter.*"); componentFinder.findComponents();
@Test public void test_addPackageName_ThrowsAnException_WhenAnEmptyPackageNameIsSpecified() { try { Container container = model.addSoftwareSystem("Software System", "").addContainer("Container", "", ""); ComponentFinder componentFinder = new ComponentFinder(container, "com.mycompany.myapp", new TypeMatcherComponentFinderStrategy(new NameSuffixTypeMatcher("Component", "", ""))); componentFinder.addPackageName(" "); fail(); } catch (IllegalArgumentException iae) { assertEquals("A package name must be specified.", iae.getMessage()); } }
@Test public void test_construction_ThrowsAnException_WhenANullContainerIsSpecified() { try { new ComponentFinder(null, null); fail(); } catch (IllegalArgumentException iae) { assertEquals("A container must be specified.", iae.getMessage()); } }
@Test public void test_findingDuplicateComponentsThrowsAnException() throws Exception { try { componentFinder.findComponents(); fail(); } catch (IllegalArgumentException iae) { assertTrue(iae.getMessage().startsWith("A component named '")); assertTrue(iae.getMessage().endsWith("' already exists for this container.")); } }
protected TypeRepository getTypeRepository() { return componentFinder.getTypeRepository(); }
parameters.add(sourcePath.getCanonicalPath()); parameters.add("-subpackages"); parameters.add(String.join(":", componentFinder.getPackageNames()));
@Test public void test_addPackageName_ThrowsAnException_WhenANullPackageNameIsSpecified() { try { Container container = model.addSoftwareSystem("Software System", "").addContainer("Container", "", ""); ComponentFinder componentFinder = new ComponentFinder(container, "com.mycompany.myapp", new TypeMatcherComponentFinderStrategy(new NameSuffixTypeMatcher("Component", "", ""))); componentFinder.addPackageName(null); fail(); } catch (IllegalArgumentException iae) { assertEquals("A package name must be specified.", iae.getMessage()); } }
@Test public void test_construction_ThrowsAnException_WhenANullPackageNameIsSpecified() { try { Container container = model.addSoftwareSystem("Software System", "").addContainer("Container", "", ""); new ComponentFinder(container, null); fail(); } catch (IllegalArgumentException iae) { assertEquals("A package name must be specified.", iae.getMessage()); } }
@Test public void test_findingDuplicateComponentsThrowsAnException() throws Exception { try { componentFinder.findComponents(); fail(); } catch (DuplicateComponentException dce) { assertTrue(dce.getMessage().startsWith("A component named \"")); assertTrue(dce.getMessage().endsWith("\" already exists in the container named \"Name\".")); } }
@Test public void test_findComponents_TruncatesComponentDescriptions_WhenComponentDescriptionsAreTooLong() throws Exception { ComponentFinder componentFinder = new ComponentFinder( webApplication, "test.SourceCodeComponentFinderStrategy", new SourceCodeComponentFinderStrategy(sourcePath, 10) ); componentFinder.findComponents(); assertEquals("A compo...", someComponent.getDescription()); }
final Container container = getComponentFinder().getContainer(); Component newComponent = addComponent( container,
@Test public void test_construction_ThrowsAnException_WhenNoComponentFinderStrategiesAreSpecified() { try { Container container = model.addSoftwareSystem("Software System", "").addContainer("Container", "", ""); new ComponentFinder(container, "com.mycompany.myapp"); fail(); } catch (IllegalArgumentException iae) { assertEquals("One or more ComponentFinderStrategy objects must be specified.", iae.getMessage()); } }
@Test public void test_findComponents_DoesNotSetTheComponentDescription_WhenTheComponentAlreadyHasADescription() throws Exception { someComponent.setDescription("An existing description."); ComponentFinder componentFinder = new ComponentFinder( webApplication, "test.SourceCodeComponentFinderStrategy", new SourceCodeComponentFinderStrategy(sourcePath, 10) ); componentFinder.findComponents(); assertEquals("An existing description.", someComponent.getDescription()); }
@Override protected Set<Component> doFindComponents() { Set<Component> components = new HashSet<>(); Container container = getComponentFinder().getContainer(); // find all types that have been annotated @Component Set<Class<?>> componentTypes = findTypesAnnotatedWith(com.structurizr.annotation.Component.class); for (Class<?> componentType : componentTypes) { Component component = container.addComponent( componentType.getSimpleName(), componentType, componentType.getAnnotation(com.structurizr.annotation.Component.class).description(), componentType.getAnnotation(com.structurizr.annotation.Component.class).technology() ); components.add(component); } return components; }
@Test public void test_findComponents() throws Exception { ComponentFinder componentFinder = new ComponentFinder( webApplication, "test.SourceCodeComponentFinderStrategy", new SourceCodeComponentFinderStrategy(sourcePath) ); componentFinder.findComponents(); assertEquals("A component that does something.", someComponent.getDescription()); assertEquals(20, someComponent.getSize()); }
private void findDependencies() { for (Component component : componentFinder.getContainer().getComponents()) { for (CodeElement codeElement : component.getCode()) { addEfferentDependencies(component, codeElement.getType(), new HashSet<>()); } } }
@Test public void test_findComponents_CorrectlyFindsDependenciesBetweenComponentsFoundByDifferentComponentFinders_WhenPackage1IsScannedFirst() throws Exception { ComponentFinder componentFinder1 = new ComponentFinder( webApplication, "com.structurizr.analysis.reflections.multipleComponentFinders.package1", new TypeMatcherComponentFinderStrategy( new NameSuffixTypeMatcher("Controller", "", "") ) ); ComponentFinder componentFinder2 = new ComponentFinder( webApplication, "com.structurizr.analysis.reflections.multipleComponentFinders.package2", new TypeMatcherComponentFinderStrategy( new NameSuffixTypeMatcher("Repository", "", "") ) ); componentFinder1.findComponents(); componentFinder2.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()); }