Author: blafond
Date: 2009-11-25 08:03:28 -0500 (Wed, 25 Nov 2009)
New Revision: 1344
Added:
branches/ddl_sequencer/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphSequencerOutput.java
branches/ddl_sequencer/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphSequencerOutputTest.java
Log:
DNA-556 new SequencerOutput class to simplify testing graph node construction during
sequencing.
Added:
branches/ddl_sequencer/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphSequencerOutput.java
===================================================================
---
branches/ddl_sequencer/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphSequencerOutput.java
(rev 0)
+++
branches/ddl_sequencer/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphSequencerOutput.java 2009-11-25
13:03:28 UTC (rev 1344)
@@ -0,0 +1,121 @@
+/*
+ * JBoss DNA (
http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.dna.graph.io;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
+import org.jboss.dna.graph.property.Name;
+import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.graph.property.PathFactory;
+import org.jboss.dna.graph.sequencer.SequencerOutput;
+
+/**
+ * Utility for wrapping sequencer output to commit to a graph.
+ * <p>
+ * Constructors allow providing either a {@link Graph} or a {@link Graph.Batch} object.
If {@link Graph} constructor is used, a
+ * {@link Graph.Batch} object is created to utilize the batching capabilities of all
graph requests.
+ * <p>
+ * Calling close() commits all batched graph requests.
+ */
+public class GraphSequencerOutput implements SequencerOutput {
+
+ private final Graph.Batch batch;
+
+ private final PathFactory pathFactory;
+
+ private final Set<Path> paths = new HashSet<Path>();
+
+ /**
+ * Create a graph sequencer output instance using {@link Graph.Batch} object.
+ *
+ * @param batch the {@link Graph.Batch} object; may not be null
+ */
+ public GraphSequencerOutput( Graph.Batch batch ) {
+ super();
+ this.batch = batch;
+ ExecutionContext context = batch.getGraph().getContext();
+ this.pathFactory = context.getValueFactories().getPathFactory();
+ }
+
+ /**
+ * Create a graph sequencer output instance using {@link Graph} object. A {@link
Graph.Batch} object is created as a result.
+ *
+ * @param graph the {@link Graph} object; may not be null
+ */
+ public GraphSequencerOutput( Graph graph ) {
+ this(graph.batch());
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.jboss.dna.graph.sequencer.SequencerOutput#setProperty(java.lang.String,
java.lang.String, java.lang.Object[])
+ */
+ public void setProperty( String nodePath,
+ String propertyName,
+ Object... values ) {
+ Path path = pathFactory.create(nodePath);
+ if (paths.add(path)) {
+ batch.create(path).and();
+ }
+ batch.set(propertyName).on(path).to(values);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see
org.jboss.dna.graph.sequencer.SequencerOutput#setProperty(org.jboss.dna.graph.property.Path,
+ * org.jboss.dna.graph.property.Name, java.lang.Object[])
+ */
+ public void setProperty( Path nodePath,
+ Name propertyName,
+ Object... values ) {
+ if (paths.add(nodePath)) {
+ batch.create(nodePath).and();
+ }
+ batch.set(propertyName).on(nodePath).to(values);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.jboss.dna.graph.sequencer.SequencerOutput#setReference(java.lang.String,
java.lang.String, java.lang.String[])
+ */
+ public void setReference( String nodePath,
+ String propertyName,
+ String... paths ) {
+ Path path = pathFactory.create(nodePath);
+ if (this.paths.add(path)) {
+ batch.create(path).and();
+ }
+ batch.set(propertyName).on(nodePath).to(paths);
+ }
+
+ public void close() {
+ batch.execute();
+ }
+
+}
Property changes on:
branches/ddl_sequencer/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphSequencerOutput.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
branches/ddl_sequencer/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphSequencerOutputTest.java
===================================================================
---
branches/ddl_sequencer/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphSequencerOutputTest.java
(rev 0)
+++
branches/ddl_sequencer/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphSequencerOutputTest.java 2009-11-25
13:03:28 UTC (rev 1344)
@@ -0,0 +1,121 @@
+/*
+ * JBoss DNA (
http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.dna.graph.io;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
+import org.jboss.dna.graph.Subgraph;
+import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
+import org.jboss.dna.graph.property.Name;
+import org.jboss.dna.graph.property.Path;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class GraphSequencerOutputTest {
+ private GraphSequencerOutput output;
+ private ExecutionContext context;
+ private Graph graph;
+
+ @Before
+ public void beforeEach() {
+ context = new ExecutionContext();
+
+ InMemoryRepositorySource source = new InMemoryRepositorySource();
+ source.setName("actual");
+ graph = Graph.create(source, context);
+
+ output = new GraphSequencerOutput(graph);
+ }
+
+ protected Path createPath( String path ) {
+ return context.getValueFactories().getPathFactory().create(path);
+ }
+
+ protected Name createName( String name ) {
+ return context.getValueFactories().getNameFactory().create(name);
+ }
+
+ @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 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 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"));
+ }
+}
Property changes on:
branches/ddl_sequencer/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphSequencerOutputTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain