@Override public Builder include(String path, HasDisplayData subComponent) { checkNotNull(subComponent, "subComponent argument cannot be null"); checkNotNull(path, "path argument cannot be null"); Path absolutePath = latestPath.extend(path); HasDisplayData existingComponent = visitedPathMap.get(absolutePath); if (existingComponent != null) { throw new IllegalArgumentException( String.format( "Specified path '%s' already used for " + "subcomponent %s. Subcomponents must be included using unique paths.", path, existingComponent)); } return include(absolutePath, subComponent); }
/** * Creates a matcher that matches if the examined {@link DisplayData.Item} contains a path * matching the specified namespace. */ public static Matcher<DisplayData.Item> hasPath(String... paths) { DisplayData.Path path = (paths.length == 0) ? DisplayData.Path.root() : DisplayData.Path.absolute(paths[0], Arrays.copyOfRange(paths, 1, paths.length)); return new FeatureMatcher<DisplayData.Item, DisplayData.Path>( is(path), " with namespace", "namespace") { @Override protected DisplayData.Path featureValueOf(DisplayData.Item actual) { return actual.getPath(); } }; }
@Test public void testRootPath() { DisplayData.Path root = DisplayData.Path.root(); assertThat(root.getComponents(), empty()); }
/** * Construct a path from an absolute component path hierarchy. * * <p>For the root path, use {@link Path#root()}. * * @param firstPath Path of the first sub-component. * @param paths Additional path components. */ public static Path absolute(String firstPath, String... paths) { ImmutableList.Builder<String> builder = ImmutableList.builder(); validatePathElement(firstPath); builder.add(firstPath); for (String path : paths) { validatePathElement(path); builder.add(path); } return new Path(builder.build()); }
private DisplayDataComparison checkSubset( DisplayData displayData, DisplayData included, String path) { DisplayDataComparison comparison = new DisplayDataComparison(displayData.items()); for (Item item : included.items()) { Item matchedItem = displayData .asMap() .get( DisplayData.Identifier.of( DisplayData.Path.absolute(path), item.getNamespace(), item.getKey())); if (matchedItem != null) { comparison.matched(matchedItem); } else { comparison.missing(item); } } return comparison; }
@Test public void testIdentifierEquality() { new EqualsTester() .addEqualityGroup( DisplayData.Identifier.of(DisplayData.Path.absolute("a"), DisplayDataTest.class, "1"), DisplayData.Identifier.of(DisplayData.Path.absolute("a"), DisplayDataTest.class, "1")) .addEqualityGroup( DisplayData.Identifier.of(DisplayData.Path.absolute("b"), DisplayDataTest.class, "1")) .addEqualityGroup( DisplayData.Identifier.of(DisplayData.Path.absolute("a"), Object.class, "1")) .addEqualityGroup( DisplayData.Identifier.of(DisplayData.Path.absolute("a"), DisplayDataTest.class, "2")) .testEquals(); }
/** Path for display data registered by a top-level component. */ public static Path root() { return new Path(ImmutableList.of()); }
@Test public void testAbsoluteValidationNullSubsequentPath() { thrown.expect(NullPointerException.class); DisplayData.Path.absolute("a", "b", null, "c"); }
@Test public void testAbsolute() { DisplayData.Path path = DisplayData.Path.absolute("a", "b", "c"); assertThat(path.getComponents(), hasItems("a", "b", "c")); }
@Test public void testAbsoluteValidationNullFirstPath() { thrown.expect(NullPointerException.class); DisplayData.Path.absolute(null, "foo", "bar"); }
/** * Extend the path by appending a sub-component path. The new path element is added to the end * of the path hierarchy. * * <p>Returns a new {@link Path} instance; the originating {@link Path} is not modified. */ public Path extend(String path) { validatePathElement(path); return new Path( ImmutableList.<String>builder().addAll(components.iterator()).add(path).build()); }
@Test public void testAbsoluteValidationEmptySubsequentPath() { thrown.expect(IllegalArgumentException.class); DisplayData.Path.absolute("a", "b", "", "c"); }
@Test public void testAbsoluteValidationEmptyFirstPath() { thrown.expect(IllegalArgumentException.class); DisplayData.Path.absolute("", "foo", "bar"); }
/** * Collect the {@link DisplayData} from a component. This will traverse all subcomponents * specified via {@link Builder#include} in the given component. Data in this component will be in * a namespace derived from the component. */ public static DisplayData from(HasDisplayData component) { checkNotNull(component, "component argument cannot be null"); InternalBuilder builder = new InternalBuilder(); builder.include(Path.root(), component); return builder.build(); }
@Test public void testExtendPath() { DisplayData.Path a = DisplayData.Path.root().extend("a"); assertThat(a.getComponents(), hasItems("a")); DisplayData.Path b = a.extend("b"); assertThat(b.getComponents(), hasItems("a", "b")); }
@Test public void testPathEquality() { new EqualsTester() .addEqualityGroup(DisplayData.Path.root(), DisplayData.Path.root()) .addEqualityGroup(DisplayData.Path.root().extend("a"), DisplayData.Path.absolute("a")) .addEqualityGroup( DisplayData.Path.root().extend("a").extend("b"), DisplayData.Path.absolute("a", "b")) .testEquals(); }