001: package org.objectweb.jonas.stests.manyops;
002:
003: import java.rmi.RemoteException;
004:
005: import javax.naming.NamingException;
006: import javax.rmi.PortableRemoteObject;
007:
008: import org.objectweb.jonas.stests.util.JTestCase;
009:
010: public abstract class A_manyops extends JTestCase {
011:
012: // Product's number initially created in the database table
013: private static final int NB_PRODUCTS = 2500;
014: // Create operation number done for one "many-create" operation
015: private static final int NB_CREATE = 20;
016:
017: //
018: protected static InitDBHome idbHome = null;
019: protected static InitDB idb = null;
020: protected static ProductHome pHome = null;
021: protected static boolean initialized = false;
022:
023: public A_manyops(String name) {
024: super (name);
025: }
026:
027: protected void setUp() throws Exception {
028: super .setUp();
029: if (idbHome == null) {
030: useBeans("manyops");
031: idbHome = (InitDBHome) PortableRemoteObject.narrow(ictx
032: .lookup("manyopsInitDBHome"), InitDBHome.class);
033: }
034: if (idb == null) {
035: idb = idbHome.create();
036: }
037: if (pHome == null) {
038: pHome = getProductHome();
039: }
040: if (!initialized) {
041: // The first time, create the database tables needed, and insert NB_PRODUCTS lines
042: idb.createTable();
043: for (int i = 1; i <= NB_PRODUCTS; i++) {
044: pHome.create("p" + i, i, 0);
045: }
046: initialized = true;
047: }
048: }
049:
050: /**
051: * return a Product Home
052: */
053: abstract ProductHome getProductHome() throws NamingException;
054:
055: protected void tearDown() throws Exception {
056: super .tearDown();
057: }
058:
059: /**
060: * Allows to do 'opNb' times of the 'opType' operation on a Product bean.
061: * The 'opType' may be:
062: * - 'create',
063: * - 'createmany' (ie one operation is N create operation)
064: * - 'delete'
065: * - 'get'
066: * - 'set'
067: * - 'findByPk'
068: * - 'findByNumber'
069: */
070: public void manyops(int opNb, String opType) throws Exception {
071: if (opNb > NB_PRODUCTS) {
072: throw new Exception(
073: "Operation number must be less than the initial product number");
074: }
075: // Do 'opNb' times the 'opType' operation
076: for (int i = 1; i <= opNb; i++) {
077: Product bean;
078: int p;
079: if ("create".equals(opType)) {
080: int ipk = NB_PRODUCTS + i;
081: String spk = "p" + ipk;
082: bean = pHome.create(spk, ipk, 1);
083: } else if ("createmany".equals(opType)) {
084: for (int ii = 1; ii <= NB_CREATE; ii++) {
085: int iipk = NB_PRODUCTS + ii;
086: bean = pHome.create("p" + iipk, iipk, 1);
087: }
088: for (int ii = 1; ii <= NB_CREATE; ii++) {
089: int iipk = NB_PRODUCTS + ii;
090: pHome.remove("p" + iipk);
091: }
092: } else if ("delete".equals(opType)) {
093: String spk = "p" + i;
094: pHome.remove(spk);
095: } else {
096: // Choose randomly the bean on which the operation is done
097: int ipk = random(NB_PRODUCTS) + 1;
098: String spk = "p" + ipk;
099: 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("Invalid operation name ("
111: + opType + ")");
112: }
113: }
114: }
115: // Undo
116: for (int i = 1; i <= opNb; i++) {
117: if ("create".equals(opType)) {
118: int ipk = NB_PRODUCTS + i;
119: String spk = "p" + ipk;
120: pHome.remove(spk);
121: } else if ("createmany".equals(opType)) {
122: // Nothing to undo
123: } else if ("get".equals(opType)) {
124: // Nothing to undo
125: } else if ("set".equals(opType)) {
126: // Nothing to undo
127: } else if ("findByPk".equals(opType)) {
128: // Nothing to undo
129: } else if ("delete".equals(opType)) {
130: String spk = "p" + i;
131: pHome.create(spk, i, 0);
132: }
133: }
134: }
135:
136: /**
137: * random returns an integer between 0 and max-1
138: */
139: private int random(int max) throws RemoteException {
140: double d = Math.random();
141: int ret = (int) (max * d);
142: return ret;
143: }
144:
145: public void test_100_create() throws Exception {
146: manyops(100, "create");
147: }
148:
149: public void test_100_get() throws Exception {
150: manyops(100, "get");
151: }
152:
153: public void test_100_set() throws Exception {
154: manyops(100, "set");
155: }
156:
157: public void test_100_findByPk() throws Exception {
158: manyops(100, "findByPk");
159: }
160:
161: public void test_100_findByNumber() throws Exception {
162: manyops(100, "findByNumber");
163: }
164:
165: public void test_100_delete() throws Exception {
166: manyops(100, "delete");
167: }
168:
169: public void test_100_createmany() throws Exception {
170: manyops(100, "createmany");
171: }
172:
173: public void test_250_create() throws Exception {
174: manyops(250, "create");
175: }
176:
177: public void test_250_get() throws Exception {
178: manyops(250, "get");
179: }
180:
181: public void test_250_set() throws Exception {
182: manyops(250, "set");
183: }
184:
185: public void test_250_findByPk() throws Exception {
186: manyops(250, "findByPk");
187: }
188:
189: public void test_250_findByNumber() throws Exception {
190: manyops(250, "findByNumber");
191: }
192:
193: public void test_250_delete() throws Exception {
194: manyops(100, "delete");
195: }
196:
197: public void test_250_createmany() throws Exception {
198: manyops(250, "createmany");
199: }
200:
201: public void test_500_create() throws Exception {
202: manyops(500, "create");
203: }
204:
205: public void test_500_get() throws Exception {
206: manyops(500, "get");
207: }
208:
209: public void test_500_set() throws Exception {
210: manyops(500, "set");
211: }
212:
213: public void test_500_findByPk() throws Exception {
214: manyops(500, "findByPk");
215: }
216:
217: public void test_500_findByNumber() throws Exception {
218: manyops(500, "findByNumber");
219: }
220:
221: public void test_500_delete() throws Exception {
222: manyops(100, "delete");
223: }
224:
225: public void test_500_createmany() throws Exception {
226: manyops(500, "createmany");
227: }
228:
229: }
|