Author: rhauch
Date: 2008-08-06 00:59:59 -0400 (Wed, 06 Aug 2008)
New Revision: 383
Added:
trunk/connectors/dna-connector-jbosscache/src/test/java/org/
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectionTest.java
trunk/connectors/dna-connector-jbosscache/src/test/resources/log4j.properties
Modified:
trunk/connectors/dna-connector-jbosscache/pom.xml
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnection.java
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java
Log:
DNA-83 - Federate content from JBoss Cache instance(s)
http://jira.jboss.com/jira/browse/DNA-83
Added some unit tests for JBossCacheConnection, and upgraded the version of JBoss Cache
from 2.2.0CR6 to 2.2.0CR7.
Modified: trunk/connectors/dna-connector-jbosscache/pom.xml
===================================================================
--- trunk/connectors/dna-connector-jbosscache/pom.xml 2008-08-06 04:58:23 UTC (rev 382)
+++ trunk/connectors/dna-connector-jbosscache/pom.xml 2008-08-06 04:59:59 UTC (rev 383)
@@ -53,7 +53,7 @@
<dependency>
<groupId>org.jboss.cache</groupId>
<artifactId>jbosscache-core</artifactId>
- <version>2.2.0.CR6</version>
+ <version>2.2.0.CR7</version>
</dependency>
<!--
Testing (note the scope)
@@ -63,8 +63,8 @@
<artifactId>junit</artifactId>
</dependency>
<dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-library</artifactId>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
</dependency>
<!--
Logging (require SLF4J API for compiling, but use Log4J and its SLF4J binding for
testing)
Modified:
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnection.java
===================================================================
---
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnection.java 2008-08-06
04:58:23 UTC (rev 382)
+++
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnection.java 2008-08-06
04:59:59 UTC (rev 383)
@@ -33,6 +33,7 @@
import org.jboss.dna.spi.ExecutionContext;
import org.jboss.dna.spi.cache.CachePolicy;
import org.jboss.dna.spi.graph.Name;
+import org.jboss.dna.spi.graph.NameFactory;
import org.jboss.dna.spi.graph.Path;
import org.jboss.dna.spi.graph.PathFactory;
import org.jboss.dna.spi.graph.PathNotFoundException;
@@ -87,13 +88,6 @@
}
/**
- * @return uuidPropertyName
- */
- public Name getUuidPropertyName() {
- return this.uuidPropertyName;
- }
-
- /**
* {@inheritDoc}
*/
public String getSourceName() {
@@ -164,15 +158,17 @@
* @param context the execution context
* @return the name, or null if the UUID should not be stored
*/
- protected Name getUuidProperty( ExecutionContext context ) {
+ protected Name getUuidPropertyName( ExecutionContext context ) {
if (!initializedUuidPropertyName) {
- this.uuidPropertyName =
this.source.getUuidPropertyName(context.getValueFactories().getNameFactory());
+ NameFactory nameFactory = context.getValueFactories().getNameFactory();
+ this.uuidPropertyName =
nameFactory.create(this.source.getUuidPropertyName());
initializedUuidPropertyName = true;
}
return this.uuidPropertyName;
}
protected Fqn<Path.Segment> getFullyQualifiedName( Path path ) {
+ assert path != null;
return Fqn.fromList(path.getSegmentsList());
}
@@ -183,16 +179,13 @@
* @return the relative fully-qualified name
*/
protected Fqn<Path.Segment> getFullyQualifiedName( Path.Segment pathSegment )
{
+ assert pathSegment != null;
return Fqn.fromElements(pathSegment);
}
protected Path getPath( PathFactory factory,
Fqn<Path.Segment> fqn ) {
- Path.Segment[] segments = new Path.Segment[fqn.size()];
- for (int i = 0; i != segments.length; ++i) {
- segments[i] = fqn.get(i);
- }
- return factory.create(factory.createRootPath(), segments);
+ return factory.create(factory.createRootPath(), fqn.peekElements());
}
protected Node<Name, Object> getNode( ExecutionContext context,
@@ -232,7 +225,7 @@
copy.clearData();
copy.putAll(original.getData());
if (uuidProperty != null) {
- // Generate a new UUID for the new node ...
+ // Generate a new UUID for the new node, overwriting any existing value from
the original ...
copy.put(uuidProperty, generateUuid());
}
int numNodesCopied = 1;
@@ -266,7 +259,7 @@
Node<Name, Object> parentNode = getNode(parent);
Node<Name, Object> node = parentNode.addChild(childFqn);
// Add the UUID property (if required), which may be overwritten by a
supplied property ...
- Name uuidPropertyName = getUuidProperty(getExecutionContext());
+ Name uuidPropertyName = getUuidPropertyName(getExecutionContext());
if (uuidPropertyName != null) {
node.put(uuidPropertyName, generateUuid());
}
@@ -287,7 +280,7 @@
@Override
public void execute( GetChildrenCommand command ) {
Node<Name, Object> node = getNode(command.getPath());
- Name uuidPropertyName = getUuidPropertyName();
+ Name uuidPropertyName = getUuidPropertyName(getExecutionContext());
// Get the names of the children ...
for (Node<Name, Object> child : node.getChildren()) {
Segment segment = (Segment)child.getFqn().getLastElement();
@@ -363,7 +356,7 @@
public void execute( MoveBranchCommand command ) {
Node<Name, Object> node = getNode(command.getPath());
boolean recursive = true;
- Name uuidProperty = getUuidProperty(getExecutionContext());
+ Name uuidProperty = getUuidPropertyName(getExecutionContext());
// Look up the new parent, which must exist ...
Path newPath = command.getNewPath();
Node<Name, Object> newParent = getNode(newPath.getAncestor());
Modified:
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java
===================================================================
---
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java 2008-08-06
04:58:23 UTC (rev 382)
+++
trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java 2008-08-06
04:59:59 UTC (rev 383)
@@ -46,7 +46,6 @@
import org.jboss.dna.spi.DnaLexicon;
import org.jboss.dna.spi.cache.CachePolicy;
import org.jboss.dna.spi.graph.Name;
-import org.jboss.dna.spi.graph.NameFactory;
import org.jboss.dna.spi.graph.connection.AbstractRepositorySource;
import org.jboss.dna.spi.graph.connection.RepositoryConnection;
@@ -252,18 +251,6 @@
}
/**
- * Utility method to obtain a consistent {@link Name} for the property that should
store the UUID value on each node. This
- * method is properly coded so that it is threadsafe.
- *
- * @param factory the name factory; may not be null
- * @return the property name, or null if UUIDs are not to be maintained
- */
- /* package */synchronized Name getUuidPropertyName( NameFactory factory ) {
- if (this.uuidPropertyName.length() == 0) return null;
- return factory.create(this.uuidPropertyName);
- }
-
- /**
* {@inheritDoc}
*/
public Reference getReference() {
Added:
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectionTest.java
===================================================================
---
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectionTest.java
(rev 0)
+++
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectionTest.java 2008-08-06
04:59:59 UTC (rev 383)
@@ -0,0 +1,265 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * 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.
+ *
+ * This software 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.connector.jbosscache;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.hamcrest.core.IsSame.sameInstance;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.stub;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheFactory;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Node;
+import org.jboss.dna.spi.DnaLexicon;
+import org.jboss.dna.spi.ExecutionContext;
+import org.jboss.dna.spi.cache.CachePolicy;
+import org.jboss.dna.spi.graph.Name;
+import org.jboss.dna.spi.graph.Path;
+import org.jboss.dna.spi.graph.PathFactory;
+import org.jboss.dna.spi.graph.PathNotFoundException;
+import org.jboss.dna.spi.graph.connection.BasicExecutionContext;
+import org.jboss.dna.spi.graph.connection.RepositorySourceListener;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+import org.mockito.MockitoAnnotations.Mock;
+
+/**
+ * @author Randall Hauch
+ */
+public class JBossCacheConnectionTest {
+
+ private JBossCacheConnection connection;
+ private CacheFactory<Name, Object> cacheFactory;
+ private Cache<Name, Object> cache;
+ private ExecutionContext context;
+ private PathFactory pathFactory;
+ @Mock
+ private JBossCacheSource source;
+
+ @Before
+ public void beforeEach() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ context = new BasicExecutionContext();
+ context.getNamespaceRegistry().register(DnaLexicon.NAMESPACE_PREFIX,
DnaLexicon.NAMESPACE_URI);
+ pathFactory = context.getValueFactories().getPathFactory();
+ cacheFactory = new DefaultCacheFactory<Name, Object>();
+ cache = cacheFactory.createCache();
+ connection = new JBossCacheConnection(source, cache);
+ }
+
+ @Test( expected = AssertionError.class )
+ public void shouldFailToInstantiateIfCacheReferenceIsNull() {
+ cache = null;
+ connection = new JBossCacheConnection(source, cache);
+ }
+
+ @Test( expected = AssertionError.class )
+ public void shouldFailToInstantiateIfSourceReferenceIsNull() {
+ source = null;
+ connection = new JBossCacheConnection(source, cache);
+ }
+
+ @Test
+ public void shouldInstantiateWithValidSourceAndCacheReferences() {
+ assertThat(connection, is(notNullValue()));
+ }
+
+ @Test
+ public void shouldDelegateToTheSourceForTheConnectionsSourceName() {
+ stub(source.getName()).toReturn("the source name");
+ assertThat(connection.getSourceName(), is("the source name"));
+ verify(source).getName();
+ }
+
+ @Test
+ public void shouldDelegateToTheSourceForTheConnectionsDefaultCachePolicy() {
+ CachePolicy policy = mock(CachePolicy.class);
+ stub(source.getDefaultCachePolicy()).toReturn(policy);
+ assertThat(connection.getDefaultCachePolicy(), is(sameInstance(policy)));
+ verify(source).getDefaultCachePolicy();
+ }
+
+ @SuppressWarnings( "unchecked" )
+ @Test
+ public void shouldGetTheRootFromTheCacheWhenPinged() {
+ cache = mock(Cache.class);
+ connection = new JBossCacheConnection(source, cache);
+ stub(cache.getRoot()).toReturn(null);
+ assertThat(connection.ping(1, TimeUnit.SECONDS), is(true));
+ verify(cache).getRoot();
+ }
+
+ @Test
+ public void shouldHaveNoOpListenerWhenCreated() {
+ assertThat(connection.getListener(),
is(sameInstance(JBossCacheConnection.NO_OP_LISTENER)));
+ }
+
+ @Test
+ public void shouldUseNoOpListenerWhenSettingListenerToNull() {
+ connection.setListener(null);
+ assertThat(connection.getListener(),
is(sameInstance(JBossCacheConnection.NO_OP_LISTENER)));
+ }
+
+ @Test
+ public void shouldSetListenerToNonNullValue() {
+ RepositorySourceListener listener = mock(RepositorySourceListener.class);
+ connection.setListener(listener);
+ assertThat(connection.getListener(), is(sameInstance(listener)));
+ connection.setListener(null);
+ assertThat(connection.getListener(),
is(sameInstance(JBossCacheConnection.NO_OP_LISTENER)));
+ }
+
+ @Test
+ public void
shouldGetUuidPropertyNameFromSouceAndShouldNotChangeDuringLifetimeOfConnection() {
+ stub(source.getUuidPropertyName()).toReturn(DnaLexicon.PropertyNames.UUID);
+ Name name = connection.getUuidPropertyName(context);
+ verify(source).getUuidPropertyName();
+ assertThat(name.getLocalName(), is("uuid"));
+ assertThat(name.getNamespaceUri(), is(DnaLexicon.NAMESPACE_URI));
+ stub(source.getUuidPropertyName()).toReturn("something else");
+ for (int i = 0; i != 10; ++i) {
+ Name name2 = connection.getUuidPropertyName(context);
+ assertThat(name2, is(sameInstance(name)));
+ }
+ verifyNoMoreInteractions(source);
+ }
+
+ @Test
+ public void shouldGenerateUuid() {
+ for (int i = 0; i != 100; ++i) {
+ assertThat(connection.generateUuid(), is(notNullValue()));
+ }
+ }
+
+ @Test
+ public void shouldCreateFullyQualifiedNodeOfPathSegmentsFromPath() {
+ Path path = pathFactory.create("/a/b/c/d");
+ Fqn<Path.Segment> fqn = connection.getFullyQualifiedName(path);
+ assertThat(fqn.size(), is(4));
+ assertThat(fqn.isRoot(), is(false));
+ for (int i = 0; i != path.size(); ++i) {
+ assertThat(fqn.get(i), is(path.getSegment(i)));
+ }
+ }
+
+ @Test
+ public void shouldCreateFullyQualifiedNodeOfPathSegmentsFromRootPath() {
+ Path path = pathFactory.createRootPath();
+ Fqn<Path.Segment> fqn = connection.getFullyQualifiedName(path);
+ assertThat(fqn.size(), is(0));
+ assertThat(fqn.isRoot(), is(true));
+ }
+
+ @Test( expected = AssertionError.class )
+ public void shouldFailToCreateFullyQualifiedNodeFromNullPath() {
+ connection.getFullyQualifiedName((Path)null);
+ }
+
+ @Test
+ public void shouldCreateFullyQualifiedNodeFromPathSegment() {
+ Path.Segment segment = pathFactory.createSegment("a");
+ Fqn<Path.Segment> fqn = connection.getFullyQualifiedName(segment);
+ assertThat(fqn.size(), is(1));
+ assertThat(fqn.isRoot(), is(false));
+ assertThat(fqn.get(0), is(segment));
+ }
+
+ @Test( expected = AssertionError.class )
+ public void shouldFailToCreateFullyQualifiedNodeFromNullPathSegment() {
+ connection.getFullyQualifiedName((Path.Segment)null);
+ }
+
+ @Test
+ public void shouldCreatePathFromFullyQualifiedNode() {
+ Path path = pathFactory.create("/a/b/c/d");
+ Fqn<Path.Segment> fqn = connection.getFullyQualifiedName(path);
+ assertThat(connection.getPath(pathFactory, fqn), is(path));
+ }
+
+ @Test
+ public void shouldCreateRootPathFromRootFullyQualifiedNode() {
+ Path path = pathFactory.createRootPath();
+ Fqn<Path.Segment> fqn = connection.getFullyQualifiedName(path);
+ assertThat(connection.getPath(pathFactory, fqn), is(path));
+ }
+
+ @Test
+ public void shouldGetNodeIfItExistsInCache() {
+ // Set up the cache with data ...
+ Name uuidProperty = connection.getUuidPropertyName(context);
+ Path[] paths = {pathFactory.create("/a"),
pathFactory.create("/a/b"), pathFactory.create("/a/b/c")};
+ Path nonExistantPath = pathFactory.create("/a/d");
+ UUID[] uuids = {UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()};
+ cache.put(Fqn.fromList(paths[0].getSegmentsList()), uuidProperty, uuids[0]);
+ cache.put(Fqn.fromList(paths[1].getSegmentsList()), uuidProperty, uuids[1]);
+ cache.put(Fqn.fromList(paths[2].getSegmentsList()), uuidProperty, uuids[2]);
+ Node<Name, Object> nodeA =
cache.getNode(Fqn.fromList(paths[0].getSegmentsList()));
+ Node<Name, Object> nodeB =
cache.getNode(Fqn.fromList(paths[1].getSegmentsList()));
+ Node<Name, Object> nodeC =
cache.getNode(Fqn.fromList(paths[2].getSegmentsList()));
+ Node<Name, Object> nodeD =
cache.getNode(Fqn.fromList(nonExistantPath.getSegmentsList()));
+ assertThat(nodeA, is(notNullValue()));
+ assertThat(nodeB, is(notNullValue()));
+ assertThat(nodeC, is(notNullValue()));
+ assertThat(nodeD, is(nullValue()));
+ // Test the getNode(...) method for existing nodes ...
+ assertThat(connection.getNode(context, paths[0]), is(sameInstance(nodeA)));
+ assertThat(connection.getNode(context, paths[1]), is(sameInstance(nodeB)));
+ assertThat(connection.getNode(context, paths[2]), is(sameInstance(nodeC)));
+ }
+
+ @Test
+ public void
shouldThrowExceptionWithLowestExistingNodeFromGetNodeIfTheNodeDoesNotExist() {
+ // Set up the cache with data ...
+ Name uuidProperty = connection.getUuidPropertyName(context);
+ Path[] paths = {pathFactory.create("/a"),
pathFactory.create("/a/b"), pathFactory.create("/a/b/c")};
+ Path nonExistantPath = pathFactory.create("/a/d");
+ UUID[] uuids = {UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()};
+ cache.put(Fqn.fromList(paths[0].getSegmentsList()), uuidProperty, uuids[0]);
+ cache.put(Fqn.fromList(paths[1].getSegmentsList()), uuidProperty, uuids[1]);
+ cache.put(Fqn.fromList(paths[2].getSegmentsList()), uuidProperty, uuids[2]);
+ Node<Name, Object> nodeA =
cache.getNode(Fqn.fromList(paths[0].getSegmentsList()));
+ Node<Name, Object> nodeB =
cache.getNode(Fqn.fromList(paths[1].getSegmentsList()));
+ Node<Name, Object> nodeC =
cache.getNode(Fqn.fromList(paths[2].getSegmentsList()));
+ Node<Name, Object> nodeD =
cache.getNode(Fqn.fromList(nonExistantPath.getSegmentsList()));
+ assertThat(nodeA, is(notNullValue()));
+ assertThat(nodeB, is(notNullValue()));
+ assertThat(nodeC, is(notNullValue()));
+ assertThat(nodeD, is(nullValue()));
+ try {
+ connection.getNode(context, nonExistantPath);
+ fail();
+ } catch (PathNotFoundException e) {
+ assertThat(e.getLowestAncestorThatDoesExist(), is(paths[0]));
+ }
+ }
+
+}
Property changes on:
trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectionTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: trunk/connectors/dna-connector-jbosscache/src/test/resources/log4j.properties
===================================================================
--- trunk/connectors/dna-connector-jbosscache/src/test/resources/log4j.properties
(rev 0)
+++
trunk/connectors/dna-connector-jbosscache/src/test/resources/log4j.properties 2008-08-06
04:59:59 UTC (rev 383)
@@ -0,0 +1,15 @@
+# Direct log messages to stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.out
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %m%n
+
+# Root logger option
+log4j.rootLogger=INFO, stdout
+
+# Set up the default logging to be INFO level, then override specific units
+log4j.logger.org.jboss.dna=INFO
+
+# JBoss Cache logging
+log4j.logger.org.jboss.cache=WARN, stdout
+
Property changes on:
trunk/connectors/dna-connector-jbosscache/src/test/resources/log4j.properties
___________________________________________________________________
Name: svn:mime-type
+ text/plain