KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > alljdbc > AllJDBCDriversTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 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 JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.sql.Blob JavaDoc;
28 import java.sql.Clob JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.DriverManager JavaDoc;
31 import java.sql.Timestamp JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Properties JavaDoc;
38
39
40 /**
41  * Test validity data conversion between databases
42  * TODO Refactor this class
43  *
44  * @author Fyodor Kupolov
45  * @version 1.0
46  */

47 public class AllJDBCDriversTest extends AbstractTestCase {
48     private static List JavaDoc<Object JavaDoc[]> rows = new ArrayList JavaDoc<Object JavaDoc[]>(); //modified by janino
49
private static final byte[] blob=new byte[100000];
50     private static final String JavaDoc clob;
51     private static final String JavaDoc[] drivers;
52     private static final String JavaDoc[] urls;
53     private static final String JavaDoc[] users;
54     private static final String JavaDoc[] passwords;
55     private List JavaDoc<Connection JavaDoc> connections;
56
57     static {
58         //Initializing blob/clob
59

60         for (int i=0;i<blob.length;i++) {
61             blob[i]= (byte) (0x30+i%10);
62         }
63         clob=new String JavaDoc(blob);
64         //reading test properties containing a set of drivers
65
Properties JavaDoc props = new Properties JavaDoc();
66         InputStream JavaDoc is = AllJDBCDriversTest.class.getResourceAsStream("test.properties");
67         try {
68             props.load(is);
69         } catch (IOException JavaDoc e) {
70             throw new IllegalStateException JavaDoc(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     /**
81      * Callback method to be called by Janino script in xml file.
82      * @param row selected row.
83      */

84     public static void addRow(Object JavaDoc[] row) {
85         rows.add(row);
86         //Now normalize the row
87
for (int i = 0; i < row.length; i++) {
88             if (row[i]==null) {
89                 continue;
90             }
91             try {
92                 if (row[i] instanceof Blob JavaDoc) {
93                     Blob JavaDoc b = (Blob JavaDoc) row[i];
94                     row[i]= IOUtils.toByteArray(b.getBinaryStream());
95                 } else if (row[i] instanceof Clob JavaDoc) {
96                     Clob JavaDoc c = (Clob JavaDoc) row[i];
97                     row[i]= IOUtils.toString(c.getCharacterStream());
98                 }
99
100             } catch (Exception JavaDoc e) {
101                 throw new IllegalStateException JavaDoc(e);
102             }
103         }
104
105
106     }
107
108     private static String JavaDoc[] split(String JavaDoc value) {
109         String JavaDoc[] 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 JavaDoc<String JavaDoc,Object JavaDoc> externalProperties;
118     @Override JavaDoc
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 JavaDoc {
126         int n = drivers.length;
127         //just to make sure properties are valid
128
assertTrue(n == urls.length && n == users.length && n == passwords.length);
129
130         externalProperties = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
131         //test any combination of drivers in both directions
132
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 JavaDoc[] row = rows.get(0);
148                     rows.clear();//clear the callback variable for the next loop
149
//checks for columns
150
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 JavaDoc {
164         connections = new ArrayList JavaDoc<Connection JavaDoc>(drivers.length);
165         //Initialize drivers one by one
166
Properties JavaDoc p = new Properties JavaDoc();
167         externalProperties = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
168         for (int i = 0; i < drivers.length; i++) {
169             String JavaDoc driver = drivers[i];
170             String JavaDoc url = urls[i];
171             String JavaDoc user = users[i];
172             String JavaDoc 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 JavaDoc)p);
182             } catch (IOException JavaDoc e) {
183                 throw new IllegalStateException JavaDoc(e);
184             }
185             //Initialize a driver and obtain a connection to turn off automatic shutdown on last connection close
186
try {
187                 DriverFactory.getDriver(driver, getClass().getClassLoader());
188                 connections.add(DriverManager.getConnection(url, user,password));
189             } catch (Exception JavaDoc e) {
190                 throw new IllegalStateException JavaDoc(e);
191             }
192             newEtlExecutor("schema.xml").execute();
193         }
194     }
195
196     protected void tearDown() throws Exception JavaDoc {
197         for (Connection JavaDoc connection : connections) {
198             JdbcUtils.closeSilent(connection);
199         }
200         rows.clear();
201     }
202
203     private void checkId(Object JavaDoc id) {
204         assertEquals("1", String.valueOf(id));
205     }
206
207     private void checkNum(Object JavaDoc num) {
208         assertEquals("3.14", String.valueOf(num));
209     }
210
211     private void checkStr(Object JavaDoc str) {
212         assertEquals("String", str);
213     }
214
215     private void checkFlag(Object JavaDoc flag) {
216         assertNotNull(flag);
217         if (flag instanceof Boolean JavaDoc) {
218             assertTrue((Boolean JavaDoc)flag);
219         } else {
220             assertEquals("1", String.valueOf(flag));
221         }
222
223     }
224
225     private void checkTi(Object JavaDoc ti) {
226         assertEquals(Timestamp.valueOf("2006-07-21 19:43:00"), ti);
227     }
228
229     public void checkData(Object JavaDoc data) {
230         byte[] exp = new byte[]{1, 1, 1, 1};
231         assertTrue(Arrays.equals(exp, (byte[])data));
232     }
233
234     public void checkBData(Object JavaDoc bdata) {
235         assertTrue(Arrays.equals(blob, (byte[]) bdata));
236     }
237
238     public void checkCData(Object JavaDoc cdata) {
239         assertEquals(clob, cdata);
240     }
241
242
243 }
244
Popular Tags