1 25 26 package org.objectweb.jonas.jtests.util; 27 28 import java.lang.reflect.Constructor ; 29 import java.rmi.RemoteException ; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.rmi.PortableRemoteObject ; 35 import javax.transaction.UserTransaction ; 36 37 import junit.extensions.RepeatedTest; 38 import junit.framework.TestCase; 39 40 import org.objectweb.jonas.adm.AdmInterface; 41 import org.objectweb.jonas.jtests.tables.DBEnv; 42 import org.objectweb.jonas.jtests.tables.DBEnvHome; 43 44 48 public abstract class JTestCase extends TestCase { 49 protected static String jonasName = "jonas"; protected static String testtorun = null; 51 protected static Context ictx = null; 52 public static UserTransaction utx = null; 53 private static AdmInterface admI = null; 54 private static DBEnv dbEnv = null; 55 private static boolean tableSessionLoaded = false; 56 57 protected StringBuffer msgerror = null; 58 59 public static final String PACKAGE = "org.objectweb.jonas.jtests.clients."; 60 61 public JTestCase(String name) { 62 super(name); 63 } 64 65 protected static void callTest(String classname, String testname) throws Exception { 66 Class clazz = Class.forName(PACKAGE + classname); 67 Class [] paramTypes = {String .class}; 68 Object [] arguments = {testname}; 69 Constructor constructor = clazz.getDeclaredConstructor(paramTypes); 70 JTestCase mytestcase = (JTestCase) constructor.newInstance(arguments); 71 System.out.print("Running " + classname + " (" + testname + ")\t"); 72 junit.textui.TestRunner.run(mytestcase); 73 } 74 75 protected static void callrepeatedTest(String classname, String testname, int n) throws Exception { 76 Class clazz = Class.forName(PACKAGE + classname); 77 Class [] paramTypes = {String .class}; 78 Object [] arguments = {testname}; 79 Constructor constructor = clazz.getDeclaredConstructor(paramTypes); 80 JTestCase mytestcase = (JTestCase) constructor.newInstance(arguments); 81 RepeatedTest myrepeatedtest = new RepeatedTest(mytestcase,n); 82 System.out.print("Running repeated " + n + "times " + classname + " (" + testname + ")\t"); 83 junit.textui.TestRunner.run(myrepeatedtest); 84 } 85 86 89 public int random(int max) throws RemoteException { 90 double d = Math.random(); 91 int ret = (int) (max * d); 92 return ret; 93 } 94 95 private Context getInitialContext() throws NamingException { 96 97 String registryPort = System.getProperty("jonas.registryport"); 98 return new InitialContext (); 99 } 100 101 104 protected void setUp() { 105 try { 106 if (ictx == null) { 108 ictx = getInitialContext(); 109 } 110 111 if (utx == null) { 113 utx = (UserTransaction ) PortableRemoteObject.narrow(ictx.lookup("java:comp/UserTransaction"), UserTransaction .class); 114 } 115 116 if (!tableSessionLoaded) { 118 if (admI == null) { 119 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class); 120 } 121 String filename = "tables"; 122 if (!admI.isLoaded(filename + ".jar")) { 123 admI.addBeans(filename + ".jar"); 124 } 125 tableSessionLoaded = true; 126 } 127 } catch (NamingException e) { 128 System.err.println("Cannot setup test: " + e); 129 e.printStackTrace(); 130 } catch (RemoteException e) { 131 System.err.println("Cannot load session bean: " + e); 132 } 133 debug("Junit Running " + getName()); 134 } 135 136 protected void tearDown() throws Exception { 137 } 138 139 142 public void unloadBeans(String filename) { 143 debug("unloadBeans " + filename); 144 try { 145 if (ictx == null) { 147 ictx = getInitialContext(); 148 } 149 if (admI == null) { 150 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class); 151 } 152 admI.removeBeans(filename + ".jar"); 153 } catch (Exception e) { 154 System.err.println("Cannot unload bean: " + e); 155 } 156 } 157 158 162 public void sync(boolean passivate) { 163 debug("sync"); 164 try { 165 if (ictx == null) { 166 ictx = getInitialContext(); 167 } 168 if (admI == null) { 169 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class); 170 } 171 admI.syncAllEntities(passivate); 172 } catch (Exception e) { 173 error("Cannot sync entities" + e); 174 } 175 } 176 177 184 public void useBeans(String filename, boolean create) { 185 debug("useBeans " + filename); 186 try { 187 if (ictx == null) { 189 ictx = getInitialContext(); 190 } 191 if (admI == null) { 192 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class); 193 } 194 if (!admI.isLoaded(filename + ".jar")) { 195 admI.addBeans(filename + ".jar"); 196 197 if (create) { 199 getDBEnv().initTable(filename); 200 } 201 } 202 } catch (Exception e) { 203 error("Cannot load bean: " + e); 204 } 205 } 206 207 211 public static void stopTxAt(int tt) { 212 try { 213 if (admI == null) { 214 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class); 215 } 216 admI.setTransactionTimeout(tt); 217 } catch (Exception e) { 218 System.err.println("Cannot set transaction timeout: " + e); 219 } 220 } 221 222 225 public DBEnv getDBEnv() { 226 if (dbEnv == null) { 227 228 useBeans("tables", false); 229 230 DBEnvHome dbhome = null; 232 try { 233 dbhome = (DBEnvHome) PortableRemoteObject.narrow(ictx.lookup("tablesDBEnvHome"), DBEnvHome.class); 234 } catch (NamingException e) { 235 System.err.println(">>> " + e); 236 fail("Cannot bind to DBEnvHome"); 237 } 238 239 try { 241 dbEnv = dbhome.create(); 242 } catch (Exception e) { 243 fail(e.toString()); 244 } 245 } 246 return dbEnv; 247 } 248 249 252 public static void debug(String msg) { 253 } 255 256 259 public static void error(String msg) { 260 System.err.println("ERROR: " + msg); 261 } 262 263 266 public void sleep(int msec) { 267 try { 268 Thread.sleep(msec); 269 } catch (InterruptedException e) { 270 fail(e.toString()); 271 } 272 } 273 274 public void testEmpty() throws Exception { 275 } 276 277 } 278 | Popular Tags |