1 24 package org.objectweb.jalisto.se.test.workbench; 25 26 import junit.extensions.jfunc.JFuncTestCase; 27 import org.objectweb.jalisto.se.api.ClassDescription; 28 import org.objectweb.jalisto.se.api.Session; 29 import org.objectweb.jalisto.se.api.Transaction; 30 import org.objectweb.jalisto.se.api.query.IndexManager; 31 import org.objectweb.jalisto.se.api.query.QueryManager; 32 import org.objectweb.jalisto.se.JalistoFactory; 33 import org.objectweb.jalisto.se.test.data.Book; 34 import org.objectweb.jalisto.se.test.data.BookWithAuthor; 35 36 import java.util.Iterator ; 37 38 public class JalistoTestCase extends JFuncTestCase { 39 40 public JalistoTestCase() { 41 super(); 42 } 43 44 public JalistoTestCase(String name) { 45 super(name); 46 } 47 48 protected void setTestEngine(JalistoTestEngine testEngine) { 49 this.testEngine = testEngine; 50 } 51 52 protected void setUpOnce() throws Exception { 53 System.out.println("=== INIT ==="); 54 super.setUpOnce(); 55 } 56 57 protected void tearDownOnce() throws Exception { 58 super.tearDownOnce(); 59 System.out.println("=== CLOSE ==="); 60 } 61 62 protected void tearDown() throws Exception { 63 super.tearDown(); 64 endTransaction(); 65 } 66 67 protected static Object newTestCase(JalistoTestSuite suite, 68 JalistoTestCase testCase) { 69 return suite.getTestProxy(testCase); 70 } 71 72 protected String getJalistoPropertiesFilename() { 73 return testEngine.getDBPropertiesFilename(); 74 } 75 76 79 80 public void initSession(boolean withErase) { 81 finishTests(); 82 session = JalistoFactory.getSession(getJalistoPropertiesFilename()); 83 if (withErase && session.getInternalSession().getProperties().allowsSpecialFunctionnalities()) { 84 session.eraseStorage(); 85 } 86 session.openSession(); 87 tx = session.currentTransaction(); 88 if(tx == null) { 89 throw new IllegalStateException ("tx == null"); 90 } 91 queryManager = session.getQueryManager(); 92 if (queryManager != null) { 93 indexManager = queryManager.getIndexManager(); 94 } 95 } 96 97 public void define(ClassDescription meta) { 98 session.defineClass(meta); 99 } 100 101 public void cleanUp(Class aClass) { 102 tx.begin(); 103 Iterator extentIt = session.getExtent(aClass).readFully().iterator(); 104 while (extentIt.hasNext()) { 105 Object oid = extentIt.next(); 106 session.deleteObjectByOid(oid); 107 } 108 tx.commit(); 109 } 110 111 public void populate(int nbr) { 112 tx.begin(); 113 for (int i = 0; i < nbr; i++) { 114 session.createObject(Book.newBook().toArray(), Book.class); 115 if ((i != 0) && ((i % 1000) == 0)) { 116 tx.commit(); 117 tx.begin(); 118 } 119 } 120 tx.commit(); 121 } 122 123 public void populateBookWithAuthor(int nbr) { 124 tx.begin(); 125 for (int i = 0; i < nbr; i++) { 126 session.createObject(BookWithAuthor.newBook().toArray(), BookWithAuthor.class); 127 if ((i != 0) && ((i % 1000) == 0)) { 128 tx.commit(); 129 tx.begin(); 130 } 131 } 132 tx.commit(); 133 } 134 135 public void finishTests() { 136 endTransaction(); 137 closeSession(); 138 } 139 140 public void endTransaction() { 141 if ((tx != null) && (tx.isActive())) { 142 tx.rollback(); 143 } 144 } 145 146 public void closeSession() { 147 endTransaction(); 148 if ((session != null) && (session.isOpen())) { 149 session.closeSession(); 150 } 151 } 152 153 protected Session session = null; 154 protected Transaction tx = null; 155 protected QueryManager queryManager = null; 156 protected IndexManager indexManager = null; 157 158 159 protected final Object [] oneObjectArray = new Object [1]; 160 private JalistoTestEngine testEngine; 161 162 public static final int numberOfQueryExecute = 30; 163 } 164 | Popular Tags |