[dna-commits] DNA SVN: r402 - in trunk: connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache and 1 other directories.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Thu Aug 7 15:56:15 EDT 2008


Author: rhauch
Date: 2008-08-07 15:56:15 -0400 (Thu, 07 Aug 2008)
New Revision: 402

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/JBossCacheSource.java
   trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java
   trunk/dna-spi/src/main/java/org/jboss/dna/spi/cache/BasicCachePolicy.java
Log:
DNA-83 - Federate content from JBoss Cache instance(s)
http://jira.jboss.com/jira/browse/DNA-83

Added more tests.

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-07 16:37:58 UTC (rev 401)
+++ trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnection.java	2008-08-07 19:56:15 UTC (rev 402)
@@ -88,6 +88,13 @@
     }
 
     /**
+     * @return cache
+     */
+    /*package*/Cache<Name, Object> getCache() {
+        return cache;
+    }
+
+    /**
      * {@inheritDoc}
      */
     public String getSourceName() {

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-07 16:37:58 UTC (rev 401)
+++ trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java	2008-08-07 19:56:15 UTC (rev 402)
@@ -390,6 +390,24 @@
     /**
      * {@inheritDoc}
      */
+    @Override
+    public boolean equals( Object obj ) {
+        if (obj == this) return true;
+        if (obj instanceof JBossCacheSource) {
+            JBossCacheSource that = (JBossCacheSource)obj;
+            if (this.getName() == null) {
+                if (that.getName() != null) return false;
+            } else {
+                if (!this.getName().equals(that.getName())) return false;
+            }
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     public synchronized Reference getReference() {
         String className = getClass().getName();
         String factoryClassName = this.getClass().getName();

Modified: trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java
===================================================================
--- trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java	2008-08-07 16:37:58 UTC (rev 401)
+++ trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java	2008-08-07 19:56:15 UTC (rev 402)
@@ -25,8 +25,28 @@
 import static org.hamcrest.core.IsNull.notNullValue;
 import static org.hamcrest.core.IsNull.nullValue;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.stub;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.RefAddr;
+import javax.naming.Reference;
+import javax.naming.spi.ObjectFactory;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheFactory;
+import org.jboss.dna.spi.cache.BasicCachePolicy;
+import org.jboss.dna.spi.connector.RepositoryConnection;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+import org.mockito.MockitoAnnotations.Mock;
 
 /**
  * @author Randall Hauch
@@ -34,12 +54,47 @@
 public class JBossCacheSourceTest {
 
     private JBossCacheSource source;
+    private RepositoryConnection connection;
+    private String validName;
+    private String validUuidPropertyName;
+    private String validCacheConfigurationName;
+    private String validCacheFactoryJndiName;
+    private String validCacheJndiName;
+    private UUID validRootNodeUuid;
+    @Mock
+    private Context jndiContext;
+    @Mock
+    private CacheFactory<Name, Object> cacheFactory;
+    @Mock
+    private Cache<Name, Object> cache;
 
     @Before
     public void beforeEach() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        validName = "cache source";
+        validUuidPropertyName = "dna:uuid";
+        validCacheConfigurationName = "cache config name";
+        validCacheFactoryJndiName = "cache factory jndi name";
+        validCacheJndiName = "cache jndi name";
+        validRootNodeUuid = UUID.randomUUID();
         source = new JBossCacheSource();
+
+        // Set up the fake JNDI context ...
+        source.setContext(jndiContext);
+        stub(jndiContext.lookup(validCacheFactoryJndiName)).toReturn(cacheFactory);
+        stub(jndiContext.lookup(validCacheJndiName)).toReturn(cache);
     }
 
+    @After
+    public void afterEach() throws Exception {
+        if (connection != null) {
+            connection.close();
+        }
+        source.shutdownNow();
+        source.awaitTermination(4, TimeUnit.SECONDS);
+        // assertThat(source.isTerminated(), is(true));
+    }
+
     @Test
     public void shouldReturnNonNullCapabilities() {
         assertThat(source.getCapabilities(), is(notNullValue()));
@@ -75,4 +130,139 @@
         source.setName(null);
         assertThat(source.getName(), is(nullValue()));
     }
+
+    @Test
+    public void shouldHaveDefaultRetryLimit() {
+        assertThat(source.getRetryLimit(), is(JBossCacheSource.DEFAULT_RETRY_LIMIT));
+    }
+
+    @Test
+    public void shouldSetRetryLimitToZeroWhenSetWithNonPositiveValue() {
+        source.setRetryLimit(0);
+        assertThat(source.getRetryLimit(), is(0));
+        source.setRetryLimit(-1);
+        assertThat(source.getRetryLimit(), is(0));
+        source.setRetryLimit(-100);
+        assertThat(source.getRetryLimit(), is(0));
+    }
+
+    @Test
+    public void shouldAllowRetryLimitToBeSet() {
+        for (int i = 0; i != 100; ++i) {
+            source.setRetryLimit(i);
+            assertThat(source.getRetryLimit(), is(i));
+        }
+    }
+
+    @Test
+    public void shouldCreateJndiReferenceAndRecreatedObjectFromReference() throws Exception {
+        BasicCachePolicy cachePolicy = new BasicCachePolicy();
+        cachePolicy.setTimeToLive(1000L);
+        convertToAndFromJndiReference(validName,
+                                      validRootNodeUuid,
+                                      validCacheConfigurationName,
+                                      validCacheJndiName,
+                                      validCacheFactoryJndiName,
+                                      validUuidPropertyName,
+                                      cachePolicy,
+                                      100);
+    }
+
+    @Test
+    public void shouldCreateJndiReferenceAndRecreatedObjectFromReferenceWithNullProperties() throws Exception {
+        BasicCachePolicy cachePolicy = new BasicCachePolicy();
+        cachePolicy.setTimeToLive(1000L);
+        convertToAndFromJndiReference("some source", null, null, null, null, null, null, 100);
+        convertToAndFromJndiReference(null, null, null, null, null, null, null, 100);
+    }
+
+    private void convertToAndFromJndiReference( String sourceName,
+                                                UUID rootNodeUuid,
+                                                String cacheConfigName,
+                                                String cacheJndiName,
+                                                String cacheFactoryJndiName,
+                                                String uuidPropertyName,
+                                                BasicCachePolicy cachePolicy,
+                                                int retryLimit ) throws Exception {
+        source.setRetryLimit(retryLimit);
+        source.setName(sourceName);
+        source.setCacheConfigurationName(cacheConfigName);
+        source.setCacheFactoryJndiName(cacheFactoryJndiName);
+        source.setCacheJndiName(cacheJndiName);
+        source.setDefaultCachePolicy(cachePolicy);
+        source.setRootNodeUuid(rootNodeUuid != null ? rootNodeUuid.toString() : null);
+        source.setUuidPropertyName(uuidPropertyName);
+
+        Reference ref = source.getReference();
+        assertThat(ref.getClassName(), is(JBossCacheSource.class.getName()));
+        assertThat(ref.getFactoryClassName(), is(JBossCacheSource.class.getName()));
+
+        Map<String, Object> refAttributes = new HashMap<String, Object>();
+        Enumeration<RefAddr> enumeration = ref.getAll();
+        while (enumeration.hasMoreElements()) {
+            RefAddr addr = enumeration.nextElement();
+            refAttributes.put(addr.getType(), addr.getContent());
+        }
+
+        assertThat((String)refAttributes.remove(JBossCacheSource.SOURCE_NAME), is(source.getName()));
+        assertThat((String)refAttributes.remove(JBossCacheSource.ROOT_NODE_UUID), is(source.getRootNodeUuid()));
+        assertThat((String)refAttributes.remove(JBossCacheSource.UUID_PROPERTY_NAME), is(source.getUuidPropertyName()));
+        assertThat((String)refAttributes.remove(JBossCacheSource.CACHE_JNDI_NAME), is(source.getCacheJndiName()));
+        assertThat((String)refAttributes.remove(JBossCacheSource.CACHE_FACTORY_JNDI_NAME), is(source.getCacheFactoryJndiName()));
+        assertThat((String)refAttributes.remove(JBossCacheSource.CACHE_CONFIGURATION_NAME),
+                   is(source.getCacheConfigurationName()));
+        assertThat((String)refAttributes.remove(JBossCacheSource.RETRY_LIMIT), is(Integer.toString(source.getRetryLimit())));
+        refAttributes.remove(JBossCacheSource.DEFAULT_CACHE_POLICY);
+        assertThat(refAttributes.isEmpty(), is(true));
+
+        // Recreate the object, use a newly constructed source ...
+        ObjectFactory factory = new JBossCacheSource();
+        Name name = mock(Name.class);
+        Context context = mock(Context.class);
+        Hashtable<?, ?> env = new Hashtable<Object, Object>();
+        JBossCacheSource recoveredSource = (JBossCacheSource)factory.getObjectInstance(ref, name, context, env);
+        assertThat(recoveredSource, is(notNullValue()));
+
+        assertThat(recoveredSource.getName(), is(source.getName()));
+        assertThat(recoveredSource.getRootNodeUuid(), is(source.getRootNodeUuid()));
+        assertThat(recoveredSource.getUuidPropertyName(), is(source.getUuidPropertyName()));
+        assertThat(recoveredSource.getCacheJndiName(), is(source.getCacheJndiName()));
+        assertThat(recoveredSource.getRetryLimit(), is(source.getRetryLimit()));
+        assertThat(recoveredSource.getCacheFactoryJndiName(), is(source.getCacheFactoryJndiName()));
+        assertThat(recoveredSource.getCacheConfigurationName(), is(source.getCacheConfigurationName()));
+        assertThat(recoveredSource.getDefaultCachePolicy(), is(source.getDefaultCachePolicy()));
+
+        assertThat(recoveredSource.equals(source), is(true));
+        assertThat(source.equals(recoveredSource), is(true));
+    }
+
+    @Test
+    public void shouldCreateCacheUsingDefaultCacheFactoryWhenNoCacheOrCacheFactoryOrCacheConfigurationNameIsFound()
+        throws Exception {
+        source.setName(validName);
+        connection = source.getConnection();
+        assertThat(connection, is(notNullValue()));
+        // assertThat(connection.getCache(), is(notNullValue()));
+    }
+
+    @Test
+    public void shouldCreateCacheUsingDefaultCacheFactoryWithConfigurationNameWhenNoCacheOrCacheFactoryIsFound() throws Exception {
+
+    }
+
+    @Test
+    public void shouldCreateCacheUsingCacheFactoryAndDefaultConfigurationWhenNoCacheOrCacheConfigurationNameIsFound()
+        throws Exception {
+
+    }
+
+    @Test
+    public void shouldCreateCacheUsingCacheFactoryAndConfigurationWhenNoCacheIsFound() throws Exception {
+
+    }
+
+    @Test
+    public void shouldUseCacheIfFoundInJndi() throws Exception {
+
+    }
 }

Modified: trunk/dna-spi/src/main/java/org/jboss/dna/spi/cache/BasicCachePolicy.java
===================================================================
--- trunk/dna-spi/src/main/java/org/jboss/dna/spi/cache/BasicCachePolicy.java	2008-08-07 16:37:58 UTC (rev 401)
+++ trunk/dna-spi/src/main/java/org/jboss/dna/spi/cache/BasicCachePolicy.java	2008-08-07 19:56:15 UTC (rev 402)
@@ -60,4 +60,30 @@
         return new ImmutableCachePolicy(this.getTimeToLive());
     }
 
+    /**
+     * {@inheritDoc}
+     * 
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals( Object obj ) {
+        if (obj == this) return true;
+        if (obj instanceof CachePolicy) {
+            CachePolicy that = (CachePolicy)obj;
+            if (this.getTimeToLive() != that.getTimeToLive()) return false;
+            if (obj instanceof BasicCachePolicy) return true;
+        }
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return "{ TTL=" + this.timeToLive + " }";
+    }
+
 }




More information about the dna-commits mailing list