DefaultPackageArchitectureFactory { public Architecture createArchitecture() { return createArchitecture(0); } DefaultPackageArchitectureFactory(DependencyGraph dependencyGraph); void setArchitectureName(String architectureName); void setPathSeparator(String separator); Architecture createArchitecture(); Architecture createArchitecture(int maxLength);  }
@Test public void testCreateArchitectureMoreComponents() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); dependencyGraph.getOrCreateNodeByName("com.example.api.A"); dependencyGraph.getOrCreateNodeByName("com.example.api.B"); dependencyGraph.getOrCreateNodeByName("com.example.impl.AImpl"); Architecture architecture = new DefaultPackageArchitectureFactory(dependencyGraph).createArchitecture(); assertThat(architecture.getName(), is("Package")); assertThat(architecture.getAllComponents(), hasSize(4)); assertThat(getComponentNameList(architecture), is(Lists.newArrayList("com.example.impl", "com.example.api", "com.example", "com"))); assertThat(architecture.getParentComponentName("com.example.api.A"), is("com.example.api")); assertThat(architecture.getParentComponentName("com.example.impl.AImpl"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.impl.AnotherImpl"), is("com.example.impl")); Component component = architecture.getApiNameToComponent().get("com.example.api"); assertThat(component.getName(), is("com.example.api")); ClassSet apiDef = component.getApi().get("com.example.api"); assertThat(apiDef.getPatterns(), hasSize(1)); assertThat(apiDef.getPatterns().get(0), is("com.example.api.**")); Component impl = architecture.getApiNameToComponent().get("com.example.impl"); assertThat(impl.getName(), is("com.example.impl")); ClassSet implDef = impl.getApi().get("com.example.impl"); assertThat(implDef.getPatterns(), hasSize(1)); assertThat(implDef.getPatterns().get(0), is("com.example.impl.**")); }
@Test public void testCreateArchitectureNestedComponents() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); dependencyGraph.getOrCreateNodeByName("com.example.api.A"); dependencyGraph.getOrCreateNodeByName("com.example.api.B"); dependencyGraph.getOrCreateNodeByName("com.example.impl.AImpl"); dependencyGraph.getOrCreateNodeByName("com.example.impl.a.X"); dependencyGraph.getOrCreateNodeByName("com.example.impl.b.Y"); dependencyGraph.getOrCreateNodeByName("org.abc.api.d.z"); Architecture architecture = new DefaultPackageArchitectureFactory(dependencyGraph).createArchitecture(); assertThat(architecture.getName(), is("Package")); assertThat(architecture.getAllComponents(), hasSize(10)); assertThat(getComponentNameList(architecture), is(Lists.newArrayList("com.example.impl.a", "com.example.impl.b", "com.example.impl", "com.example.api", "org.abc.api.d", "com.example", "org.abc.api", "org.abc", "com", "org"))); assertThat(architecture.getParentComponentName("com.example.api.A"), is("com.example.api")); assertThat(architecture.getParentComponentName("com.example.impl.AImpl"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.impl.AnotherImpl"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.impl.a.Something"), is("com.example.impl.a")); assertThat(architecture.getParentComponentName("com.example.impl.b.Something"), is("com.example.impl.b")); assertThat(architecture.getParentComponentName("com.example.impl.c.Something"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.SomethingElse"), is("com.example")); Component component = architecture.getApiNameToComponent().get("com.example.api"); assertThat(component.getName(), is("com.example.api")); ClassSet apiDef = component.getApi().get("com.example.api"); assertThat(apiDef.getPatterns(), hasSize(1)); assertThat(apiDef.getPatterns().get(0), is("com.example.api.**")); Component impl = architecture.getApiNameToComponent().get("com.example.impl.a"); assertThat(impl.getName(), is("com.example.impl.a")); ClassSet implDef = impl.getApi().get("com.example.impl.a"); assertThat(implDef.getPatterns(), hasSize(1)); assertThat(implDef.getPatterns().get(0), is("com.example.impl.a.**")); }
@Test public void testCreateArchitectureNestedComponentsLimitedDepth() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); dependencyGraph.getOrCreateNodeByName("com.example.api.A"); dependencyGraph.getOrCreateNodeByName("com.example.api.B"); dependencyGraph.getOrCreateNodeByName("com.example.impl.AImpl"); dependencyGraph.getOrCreateNodeByName("com.example.impl.a.X"); dependencyGraph.getOrCreateNodeByName("com.example.impl.b.Y"); dependencyGraph.getOrCreateNodeByName("org.abc.api.d.z"); Architecture architecture = new DefaultPackageArchitectureFactory(dependencyGraph).createArchitecture(3); assertThat(architecture.getName(), is("Package-3")); assertThat(architecture.getAllComponents(), hasSize(7)); assertThat(getComponentNameList(architecture), is(Lists.newArrayList("com.example.impl", "com.example.api", "com.example", "org.abc.api", "org.abc", "com", "org"))); assertThat(architecture.getParentComponentName("com.example.api.A"), is("com.example.api")); assertThat(architecture.getParentComponentName("com.example.impl.AImpl"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.impl.AnotherImpl"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.impl.a.Something"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.impl.b.Something"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.impl.c.Something"), is("com.example.impl")); assertThat(architecture.getParentComponentName("com.example.SomethingElse"), is("com.example")); Component component = architecture.getApiNameToComponent().get("com.example.api"); assertThat(component.getName(), is("com.example.api")); ClassSet apiDef = component.getApi().get("com.example.api"); assertThat(apiDef.getPatterns(), hasSize(1)); assertThat(apiDef.getPatterns().get(0), is("com.example.api.**")); Component impl = architecture.getApiNameToComponent().get("com.example.impl"); assertThat(impl.getName(), is("com.example.impl")); ClassSet implDef = impl.getApi().get("com.example.impl"); assertThat(implDef.getPatterns(), hasSize(1)); assertThat(implDef.getPatterns().get(0), is("com.example.impl.**")); }
@Test public void testCreateArchitectureEmptyGraph() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); Architecture architecture = new DefaultPackageArchitectureFactory(dependencyGraph).createArchitecture(); assertThat(architecture.getName(), is("Package")); assertThat(architecture.getAllComponents(), hasSize(0)); }
@Test public void testCreateArchitectureOnePackage() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); dependencyGraph.getOrCreateNodeByName("ClassInDefaultPackage"); Architecture architecture = new DefaultPackageArchitectureFactory(dependencyGraph).createArchitecture(); assertThat(architecture.getName(), is("Package")); assertThat(architecture.getAllComponents(), hasSize(1)); assertThat(getComponentNameList(architecture), is(Lists.newArrayList("ClassInDefaultPackage"))); assertThat(architecture.getParentComponent("ClassInDefaultPackage"), is(architecture)); assertThat(architecture.getParentComponent("anythingElse"), nullValue()); }
@Test public void testCreateArchitectureTwoPackages() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); dependencyGraph.getOrCreateNodeByName("org.a"); Architecture architecture = new DefaultPackageArchitectureFactory(dependencyGraph).createArchitecture(); assertThat(architecture.getName(), is("Package")); assertThat(architecture.getAllComponents(), hasSize(1)); assertThat(getComponentNameList(architecture), is(Lists.newArrayList("org"))); assertThat(architecture.getParentComponent("org.a.A").getName(), is("org")); assertThat(architecture.getParentComponent("org.a").getName(), is("org")); assertThat(architecture.getParentComponent("org"), is(architecture)); assertThat(architecture.getParentComponent("anythingElse"), nullValue()); }
@Test public void testCreateArchitecture() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); dependencyGraph.getOrCreateNodeByName("com.example.api.A"); dependencyGraph.getOrCreateNodeByName("com.example.api.B"); dependencyGraph.getOrCreateNodeByName("com.example.api.C"); DefaultPackageArchitectureFactory factory = new DefaultPackageArchitectureFactory(dependencyGraph); factory.setArchitectureName("my-architecture"); Architecture architecture = factory.createArchitecture(); assertThat(architecture.getName(), is("my-architecture")); assertThat(architecture.getAllComponents(), hasSize(3)); assertThat(getComponentNameList(architecture), is(Lists.newArrayList("com.example.api", "com.example", "com"))); assertThat(architecture.getParentComponentName("com.example.api.A"), is("com.example.api")); Component component = architecture.getApiNameToComponent().get("com.example.api"); assertThat(component.getName(), is("com.example.api")); ClassSet apiDef = component.getApi().get("com.example.api"); assertThat(apiDef.getPatterns(), hasSize(1)); assertThat(apiDef.getPatterns().get(0), is("com.example.api.**")); }