001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: /*
051: * Wrapper.java
052: *
053: * Created on April 2, 2002, 11:16 AM
054: */
055:
056: package org.jaffa.persistence.blackboxtests;
057:
058: import junit.extensions.TestSetup;
059: import junit.framework.Test;
060:
061: import java.sql.Connection;
062: import java.sql.DriverManager;
063: import java.sql.SQLException;
064: import java.sql.Statement;
065:
066: import java.sql.PreparedStatement;
067: import java.io.*;
068: import java.util.*;
069: import java.net.URL;
070: import javax.xml.bind.JAXBContext;
071: import javax.xml.bind.Unmarshaller;
072:
073: import org.jaffa.config.Config;
074: import org.jaffa.util.URLHelper;
075: import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Init;
076: import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Database;
077: import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Param;
078: import org.jaffa.persistence.engines.jdbcengine.variants.Variant;
079: import org.jaffa.datatypes.Parser;
080: import org.jaffa.util.XmlHelper;
081:
082: /** This class has the methods for one time setup/cleanup of data, before all the tests executed by the suite utilising this class.
083: *
084: * @author GautamJ
085: */
086: public class Wrapper extends TestSetup {
087:
088: private static StringBuffer buf = new StringBuffer();
089: static {
090: for (int i = 0; i < 100000; i++)
091: buf.append('Z');
092: }
093: static final String CATZ_REMARKS = buf.toString();
094:
095: static final String ENGINE = "engine";
096: static final String URL = "url";
097: static final String DRIVER_CLASS = "driverClass";
098: static final String USER = "user";
099: static final String PASSWORD = "password";
100: static final String MAXIMUM_CONNECTIONS = "maximumConnections";
101:
102: /** The constructor.
103: * @param test The Test class, for which the Wrapper will be utilised.
104: */
105: public Wrapper(Test test) {
106: super (test);
107: }
108:
109: /** This sets up the data before the test fixtures are invoked.
110: */
111: protected void setUp() {
112: Connection connection = null;
113: try {
114: Map info = getDatabaseInfo();
115:
116: connection = getConnection((String) info.get(DRIVER_CLASS),
117: (String) info.get(URL), (String) info.get(USER),
118: (String) info.get(PASSWORD));
119:
120: // cleanup any errorneous data
121: dropTables(connection, (String) info.get(ENGINE));
122:
123: // create brand new tables
124: createTables(connection, (String) info.get(ENGINE));
125:
126: // now add new data
127: addNewData(connection);
128:
129: connection.commit();
130: } catch (Exception e) {
131: e.printStackTrace();
132: fail("Failed to setup initial data: " + e.toString());
133: } finally {
134: if (connection != null) {
135: try {
136: connection.rollback();
137: connection.close();
138: } catch (Exception e) {
139: e.printStackTrace();
140: fail("Failed to setup initial data: "
141: + e.toString());
142: }
143: }
144: }
145: }
146:
147: /** This cleans up the data after all the test fixtures are invoked.
148: */
149: protected void tearDown() {
150: Connection connection = null;
151: try {
152: Map info = getDatabaseInfo();
153: connection = getConnection((String) info.get(DRIVER_CLASS),
154: (String) info.get(URL), (String) info.get(USER),
155: (String) info.get(PASSWORD));
156: dropTables(connection, (String) info.get(ENGINE));
157: } catch (Exception e) {
158: e.printStackTrace();
159: fail("Failed to cleanup initial data: " + e.toString());
160: } finally {
161: if (connection != null) {
162: try {
163: connection.rollback();
164: connection.close();
165: } catch (Exception e) {
166: e.printStackTrace();
167: fail("Failed to cleanup initial data: "
168: + e.toString());
169: }
170: }
171: }
172: }
173:
174: static Map getDatabaseInfo() throws Exception {
175: URL initUrl = URLHelper.newExtendedURL((String) Config
176: .getProperty(Config.PROP_JDBC_ENGINE_INIT));
177:
178: // create a JAXBContext capable of handling classes generated into the package
179: JAXBContext jc = JAXBContext
180: .newInstance("org.jaffa.persistence.engines.jdbcengine.configservice.initdomain");
181:
182: // create an Unmarshaller
183: Unmarshaller u = jc.createUnmarshaller();
184:
185: // enable validation
186: u.setValidating(true);
187:
188: // unmarshal a document into a tree of Java content objects composed of classes from the package.
189: Init init = (Init) u.unmarshal(XmlHelper
190: .stripDoctypeDeclaration(initUrl));
191: Database database = null;
192: for (Iterator i = init.getDatabase().iterator(); i.hasNext();) {
193: database = (Database) i.next();
194: if (database.getName().equals("default"))
195: break;
196: }
197:
198: if (database == null)
199: throw new Exception(
200: "The 'default' database has not been defined in init.xml ");
201:
202: Map info = new HashMap();
203: info.put(ENGINE, database.getEngine());
204: for (Iterator i = database.getConnectionFactory().getParam()
205: .iterator(); i.hasNext();) {
206: Param param = (Param) i.next();
207: if (URL.equals(param.getName()))
208: info.put(URL, param.getValue());
209: else if (DRIVER_CLASS.equals(param.getName()))
210: info.put(DRIVER_CLASS, param.getValue());
211: else if (USER.equals(param.getName()))
212: info.put(USER, param.getValue());
213: else if (PASSWORD.equals(param.getName()))
214: info.put(PASSWORD, param.getValue());
215: else if (MAXIMUM_CONNECTIONS.equals(param.getName()))
216: info.put(MAXIMUM_CONNECTIONS, param.getValue());
217: }
218: return info;
219: }
220:
221: private Connection getConnection(String driverClass, String url,
222: String user, String password)
223: throws ClassNotFoundException, SQLException {
224: Class.forName(driverClass);
225: Connection connection = DriverManager.getConnection(url, user,
226: password);
227: connection.setAutoCommit(false);
228: return connection;
229: }
230:
231: private void addNewData(Connection connection) throws Exception {
232: addToInstrument(connection);
233: addToPart(connection);
234: addToPartRem(connection);
235: addToCondition(connection);
236: addToItem(connection);
237: addToValidFieldValue(connection);
238: }
239:
240: private void addToInstrument(Connection connection)
241: throws SQLException {
242: String sql = "insert into ZZ_JUT_INSTRUMENT(CATEGORY_INSTRUMENT, DESCRIPTION, SUPPORT_EQUIP_B, CALCULATE_MTBF_B) values('Z-TESTCI-01', 'Z-TESTCIDESC-01', '1', 'F')";
243: executeSql(connection, sql);
244: }
245:
246: private void addToPart(Connection connection) throws SQLException {
247: String sql = "insert into ZZ_JUT_PART(PART, NOUN, CATEGORY_INSTRUMENT) values('Z-TESTPART-01', 'Z-TESTNOUN-01', 'Z-TESTCI-01')";
248: executeSql(connection, sql);
249: sql = "insert into ZZ_JUT_PART(PART, NOUN, CATEGORY_INSTRUMENT) values('Z-TESTPART-02', 'Z-TESTNOUN-02', 'Z-TESTCI-01')";
250: executeSql(connection, sql);
251: sql = "insert into ZZ_JUT_PART(PART, NOUN, CATEGORY_INSTRUMENT) values('Z-TESTPART-03', 'Z-TESTNOUN-03', 'Z-TESTCI-01')";
252: executeSql(connection, sql);
253: }
254:
255: private void addToPartRem(Connection connection)
256: throws SQLException {
257: //String sql = "insert into ZZ_JUT_PART_REM VALUES('Z-TESTPART-01', 'SOME REMARKS'";
258: //executeSql(connection, sql);
259:
260: // the following is needed, since jdbc requires that fields having 4000+ characters be passed in via streams
261: String sql = "insert into ZZ_JUT_PART_REM VALUES(?,?)";
262: PreparedStatement pstmnt = connection.prepareStatement(sql);
263: pstmnt.setString(1, "Z-TESTPART-01");
264: pstmnt.setCharacterStream(2, new BufferedReader(
265: new StringReader(CATZ_REMARKS)), CATZ_REMARKS.length());
266: pstmnt.execute();
267: pstmnt.close();
268: }
269:
270: private void addToCondition(Connection connection)
271: throws SQLException {
272: String sql = "insert into ZZ_JUT_CONDITION(CONDITION, DESCRIPTION) values('Z-TESTSYCD-01', 'Z-TESTSYCDDESC-01')";
273: executeSql(connection, sql);
274: sql = "insert into ZZ_JUT_CONDITION(CONDITION, DESCRIPTION) values('Z-TESTSYCD-02', 'Z-TESTSYCDDESC-02')";
275: executeSql(connection, sql);
276: sql = "insert into ZZ_JUT_CONDITION(CONDITION, DESCRIPTION) values('Z-TESTSYCD-03', 'Z-TESTSYCDDESC-03')";
277: executeSql(connection, sql);
278: }
279:
280: private void addToItem(Connection connection) throws Exception {
281: String date1, date2, date3;
282: Map info = getDatabaseInfo();
283: String engineType = (String) info.get(ENGINE);
284: if (Parser.parseBoolean(
285: Variant.getProperty(engineType,
286: Variant.PROP_USE_TO_DATE_SQL_FUNCTION))
287: .booleanValue()) {
288: date1 = "to_date('2003-09-10 20:30:40', 'yyyy-MM-dd hh24:mi:ss')";
289: date2 = "to_date('2003-09-10', 'yyyy-MM-dd')";
290: date3 = "null";
291: } else {
292: date1 = "'2003-09-10 20:30:40'";
293: date2 = "'2003-09-10'";
294: date3 = "null";
295: }
296: String sql = "insert into ZZ_JUT_ITEM(ITEM_ID, RECEIVED_ITEM_ID, SC, PART, PRIME, CONDITION, STATUS_1, STATUS_2, STATUS_3, QTY, KEY_REF, CREATED_DATETIME) VALUES('Z-TESTITEM-01', 'Z-TESTITEM-01', 'SOME SC', 'Z-TESTPART-01', 'Z-TESTPRIME-01', 'Z-TESTSYCD-01', 'X', 'S', 'A', 2, 'Z-TEST-KEY-REF', "
297: + date1 + ")";
298: executeSql(connection, sql);
299: sql = "insert into ZZ_JUT_ITEM(ITEM_ID, RECEIVED_ITEM_ID, SC, PART, PRIME, CONDITION, STATUS_1, STATUS_2, STATUS_3, QTY, KEY_REF, CREATED_DATETIME) VALUES('Z-TESTITEM-02', 'Z-TESTITEM-02', 'SOME SC', 'Z-TESTPART-01', 'Z-TESTPRIME-01', 'Z-TESTSYCD-01', 'X', 'S', 'A', 2, 'Z-TEST-KEY-REF', "
300: + date2 + ")";
301: executeSql(connection, sql);
302: sql = "insert into ZZ_JUT_ITEM(ITEM_ID, RECEIVED_ITEM_ID, SC, PART, PRIME, CONDITION, STATUS_1, STATUS_2, STATUS_3, QTY, KEY_REF, CREATED_DATETIME) VALUES('Z-TESTITEM-03', 'Z-TESTITEM-03', 'SOME SC', 'Z-TESTPART-01', 'Z-TESTPRIME-01', 'Z-TESTSYCD-01', 'X', 'S', 'A', 2, 'Z-TEST-KEY-REF', "
303: + date3 + ")";
304: executeSql(connection, sql);
305: }
306:
307: private void addToValidFieldValue(Connection connection)
308: throws SQLException {
309: String sql = null;
310:
311: // Valid values for the field STATUS fields of ZZ_JUT_ITEM
312: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', 'X', '', '')";
313: executeSql(connection, sql);
314: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', 'S', '', '')";
315: executeSql(connection, sql);
316: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', 'A', '', '')";
317: executeSql(connection, sql);
318: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', 'Y', '', '')";
319: executeSql(connection, sql);
320: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', 'T', '', '')";
321: executeSql(connection, sql);
322: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', 'B', '', '')";
323: executeSql(connection, sql);
324: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', '1', '', '')";
325: executeSql(connection, sql);
326: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'STATUS', '2', '', '')";
327: executeSql(connection, sql);
328:
329: // Valid values for the field KEY_REF of ZZ_JUT_ITEM
330: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'KEY_REF', 'Z-TEST-KEY-REF', '', '')";
331: executeSql(connection, sql);
332: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'KEY_REF', 'Z-TEST-KEY-REF-2', '', '')";
333: executeSql(connection, sql);
334: sql = "insert into ZZ_JUT_VALID_FIELD_VALUE(TABLE_NAME, FIELD_NAME, LEGAL_VALUE, DESCRIPTION, REMARKS) VALUES('ZZ_JUT_ITEM', 'KEY_REF', 'Z-TEST-KEY-REF-3', '', '')";
335: executeSql(connection, sql);
336: }
337:
338: private void executeSql(Connection connection, String sql)
339: throws SQLException {
340: Statement statement = connection.createStatement();
341: statement.execute(sql);
342: statement.close();
343: }
344:
345: private void createTables(Connection connection, String engine)
346: throws IOException, SQLException {
347: executeSql(connection, loadCreateTableScript(
348: "ZZ_JUT_INSTRUMENT", engine));
349: executeSql(connection, loadCreateTableScript(
350: "ZZ_JUT_CONDITION", engine));
351: executeSql(connection, loadCreateTableScript("ZZ_JUT_PART",
352: engine));
353: executeSql(connection, loadCreateTableScript("ZZ_JUT_PART_REM",
354: engine));
355: executeSql(connection, loadCreateTableScript("ZZ_JUT_PART_PIC",
356: engine));
357: executeSql(connection, loadCreateTableScript(
358: "ZZ_JUT_PART_REM_PIC", engine));
359: executeSql(connection, loadCreateTableScript("ZZ_JUT_ITEM",
360: engine));
361: executeSql(connection, loadCreateTableScript(
362: "ZZ_JUT_VALID_FIELD_VALUE", engine));
363: executeSql(connection, loadCreateTableScript("ZZ_JUT_ASSET",
364: engine));
365:
366: // create the stored-procedure and its body
367: executeSql(connection, loadCreateTableScript("ZZ_JUT_VOUCHER",
368: engine));
369: executeSql(connection, loadCreateTableScript(
370: "ZZ_JUT_VOUCHER_BODY", engine));
371:
372: if ("oracle".equals(engine)) {
373: // Special set up for auto key generation
374: executeSql(connection,
375: "CREATE SEQUENCE asset_tk_sequence START WITH 1");
376: executeSql(
377: connection,
378: "CREATE OR REPLACE TRIGGER zz_jut_asset_asset_tk BEFORE INSERT ON zz_jut_asset FOR EACH ROW BEGIN select asset_tk_sequence.NEXTVAL into :new.asset_tk from dual; END;");
379: }
380:
381: }
382:
383: private void dropTables(Connection connection, String engine) {
384:
385: if ("oracle".equals(engine)) {
386: // Special teardown for auto key generation
387: try {
388: executeSql(connection,
389: "DROP SEQUENCE asset_tk_sequence");
390: } catch (Exception e) {
391: }
392: }
393:
394: try {
395: executeSql(connection, loadDropTableScript(
396: "ZZ_JUT_VOUCHER", engine));
397: } catch (Exception e) {
398: }
399:
400: try {
401: executeSql(connection, loadDropTableScript("ZZ_JUT_ASSET",
402: engine));
403: } catch (Exception e) {
404: }
405:
406: try {
407: executeSql(connection, loadDropTableScript(
408: "ZZ_JUT_VALID_FIELD_VALUE", engine));
409: } catch (Exception e) {
410: }
411:
412: try {
413: executeSql(connection, loadDropTableScript("ZZ_JUT_ITEM",
414: engine));
415: } catch (Exception e) {
416: }
417:
418: try {
419: executeSql(connection, loadDropTableScript(
420: "ZZ_JUT_PART_REM_PIC", engine));
421: } catch (Exception e) {
422: }
423:
424: try {
425: executeSql(connection, loadDropTableScript(
426: "ZZ_JUT_PART_PIC", engine));
427: } catch (Exception e) {
428: }
429:
430: try {
431: executeSql(connection, loadDropTableScript(
432: "ZZ_JUT_PART_REM", engine));
433: } catch (Exception e) {
434: }
435:
436: try {
437: executeSql(connection, loadDropTableScript("ZZ_JUT_PART",
438: engine));
439: } catch (Exception e) {
440: }
441:
442: try {
443: executeSql(connection, loadDropTableScript(
444: "ZZ_JUT_CONDITION", engine));
445: } catch (Exception e) {
446: }
447:
448: try {
449: executeSql(connection, loadDropTableScript(
450: "ZZ_JUT_INSTRUMENT", engine));
451: } catch (Exception e) {
452: }
453: }
454:
455: private String loadCreateTableScript(String tableName, String engine)
456: throws IOException {
457: return loadFile("resources/create-table-scripts/" + engine
458: + '/' + tableName + ".sql");
459: }
460:
461: private String loadDropTableScript(String tableName, String engine)
462: throws IOException {
463: return loadFile("resources/drop-table-scripts/" + engine + '/'
464: + tableName + ".sql");
465: }
466:
467: private String loadFile(String resourceName) throws IOException {
468: StringBuffer buf = new StringBuffer();
469: BufferedReader reader = new BufferedReader(
470: new InputStreamReader(URLHelper
471: .getInputStream(resourceName)));
472: String line = null;
473: while ((line = reader.readLine()) != null) {
474: buf.append(line);
475: buf.append('\n');
476: }
477: return buf.toString();
478: }
479: }
|