KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > ha > singleton > test > HASingletonSupportUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.ha.singleton.test;
23  
24 import java.util.ArrayList JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.jboss.test.ha.singleton.HASingletonSupportTester;
29
30
31 public class HASingletonSupportUnitTestCase extends TestCase
32 {
33
34   private HASingletonSupportTester singletonSupportTester = null;
35
36   public HASingletonSupportUnitTestCase(String JavaDoc testCaseName)
37   {
38     super(testCaseName);
39   }
40
41
42   public void setUp()
43   {
44     singletonSupportTester = new HASingletonSupportTester();
45   }
46   
47   public void tearDown()
48   {
49     singletonSupportTester = null;
50   }
51   
52   public void testStartService() throws Exception JavaDoc
53   {
54     singletonSupportTester.start();
55
56     // test that the correct start sequence was followed correctly
57
assertEquals("method not invoked as expected",
58       singletonSupportTester.__invokationStack__.pop(), "registerDRMListener");
59     assertEquals("method not invoked as expected",
60       singletonSupportTester.__invokationStack__.pop(), "registerRPCHandler");
61     assertEquals("method not invoked as expected",
62       singletonSupportTester.__invokationStack__.pop(), "setupPartition");
63       
64   }
65
66   public void testStopService() throws Exception JavaDoc
67   {
68     singletonSupportTester.start();
69     singletonSupportTester.stop();
70
71     assertEquals("method not invoked as expected",
72       singletonSupportTester.__invokationStack__.pop(), "unregisterRPCHandler");
73     assertEquals("method not invoked as expected",
74       singletonSupportTester.__invokationStack__.pop(), "unregisterDRMListener");
75     
76   }
77   
78   public void testBecomeMasterNode() throws Exception JavaDoc
79   {
80     singletonSupportTester.start();
81     
82     // register DRM Listener is expected to call back
83
singletonSupportTester.__isDRMMasterReplica__ = true;
84     singletonSupportTester.partitionTopologyChanged( new ArrayList JavaDoc(2), 1);
85
86     // test whether it was elected
87
assertTrue("expected to become master", singletonSupportTester.isMasterNode());
88     
89     // test whether the election sequence was followed correctly
90
assertEquals("method not invoked as expected",
91       singletonSupportTester.__invokationStack__.pop(), "startSingleton");
92     //assertEquals("method not invoked as expected",
93
// singletonSupportTester.__invokationStack__.pop(), "callMethodOnCluster:_stopOldMaster");
94
assertEquals("method not invoked as expected",
95       singletonSupportTester.__invokationStack__.pop(), "makeThisNodeMaster");
96   }
97   
98   public void testBecomeSlaveNodeWithAnotherMaster() throws Exception JavaDoc
99   {
100     singletonSupportTester.start();
101     
102     boolean savedIsMasterNode = singletonSupportTester.isMasterNode();
103     
104     // register DRM Listener is expected to call back
105
singletonSupportTester.__isDRMMasterReplica__ = false;
106     singletonSupportTester.partitionTopologyChanged(new ArrayList JavaDoc(2), 1);
107     
108     // this call back should not change the master/slave status
109
assertEquals("expected to be still in old master/slave state", singletonSupportTester.isMasterNode(), savedIsMasterNode );
110     
111     // the new master is expected to call back
112
singletonSupportTester._stopOldMaster();
113     
114     if (savedIsMasterNode)
115     {
116       assertEquals("this node was the old master, but method not invoked as expected",
117         singletonSupportTester.__invokationStack__.pop(), "stopSingleton");
118     }
119       
120     // now it should be slave
121
assertTrue("expected to be slave", !singletonSupportTester.isMasterNode());
122             
123   }
124
125   public void testStopOnlyNode() throws Exception JavaDoc
126   {
127     singletonSupportTester.start();
128     
129     // register DRM Listener is expected to call back
130
singletonSupportTester.__isDRMMasterReplica__ = true;
131     singletonSupportTester.partitionTopologyChanged( new ArrayList JavaDoc(2), 1);
132
133     // test whether it was elected for master
134
assertTrue("expected to become master", singletonSupportTester.isMasterNode());
135     
136     singletonSupportTester.stop();
137     
138     // register DRM Listener is expected to call back
139
singletonSupportTester.__isDRMMasterReplica__ = false;
140     // since the only node (this one) in the partition is now removed, the replicants list should be empty
141
singletonSupportTester.partitionTopologyChanged(new ArrayList JavaDoc(0), 1);
142     
143     assertTrue("expected to have made a call to _stopOldMater(), thus become slave", !singletonSupportTester.isMasterNode() );
144     
145     assertEquals("method not invoked as expected",
146       singletonSupportTester.__invokationStack__.pop(), "stopSingleton");
147       
148   }
149   
150 }
151
Popular Tags