001: /*
002: * User: Michael Rettig
003: * Date: Aug 10, 2002
004: * Time: 3:02:04 PM
005: */
006: package net.sourceforge.jaxor.example.tests;
007:
008: import net.sourceforge.jaxor.JaxorSession;
009: import net.sourceforge.jaxor.MetaRow;
010: import net.sourceforge.jaxor.api.JaxorContext;
011: import net.sourceforge.jaxor.api.JaxorTransaction;
012: import net.sourceforge.jaxor.example.db.*;
013:
014: import java.sql.Connection;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: /**
019: * This is base test cases for testing against an in-memory hypersonic database.
020: * The test case creates and drops the tables in setup/teardown.
021: * Also a JaxorContext is created and registered to JaxorSession for convenience.
022: */
023: public abstract class MultiTableTestCase extends JaxorTestCase {
024:
025: public static JaxorContextTestingFactory JAXOR_Factory;
026:
027: public MultiTableTestCase() {
028: this (new HypersonicContextTestingFactory());
029: }
030:
031: public MultiTableTestCase(JaxorContextTestingFactory context) {
032: JAXOR_Factory = context;
033: }
034:
035: public JaxorContextTestingFactory getTestingContext() {
036: return JAXOR_Factory;
037: }
038:
039: protected abstract List getRows();
040:
041: /**
042: * Commits the context and begins a new session
043: */
044: protected void commit() {
045: JaxorSession.commit();
046: beginSession();
047: }
048:
049: private void beginSession() {
050: createSession(getNewContext());
051: }
052:
053: /**
054: * The version of sqlserver used for integration testing does not support
055: * certain transaction isolation levels. i.e. insert and delete in of the same
056: * row in the same transactions.
057: * @return
058: */
059: public boolean isSqlServerTest() {
060: return JAXOR_Factory.getClass() == SqlServerContextTestingFactory.class;
061: }
062:
063: public boolean isMckoiTest() {
064: return JAXOR_Factory.getClass().equals(
065: MckoiContextTestingFactory.class);
066: }
067:
068: public JaxorContext getNewContext() {
069: JaxorContext context = JAXOR_Factory.create();
070: context.setUser(getClass().getName());
071: return context;
072: }
073:
074: private void createSession(JaxorContext conn) {
075: JaxorSession.setJaxorContext(conn);
076: }
077:
078: protected final void setUp() throws Exception {
079: beginSession();
080: try {
081: List allRows = getRows();
082: Connection connection = JaxorSession.getJaxorContext()
083: .getConnection();
084: for (Iterator iterator = allRows.iterator(); iterator
085: .hasNext();) {
086: MetaRow entityRow = (MetaRow) iterator.next();
087: try {
088: TableMaker.dropTable(entityRow.getTableName(),
089: connection);
090: } catch (Exception e) {
091: //e.printStackTrace();
092: }
093: JaxorSession.getJaxorContext().commit();
094: }
095: for (Iterator iterator = allRows.iterator(); iterator
096: .hasNext();) {
097: MetaRow metaRow = (MetaRow) iterator.next();
098: TableMaker.createTable(metaRow, connection,
099: JAXOR_Factory);
100: }
101: JaxorSession.getJaxorContext().commit();
102: } catch (Exception exc) {
103: JaxorSession.getJaxorContext().getTransaction().rollback(
104: exc);
105: JaxorSession.getJaxorContext().end();
106: throw exc;
107: }
108: prep();
109: }
110:
111: public JaxorContext getJaxor() {
112: return JaxorSession.getJaxorContext();
113: }
114:
115: protected void prep() throws Exception {
116: }
117:
118: protected Connection getConnection() {
119: return getTransaction().getConnection();
120: }
121:
122: protected JaxorTransaction getTransaction() {
123: return JaxorSession.getJaxorContext().getTransaction();
124: }
125:
126: protected final void tearDown() throws Exception {
127: try {
128: cleanUp();
129: } finally {
130: JaxorSession.end();
131: }
132: JAXOR_Factory.cleanupTest();
133: }
134:
135: protected void cleanUp() throws Exception {
136:
137: }
138: }
|