1 64 65 package com.jcorporate.expresso.core.dbobj.tests; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.db.TableCreator; 69 import com.jcorporate.expresso.core.dbobj.MultiDBObject; 70 import com.jcorporate.expresso.core.misc.ConfigManager; 71 import com.jcorporate.expresso.core.misc.ConfigurationException; 72 import com.jcorporate.expresso.services.test.TestSystemInitializer; 73 import junit.framework.Test; 74 import junit.framework.TestCase; 75 import junit.framework.TestSuite; 76 import org.apache.log4j.Logger; 77 78 import java.util.Enumeration ; 79 import java.util.Iterator ; 80 import java.util.Vector ; 81 82 83 86 public class MultiDBObjectTest 87 extends TestCase { 88 private static Logger log = Logger.getLogger(MultiDBObjectTest.class); 89 90 91 public static void main(String [] args) 92 throws Exception { 93 94 junit.textui.TestRunner.run(suite()); 96 } 97 98 103 public MultiDBObjectTest(String name) { 104 super(name); 105 } 106 107 112 public int countTestCases() { 113 return 1; 114 } 115 116 120 protected void setUp() 121 throws Exception { 122 TestSystemInitializer.setUp(); 123 124 try { 125 ConfigManager.getContext(TestSystemInitializer.getTestContext()); 126 } catch (ConfigurationException ce) { 127 fail("There is no 'test' db/context set up - cannot run db object tests without a test context"); 128 } 129 130 TestSchema ts = new TestSchema(); 131 Vector v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext()); 132 133 for (Enumeration ev = v.elements(); ev.hasMoreElements();) { 134 if (log.isInfoEnabled()) { 135 log.info((String ) ev.nextElement()); 136 } 137 } 138 try { 139 Test1 oneTest = new Test1(); 140 oneTest.setDataContext(TestSystemInitializer.getTestContext()); 141 Test2 oneTest2 = new Test2(); 142 oneTest2.setDataContext(TestSystemInitializer.getTestContext()); 143 try { 144 oneTest.deleteAll(); 145 oneTest2.deleteAll(); 146 } catch (Exception ex) { 147 } 148 oneTest.setField("TestKey", "1"); 149 oneTest.add(); 150 151 oneTest2.setField("TestKey", "1"); 152 oneTest2.setField("Descrip", "Test Record"); 153 oneTest2.add(); 154 } catch (DBException de) { 155 de.printStackTrace(); 156 log.error(de); 157 fail("Unable to clean up test object tables"); 158 } 159 } 160 161 162 166 protected void tearDown() 167 throws Exception { 168 boolean error = false; 169 170 171 172 try { 173 Test1 test1List = new Test1(); 174 test1List.setDataContext(TestSystemInitializer.getTestContext()); 175 176 Test1 oneTest1 = null; 177 for (Iterator e = test1List.searchAndRetrieveList().iterator(); 178 e.hasNext();) { 179 oneTest1 = (Test1) e.next(); 180 oneTest1.delete(); 181 } 182 } catch (DBException de) { 183 log.error("Error cleaning up test case", de); 184 } 185 186 try { 187 Test2 test2List = new Test2(); 188 test2List.setDataContext(TestSystemInitializer.getTestContext()); 189 190 Test2 oneTest2 = null; 191 192 for (Iterator e2 = test2List.searchAndRetrieveList().iterator(); 193 e2.hasNext();) { 194 oneTest2 = (Test2) e2.next(); 195 oneTest2.delete(); 196 } 197 } catch (DBException de) { 198 log.error("Error cleaning up test case", de); 199 } 200 201 assertTrue("Error cleaning up test values", !error); 202 } 203 204 205 208 public void testMulti() { 209 try { 210 MultiDBObject md = new MultiDBObject(); 211 md.setDBName(TestSystemInitializer.getTestContext()); 212 md.addDBObj("com.jcorporate.expresso.core.dbobj.tests.Test1", 213 "test1"); 214 md.addDBObj("com.jcorporate.expresso.core.dbobj.tests.Test2", 215 "test2"); 216 md.setForeignKey("test1", "TestKey", "test2", "TestKey"); 217 218 int recCount = 0; 219 MultiDBObject oneMD = null; 220 221 for (Iterator ee = md.searchAndRetrieveList().iterator(); 222 ee.hasNext();) { 223 recCount++; 224 oneMD = (MultiDBObject) ee.next(); 225 226 227 if (!oneMD.getField("test2", "Descrip").equals("Test Record")) { 228 fail("Got wrong Descrip field (" + 229 oneMD.getField("test2", "Descrip") + 230 ") from retrieve of joined objects."); 231 } 232 } 233 if (recCount == 0) { 234 fail("Did not retrieve the joined records correctly"); 235 } 236 } catch (DBException ce) { 237 ce.printStackTrace(); 238 log.error(ce); 239 fail("DB exception occurred - see log"); 240 } 241 } 242 243 248 public static Test suite() { 249 TestSuite suite = new TestSuite(MultiDBObjectTest.class); 250 251 return suite; 252 } 253 254 } 255 256 | Popular Tags |