Source Code Cross Referenced for TestBase.java in  » Database-JDBC-Connection-Pool » jTDS » net » sourceforge » jtds » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » jTDS » net.sourceforge.jtds.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.