protected void assertInitialData() { // Now check that the data is all there ... Subgraph subgraph = graph.getSubgraphOfDepth(10).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("/a").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a/b").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a/b").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a/b/c").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/c").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a/b/c").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/c").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a/b/d").getProperty("propB").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/d").getProperty("propB").getFirstValue(), is((Object)"valueB")); assertThat(subgraph.getNode("/a/b/d").getProperty("propC").isSingle(), is(true)); assertThat(subgraph.getNode("/a/b/d").getProperty("propC").getFirstValue(), is((Object)"valueC")); assertThat(subgraph.getNode("/a").getChildrenSegments().size(), is(1)); assertThat(subgraph.getNode("/a").getChildrenSegments().get(0), is(segment("b"))); assertThat(subgraph.getNode("/a/b").getChildrenSegments().size(), is(2)); assertThat(subgraph.getNode("/a/b").getChildrenSegments().get(0), is(segment("c"))); assertThat(subgraph.getNode("/a/b").getChildrenSegments().get(1), is(segment("d"))); assertThat(subgraph.getNode("/a/b/c").getChildrenSegments().isEmpty(), is(true)); assertThat(subgraph.getNode("/a/b/d").getChildrenSegments().isEmpty(), is(true)); }
protected int countNodes( SubgraphNode node, Subgraph subgraph ) { assertThat(node, is(notNullValue())); assertThat(subgraph, is(notNullValue())); int count = 1; for (Location child : node.getChildren()) { count += countNodes(subgraph.getNode(child), subgraph); } return count; }
@Test public void shouldSetPropertyWithString() { String path1 = "/a"; String prop1_name = "prop1"; String value_1 = "blue"; output.setProperty(path1, prop1_name, value_1); String path2 = "/a/b"; String prop2_name = "prop2"; String value_2 = "red"; output.setProperty(path2, prop2_name, value_2); output.close(); Subgraph result = graph.getSubgraphOfDepth(10).at("/"); String v1 = (String)result.getNode(path1).getProperty(prop1_name).getFirstValue(); assertThat(v1, is(value_1)); String v2 = (String)result.getNode(path2).getProperty(prop2_name).getFirstValue(); assertThat(v2, is(value_2)); assertNull(result.getNode("/c")); }
@Test public void shouldAllowAddingRepositorySourceByClassNameAndSettingProperties() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setDescription("description") .setRetryLimit(5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.DESCRIPTION, "description")); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); }
@Test public void shouldAllowAddingRepositorySourceByClassNameAndClasspathAndSettingProperties() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFrom("cp1", "cp2") .setProperty("retryLimit", 5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSPATH, "cp1", "cp2")); }
@Test public void shouldSaveSimpleConfigurationToContentHandler() throws Exception { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setProperty("retryLimit", 5) .and() .save(); GraphHandler handler = new GraphHandler(); configuration.storeTo(handler); Subgraph subgraph = handler.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, "5")); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); }
@Test public void shouldAllowAddingRepositorySourceByClassReferenceAndSettingProperties() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class) .setProperty("retryLimit", 5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.CLASSNAME, InMemoryRepositorySource.class.getName())); }
@Test public void shouldSetReferenceWithString() { String path1 = "/a"; String prop1_name = "prop1"; String value_1 = "blue"; output.setReference(path1, prop1_name, value_1); String path2 = "/a/b"; String prop2_name = "prop2"; String value_2 = "red"; output.setReference(path2, prop2_name, value_2); output.close(); Subgraph result = graph.getSubgraphOfDepth(10).at("/"); String v1 = (String)result.getNode(path1).getProperty(prop1_name).getFirstValue(); assertThat(v1, is(value_1)); String v2 = (String)result.getNode(path2).getProperty(prop2_name).getFirstValue(); assertThat(v2, is(value_2)); assertNull(result.getNode("/c")); }
@Test public void shouldAllowSettingDescriptionOnRepositorySourceUsingPrefixedPropertyName() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setProperty("mode:description", "desc") .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.DESCRIPTION, "desc")); }
@Test public void shouldAllowSettingDescriptionOnRepositorySourceUsingNonPrefixedPropertyName() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setProperty("description", "desc") .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.DESCRIPTION, "desc")); }
@Test public void shouldAllowSettingRetryLimitOnRepositorySourceUsingNonPrefixedPropertyName() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setProperty("retryLimit", 5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); }
@Test public void shouldAllowSettingRetryLimitOnRepositorySourceUsingPrefixedPropertyName() { // Update the configuration and save it ... configuration.repositorySource("Source1") .usingClass(InMemoryRepositorySource.class.getName()) .loadedFromClasspath() .setProperty("mode:retryLimit", 5) .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sources"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sources/Source1"), hasProperty(ModeShapeLexicon.RETRY_LIMIT, 5)); }
@Test public void shouldAllowAddingSequencer() { // Update the configuration and save it ... configuration.sequencer("sequencerA") .usingClass(MockStreamSequencerA.class) .setDescription("Mock Sequencer A") .sequencingFrom("/foo/source") .andOutputtingTo("/foo/target") .sequencingFrom("/bar/source") .andOutputtingTo("/bar/target") .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:sequencers").getChildren(), hasChild(segment("sequencerA"))); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), is(notNullValue())); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), hasProperty(ModeShapeLexicon.DESCRIPTION, "Mock Sequencer A")); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), hasProperty(ModeShapeLexicon.CLASSNAME, MockStreamSequencerA.class.getName())); System.out.println(subgraph.getNode("/mode:sequencers/sequencerA").getProperty(ModeShapeLexicon.PATH_EXPRESSION)); assertThat(subgraph.getNode("/mode:sequencers/sequencerA"), hasProperty(ModeShapeLexicon.PATH_EXPRESSION, "/foo/source => /foo/target", "/bar/source => /bar/target")); }
@Test public void shouldAllowAddingMimeTypeDetector() { // Update the configuration and save it ... configuration.mimeTypeDetector("detector") .usingClass(ExtensionBasedMimeTypeDetector.class) .setDescription("default detector") .and() .save(); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(3).at("/"); assertThat(subgraph.getNode("/mode:mimeTypeDetectors").getChildren(), hasChild(segment("detector"))); assertThat(subgraph.getNode("/mode:mimeTypeDetectors/detector"), is(notNullValue())); assertThat(subgraph.getNode("/mode:mimeTypeDetectors/detector"), hasProperty(ModeShapeLexicon.DESCRIPTION, "default detector")); assertThat(subgraph.getNode("/mode:mimeTypeDetectors/detector"), hasProperty(ModeShapeLexicon.CLASSNAME, ExtensionBasedMimeTypeDetector.class.getName())); }
@Test public void shouldSetPropertyWithPath() { Path path1 = createPath("/a"); Name prop1_name = createName("prop1"); String value_1 = "blue"; output.setProperty(path1, prop1_name, value_1); Path path2 = createPath("/a/b"); Name prop2_name = createName("prop2"); String value_2 = "red"; output.setProperty(path2, prop2_name, value_2); output.close(); Subgraph result = graph.getSubgraphOfDepth(10).at("/"); String v1 = (String)result.getNode(path1).getProperty(prop1_name).getFirstValue(); assertThat(v1, is(value_1)); String v2 = (String)result.getNode(path2).getProperty(prop2_name).getFirstValue(); assertThat(v2, is(value_2)); assertNull(result.getNode("/c")); } }
@Ignore @Test public void shouldReadSubgraphStartingAtRootAndWithMaximumDepthOfThree() { Subgraph subgraph = graph.getSubgraphOfDepth(3).at("/"); assertThat(subgraph, is(notNullValue())); // Verify that the root node is the same as getting it directly ... Node root = subgraph.getRoot(); assertSameNode(root, graph.getNodeAt("/")); // Verify the first-level children ... List<Location> children = graph.getChildren().of("/"); assertThat(children, is(notNullValue())); for (Location childLocation : children) { // Verify the child in the subgraph matches the same node obtained directly from the graph ... Node child = subgraph.getNode(childLocation); assertSameNode(child, graph.getNodeAt(childLocation)); // Now get the second-level children ... List<Location> grandChildren = graph.getChildren().of(childLocation); assertThat(grandChildren, is(notNullValue())); for (Location grandchildLocation : grandChildren) { // Verify the grandchild in the subgraph matches the same node obtained directly from the graph ... Node grandchild = subgraph.getNode(grandchildLocation); assertSameNode(grandchild, graph.getNodeAt(grandchildLocation)); // The subgraph should contain the children locations and properties for the grandchildren. // However, the subgraph should not a node for the children of the grandchildren ... for (Location greatGrandchild : grandchild.getChildren()) { assertThat(subgraph.getNode(greatGrandchild), is(nullValue())); } } } }
@Test public void shouldAddValuesToNonExistantProperty() { String initialPath = ""; int depth = 1; int numChildrenPerNode = 1; int numPropertiesPerNode = 1; Stopwatch sw = new Stopwatch(); boolean batch = true; createSubgraph(graph, initialPath, depth, numChildrenPerNode, numPropertiesPerNode, batch, sw, output, null); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); graph.addValue("foo").andValue("bar").to("newProperty").on("node1"); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); Subgraph subgraph = graph.getSubgraphOfDepth(2).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("node1"), hasProperty("property1", "The quick brown fox jumped over the moon. What? ")); assertThat(subgraph.getNode("node1"), hasProperty("newProperty", "foo", "bar")); }
@Test public void shouldAddValuesToExistingProperty() { String initialPath = ""; int depth = 1; int numChildrenPerNode = 1; int numPropertiesPerNode = 1; Stopwatch sw = new Stopwatch(); boolean batch = true; createSubgraph(graph, initialPath, depth, numChildrenPerNode, numPropertiesPerNode, batch, sw, output, null); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); Subgraph subgraph = graph.getSubgraphOfDepth(2).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("node1"), hasProperty("property1", "The quick brown fox jumped over the moon. What? ")); graph.addValue("foo").andValue("bar").to("property1").on("node1"); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); subgraph = graph.getSubgraphOfDepth(2).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("node1"), hasProperty("property1", "The quick brown fox jumped over the moon. What? ", "foo", "bar")); }
@Test public void shouldNotRemoveValuesFromNonExistantProperty() { String initialPath = ""; int depth = 1; int numChildrenPerNode = 1; int numPropertiesPerNode = 1; Stopwatch sw = new Stopwatch(); boolean batch = true; createSubgraph(graph, initialPath, depth, numChildrenPerNode, numPropertiesPerNode, batch, sw, output, null); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); graph.removeValue("The quick brown fox jumped over the moon. What? ").from("noSuchProperty").on("node1"); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); Subgraph subgraph = graph.getSubgraphOfDepth(2).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("node1"), hasProperty("property1", "The quick brown fox jumped over the moon. What? ")); }
@Test public void shouldRemoveValuesFromExistingProperty() { String initialPath = ""; int depth = 1; int numChildrenPerNode = 1; int numPropertiesPerNode = 1; Stopwatch sw = new Stopwatch(); boolean batch = true; createSubgraph(graph, initialPath, depth, numChildrenPerNode, numPropertiesPerNode, batch, sw, output, null); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); graph.removeValue("The quick brown fox jumped over the moon. What? ").andValue("bar").from("property1").on("node1"); assertThat(graph.getChildren().of("/"), hasChildren(segment("node1"))); Subgraph subgraph = graph.getSubgraphOfDepth(2).at("/"); assertThat(subgraph, is(notNullValue())); assertThat(subgraph.getNode("node1"), hasProperty("property1")); }