KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > SQLExecTest


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  */

17 package org.apache.tools.ant.taskdefs;
18
19 import java.sql.Driver JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.DriverPropertyInfo JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.tools.ant.Project;
30 import 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  */

41 public 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 JavaDoc DRIVER = "driver";
50     public final static String JavaDoc USER = "user";
51     public final static String JavaDoc PASSWORD = "password";
52     public final static String JavaDoc URL = "url";
53     public final static String JavaDoc PATH = "path";
54     public final static String JavaDoc SQL = "sql";
55
56     public SQLExecTest(String JavaDoc s) {
57         super(s);
58     }
59
60     protected void setUp() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc findResourcePath(String JavaDoc resource){
150         resource = resource.replace('.', '/') + ".class";
151         URL JavaDoc url = getClass().getClassLoader().getResource(resource);
152         if (url == null) {
153             return null;
154         }
155         String JavaDoc 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 JavaDoc getProperties(int database){
173         Properties JavaDoc 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 JavaDoc 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 JavaDoc getProperties(String JavaDoc driver, String JavaDoc user, String JavaDoc pwd, String JavaDoc url){
194         Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc NULL_DRIVER = NullDriver.class.getName();
207
208     public static class NullDriver implements Driver JavaDoc {
209         public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info)
210                 throws SQLException JavaDoc {
211             return null;
212         }
213
214         public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
215             return false;
216         }
217
218         public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info)
219                 throws SQLException JavaDoc {
220             return new DriverPropertyInfo JavaDoc[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 }
237
Popular Tags