DefaultPackageArchitectureFactory { String getComponentName(String name, int maxLength) { checkNotNull(name, "name"); String[] parts = name.split(pathSplitterRegExp, maxLength == 0 ? 0 : (maxLength + 1)); if (parts.length == 1) { return null; } int endIndex = maxLength == 0 ? (parts.length - 1) : Math.min(maxLength, parts.length - 1); return StringUtils.join(parts, pathSeparator, 0, endIndex); } DefaultPackageArchitectureFactory(DependencyGraph dependencyGraph); void setArchitectureName(String architectureName); void setPathSeparator(String separator); Architecture createArchitecture(); Architecture createArchitecture(int maxLength);  }
@Test public void testGetComponentName() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); DefaultPackageArchitectureFactory factory = new DefaultPackageArchitectureFactory(dependencyGraph); assertThat(factory.getComponentName("com.example.api.A", 0), is("com.example.api")); assertThat(factory.getComponentName("com.example.api", 0), is("com.example")); assertThat(factory.getComponentName("com.example", 0), is("com")); assertThat(factory.getComponentName("com", 0), nullValue()); assertThat(factory.getComponentName("", 0), nullValue()); try { factory.getComponentName(null, 0); fail("Expected NPE"); } catch(NullPointerException e) { assertThat(e.getMessage(), is("name")); } }
@Test public void testGetComponentNameWithMaxLength() { DependencyGraph dependencyGraph = DependencyGraphFactory.createGraph(); DefaultPackageArchitectureFactory factory = new DefaultPackageArchitectureFactory(dependencyGraph); assertThat(factory.getComponentName("com.example.app.web.api.A", 4), is("com.example.app.web")); assertThat(factory.getComponentName("com.example.app.biz.api.B", 4), is("com.example.app.biz")); assertThat(factory.getComponentName("com.example.app.db.api.C", 4), is("com.example.app.db")); assertThat(factory.getComponentName("com.example.api.A", 4), is("com.example.api")); assertThat(factory.getComponentName("com.example.api", 4), is("com.example")); assertThat(factory.getComponentName("com.example", 4), is("com")); assertThat(factory.getComponentName("com", 4), nullValue()); assertThat(factory.getComponentName("", 4), nullValue()); try { factory.getComponentName(null, 4); fail("Expected NPE"); } catch(NullPointerException e) { assertThat(e.getMessage(), is("name")); } }