[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/notifications ...
Ben Wang
bwang at jboss.com
Mon Oct 30 03:10:06 EST 2006
User: bwang
Date: 06/10/30 03:10:06
Added: tests/functional/org/jboss/cache/notifications
RemoteCacheListenerTest.java
Log:
Test remote node event notification as the origination is remote.
Revision Changes Path
1.1 date: 2006/10/30 08:10:06; author: bwang; state: Exp;JBossCache/tests/functional/org/jboss/cache/notifications/RemoteCacheListenerTest.java
Index: RemoteCacheListenerTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, 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.cache.notifications;
import junit.framework.TestCase;
import org.jboss.cache.Cache;
import org.jboss.cache.Fqn;
import org.jboss.cache.Node;
import org.jboss.cache.AbstractCacheListener;
import org.jboss.cache.factories.DefaultCacheFactory;
import org.jboss.cache.lock.IsolationLevel;
import org.jboss.cache.config.Configuration;
import javax.transaction.TransactionManager;
import javax.transaction.Transaction;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
/**
* Remote conterpart of CacheListenerTest. Main difference is event is originating as local.
*
* @author Ben Wang
* @since 2.0.0
*/
public class RemoteCacheListenerTest extends TestCase
{
protected boolean optLocking = false;
public RemoteCacheListenerTest(String s)
{
super(s);
}
private Cache cache, cache1;
private TransactionManager tm;
private EventLog eventLog = new EventLog();
private Fqn fqn = Fqn.fromString("/test");
protected void setUp() throws Exception
{
super.setUp();
Configuration c = new Configuration();
c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
c.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
if (optLocking) c.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
cache = new DefaultCacheFactory().createCache(c);
cache1 = new DefaultCacheFactory().createCache(c);
tm = cache1.getTransactionManager();
eventLog.events.clear();
cache1.addCacheListener(eventLog);
}
protected void tearDown() throws Exception
{
super.tearDown();
Transaction t = tm.getTransaction();
if (t != null) t.rollback();
cache.stop();
cache.destroy();
cache1.stop();
cache1.destroy();
}
// simple tests first
public void testCreation() throws Exception
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
cache.put(fqn, "key", "value");
Map data = new HashMap();
data.put("key", "value");
//expected
List<Event> expected = new ArrayList<Event>();
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, true, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, false, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, true, false, Collections.emptyMap()));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, false, false, data));
assertEquals(expected, eventLog.events);
assertEquals("value", cache.get(fqn, "key"));
}
public void testOnlyModification() throws Exception
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
cache.put(fqn, "key", "value");
Map oldData = new HashMap();
oldData.put("key", "value");
// clear event log
eventLog.events.clear();
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
// modify existing node
cache.put(fqn, "key", "value2");
Map newData = new HashMap();
newData.put("key", "value2");
//expected
List<RemoteCacheListenerTest.Event> expected = new ArrayList<RemoteCacheListenerTest.Event>();
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, true, false, oldData));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, false, false, newData));
assertEquals(expected, eventLog.events);
}
public void testOnlyRemoval() throws Exception
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
cache.put(fqn, "key", "value");
Map oldData = new HashMap();
oldData.put("key", "value");
assertEquals("value", cache.get(fqn, "key"));
// clear event log
eventLog.events.clear();
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
// modify existing node
cache.removeNode(fqn);
//expected
List<RemoteCacheListenerTest.Event> expected = new ArrayList<RemoteCacheListenerTest.Event>();
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_REMOVED, fqn, true, false, oldData));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_REMOVED, fqn, false, false, null));
assertEquals(expected, eventLog.events);
// test that the node has in fact been removed.
assertNull("Should be null", cache.getChild(fqn));
}
public void testRemoveData() throws Exception
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
cache.put(fqn, "key", "value");
cache.put(fqn, "key2", "value2");
Map oldData = new HashMap();
oldData.put("key", "value");
oldData.put("key2", "value2");
// clear event log
eventLog.events.clear();
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
// modify existing node
cache.remove(fqn, "key2");
Map newData = new HashMap();
newData.put("key", "value");
//expected
List<RemoteCacheListenerTest.Event> expected = new ArrayList<RemoteCacheListenerTest.Event>();
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, true, false, oldData));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, false, false, newData));
assertEquals(expected, eventLog.events);
}
public void testPutMap() throws Exception
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
Map oldData = new HashMap();
oldData.put("key", "value");
oldData.put("key2", "value2");
// clear event log
eventLog.events.clear();
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
// modify existing node
cache.put(fqn, oldData);
//expected
List<RemoteCacheListenerTest.Event> expected = new ArrayList<RemoteCacheListenerTest.Event>();
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, true, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, false, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, true, false, Collections.emptyMap()));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, false, false, oldData));
assertEquals(expected, eventLog.events);
}
public void testMove()
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
Fqn newParent = Fqn.fromString("/a");
cache.put(fqn, "key", "value");
cache.put(newParent, "key", "value");
Node n1 = cache.getChild(fqn);
Node n2 = cache.getChild(newParent);
eventLog.events.clear(); // clear events
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
n1.move(n2);
//expected
Fqn newFqn = new Fqn(newParent, fqn.getLast());
List<RemoteCacheListenerTest.Event> expected = new ArrayList<RemoteCacheListenerTest.Event>();
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, true, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, newFqn, true, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, false, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, newFqn, false, false, null));
}
// -- now the transactional ones
/*
// TODO: Reinstate these once we have a proper plan for dealing with transactions and notifications.
public void testTxCreationCommit() throws Exception
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
tm.begin();
cache.put(fqn, "key", "value");
// assertEquals("Events log should be empty until commit time", 0, eventLog.events.size());
tm.commit();
Map data = new HashMap();
data.put("key", "value");
//expected
List<RemoteCacheListenerTest.Event> expected = new ArrayList<RemoteCacheListenerTest.Event>();
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, true, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, false, false, null));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, true, false, Collections.emptyMap()));
expected.add(new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, false, false, data));
assertEquals(expected, eventLog.events);
assertEquals("value", cache.get(fqn, "key"));
}
public void testTxCreationRollback() throws Exception
{
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
tm.begin();
cache.put(fqn, "key", "value");
assertEquals("Events log should be empty until commit time", 0, eventLog.events.size());
tm.rollback();
assertEquals("Events log should be empty until commit time", 0, eventLog.events.size());
}
public void testTxOnlyModification() throws Exception
{
fail("implement me");
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
cache.put(fqn, "key", "value");
Map oldData = new HashMap();
oldData.put("key", "value");
// clear event log
eventLog.events.clear();
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
// modify existing node
cache.put(fqn, "key", "value2");
Map newData = new HashMap();
newData.put("key", "value2");
//expected
List<Event> expected = new ArrayList<Event>();
expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
assertEquals(expected, eventLog.events);
}
public void testTxOnlyRemoval() throws Exception
{
fail("implement me");
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
cache.put(fqn, "key", "value");
Map oldData = new HashMap();
oldData.put("key", "value");
assertEquals("value", cache.get(fqn, "key"));
// clear event log
eventLog.events.clear();
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
// modify existing node
cache.removeNode(fqn);
//expected
List<Event> expected = new ArrayList<Event>();
expected.add(new Event(ListenerMethod.NODE_REMOVED, fqn, true, true, oldData));
expected.add(new Event(ListenerMethod.NODE_REMOVED, fqn, false, true, null));
assertEquals(expected, eventLog.events);
// test that the node has in fact been removed.
assertNull("Should be null", cache.getChild(fqn));
}
public void testTxRemoveData() throws Exception
{
fail("implement me");
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
cache.put(fqn, "key", "value");
cache.put(fqn, "key2", "value2");
Map oldData = new HashMap();
oldData.put("key", "value");
oldData.put("key2", "value2");
// clear event log
eventLog.events.clear();
assertEquals("Event log should be empty", Collections.emptyList(), eventLog.events);
// modify existing node
cache.remove(fqn, "key2");
Map newData = new HashMap();
newData.put("key", "value");
//expected
List<Event> expected = new ArrayList<Event>();
expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, true, true, oldData));
expected.add(new Event(ListenerMethod.NODE_MODIFIED, fqn, false, true, newData));
assertEquals(expected, eventLog.events);
}
public void testTxMove()
{
fail("implement me");
}
*/
// ============= supporting classes and enums =======================
public static class EventLog extends AbstractCacheListener
{
public final List<RemoteCacheListenerTest.Event> events = new ArrayList<RemoteCacheListenerTest.Event>();
public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
{
RemoteCacheListenerTest.Event e = new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_CREATED, fqn, pre, isLocal, null);
events.add(e);
}
public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map data)
{
RemoteCacheListenerTest.Event e = new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_REMOVED, fqn, pre, isLocal, data);
events.add(e);
}
public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, Map data)
{
RemoteCacheListenerTest.Event e = new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MODIFIED, fqn, pre, isLocal, data);
events.add(e);
}
public void nodeVisited(Fqn fqn, boolean pre)
{
RemoteCacheListenerTest.Event e = new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_VISITED, fqn, pre, false, null);
events.add(e);
}
public void nodeMoved(Fqn from, Fqn to, boolean pre)
{
RemoteCacheListenerTest.Event e = new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MOVED, from, pre, false, null);
events.add(e);
e = new RemoteCacheListenerTest.Event(RemoteCacheListenerTest.ListenerMethod.NODE_MOVED, to, pre, false, null);
events.add(e);
}
public String toString()
{
return "EventLog{" +
"events=" + events +
'}';
}
}
public static class Event
{
RemoteCacheListenerTest.ListenerMethod methodName;
Fqn fqn;
boolean pre;
boolean local;
Map data;
public Event(RemoteCacheListenerTest.ListenerMethod methodName, Fqn fqn, boolean pre, boolean local, Map data)
{
this.methodName = methodName;
this.fqn = fqn;
this.pre = pre;
this.local = local;
this.data = data;
}
public String toString()
{
return "Event{" +
"methodName=" + methodName +
", fqn=" + fqn +
", pre=" + pre +
", local=" + local +
", data=" + data +
'}';
}
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final RemoteCacheListenerTest.Event event = (RemoteCacheListenerTest.Event) o;
if (local != event.local) return false;
if (pre != event.pre) return false;
if (data != null ? !data.equals(event.data) : event.data != null) return false;
if (fqn != null ? !fqn.equals(event.fqn) : event.fqn != null) return false;
if (methodName != event.methodName) return false;
return true;
}
public int hashCode()
{
int result;
result = (methodName != null ? methodName.hashCode() : 0);
result = 29 * result + (fqn != null ? fqn.hashCode() : 0);
result = 29 * result + (pre ? 1 : 0);
result = 29 * result + (local ? 1 : 0);
result = 29 * result + (data != null ? data.hashCode() : 0);
return result;
}
}
public enum ListenerMethod
{
NODE_CREATED, NODE_REMOVED, NODE_VISITED, NODE_MODIFIED, NODE_MOVED
}
}
More information about the jboss-cvs-commits
mailing list