1 17 package org.apache.tools.ant.taskdefs; 18 19 import java.sql.Driver ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 import java.sql.DriverPropertyInfo ; 23 import java.util.Properties ; 24 import java.io.File ; 25 import java.net.URL ; 26 27 import junit.framework.TestCase; 28 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.BuildException; 31 32 41 public class SQLExecTest extends TestCase { 42 43 public final static int NULL = 0; 45 public final static int ORACLE = 1; 46 public final static int MYSQL = 2; 47 48 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 SQLExec.getLoaderMap().clear(); 63 } 64 65 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 sql = createTask(getProperties(NULL)); 80 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 100 101 105 106 107 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 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 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 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 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 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 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 } 237 | Popular Tags |