001: /*
002: * TestBase.java
003: *
004: * Created on 8. September 2001, 13:34
005: */
006: package net.sourceforge.jtds.test;
007:
008: import java.io.*;
009: import java.sql.*;
010:
011: import java.util.*;
012: import junit.framework.TestCase;
013:
014: /**
015: * @author builder
016: * @version $Id: TestBase.java,v 1.21 2005/07/27 11:02:57 alin_sinpalean Exp $
017: */
018: public abstract class TestBase extends TestCase {
019:
020: private static final String CONNECTION_PROPERTIES = "conf/connection.properties";
021: public static final Properties props = loadProperties(CONNECTION_PROPERTIES);
022: Connection con;
023:
024: public TestBase(String name) {
025: super (name);
026: }
027:
028: public void setUp() throws Exception {
029: super .setUp();
030: connect();
031: }
032:
033: public void tearDown() throws Exception {
034: disconnect();
035: super .tearDown();
036: }
037:
038: public Connection getConnection() throws Exception {
039: Class.forName(props.getProperty("driver"));
040: String url = props.getProperty("url");
041: return DriverManager.getConnection(url, props);
042: }
043:
044: public Connection getConnection(Properties override)
045: throws Exception {
046: Properties newProps = new Properties(props);
047: for (Iterator it = override.keySet().iterator(); it.hasNext();) {
048: String key = (String) it.next();
049: newProps.setProperty(key, override.getProperty(key));
050: }
051:
052: Class.forName(newProps.getProperty("driver"));
053: String url = newProps.getProperty("url");
054: return DriverManager.getConnection(url, newProps);
055: }
056:
057: private void disconnect() throws Exception {
058: if (con != null) {
059: con.close();
060: con = null;
061: }
062: }
063:
064: protected void connect() throws Exception {
065: disconnect();
066: con = getConnection();
067: }
068:
069: public void dump(ResultSet rs) throws SQLException {
070: ResultSetMetaData rsm = rs.getMetaData();
071: int cols = rsm.getColumnCount();
072:
073: for (int i = 1; i <= cols; i++) {
074: if (i > 1) {
075: System.out.print(", ");
076: }
077:
078: System.out.print(rsm.getColumnName(i));
079: }
080:
081: System.out.println();
082:
083: while (rs.next()) {
084: dumpRow(rs);
085: }
086: }
087:
088: public void dumpRow(ResultSet rs) throws SQLException {
089: ResultSetMetaData rsm = rs.getMetaData();
090: int cols = rsm.getColumnCount();
091:
092: for (int i = 1; i <= cols; i++) {
093: if (i > 1) {
094: System.out.print(", ");
095: }
096:
097: System.out.print(rs.getObject(i));
098: }
099:
100: System.out.println();
101: }
102:
103: private static Properties loadProperties(String fileName) {
104:
105: File propFile = new File(fileName);
106:
107: if (!propFile.exists()) {
108: fail("Connection properties not found (" + propFile + ").");
109: }
110:
111: try {
112: Properties props = new Properties();
113: props.load(new FileInputStream(propFile));
114: return props;
115: } catch (IOException e) {
116: throw new RuntimeException(e.getMessage());
117: }
118: }
119:
120: protected void makeTestTables(Statement stmt) throws SQLException {
121: String sql = "CREATE TABLE #test (" + " f_int INT,"
122: + " f_varchar VARCHAR(255))";
123:
124: stmt.execute(sql);
125: }
126:
127: public void makeObjects(Statement stmt, int count)
128: throws SQLException {
129: stmt.execute("TRUNCATE TABLE #test");
130:
131: for (int i = 0; i < count; i++) {
132: String sql = "INSERT INTO #test(f_int, f_varchar)"
133: + " VALUES (" + i + ", 'Row " + i + "')";
134: stmt.execute(sql);
135: }
136: }
137:
138: public void compareInputStreams(InputStream is1, InputStream is2)
139: throws IOException {
140: try {
141: if (is1 == null && is2 == null) {
142: return;
143: } else if (is1 == null) {
144: fail("is1 == null && is2 != null");
145: return;
146: } else if (is2 == null) {
147: fail("is1 != null && is2 == null");
148: return;
149: }
150:
151: long count = 0;
152: int res1 = 0, res2 = 0;
153: byte buf1[] = new byte[1024], buf2[] = new byte[1024];
154:
155: while (res1 != 0 || (res1 = is1.read(buf1)) != -1) {
156: if (res2 == 0) {
157: res2 = is2.read(buf2);
158: }
159:
160: if (res2 == -1) {
161: fail("stream 2 EOF at: " + count);
162: }
163:
164: int min = Math.min(res1, res2);
165: for (int i = 0; i < min; i++) {
166: // Do the check first rather than using assertTrue()
167: // assertTrue() would create a String at each iteration
168: if (buf1[i] != buf2[i]) {
169: fail("stream 1 value [" + buf1[i]
170: + "] differs from stream 2 value ["
171: + buf2[i] + "] at: " + (count + i));
172: }
173: }
174:
175: count += min;
176:
177: if (res1 != min) {
178: System.arraycopy(buf1, min, buf1, 0, res1 - min);
179: res1 -= min;
180: } else {
181: res1 = 0;
182: }
183:
184: if (res2 != min) {
185: System.arraycopy(buf2, min, buf2, 0, res2 - min);
186: res2 -= min;
187: } else {
188: res2 = 0;
189: }
190: }
191:
192: if (is2.read() != -1) {
193: fail("stream 1 EOF at: " + count);
194: }
195: } finally {
196: if (is1 != null) {
197: is1.close();
198: }
199:
200: if (is2 != null) {
201: is2.close();
202: }
203: }
204: }
205:
206: public void compareReaders(Reader r1, Reader r2) throws IOException {
207: try {
208: if (r1 == null && r2 == null) {
209: return;
210: } else if (r1 == null) {
211: fail("r1 == null && r2 != null");
212: return;
213: } else if (r2 == null) {
214: fail("r1 != null && r2 == null");
215: return;
216: }
217:
218: long count = 0;
219: int res1 = 0, res2 = 0;
220: char buf1[] = new char[1024], buf2[] = new char[1024];
221:
222: while (res1 != 0 || (res1 = r1.read(buf1)) != -1) {
223: if (res2 == 0) {
224: res2 = r2.read(buf2);
225: }
226:
227: if (res2 == -1) {
228: fail("reader 2 EOF at: " + count);
229: }
230:
231: int min = Math.min(res1, res2);
232: for (int i = 0; i < min; i++) {
233: // Do the check first rather than using assertTrue()
234: // assertTrue() would create a String at each iteration
235: if (buf1[i] != buf2[i]) {
236: fail("stream 1 value [" + buf1[i]
237: + "] differs from stream 2 value ["
238: + buf2[i] + "] at: " + (count + i));
239: }
240: }
241:
242: count += min;
243:
244: if (res1 != min) {
245: System.arraycopy(buf1, min, buf1, 0, res1 - min);
246: res1 -= min;
247: } else {
248: res1 = 0;
249: }
250:
251: if (res2 != min) {
252: System.arraycopy(buf2, min, buf2, 0, res2 - min);
253: res2 -= min;
254: } else {
255: res2 = 0;
256: }
257: }
258:
259: if (r2.read() != -1) {
260: fail("reader 1 EOF at: " + count);
261: }
262: } finally {
263: if (r1 != null) {
264: r1.close();
265: }
266:
267: if (r2 != null) {
268: r2.close();
269: }
270: }
271: }
272: }
|