KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > manyops > A_manyops


1
2 package org.objectweb.jonas.stests.manyops;
3
4 import java.rmi.RemoteException JavaDoc;
5
6 import javax.naming.NamingException JavaDoc;
7 import javax.rmi.PortableRemoteObject JavaDoc;
8
9 import org.objectweb.jonas.stests.util.JTestCase;
10
11
12 public abstract class A_manyops extends JTestCase {
13
14     // Product's number initially created in the database table
15
private static final int NB_PRODUCTS = 2500;
16     // Create operation number done for one "many-create" operation
17
private static final int NB_CREATE = 20;
18
19     //
20
protected static InitDBHome idbHome = null;
21     protected static InitDB idb = null;
22     protected static ProductHome pHome = null;
23     protected static boolean initialized = false;
24
25     public A_manyops(String JavaDoc name) {
26     super(name);
27     }
28
29     protected void setUp() throws Exception JavaDoc {
30     super.setUp();
31     if (idbHome == null) {
32         useBeans("manyops");
33         idbHome = (InitDBHome) PortableRemoteObject.narrow(ictx.lookup("manyopsInitDBHome"), InitDBHome.class);
34     }
35     if (idb == null) {
36         idb = idbHome.create();
37     }
38     if (pHome == null) {
39         pHome = getProductHome();
40     }
41     if (!initialized) {
42         // The first time, create the database tables needed, and insert NB_PRODUCTS lines
43
idb.createTable();
44         for (int i=1; i<=NB_PRODUCTS; i++) {
45         pHome.create("p"+i, i, 0);
46         }
47         initialized = true;
48     }
49     }
50
51     /**
52      * return a Product Home
53      */

54     abstract ProductHome getProductHome() throws NamingException JavaDoc;
55     
56     protected void tearDown() throws Exception JavaDoc {
57     super.tearDown();
58     }
59
60     /**
61      * Allows to do 'opNb' times of the 'opType' operation on a Product bean.
62      * The 'opType' may be:
63      * - 'create',
64      * - 'createmany' (ie one operation is N create operation)
65      * - 'delete'
66      * - 'get'
67      * - 'set'
68      * - 'findByPk'
69      * - 'findByNumber'
70      */

71     public void manyops(int opNb, String JavaDoc opType) throws Exception JavaDoc {
72     if (opNb > NB_PRODUCTS) {
73         throw new Exception JavaDoc("Operation number must be less than the initial product number");
74     }
75     // Do 'opNb' times the 'opType' operation
76
for (int i=1; i<=opNb; i++) {
77         Product bean;
78         int p;
79         if ("create".equals(opType)) {
80         int ipk = NB_PRODUCTS+i;
81         String JavaDoc spk = "p"+ipk;
82         bean = pHome.create(spk, ipk, 1);
83         } else if ("createmany".equals(opType)) {
84         for (int ii=1; ii<=NB_CREATE; ii++) {
85             int iipk = NB_PRODUCTS+ii;
86             bean = pHome.create("p"+iipk, iipk, 1);
87         }
88         for (int ii=1; ii<=NB_CREATE; ii++) {
89             int iipk = NB_PRODUCTS+ii;
90             pHome.remove("p"+iipk);
91         }
92         } else if ("delete".equals(opType)) {
93         String JavaDoc spk = "p"+i;
94         pHome.remove(spk);
95         } else {
96         // Choose randomly the bean on which the operation is done
97
int ipk = random(NB_PRODUCTS)+1;
98         String JavaDoc spk = "p"+ipk;
99         if ("get".equals(opType)) {
100             bean = pHome.findByPrimaryKey(spk);
101             p = bean.getPrice();
102         } else if ("set".equals(opType)) {
103             bean = pHome.findByPrimaryKey(spk);
104             bean.setPrice(2);
105         } else if ("findByPk".equals(opType)) {
106             bean = pHome.findByPrimaryKey(spk);
107         } else if ("findByNumber".equals(opType)) {
108             bean = pHome.findByNumber(ipk);
109         } else {
110             throw new Exception JavaDoc("Invalid operation name ("+opType+")");
111         }
112         }
113     }
114     // Undo
115
for (int i=1; i<=opNb; i++) {
116         if ("create".equals(opType)) {
117         int ipk = NB_PRODUCTS+i;
118         String JavaDoc spk = "p"+ipk;
119         pHome.remove(spk);
120         } else if ("createmany".equals(opType)) {
121         // Nothing to undo
122
} else if ("get".equals(opType)) {
123         // Nothing to undo
124
} else if ("set".equals(opType)) {
125         // Nothing to undo
126
} else if ("findByPk".equals(opType)) {
127         // Nothing to undo
128
} else if ("delete".equals(opType)) {
129         String JavaDoc spk = "p"+i;
130         pHome.create(spk, i, 0);
131         }
132     }
133     }
134
135     /**
136      * random returns an integer between 0 and max-1
137      */

138     private int random(int max) throws RemoteException JavaDoc {
139     double d = Math.random();
140     int ret = (int) (max * d);
141     return ret;
142     }
143
144     public void test_100_create() throws Exception JavaDoc {
145     manyops(100, "create");
146     }
147     public void test_100_get() throws Exception JavaDoc {
148     manyops(100, "get");
149     }
150     public void test_100_set() throws Exception JavaDoc {
151     manyops(100, "set");
152     }
153     public void test_100_findByPk() throws Exception JavaDoc {
154     manyops(100, "findByPk");
155     }
156     public void test_100_findByNumber() throws Exception JavaDoc {
157     manyops(100, "findByNumber");
158     }
159     public void test_100_delete() throws Exception JavaDoc {
160     manyops(100, "delete");
161     }
162     public void test_100_createmany() throws Exception JavaDoc {
163     manyops(100, "createmany");
164     }
165
166     public void test_250_create() throws Exception JavaDoc {
167     manyops(250, "create");
168     }
169     public void test_250_get() throws Exception JavaDoc {
170     manyops(250, "get");
171     }
172     public void test_250_set() throws Exception JavaDoc {
173     manyops(250, "set");
174     }
175     public void test_250_findByPk() throws Exception JavaDoc {
176     manyops(250, "findByPk");
177     }
178     public void test_250_findByNumber() throws Exception JavaDoc {
179     manyops(250, "findByNumber");
180     }
181     public void test_250_delete() throws Exception JavaDoc {
182     manyops(100, "delete");
183     }
184     public void test_250_createmany() throws Exception JavaDoc {
185     manyops(250, "createmany");
186     }
187
188     public void test_500_create() throws Exception JavaDoc {
189     manyops(500, "create");
190     }
191     public void test_500_get() throws Exception JavaDoc {
192     manyops(500, "get");
193     }
194     public void test_500_set() throws Exception JavaDoc {
195     manyops(500, "set");
196     }
197     public void test_500_findByPk() throws Exception JavaDoc {
198     manyops(500, "findByPk");
199     }
200     public void test_500_findByNumber() throws Exception JavaDoc {
201     manyops(500, "findByNumber");
202     }
203     public void test_500_delete() throws Exception JavaDoc {
204     manyops(100, "delete");
205     }
206     public void test_500_createmany() throws Exception JavaDoc {
207     manyops(500, "createmany");
208     }
209
210 }
211
Popular Tags