source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/testcases/org/apache/tools/ant/taskdefs/SQLExecTest.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 8.0 KB
Line 
1/*
2 * Copyright 2001-2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs;
18
19import java.sql.Driver;
20import java.sql.Connection;
21import java.sql.SQLException;
22import java.sql.DriverPropertyInfo;
23import java.util.Properties;
24import java.io.File;
25import java.net.URL;
26
27import junit.framework.TestCase;
28
29import org.apache.tools.ant.Project;
30import org.apache.tools.ant.BuildException;
31
32/**
33 * Simple testcase to test for driver caching.
34 * To test for your own database, you may need to tweak getProperties(int)
35 * and add a couple of keys. see testOracle and testMySQL for an example.
36 *
37 * It would be much better to extend this testcase by using HSQL
38 * as the test db, so that a db is really used.
39 *
40 */
41public class SQLExecTest extends TestCase {
42
43 // some database keys, see #getProperties(int)
44 public final static int NULL = 0;
45 public final static int ORACLE = 1;
46 public final static int MYSQL = 2;
47
48 // keys used in properties.
49 public final static String DRIVER = "driver";
50 public final static String USER = "user";
51 public final static String PASSWORD = "password";
52 public final static String URL = "url";
53 public final static String PATH = "path";
54 public final static String SQL = "sql";
55
56 public SQLExecTest(String s) {
57 super(s);
58 }
59
60 protected void setUp() throws Exception {
61 // make sure the cache is cleared.
62 SQLExec.getLoaderMap().clear();
63 }
64
65 // simple test to ensure that the caching does work...
66 public void testDriverCaching(){
67 SQLExec sql = createTask(getProperties(NULL));
68 assertTrue(!SQLExec.getLoaderMap().containsKey(NULL_DRIVER));
69 try {
70 sql.execute();
71 } catch (BuildException e){
72 assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1);
73 }
74 assertTrue(SQLExec.getLoaderMap().containsKey(NULL_DRIVER));
75 assertSame(sql.getLoader(), SQLExec.getLoaderMap().get(NULL_DRIVER));
76 ClassLoader loader1 = sql.getLoader();
77
78 // 2nd run..
79 sql = createTask(getProperties(NULL));
80 // the driver must still be cached.
81 assertTrue(sql.getLoaderMap().containsKey(NULL_DRIVER));
82 try {
83 sql.execute();
84 } catch (BuildException e){
85 assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1);
86 }
87 assertTrue(sql.getLoaderMap().containsKey(NULL_DRIVER));
88 assertSame(sql.getLoader(), sql.getLoaderMap().get(NULL_DRIVER));
89 assertSame(loader1, sql.getLoader());
90 }
91
92 public void testNull() throws Exception {
93 doMultipleCalls(1000, NULL, true, true);
94 }
95
96 /*
97 public void testOracle(){
98 doMultipleCalls(1000, ORACLE, true, false);
99 }*/
100
101 /*
102 public void testMySQL(){
103 doMultipleCalls(1000, MYSQL, true, false);
104 }*/
105
106
107 /**
108 * run a sql tasks multiple times.
109 * @param calls number of times to execute the task
110 * @param database the database to execute on.
111 * @param caching should caching be enabled ?
112 * @param catchexception true to catch exception for each call, false if not.
113 */
114 protected void doMultipleCalls(int calls, int database, boolean caching, boolean catchexception){
115 Properties props = getProperties(database);
116 for (int i = 0; i < calls; i++){
117 SQLExec sql = createTask(props);
118 sql.setCaching(caching);
119 try {
120 sql.execute();
121 } catch (BuildException e){
122 if (!catchexception){
123 throw e;
124 }
125 }
126 }
127 }
128
129 /**
130 * Create a task from a set of properties
131 * @see #getProperties(int)
132 */
133 protected SQLExec createTask(Properties props){
134 SQLExec sql = new SQLExec();
135 sql.setProject( new Project() );
136 sql.setDriver( props.getProperty(DRIVER) );
137 sql.setUserid( props.getProperty(USER) );
138 sql.setPassword( props.getProperty(PASSWORD) );
139 sql.setUrl( props.getProperty(URL) );
140 sql.createClasspath().setLocation( new File(props.getProperty(PATH)) );
141 sql.addText( props.getProperty(SQL) );
142 return sql;
143 }
144
145 /**
146 * try to find the path from a resource (jar file or directory name)
147 * so that it can be used as a classpath to load the resource.
148 */
149 protected String findResourcePath(String resource){
150 resource = resource.replace('.', '/') + ".class";
151 URL url = getClass().getClassLoader().getResource(resource);
152 if (url == null) {
153 return null;
154 }
155 String u = url.toString();
156 if (u.startsWith("jar:file:")) {
157 int pling = u.indexOf("!");
158 return u.substring("jar:file:".length(), pling);
159 } else if (u.startsWith("file:")) {
160 int tail = u.indexOf(resource);
161 return u.substring("file:".length(), tail);
162 }
163 return null;
164 }
165
166 /**
167 * returns a configuration associated to a specific database.
168 * If you want to test on your specific base, you'd better
169 * tweak this to make it run or add your own database.
170 * The driver lib should be dropped into the system classloader.
171 */
172 protected Properties getProperties(int database){
173 Properties props = null;
174 switch (database){
175 case ORACLE:
176 props = getProperties("oracle.jdbc.driver.OracleDriver", "test", "test", "jdbc:oracle:thin:@127.0.0.1:1521:orcl");
177 break;
178 case MYSQL:
179 props = getProperties("org.gjt.mm.mysql.Driver", "test", "test", "jdbc:mysql://127.0.0.1:3306/test");
180 break;
181 case NULL:
182 default:
183 props = getProperties(NULL_DRIVER, "test", "test", "jdbc:database://hostname:port/name");
184 }
185 // look for the driver path...
186 String path = findResourcePath(props.getProperty(DRIVER));
187 props.put(PATH, path);
188 props.put(SQL, "create table OOME_TEST(X INTEGER NOT NULL);\ndrop table if exists OOME_TEST;");
189 return props;
190 }
191
192 /** helper method to build properties */
193 protected Properties getProperties(String driver, String user, String pwd, String url){
194 Properties props = new Properties();
195 props.put(DRIVER, driver);
196 props.put(USER, user);
197 props.put(PASSWORD, pwd);
198 props.put(URL, url);
199 return props;
200 }
201
202
203//--- NULL JDBC driver just for simple test since there are no db driver
204// available as a default in Ant :)
205
206 public final static String NULL_DRIVER = NullDriver.class.getName();
207
208 public static class NullDriver implements Driver {
209 public Connection connect(String url, Properties info)
210 throws SQLException {
211 return null;
212 }
213
214 public boolean acceptsURL(String url) throws SQLException {
215 return false;
216 }
217
218 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
219 throws SQLException {
220 return new DriverPropertyInfo[0];
221 }
222
223 public int getMajorVersion() {
224 return 0;
225 }
226
227 public int getMinorVersion() {
228 return 0;
229 }
230
231 public boolean jdbcCompliant() {
232 return false;
233 }
234 }
235
236}
Note: See TracBrowser for help on using the repository browser.