1 16 package scriptella.driver.alljdbc; 17 18 import scriptella.AbstractTestCase; 19 import scriptella.configuration.ConfigurationFactory; 20 import scriptella.core.DriverFactory; 21 import scriptella.execution.EtlExecutorException; 22 import scriptella.jdbc.JdbcUtils; 23 import scriptella.util.IOUtils; 24 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.sql.Blob ; 28 import java.sql.Clob ; 29 import java.sql.Connection ; 30 import java.sql.DriverManager ; 31 import java.sql.Timestamp ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.HashMap ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Properties ; 38 39 40 47 public class AllJDBCDriversTest extends AbstractTestCase { 48 private static List <Object []> rows = new ArrayList <Object []>(); private static final byte[] blob=new byte[100000]; 50 private static final String clob; 51 private static final String [] drivers; 52 private static final String [] urls; 53 private static final String [] users; 54 private static final String [] passwords; 55 private List <Connection > connections; 56 57 static { 58 60 for (int i=0;i<blob.length;i++) { 61 blob[i]= (byte) (0x30+i%10); 62 } 63 clob=new String (blob); 64 Properties props = new Properties (); 66 InputStream is = AllJDBCDriversTest.class.getResourceAsStream("test.properties"); 67 try { 68 props.load(is); 69 } catch (IOException e) { 70 throw new IllegalStateException (e); 71 } 72 drivers = split(props.getProperty("drivers")); 73 urls = split(props.getProperty("urls")); 74 users = split(props.getProperty("users")); 75 passwords = split(props.getProperty("passwords")); 76 77 } 78 79 80 84 public static void addRow(Object [] row) { 85 rows.add(row); 86 for (int i = 0; i < row.length; i++) { 88 if (row[i]==null) { 89 continue; 90 } 91 try { 92 if (row[i] instanceof Blob ) { 93 Blob b = (Blob ) row[i]; 94 row[i]= IOUtils.toByteArray(b.getBinaryStream()); 95 } else if (row[i] instanceof Clob ) { 96 Clob c = (Clob ) row[i]; 97 row[i]= IOUtils.toString(c.getCharacterStream()); 98 } 99 100 } catch (Exception e) { 101 throw new IllegalStateException (e); 102 } 103 } 104 105 106 } 107 108 private static String [] split(String value) { 109 String [] strings = (' '+value+' ').split(","); 110 for (int i = 0; i < strings.length; i++) { 111 strings[i]=strings[i].trim(); 112 } 113 return strings; 114 } 115 116 117 private Map <String ,Object > externalProperties; 118 @Override 119 protected ConfigurationFactory newConfigurationFactory() { 120 ConfigurationFactory cf = super.newConfigurationFactory(); 121 cf.setExternalProperties(externalProperties); 122 return cf; 123 } 124 125 public void test() throws EtlExecutorException, ClassNotFoundException { 126 int n = drivers.length; 127 assertTrue(n == urls.length && n == users.length && n == passwords.length); 129 130 externalProperties = new HashMap <String , Object >(); 131 for (int i = 0; i < n; i++) { 133 externalProperties.put("driver1", drivers[i]); 134 externalProperties.put("url1", urls[i]); 135 externalProperties.put("user1", users[i]); 136 externalProperties.put("password1", passwords[i]); 137 externalProperties.put("blob", blob); 138 externalProperties.put("clob", clob); 139 for (int j = 0; j < n; j++) { 140 if (j != i) { 141 externalProperties.put("driver2", drivers[j]); 142 externalProperties.put("url2", urls[j]); 143 externalProperties.put("user2", users[j]); 144 externalProperties.put("password2", passwords[j]); 145 newEtlExecutor().execute(); 146 assertEquals(1, rows.size()); 147 Object [] row = rows.get(0); 148 rows.clear(); checkId(row[0]); 151 checkNum(row[1]); 152 checkStr(row[2]); 153 checkFlag(row[3]); 154 checkTi(row[4]); 155 checkData(row[5]); 156 checkBData(row[6]); 157 checkCData(row[7]); 158 } 159 } 160 } 161 } 162 163 protected void setUp() throws Exception { 164 connections = new ArrayList <Connection >(drivers.length); 165 Properties p = new Properties (); 167 externalProperties = new HashMap <String , Object >(); 168 for (int i = 0; i < drivers.length; i++) { 169 String driver = drivers[i]; 170 String url = urls[i]; 171 String user = users[i]; 172 String password = passwords[i]; 173 externalProperties.clear(); 174 externalProperties.put("driver",driver); 175 externalProperties.put("url",url); 176 externalProperties.put("user",user); 177 externalProperties.put("password",password); 178 try { 179 p.clear(); 180 p.load(getClass().getResourceAsStream(driver+".types.properties")); 181 externalProperties.putAll((Map )p); 182 } catch (IOException e) { 183 throw new IllegalStateException (e); 184 } 185 try { 187 DriverFactory.getDriver(driver, getClass().getClassLoader()); 188 connections.add(DriverManager.getConnection(url, user,password)); 189 } catch (Exception e) { 190 throw new IllegalStateException (e); 191 } 192 newEtlExecutor("schema.xml").execute(); 193 } 194 } 195 196 protected void tearDown() throws Exception { 197 for (Connection connection : connections) { 198 JdbcUtils.closeSilent(connection); 199 } 200 rows.clear(); 201 } 202 203 private void checkId(Object id) { 204 assertEquals("1", String.valueOf(id)); 205 } 206 207 private void checkNum(Object num) { 208 assertEquals("3.14", String.valueOf(num)); 209 } 210 211 private void checkStr(Object str) { 212 assertEquals("String", str); 213 } 214 215 private void checkFlag(Object flag) { 216 assertNotNull(flag); 217 if (flag instanceof Boolean ) { 218 assertTrue((Boolean )flag); 219 } else { 220 assertEquals("1", String.valueOf(flag)); 221 } 222 223 } 224 225 private void checkTi(Object ti) { 226 assertEquals(Timestamp.valueOf("2006-07-21 19:43:00"), ti); 227 } 228 229 public void checkData(Object data) { 230 byte[] exp = new byte[]{1, 1, 1, 1}; 231 assertTrue(Arrays.equals(exp, (byte[])data)); 232 } 233 234 public void checkBData(Object bdata) { 235 assertTrue(Arrays.equals(blob, (byte[]) bdata)); 236 } 237 238 public void checkCData(Object cdata) { 239 assertEquals(clob, cdata); 240 } 241 242 243 } 244 | Popular Tags |