001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test;
018:
019: import java.sql.Connection;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Statement;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import javax.sql.DataSource;
027:
028: import junit.framework.Assert;
029:
030: import org.apache.log4j.Logger;
031: import org.kuali.rice.database.XAPoolDataSource;
032: import org.kuali.rice.lifecycle.BaseLifecycle;
033: import org.springframework.context.support.ClassPathXmlApplicationContext;
034: import org.springframework.jdbc.core.ConnectionCallback;
035: import org.springframework.jdbc.core.JdbcTemplate;
036: import org.springframework.jdbc.core.StatementCallback;
037: import org.springframework.transaction.PlatformTransactionManager;
038: import org.springframework.transaction.TransactionStatus;
039: import org.springframework.transaction.support.TransactionCallback;
040: import org.springframework.transaction.support.TransactionTemplate;
041:
042: /**
043: * TODO: This class is temporary until I can get workflow upgraded to use the newest rice test case.
044: *
045: */
046: public class OldClearDatabaseLifecycle extends BaseLifecycle {
047:
048: protected static final Logger LOG = Logger
049: .getLogger(OldClearDatabaseLifecycle.class);
050:
051: private static final String SPRING_FILE = "edu/iu/uis/eden/test/TestBootstrapSpring.xml";
052: private static final String DATA_SOURCE = "dataSource";
053: private static final String TRANSACTION_MANAGER = "transactionManager";
054:
055: private List<String> tablesToClear;
056: private List<String> tablesNotToClear;
057:
058: public OldClearDatabaseLifecycle() {
059: }
060:
061: public OldClearDatabaseLifecycle(List<String> tablesToClear,
062: List<String> tablesNotToClear) {
063: this .tablesToClear = tablesToClear;
064: this .tablesNotToClear = tablesNotToClear;
065: }
066:
067: public OldClearDatabaseLifecycle(List<String> tablesToClear) {
068: this .tablesToClear = tablesToClear;
069: }
070:
071: private static final String TEST_TABLE_NAME = "EN_UNITTEST_T";
072:
073: public void start() throws Exception {
074: try {
075: final ClassPathXmlApplicationContext bootstrapContext = new ClassPathXmlApplicationContext(
076: SPRING_FILE);
077: final XAPoolDataSource dataSource = (XAPoolDataSource) bootstrapContext
078: .getBean(DATA_SOURCE);
079: final String schemaName = dataSource.getUser()
080: .toUpperCase();
081: try {
082: clearTables(
083: (PlatformTransactionManager) bootstrapContext
084: .getBean(TRANSACTION_MANAGER),
085: dataSource, schemaName);
086: } finally {
087: bootstrapContext.close();
088: }
089: super .start();
090: } catch (Exception e) {
091: e.printStackTrace();
092: throw e;
093: }
094: }
095:
096: protected void verifyTestEnvironment(final DataSource dataSource) {
097: Assert.assertNotNull("DataSource could not be located.",
098: dataSource);
099:
100: final JdbcTemplate template = new JdbcTemplate(dataSource);
101: template.execute(new ConnectionCallback() {
102: public Object doInConnection(final Connection connection)
103: throws SQLException {
104: final ResultSet resultSet = connection.getMetaData()
105: .getTables(null, null, TEST_TABLE_NAME, null);
106: Assert
107: .assertTrue(
108: "No table named '"
109: + TEST_TABLE_NAME
110: + "' was found in the configured database. "
111: + "You are attempting to run tests against a non-test database!!!",
112: resultSet.next());
113: return null;
114: }
115: });
116: }
117:
118: protected void clearTables(
119: final PlatformTransactionManager transactionManager,
120: final DataSource dataSource, final String schemaName) {
121: LOG.info("Clearing tables for schema " + schemaName);
122: Assert.assertNotNull("DataSource could not be located.",
123: dataSource);
124:
125: if (schemaName == null || schemaName.equals("")) {
126: Assert.fail("Empty schema name given");
127: }
128: new TransactionTemplate(transactionManager)
129: .execute(new TransactionCallback() {
130: public Object doInTransaction(
131: final TransactionStatus status) {
132: verifyTestEnvironment(dataSource);
133: return new JdbcTemplate(dataSource)
134: .execute(new StatementCallback() {
135: public Object doInStatement(
136: Statement statement)
137: throws SQLException {
138: final List<String> reEnableConstraints = new ArrayList<String>();
139: final ResultSet resultSet = statement
140: .getConnection()
141: .getMetaData()
142: .getTables(
143: null,
144: schemaName,
145: null,
146: new String[] { "TABLE" });
147: while (resultSet.next()) {
148: String tableName = resultSet
149: .getString("TABLE_NAME");
150: // TODO this is currently targetting only en and quartz tables, this should probably become parameterizable in the
151: // ClearDatabaseLifecycle or maybe this constraint can be removed?
152: if (tableName
153: .startsWith("EN_")
154: || tableName
155: .startsWith("KR_QRTZ_")) { /*&& !dontClear.contains(tableName)) {*/
156: if (getTablesToClear() != null
157: && !getTablesToClear()
158: .isEmpty()
159: && !getTablesToClear()
160: .contains(
161: tableName)) {
162: continue;
163: }
164: if (getTablesNotToClear() != null
165: && !getTablesNotToClear()
166: .isEmpty()
167: && getTablesNotToClear()
168: .contains(
169: tableName)) {
170: continue;
171: }
172: ResultSet keyResultSet = statement
173: .getConnection()
174: .getMetaData()
175: .getExportedKeys(
176: null,
177: schemaName,
178: tableName);
179: while (keyResultSet
180: .next()) {
181: final String fkName = keyResultSet
182: .getString("FK_NAME");
183: final String fkTableName = keyResultSet
184: .getString("FKTABLE_NAME");
185: final String disableConstraint = "ALTER TABLE "
186: + fkTableName
187: + " DISABLE CONSTRAINT "
188: + fkName;
189: LOG
190: .info("Disabling constraints using statement ->"
191: + disableConstraint
192: + "<-");
193: statement
194: .addBatch(disableConstraint);
195: reEnableConstraints
196: .add("ALTER TABLE "
197: + fkTableName
198: + " ENABLE CONSTRAINT "
199: + fkName);
200: }
201: keyResultSet.close();
202: String deleteStatement = "DELETE FROM "
203: + tableName;
204: LOG
205: .info("Clearing contents using statement ->"
206: + deleteStatement
207: + "<-");
208: statement
209: .addBatch(deleteStatement);
210: }
211: }
212: for (final String constraint : reEnableConstraints) {
213: LOG
214: .info("Enabling constraints using statement ->"
215: + constraint
216: + "<-");
217: statement
218: .addBatch(constraint);
219: }
220: statement.executeBatch();
221: resultSet.close();
222: return null;
223: }
224: });
225: }
226: });
227: LOG
228: .info("Tables successfully cleared for schema "
229: + schemaName);
230: }
231:
232: public List<String> getTablesToClear() {
233: return this .tablesToClear;
234: }
235:
236: public List<String> getTablesNotToClear() {
237: return this.tablesNotToClear;
238: }
239:
240: }
|