001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.clustering.wadi;
017:
018: import java.io.IOException;
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.apache.geronimo.clustering.ClusterListener;
025: import org.apache.geronimo.clustering.LocalNode;
026: import org.apache.geronimo.clustering.Node;
027: import org.codehaus.wadi.group.Address;
028: import org.codehaus.wadi.group.Dispatcher;
029: import org.codehaus.wadi.group.Peer;
030:
031: import com.agical.rmock.core.Action;
032: import com.agical.rmock.core.MethodHandle;
033: import com.agical.rmock.core.describe.ExpressionDescriber;
034: import com.agical.rmock.core.match.operator.AbstractExpression;
035: import com.agical.rmock.extension.junit.RMockTestCase;
036:
037: /**
038: *
039: * @version $Rev$ $Date$
040: */
041: public class BasicWADIClusterTest extends RMockTestCase {
042:
043: private Dispatcher dispatcher;
044: private org.codehaus.wadi.group.Cluster wadiCluster;
045: private LocalNode localNode;
046: private Peer peer1;
047: private Peer peer2;
048: private BasicWADICluster cluster;
049: private ClusterListener listener;
050: private NodeFactory nodeFactory;
051: private Node nodePeer1;
052: private Node nodePeer2;
053:
054: @Override
055: protected void setUp() throws Exception {
056: DispatcherHolder dispatcherHolder = (DispatcherHolder) mock(DispatcherHolder.class);
057:
058: dispatcher = dispatcherHolder.getDispatcher();
059:
060: wadiCluster = dispatcher.getCluster();
061: wadiCluster.getClusterName();
062: modify().multiplicity(expect.from(0)).returnValue("name");
063:
064: wadiCluster.addClusterListener(null);
065: modify().args(is.NOT_NULL);
066:
067: wadiCluster.getDispatcher();
068: modify().multiplicity(expect.from(0)).returnValue(dispatcher);
069:
070: Map<Address, Peer> remotePeers = new HashMap<Address, Peer>();
071: peer1 = addPeer("peer1", remotePeers);
072: peer2 = addPeer("peer2", remotePeers);
073:
074: wadiCluster.getRemotePeers();
075: modify().multiplicity(expect.from(0)).returnValue(remotePeers);
076:
077: localNode = (LocalNode) mock(LocalNode.class);
078:
079: nodeFactory = (NodeFactory) mock(NodeFactory.class);
080: nodePeer1 = nodeFactory.newNode(wadiCluster, peer1);
081: modify().multiplicity(expect.from(0));
082: nodePeer2 = nodeFactory.newNode(wadiCluster, peer2);
083: modify().multiplicity(expect.from(0));
084:
085: cluster = new BasicWADICluster(localNode, dispatcherHolder,
086: nodeFactory);
087: listener = (ClusterListener) mock(ClusterListener.class);
088: }
089:
090: private Peer addPeer(String peerName, Map<Address, Peer> remotePeers) {
091: Peer peer = (Peer) mock(Peer.class, peerName);
092: peer.getLocalStateMap();
093: modify().multiplicity(expect.from(0))
094: .returnValue(new HashMap());
095:
096: Address address = peer.getAddress();
097: modify().multiplicity(expect.from(0));
098: peer.getName();
099: modify().multiplicity(expect.from(0)).returnValue(peerName);
100:
101: remotePeers.put(address, peer);
102:
103: return peer;
104: }
105:
106: public void testGetName() throws Exception {
107: startVerificationAndInitWADICluster();
108:
109: assertEquals(wadiCluster.getClusterName(), cluster.getName());
110: }
111:
112: public void testGetLocalNode() throws Exception {
113: startVerificationAndInitWADICluster();
114:
115: assertEquals(localNode, cluster.getLocalNode());
116: }
117:
118: public void testGetRemotePeers() throws Exception {
119: startVerificationAndInitWADICluster();
120:
121: Set<Node> remoteNodes = cluster.getRemoteNodes();
122: assertEquals(2, remoteNodes.size());
123: assertTrue(remoteNodes.contains(nodePeer1));
124: assertTrue(remoteNodes.contains(nodePeer2));
125: }
126:
127: public void testAddClusterListener() throws Exception {
128: wadiCluster.addClusterListener(null);
129: modify().args(new AbstractExpression() {
130:
131: public void describeWith(ExpressionDescriber arg0)
132: throws IOException {
133: }
134:
135: public boolean passes(Object arg0) {
136: assertTrue(arg0 instanceof org.codehaus.wadi.group.ClusterListener);
137: return true;
138: }
139:
140: });
141:
142: startVerificationAndInitWADICluster();
143:
144: cluster.addClusterListener(listener);
145: }
146:
147: public void testAddNullClusterListenerThrowsException()
148: throws Exception {
149: startVerificationAndInitWADICluster();
150:
151: try {
152: cluster.addClusterListener(null);
153: fail();
154: } catch (IllegalArgumentException e) {
155: }
156: }
157:
158: public void testRemoveClusterListener() throws Exception {
159: AbstractExpression assertSame = new AssertSameWADIListener();
160: wadiCluster.addClusterListener(null);
161: modify().args(assertSame);
162: wadiCluster.removeClusterListener(null);
163: modify().args(assertSame);
164:
165: startVerificationAndInitWADICluster();
166:
167: cluster.addClusterListener(listener);
168: cluster.removeClusterListener(listener);
169: }
170:
171: public void testRemoveUndefinedClusterListenerThrowsException()
172: throws Exception {
173: startVerificationAndInitWADICluster();
174:
175: try {
176: cluster.removeClusterListener(listener);
177: fail();
178: } catch (IllegalArgumentException e) {
179: }
180: }
181:
182: public void testClusterListenerRegistrationCallback()
183: throws Exception {
184: wadiCluster.addClusterListener(null);
185: modify().args(is.ANYTHING).perform(new Action() {
186:
187: public Object invocation(Object[] arg0, MethodHandle arg1)
188: throws Throwable {
189: org.codehaus.wadi.group.ClusterListener wadiListener = (org.codehaus.wadi.group.ClusterListener) arg0[0];
190: wadiListener.onListenerRegistration(wadiCluster,
191: Collections.singleton(peer1));
192: ;
193: return null;
194: }
195:
196: });
197:
198: listener.onListenerRegistration(cluster, Collections
199: .singleton(nodePeer1));
200:
201: startVerificationAndInitWADICluster();
202:
203: cluster.addClusterListener(listener);
204: }
205:
206: public void testClusterListenerMembershipChangeCallback()
207: throws Exception {
208: wadiCluster.addClusterListener(null);
209: modify().args(is.ANYTHING).perform(new Action() {
210:
211: public Object invocation(Object[] arg0, MethodHandle arg1)
212: throws Throwable {
213: org.codehaus.wadi.group.ClusterListener wadiListener = (org.codehaus.wadi.group.ClusterListener) arg0[0];
214: wadiListener.onMembershipChanged(wadiCluster,
215: Collections.singleton(peer1), Collections
216: .singleton(peer2));
217: return null;
218: }
219:
220: });
221:
222: listener
223: .onMembershipChanged(cluster, Collections
224: .singleton(nodePeer1), Collections
225: .singleton(nodePeer2));
226:
227: startVerificationAndInitWADICluster();
228:
229: cluster.addClusterListener(listener);
230: }
231:
232: public void testRemoveListenersOnStopOrFail() throws Exception {
233: wadiCluster.removeClusterListener(null);
234: modify().args(is.NOT_NULL);
235:
236: AbstractExpression assertSame = new AssertSameWADIListener();
237: wadiCluster.addClusterListener(null);
238: modify().args(assertSame);
239: wadiCluster.removeClusterListener(null);
240: modify().args(assertSame);
241:
242: startVerificationAndInitWADICluster();
243:
244: cluster.addClusterListener(listener);
245: cluster.doStop();
246: }
247:
248: private void startVerificationAndInitWADICluster() throws Exception {
249: startVerification();
250: cluster.doStart();
251: cluster.getRemoteNodes();
252: }
253:
254: private final class AssertSameWADIListener extends
255: AbstractExpression {
256: private org.codehaus.wadi.group.ClusterListener wadiListener;
257:
258: public void describeWith(ExpressionDescriber arg0)
259: throws IOException {
260: }
261:
262: public boolean passes(Object arg0) {
263: if (null == wadiListener) {
264: wadiListener = (org.codehaus.wadi.group.ClusterListener) arg0;
265: } else {
266: assertSame(wadiListener, arg0);
267: }
268: return true;
269: }
270: }
271:
272: }
|