KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > distribution > F_Cluster


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_Cluster.java,v 1.7 2005/05/06 17:03:21 ashah Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.distribution;
27
28 import java.util.Collection JavaDoc;
29
30 import javax.naming.NamingException JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32 import javax.transaction.RollbackException JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37
38 import org.objectweb.jonas.jtests.beans.cluster.Identity;
39 import org.objectweb.jonas.jtests.beans.cluster.IdentityHome;
40 import org.objectweb.jonas.jtests.util.JTestCase;
41     
42 /**
43  * Test clustering first level (mainly test the "shared" flag for entity beans)
44  * Assume that transaction isolation is "serialized" !
45  * Beans used: cluster
46  * @author Philippe Durieux
47  */

48
49 /**
50  * A thread will work on a Home. It executes one of the predefined functions,
51  * inside a transaction.
52  */

53 class A_clthread extends Thread JavaDoc {
54     int function;
55     IdentityHome home;
56     String JavaDoc name;
57     int number;
58     UserTransaction JavaDoc utx = F_Cluster.utx;
59
60     public A_clthread(IdentityHome home, int function, String JavaDoc name, int number) {
61         this.home = home;
62         this.function = function;
63         this.name = name;
64         this.number = number;
65     }
66
67     public void run() {
68         Identity id = null;
69
70         try {
71             // start transaction
72
utx.begin();
73
74             // process the required function
75
switch (function) {
76             case F_Cluster.READ:
77                 id = home.findByPrimaryKey(name);
78                 id.getNumber();
79                 break;
80             case F_Cluster.WRITE:
81                 id = home.findByPrimaryKey(name);
82                 id.setNumber(number);
83                 break;
84             case F_Cluster.CREATE:
85                 id = home.create(name, number);
86                 break;
87             case F_Cluster.REMOVE:
88                 home.remove(name);
89                 break;
90             }
91         
92             // commit transaction
93
utx.commit();
94         } catch (Exception JavaDoc e) {
95             F_Cluster.threadError = e;
96         }
97     }
98 }
99
100 public class F_Cluster extends JTestCase {
101
102     protected static IdentityHome home1 = null;
103     protected static IdentityHome home2 = null;
104     protected static IdentityHome home3 = null;
105
106     public static Exception JavaDoc threadError = null;
107
108     // possible values for function
109
public static final int READ = 1;
110     public static final int WRITE = 2;
111     public static final int CREATE = 3;
112     public static final int REMOVE = 4;
113
114     public F_Cluster(String JavaDoc name) {
115         super(name);
116     }
117
118     protected void setUp() {
119         super.setUp();
120         if (home3 == null) {
121             useBeans("cluster", true);
122             try {
123                 home1 = (IdentityHome) PortableRemoteObject.narrow(ictx.lookup("clusterId_1"), IdentityHome.class);
124                 assertNotNull("home of clusterId_1 is null", home1);
125                 home2 = (IdentityHome) PortableRemoteObject.narrow(ictx.lookup("clusterId_2"), IdentityHome.class);
126                 assertNotNull("home of clusterId_2 is null", home2);
127                 home3 = (IdentityHome) PortableRemoteObject.narrow(ictx.lookup("clusterId_3"), IdentityHome.class);
128                 assertNotNull("home of clusterId_3 is null", home3);
129             } catch (NamingException JavaDoc e) {
130                 fail("Cannot get bean home: " + e.getMessage());
131             }
132         }
133     }
134
135     protected void tearDown() throws Exception JavaDoc {
136         threadError = null;
137         super.tearDown();
138     }
139
140     /**
141      * Basic Test to see if all works with a single bean (it should!)
142      * Pre-requisite: the table has been initialized first with some
143      * well-known values (name1, ...)
144      */

145     public void testSingle() throws Exception JavaDoc {
146         // test finder methods
147
Identity name1 = home3.findByPrimaryKey("name1");
148         assertTrue(name1.getNumber() == 1000);
149         Collection JavaDoc c1000 = home3.findByNumber(1000);
150         home3.findAll();
151         // test basic create/access/modify/remove operations
152
Identity newname = home3.create("name28", 300);
153         int nv = 200;
154         newname.setNumber(nv);
155         assertEquals("Wrong field value", nv, newname.getNumber());
156         newname.remove();
157     }
158
159     /**
160      * Test with 2 homes (no parallellism)
161      */

162     public void test2Home() throws Exception JavaDoc {
163         int val1 = 31;
164         int val2 = 32;
165         Identity name1 = home1.create("john", val1);
166         Identity name2 = home2.findByPrimaryKey("john");
167         assertTrue(name2.getNumber() == val1);
168         name2.setNumber(val2);
169         assertEquals("Wrong field value", val2, name1.getNumber());
170         name2.remove();
171         name1 = home1.create("john", val1);
172         home2.remove("john");
173     }
174
175     /**
176      * Test isolation case 1:
177      * 2 parallell transactions writing the same instance.
178      * Assumes that the bean has the shared attribute.
179      * This test should pass if the transaction isolation is set
180      * to "serializable" for the database.
181      */

182     public void testIso1() throws Exception JavaDoc {
183         int val1 = random(500);
184         int val2 = val1+1;
185         Identity name11 = home1.findByPrimaryKey("name11");
186         utx.begin();
187         name11.setNumber(val1);
188         // start a thread that should be put waiting for transaction finish
189
A_clthread th2 = new A_clthread(home2, WRITE, "name11", val2);
190         th2.start();
191         // wait to let the other thread finish
192
th2.join();
193         // check value has not been modified (transaction isolation)
194
assertEquals("Value has been modified (no transaction isolation)",
195                      val1, name11.getNumber());
196         try {
197             utx.commit();
198         } catch (RollbackException JavaDoc e) {
199             // the second transaction passed first!
200
assertEquals("Both transactions are not serialized",
201                          val2, name11.getNumber());
202             return;
203         }
204         if (threadError != null) {
205             // 1st transaction passed, second rolled back
206
throw new RuntimeException JavaDoc("error in 2nd thread", threadError);
207         }
208         fail("both transactions should not pass");
209     }
210
211
212     public static Test suite() {
213         return new TestSuite(F_Cluster.class);
214     }
215
216     public static void main (String JavaDoc args[]) {
217         String JavaDoc testtorun = null;
218         // Get args
219
for (int argn = 0; argn < args.length; argn++) {
220             String JavaDoc s_arg = args[argn];
221             Integer JavaDoc i_arg;
222             if (s_arg.equals("-n")) {
223                 testtorun = args[++argn];
224             }
225         }
226         if (testtorun == null) {
227             junit.textui.TestRunner.run(suite());
228         } else {
229             junit.textui.TestRunner.run(new F_Cluster(testtorun));
230         }
231     }
232 }
233
Popular Tags