001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: UnivPrepStmt.java,v 1.4 2006/09/29 12:32:09 drmlipp Exp $
021: *
022: * $Log: UnivPrepStmt.java,v $
023: * Revision 1.4 2006/09/29 12:32:09 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.3 2005/04/22 15:11:07 drmlipp
027: * Merged changes from 1.3 branch up to 1.3p15.
028: *
029: * Revision 1.1.1.2.6.2 2005/04/13 16:14:08 drmlipp
030: * Optimized db access.
031: *
032: * Revision 1.2 2005/02/23 15:43:37 drmlipp
033: * Synchronized with 1.3.
034: *
035: * Revision 1.1.1.2.6.1 2005/02/16 16:17:51 drmlipp
036: * Fixed SQL statement.
037: *
038: * Revision 1.1.1.2 2004/08/18 15:18:47 drmlipp
039: * Update to 1.2
040: *
041: * Revision 1.6 2004/01/27 11:45:33 lipp
042: * Preserve newlines when reading process definitions.
043: *
044: * Revision 1.5 2003/06/27 09:44:13 lipp
045: * Fixed copyright/license information.
046: *
047: * Revision 1.4 2003/03/06 11:03:51 huaiyang
048: * clean up the data after the test.
049: *
050: * Revision 1.3 2003/03/05 08:21:53 huaiyang
051: * do not use autocommit.
052: *
053: * Revision 1.2 2003/03/03 15:49:16 huaiyang
054: * isOracle must be static.
055: *
056: * Revision 1.1 2003/02/28 15:29:30 huaiyang
057: * Initial.
058: *
059: *
060: */
061: package util;
062:
063: import java.io.BufferedReader;
064: import java.io.InputStream;
065: import java.io.InputStreamReader;
066:
067: import java.util.HashMap;
068: import java.util.Properties;
069:
070: import java.sql.Connection;
071: import java.sql.DriverManager;
072: import java.sql.ResultSet;
073: import java.sql.SQLException;
074:
075: import de.danet.an.util.JDBCUtil;
076: import de.danet.an.util.UniversalPrepStmt;
077:
078: import junit.framework.Test;
079: import junit.framework.TestCase;
080: import junit.framework.TestSuite;
081:
082: /**
083: * Tests the UniversalPrepStmt
084: * @version 1.0
085: */
086: public class UnivPrepStmt extends TestCase {
087: /**
088: * Properties to use in test cases
089: */
090: private static Properties props = new Properties();
091: /**
092: * Default connection used in test cases
093: */
094: private static Connection con = null;
095: /**
096: * Name of DB product
097: */
098: private static String dbDriver = null;
099:
100: /** flag of oracle database */
101: private static boolean isOracle = false;
102:
103: /**
104: * Konstruktor zum Erzeugen eines TestCase
105: * @param name a <code>String</code> value
106: */
107: public UnivPrepStmt(String name) {
108: super (name);
109: }
110:
111: /**
112: * Assembling the test suite
113: * @return a <code>Test</code> value
114: * @exception Exception if an error occurs
115: */
116: public static Test suite() throws Exception {
117: TestSuite suite = new TestSuite();
118: // test case for oracle
119: suite.addTest(new UnivPrepStmt("setOracleConnection"));
120: suite.addTest(new UnivPrepStmt("insertLobs"));
121: suite.addTest(new UnivPrepStmt("updateLobs"));
122: // test case for sapdb
123: suite.addTest(new UnivPrepStmt("setSAPDBConnection"));
124: suite.addTest(new UnivPrepStmt("insertLobs"));
125: suite.addTest(new UnivPrepStmt("updateLobs"));
126: return suite;
127: }
128:
129: public void insertLobs() throws Exception {
130: String xpdl = importXPDL("test1.xml");
131: Object header = new HashMap();
132: UniversalPrepStmt prepStmt = null;
133: try {
134: // clean up
135: assertTrue(cleanup() == 0);
136: //con.commit();
137: // insert
138: prepStmt = new UniversalPrepStmt(
139: con,
140: "INSERT INTO ProcessDefinition ("
141: + "DBId, PackageId, ProcessId, Xpdl, Header, Enabled) "
142: + "VALUES (?, ?, ?, ?, ?, 'T')");
143: int offset = 1;
144: prepStmt.setLong(offset++, 1000000);
145: prepStmt.setString(offset++, "univPrepStmtPackage");
146: prepStmt.setString(offset++, "univPrepStmtTest");
147: /*
148: prepStmt.setLargeString(offset++, null);
149: prepStmt.setBinary(offset++, null);
150: prepStmt.setLargeString(offset++, xpdl);
151: prepStmt.setBinary(offset++, header);
152: */
153: prepStmt.setLargeString(offset++, null);
154: prepStmt.setBinary(offset++, null);
155: prepStmt.executeUpdate();
156: //con.commit();
157: // select
158: offset = 1;
159: prepStmt = new UniversalPrepStmt(con,
160: "SELECT Header, Xpdl FROM ProcessDefinition "
161: + "WHERE DBId = 1000000");
162: ResultSet rs = prepStmt.executeQuery();
163: rs.next();
164: if (!isOracle) {
165: return;
166: }
167: Object blobObject = JDBCUtil.getBinary(rs, offset++);
168: assertTrue(rs.wasNull());
169: if (rs.wasNull()) {
170: System.out.println("Got null BLOB!");
171: }
172: String clobObject = JDBCUtil.getString(null, rs, offset++);
173: assertTrue(rs.wasNull());
174: if (rs.wasNull()) {
175: System.out.println("Got null CLOB!");
176: }
177: } catch (SQLException sqle) {
178: sqle.printStackTrace();
179: } finally {
180: //JDBCUtil.closeAll (null, prepStmt, con);
181: }
182: }
183:
184: public void updateLobs() throws Exception {
185: String xpdl = importXPDL("test2.xml");
186: Object header = new HashMap();
187: UniversalPrepStmt prepStmt = null;
188: try {
189: prepStmt = new UniversalPrepStmt(
190: con,
191: "UPDATE ProcessDefinition SET "
192: + "PackageId = ?, ProcessId = ?, Xpdl = ?, Header = ? "
193: + "WHERE DBId = ?");
194: int offset = 1;
195: prepStmt.setString(offset++, "updatedUnivPrepStmtPackage");
196: prepStmt.setString(offset++, "updatedunivPrepStmtTest");
197: prepStmt.setLargeString(offset++, xpdl);
198: prepStmt.setBinary(offset++, header);
199: prepStmt.setLong(offset++, 1000000);
200: //prepStmt.setString(offset++, "univPrepStmtPackage");
201: prepStmt.executeUpdate();
202: //con.commit();
203: assertTrue(cleanup() == 1);
204: } catch (SQLException sqle) {
205: sqle.printStackTrace();
206: } finally {
207: JDBCUtil.closeAll(null, prepStmt, con);
208: }
209: }
210:
211: /**
212: * Test setting of connections
213: * @exception Exception if an error occurs
214: */
215: public void setOracleConnection() throws Exception {
216: isOracle = true;
217: setUpConnection("jdbc-oracle.properties");
218: }
219:
220: /**
221: * Test setting of connections
222: * @exception Exception if an error occurs
223: */
224: public void setSAPDBConnection() throws Exception {
225: isOracle = false;
226: setUpConnection("jdbc-sapdb.properties");
227: }
228:
229: private void setUpConnection(String properties) throws Exception {
230: try {
231: props.load(Thread.currentThread().getContextClassLoader()
232: .getResourceAsStream(properties));
233: } catch (NullPointerException exc) {
234: props.load(Thread.currentThread().getContextClassLoader()
235: .getResourceAsStream("jdbc.properties"));
236: }
237: // register oracle database driver
238: Class.forName(props.getProperty("driver"));
239: con = DriverManager.getConnection(
240: props.getProperty("database"), props
241: .getProperty("user"), props
242: .getProperty("password"));
243: System.out.println("Driver: "
244: + con.getMetaData().getDriverName());
245: //con.setAutoCommit(false);
246: }
247:
248: private String importXPDL(String filename) throws Exception {
249: InputStream is = getClass().getResourceAsStream(filename);
250: assertTrue(is != null);
251: BufferedReader br = new BufferedReader(new InputStreamReader(
252: is, "ISO-8859-1"));
253: StringBuffer sb = new StringBuffer();
254: String st;
255: while ((st = br.readLine()) != null) {
256: sb.append(st + "\n");
257: }
258: return sb.toString();
259: }
260:
261: private int cleanup() throws Exception {
262: UniversalPrepStmt deletePrepStmt = new UniversalPrepStmt(con,
263: "DELETE FROM ProcessDefinition WHERE"
264: + " DBId = 1000000");
265: return deletePrepStmt.executeUpdate();
266: }
267:
268: }
|