1 64 65 package com.jcorporate.expresso.ext.tests; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.registry.ExpressoThread; 69 import com.jcorporate.expresso.services.test.TestSystemInitializer; 70 71 import java.util.ArrayList ; 72 import java.util.Iterator ; 73 74 75 85 public class StressThread 86 extends ExpressoThread { 87 static final int NUM_RECORDS = 60; 88 ArrayList myIds = new ArrayList (NUM_RECORDS); 89 int numLoops; 90 int userUid; 91 boolean success = true; 92 int maxID = 0; 93 94 public StressThread(int loops, int uid) { 95 numLoops = loops; 96 userUid = uid; 97 } 98 99 public boolean getSuccess() { 100 return success; 101 } 102 103 public void run() { 104 super.run(); 105 try { 106 for (int i = 0; i < numLoops; i++) { 107 addTestObjects(); 108 System.gc(); 109 retrieveTestObjects(); 110 System.gc(); 111 updateTestObjects(); 112 System.gc(); 113 searchAndRetrieveTestObjects(); 114 System.gc(); 115 deleteTestObjects(); 116 } 117 } catch (DBException dbe) { 118 success = false; 119 System.out.println(dbe.getMessage()); 120 dbe.printStackTrace(); 121 } 122 } 123 124 protected void addTestObjects() 125 throws DBException { 126 StressTestObject sto = new StressTestObject(userUid); 127 sto.setDataContext(TestSystemInitializer.getTestContext()); 128 sto.setField("SomeData", "abcdefg"); 129 sto.setField("SomeMoreData", "hijklmnop"); 130 131 for (int i = 0; i < NUM_RECORDS; i++) { 132 sto.add(); 133 myIds.add(sto.getField("RecordID")); 134 } 135 136 maxID = Integer.parseInt(sto.getField("RecordID")); 137 } 138 139 142 protected void updateTestObjects() 143 throws DBException { 144 StressTestObject sto = new StressTestObject(userUid); 145 sto.setDataContext(TestSystemInitializer.getTestContext()); 146 147 for (Iterator i = myIds.iterator(); i.hasNext();) { 148 sto.setField("RecordID", (String ) i.next()); 149 sto.retrieve(); 150 sto.setField("SomeData", "oser"); 151 sto.setField("SomeMoreData", "good morning vietnam"); 152 sto.update(); 153 } 154 } 155 156 159 protected void searchAndRetrieveTestObjects() 160 throws DBException { 161 StressTestObject sto = new StressTestObject(userUid); 162 sto.setDataContext(TestSystemInitializer.getTestContext()); 163 sto.setMaxRecords(25); 164 165 for (int i = 0; i < 20; i++) { 166 ArrayList al = sto.searchAndRetrieveList(); 167 168 if (al.size() == 0) { 169 throw new DBException("ArrayList was empty"); 170 } 171 } 172 } 173 174 protected void retrieveTestObjects() 175 throws DBException { 176 StressTestObject sto = new StressTestObject(userUid); 177 sto.setDataContext(TestSystemInitializer.getTestContext()); 178 179 for (int j = 0; j < 5; j++) { 180 for (Iterator i = myIds.iterator(); i.hasNext();) { 181 sto.clear(); 182 sto.setField("RecordID", (String ) i.next()); 183 sto.retrieve(); 184 } 185 } 186 } 187 188 protected void deleteTestObjects() 189 throws DBException { 190 StressTestObject sto = new StressTestObject(userUid); 191 sto.setDataContext(TestSystemInitializer.getTestContext()); 192 193 for (Iterator i = myIds.iterator(); i.hasNext();) { 194 sto.setField("RecordID", (String ) i.next()); 195 196 if (sto.find()) { 197 sto.delete(); 198 } 199 } 200 201 myIds = new ArrayList (NUM_RECORDS); 202 } 203 } 204 | Popular Tags |