KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbeancluster > test > BeanUnitTestCaseNew


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.testbeancluster.test;
23
24
25 import java.util.Date JavaDoc;
26
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import junit.framework.Test;
31
32 import org.jboss.test.testbeancluster.interfaces.StatelessSession;
33 import org.jboss.test.testbean.interfaces.StatelessSessionHome;
34 import org.jboss.test.testbeancluster.interfaces.StatefulSession;
35 import org.jboss.test.testbean.interfaces.StatefulSessionHome;
36
37 import org.jboss.test.JBossClusteredTestCase;
38 import org.jboss.test.testbeancluster.interfaces.NodeAnswer;
39
40 /**
41  * Test SLSB for load-balancing behaviour
42  *
43  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
44  * @version $Revision: 37406 $
45  *
46  * <p><b>Revisions:</b>
47  *
48  * <p><b>12 avril 2002 Sacha Labourey:</b>
49  * <ul>
50  * <li> First implementation </li>
51  * </ul>
52  * <UL>
53  * <LI>Anil.Saldhana@jboss.org</LI>
54  * </UL>
55  */

56
57 public class BeanUnitTestCaseNew extends JBossClusteredTestCase
58 {
59    static boolean deployed = false;
60    public static int test = 0;
61    static Date JavaDoc startDate = new Date JavaDoc();
62    
63    private String JavaDoc[] servernames= {"jnp://localhost:1099", "jnp://localhost:1199"};
64    
65    protected final String JavaDoc namingFactory =
66    System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
67    
68    protected final String JavaDoc providerURL =
69    System.getProperty(Context.PROVIDER_URL);
70    
71    public BeanUnitTestCaseNew (String JavaDoc name) {
72       super(name);
73       try{
74          //Setting servernames gives us flexibility.
75
//Further flexibility can be brought by reading the servernames
76
//from a properties file
77
this.setServerNames(servernames);
78       }catch(Exception JavaDoc e){
79             getLog().debug( "Passing servernames croacked");
80           }
81    }
82
83    public static Test suite() throws Exception JavaDoc
84    {
85       Test t1 = JBossClusteredTestCase.getDeploySetup(BeanUnitTestCaseNew.class, "testbeancluster.jar");
86       return t1;
87    }
88
89    public void testStatelessBeanLoadBalancing()
90    throws Exception JavaDoc
91    {
92       getLog().debug(++test+"- "+"Trying the context...");
93       
94       Context JavaDoc ctx = new InitialContext JavaDoc();
95       getLog().debug("OK");
96       
97       ///*
98
getLog().debug("");
99       getLog().debug("Test Stateless Bean load-balancing");
100       getLog().debug("==================================");
101       getLog().debug("");
102       getLog().debug(++test+"- "+"Looking up the home nextgen.StatelessSession...");
103       StatelessSessionHome statelessSessionHome =
104       (StatelessSessionHome) ctx.lookup("nextgen.StatelessSession");
105       if (statelessSessionHome!= null ) getLog().debug("ok");
106       getLog().debug(++test+"- "+"Calling create on StatelessSessionHome...");
107       StatelessSession statelessSession =
108       (StatelessSession)statelessSessionHome.create();
109       assertTrue("statelessSessionHome.create() != null", statelessSession != null);
110       getLog().debug("ok");
111       
112       getLog().debug(++test+"- "+"Calling getEJBHome() on StatelessSession...");
113       assertTrue("statelessSession.getEJBHome() != null", statelessSession.getEJBHome() != null);
114       getLog().debug("ok");
115       
116       getLog().debug(++test+"- "+"Reseting the number of calls made on beans (making 2 calls)... ");
117       for (int i=0; i<6; i++)
118       {
119          getLog().debug(++test+"- "+" Reseting number... ");
120          statelessSession.resetNumberOfCalls ();
121       }
122       
123       getLog().debug(++test+"- "+"Now making 20 calls on this remote... ");
124       for (int i=0; i<20; i++)
125       {
126          getLog().debug(++test+"- "+" Calling remote... ");
127          statelessSession.makeCountedCall ();
128       }
129       
130       getLog().debug(++test+"- "+"Getting the number of calls that have been performed on each bean... ");
131       long node1 = statelessSession.getCallCount();
132       getLog().debug(++test+"- "+"One node has received: " + node1);
133       
134       long node2 = statelessSession.getCallCount();
135       getLog().debug(++test+"- "+"The other node has received: " + node2);
136       
137       if (node1 == node2 &&
138           node1 == 10)
139       {
140          getLog().debug(++test+"- "+"Test is ok.");
141       }
142       else
143       {
144          getLog().debug(++test+"- "+"Something wrong has happened! Calls seems not to have been load-balanced.");
145          fail ("Calls have not been correctly load-balanced on the SLSB remote interface.");
146       }
147       
148       statelessSession.remove();
149       getLog().debug("ok");
150    }
151    
152    public void testStatefulBeanFailover()
153    throws Exception JavaDoc
154    {
155       getLog().debug(++test+"- "+"Trying the context...");
156       
157       Context JavaDoc ctx = new InitialContext JavaDoc();
158       getLog().debug("OK");
159       
160       ///*
161
getLog().debug("");
162       getLog().debug("Test Stateful Bean Failover");
163       getLog().debug("==================================");
164       getLog().debug("");
165       getLog().debug(++test+"- "+"Looking up the home nextgen.StatefulSession...");
166       StatefulSessionHome statefulSessionHome =
167       (StatefulSessionHome) ctx.lookup("nextgen.StatefulSession");
168       if (statefulSessionHome!= null ) getLog().debug("ok");
169          getLog().debug(++test+"- "+"Calling create on StatefulSessionHome...");
170       StatefulSession statefulSession =
171       (StatefulSession)statefulSessionHome.create("Bupple-Dupple");
172       assertTrue("statefulSessionHome.create() != null", statefulSession != null);
173       getLog().debug("ok");
174       
175       NodeAnswer node1 = statefulSession.getNodeState ();
176       getLog ().debug (node1);
177       
178       // Now we switch to the other node, simulating a failure on node 1
179
//
180
System.setProperty ("JBossCluster-DoFail", "once");
181       NodeAnswer node2 = statefulSession.getNodeState ();
182       getLog ().debug (node2);
183       
184       assertTrue ("No failover has occured!", !node1.nodeId.equals (node2.nodeId));
185       
186       assertTrue ("Value is not identical on replicated node", "Bupple-Dupple".equals (node1.answer) &&
187                      node1.answer.equals (node2.answer) );
188
189       // we change our name to see if it replicates to node 1
190
//
191
statefulSession.setName ("Changed");
192       
193       // now we travel back on node 1
194
//
195
System.setProperty ("JBossCluster-DoFail", "once");
196       node1 = statefulSession.getNodeState ();
197       getLog ().debug (node1);
198       
199       assertTrue ("No failover has occured!", !node1.nodeId.equals (node2.nodeId));
200       
201       assertTrue ("Value is not identical on replicated node", "Changed".equals (node1.answer) );
202       
203       statefulSession.remove();
204       getLog().debug("ok");
205    }
206
207 }
208
Popular Tags