KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > util > DriverConformTest


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23 package org.objectweb.jorm.mapper.rdb.util;
24
25 import java.io.PrintStream JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.Date JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.sql.Types JavaDoc;
35
36 /**
37  * This tools clas permits to check the conformance of a jdbc driver to the
38  * standard. The -interac option is an interactive mode that permits to specify
39  * the SQL type, the java type and optionaly the jdbc accessors.
40  *
41  * @author S.Chassande-Barrioz
42  */

43 public class DriverConformTest {
44
45     /**
46      * Main method to launch the tests manually.
47      */

48     public static void main(String JavaDoc[] args) {
49         boolean interac = false;
50         boolean debug = false;
51         out = System.out;
52         String JavaDoc sql = null;
53         String JavaDoc javaType = null;
54         String JavaDoc value = null;
55         for (int i = 0; i < args.length; i++) {
56             if (args[i].equals("-driver") && (i + 1) < args.length)
57                 drvCN = args[++i];
58             else if (args[i].equals("-url") && (i + 1) < args.length)
59                 url = args[++i];
60             else if (args[i].equals("-user") && (i + 1) < args.length)
61                 user = args[++i];
62             else if (args[i].equals("-passwd") && (i + 1) < args.length)
63                 passwd = args[++i];
64             else if (args[i].equals("-sql") && (i + 1) < args.length)
65                 sql = args[++i];
66             else if (args[i].equals("-java") && (i + 1) < args.length)
67                 javaType = args[++i];
68             else if (args[i].equals("-value") && (i + 1) < args.length)
69                 value = args[++i];
70             else if (args[i].equals("-debug"))
71                 debug = true;
72             else {
73                 System.out.println("Unknown argument: " + args[i]);
74                 usage();
75                 System.exit(1);
76             }
77
78         }
79         DriverConformTest d = new DriverConformTest(debug);
80         if (interac) {
81             String JavaDoc getter = null;
82             String JavaDoc setter = null;
83             try {
84                 sql = readString("Enter a SQL TYPE: ");
85                 javaType = readString("Enter the java type (ex: java.lang.Integer): ");
86                 value = readString("Enter a value(ex: 0): ");
87                 String JavaDoc answer = readString("Do you wan to use standard sql accessors [Y|n]:");
88                 getter = null;
89                 setter = null;
90                 if ("n".equalsIgnoreCase(answer.trim())) {
91                     getter = readString("Enter the sql getter (ex: getBoolean): ");
92                     setter = readString("Enter the sql setter (ex: setBoolean): ");
93                 }
94             } catch (Exception JavaDoc e) {
95                 e.printStackTrace();
96             }
97             d.specif(sql, javaType, value, getter, setter);
98         } else if (sql != null && javaType != null && value != null) {
99             d.specif(sql, javaType, value, null, null);
100         } else {
101             d.standard();
102         }
103         d.close();
104     }
105
106     private static void usage() {
107         System.out.println("\nUsage: DriverConformTest"
108                            + " -driver <driver class name> "
109                            + " -url <database url> "
110                            + "\n [-user <user name> -passwd <password of the user>]"
111                            + " -debug"
112                            + "\n -sql <SQL type> "
113                            + " -java <java type> "
114                            + " -value <a value> "
115         );
116         System.out.println("* debug: The debug option prints all test results.");
117         //System.out.println("* interac: This mode permits to test a non standard SQL type. This mode is interactive.");
118
}
119
120     private static String JavaDoc drvCN;
121     private static String JavaDoc user;
122     private static String JavaDoc url;
123     private static String JavaDoc passwd;
124     private static PrintStream JavaDoc out;
125
126     private Connection JavaDoc conn = null;
127     private boolean debug;
128
129     public DriverConformTest(boolean debug) {
130         this.debug = debug;
131         if (drvCN == null || drvCN.length() == 0) {
132             System.out.println("No driver specified !");
133             usage();
134             System.exit(1);
135         }
136         try {
137             //Load the driver
138
Class.forName(drvCN);
139         } catch (Exception JavaDoc e) {
140             out.println("Impossible to load the driver: " + drvCN);
141             out.println(e.getMessage());
142             System.exit(1);
143         }
144         if (url == null || url.length() == 0) {
145             out.println("No url specified !");
146             usage();
147             System.exit(1);
148         }
149         //Fetch a connection
150
try {
151             if (user == null || user.length() == 0)
152                 conn = DriverManager.getConnection(url);
153             else
154                 conn = DriverManager.getConnection(url, user, passwd);
155         } catch (SQLException JavaDoc e) {
156             out.println("Impossible to fetch a connection:");
157             out.println(e.getMessage());
158             System.exit(1);
159         }
160     }
161
162     public void specif(String JavaDoc sqlType, String JavaDoc stype, String JavaDoc sval, String JavaDoc getter, String JavaDoc setter) {
163         try {
164             Object JavaDoc val = null;
165             Class JavaDoc type = null;
166             try {
167                 type = Class.forName(stype);
168                 val = type
169                         .getDeclaredMethod("valueOf", new Class JavaDoc[]{String JavaDoc.class})
170                         .invoke(null, new Object JavaDoc[]{sval});
171             } catch (Exception JavaDoc e) {
172                 try {
173                     val = type
174                             .getDeclaredMethod("valueOf", new Class JavaDoc[]{Object JavaDoc.class})
175                             .invoke(null, new Object JavaDoc[]{sval});
176                 } catch (Exception JavaDoc e2) {
177                     System.err.println("Impossible to build a " + sval);
178                     System.exit(1);
179                 }
180             }
181             String JavaDoc tableName = "table_" + sqlType;
182             _testCreateTable(tableName, sqlType);
183             _testInsertSelect(sqlType, tableName, val, type, getter, setter);
184         } catch (Exception JavaDoc e) {
185             e.printStackTrace();
186         }
187     }
188
189     public void standard() {
190         testBoolean();
191         testByte();
192         testShort();
193         testInt();
194         testLong();
195         testFloat();
196         testDouble();
197         testDate();
198         testString();
199         testBigDecimal();
200     }
201
202     public void close() {
203         if (conn != null)
204             try {
205                 conn.close();
206             } catch (SQLException JavaDoc e) {
207             }
208     }
209
210     private boolean _testCreateTable(String JavaDoc tableName, String JavaDoc colType) {
211         Statement JavaDoc s = null;
212         try {
213             s = conn.createStatement();
214             s.execute("DROP TABLE " + tableName);
215
216         } catch (SQLException JavaDoc e) {
217         } finally {
218             try {
219                 s.close();
220             } catch (SQLException JavaDoc e) {
221             }
222         }
223         try {
224             s = conn.createStatement();
225             s.execute(
226                     "CREATE TABLE " + tableName + "(col1 " + colType + ")");
227             ok("create table", colType);
228             return true;
229         } catch (SQLException JavaDoc e) {
230             fail("create table: ", e, colType);
231             return false;
232         } finally {
233             try {
234                 s.close();
235             } catch (SQLException JavaDoc e) {
236             }
237         }
238     }
239
240     private void _testInsertSelect(String JavaDoc colType,
241                                    String JavaDoc tableName,
242                                    Object JavaDoc val,
243                                    Class JavaDoc type,
244                                    String JavaDoc getter,
245                                    String JavaDoc setter) {
246         String JavaDoc sval = "" + val;
247         PreparedStatement JavaDoc ps = null;
248         ResultSet JavaDoc rs = null;
249         try {
250             ps = conn.prepareStatement("INSERT INTO " + tableName + " VALUES (?)");
251             assignValue(ps, 1, val, type, setter);
252             ps.execute();
253             ok("insert value " + sval, colType);
254
255             ps = conn.prepareStatement("SELECT * from " + tableName + " WHERE col1 = ?");
256             assignValue(ps, 1, val, type, setter);
257             rs = ps.executeQuery();
258             if (rs.next()) {
259                 if (val.equals(retrieveValue(rs, 1, type, getter)))
260                     ok("select value " + sval, colType);
261                 else
262                     fail("select value " + sval + ": bad value", colType);
263
264                 Object JavaDoc o = rs.getObject(1);
265                 if (type.equals(o.getClass()) &&
266                         ((val == null && o == null) || (val != null && val.equals(o))))
267                     ok("select value " + sval + " with getObject", colType);
268                 else
269                     fail("select value " + sval + " with getObject", colType);
270
271                 if (rs.next())
272                     fail("select value " + sval + ": more than one result", colType);
273             } else
274                 fail("select value " + sval + ": no result", colType);
275         } catch (Exception JavaDoc e) {
276             fail("insert & select: ", e, colType);
277         } finally {
278             try {
279                 if (rs != null)
280                     rs.close();
281             } catch (SQLException JavaDoc e) {
282             }
283             try {
284                 if (ps != null)
285                     ps.close();
286             } catch (SQLException JavaDoc e) {
287             }
288         }
289     }
290
291     private void assignValue(PreparedStatement JavaDoc ps,
292                              int index,
293                              Object JavaDoc value,
294                              Class JavaDoc type,
295                              String JavaDoc setter) throws Exception JavaDoc {
296         if (value == null) {
297             ps.setNull(index, class2sqlType(type));
298         } else if (setter != null && setter.length() > 0) {
299             try {
300                 PreparedStatement JavaDoc.class.getMethod(setter, new Class JavaDoc[]{Integer.TYPE, type})
301                         .invoke(ps, new Object JavaDoc[]{new Integer JavaDoc(1), value});
302             } catch (NoSuchMethodException JavaDoc e) {
303                 fail("Method " + setter + " not found");
304                 throw e;
305             }
306         } else if (Boolean JavaDoc.class.equals(type))
307             ps.setBoolean(index, ((Boolean JavaDoc) value).booleanValue());
308
309         else if (Byte JavaDoc.class.equals(type))
310             ps.setByte(index, ((Byte JavaDoc) value).byteValue());
311
312         else if (Short JavaDoc.class.equals(type))
313             ps.setShort(index, ((Short JavaDoc) value).shortValue());
314
315         else if (Integer JavaDoc.class.equals(type))
316             ps.setInt(index, ((Integer JavaDoc) value).intValue());
317
318         else if (Long JavaDoc.class.equals(type))
319             ps.setLong(index, ((Long JavaDoc) value).longValue());
320
321         else if (Float JavaDoc.class.equals(type))
322             ps.setFloat(index, ((Float JavaDoc) value).floatValue());
323
324         else if (Double JavaDoc.class.equals(type))
325             ps.setDouble(index, ((Double JavaDoc) value).doubleValue());
326
327         else if (String JavaDoc.class.equals(type))
328             ps.setString(index, (String JavaDoc) value);
329
330         else if (Date JavaDoc.class.equals(type))
331             ps.setDate(index, (Date JavaDoc) value);
332
333         else if (BigDecimal JavaDoc.class.equals(type))
334             ps.setBigDecimal(index, (BigDecimal JavaDoc) value);
335
336         else
337             throw new Exception JavaDoc("Unmanaged type: " + type);
338     }
339
340     private int class2sqlType(Class JavaDoc type) throws Exception JavaDoc {
341         if (Boolean JavaDoc.class.equals(type))
342             return Types.BIT;
343         else if (Byte JavaDoc.class.equals(type))
344             return Types.TINYINT;
345         else if (Short JavaDoc.class.equals(type))
346             return Types.SMALLINT;
347         else if (Integer JavaDoc.class.equals(type))
348             return Types.INTEGER;
349         else if (Long JavaDoc.class.equals(type))
350             return Types.BIGINT;
351         else if (Float JavaDoc.class.equals(type))
352             return Types.REAL;
353         else if (Double JavaDoc.class.equals(type))
354             return Types.DOUBLE;
355         else if (String JavaDoc.class.equals(type))
356             return Types.VARCHAR;
357         else if (Date JavaDoc.class.equals(type))
358             return Types.DATE;
359         else if (BigDecimal JavaDoc.class.equals(type))
360             return Types.DECIMAL;
361         else
362             throw new Exception JavaDoc("Unmanaged type: " + type);
363     }
364
365     private Object JavaDoc retrieveValue(ResultSet JavaDoc rs,
366                                  int index,
367                                  Class JavaDoc type,
368                                  String JavaDoc getter) throws Exception JavaDoc {
369         Object JavaDoc o = null;
370         if (getter != null && getter.length() > 0) {
371             try {
372                 o = ResultSet JavaDoc.class.getMethod(getter, new Class JavaDoc[]{Integer.TYPE})
373                         .invoke(rs, new Object JavaDoc[]{new Integer JavaDoc(1)});
374             } catch (NoSuchMethodException JavaDoc e) {
375                 fail("Method " + getter + " not found");
376                 throw e;
377             }
378         } else if (Boolean JavaDoc.class.equals(type))
379             o = new Boolean JavaDoc(rs.getBoolean(index));
380
381         else if (Byte JavaDoc.class.equals(type))
382             o = new Byte JavaDoc(rs.getByte(index));
383
384         else if (Short JavaDoc.class.equals(type))
385             o = new Short JavaDoc(rs.getShort(index));
386
387         else if (Integer JavaDoc.class.equals(type))
388             o = new Integer JavaDoc(rs.getInt(index));
389
390         else if (Long JavaDoc.class.equals(type))
391             o = new Long JavaDoc(rs.getLong(index));
392
393         else if (Float JavaDoc.class.equals(type))
394             o = new Float JavaDoc(rs.getFloat(index));
395
396         else if (Double JavaDoc.class.equals(type))
397             o = new Double JavaDoc(rs.getDouble(index));
398
399         else if (String JavaDoc.class.equals(type))
400             o = rs.getString(index);
401
402         else if (Date JavaDoc.class.equals(type))
403             o = rs.getDate(index);
404
405         else if (BigDecimal JavaDoc.class.equals(type))
406             o = rs.getBigDecimal(index);
407
408         else
409             throw new Exception JavaDoc("Unknown type: " + type);
410         return (rs.wasNull() ? null : o);
411
412     }
413
414
415     public void testBoolean() {
416         String JavaDoc tableName = "JormBoolean";
417         String JavaDoc colType = "BIT";
418         if (_testCreateTable(tableName, colType)) {
419             _testInsertSelect(colType, tableName, Boolean.TRUE, Boolean JavaDoc.class, null, null);
420             _testInsertSelect(colType, tableName, Boolean.FALSE, Boolean JavaDoc.class, null, null);
421         }
422     }
423
424     public void testByte() {
425         String JavaDoc tableName = "JormByte";
426         String JavaDoc colType = "TINYINT";
427         if (_testCreateTable(tableName, colType)) {
428             _testInsertSelect(colType, tableName, new Byte JavaDoc((byte) 0), Byte JavaDoc.class, null, null);
429             _testInsertSelect(colType, tableName, new Byte JavaDoc((byte) 1), Byte JavaDoc.class, null, null);
430             _testInsertSelect(colType, tableName, new Byte JavaDoc((byte) 254), Byte JavaDoc.class, null, null);
431         }
432     }
433
434     public void testShort() {
435         String JavaDoc tableName = "JormShort";
436         String JavaDoc colType = "SMALLINT";
437         if (_testCreateTable(tableName, colType)) {
438             _testInsertSelect(colType, tableName, new Short JavaDoc((short) 0), Short JavaDoc.class, null, null);
439             _testInsertSelect(colType, tableName, new Short JavaDoc((short) 1), Short JavaDoc.class, null, null);
440             _testInsertSelect(colType, tableName, new Short JavaDoc((short) 254), Short JavaDoc.class, null, null);
441         }
442     }
443
444
445     public void testInt() {
446         String JavaDoc tableName = "JormInt";
447         String JavaDoc colType = "INTEGER";
448         if (_testCreateTable(tableName, colType)) {
449             _testInsertSelect(colType, tableName, new Integer JavaDoc(0), Integer JavaDoc.class, null, null);
450             _testInsertSelect(colType, tableName, new Integer JavaDoc(1), Integer JavaDoc.class, null, null);
451             _testInsertSelect(colType, tableName, new Integer JavaDoc(254), Integer JavaDoc.class, null, null);
452         }
453     }
454
455     public void testLong() {
456         String JavaDoc tableName = "JormLong";
457         String JavaDoc colType = "BIGINT";
458         if (_testCreateTable(tableName, colType)) {
459             _testInsertSelect(colType, tableName, new Long JavaDoc(0), Long JavaDoc.class, null, null);
460             _testInsertSelect(colType, tableName, new Long JavaDoc(1), Long JavaDoc.class, null, null);
461             _testInsertSelect(colType, tableName, new Long JavaDoc(254), Long JavaDoc.class, null, null);
462         }
463     }
464
465     public void testFloat() {
466         String JavaDoc tableName = "JormFloat";
467         String JavaDoc colType = "REAL";
468         if (_testCreateTable(tableName, colType)) {
469             _testInsertSelect(colType, tableName, new Float JavaDoc(0.0), Float JavaDoc.class, null, null);
470             _testInsertSelect(colType, tableName, new Float JavaDoc(1), Float JavaDoc.class, null, null);
471             _testInsertSelect(colType, tableName, new Float JavaDoc(254), Float JavaDoc.class, null, null);
472         }
473     }
474
475     public void testDouble() {
476         String JavaDoc tableName = "JormDouble";
477         String JavaDoc colType = "DOUBLE";
478         if (_testCreateTable(tableName, colType)) {
479             _testInsertSelect(colType, tableName, new Double JavaDoc(0.0), Double JavaDoc.class, null, null);
480             _testInsertSelect(colType, tableName, new Double JavaDoc(1), Double JavaDoc.class, null, null);
481             _testInsertSelect(colType, tableName, new Double JavaDoc(254), Double JavaDoc.class, null, null);
482         }
483     }
484
485     public void testDate() {
486         String JavaDoc tableName = "JormDate";
487         String JavaDoc colType = "DATE";
488         if (_testCreateTable(tableName, colType)) {
489             //_testInsertSelect(colType, tableName, new Date(), Double.class, null, null);
490
}
491     }
492
493     public void testString() {
494         _testCreateTable("JormString", "VARCHAR");
495         String JavaDoc tableName = "JormString";
496         String JavaDoc colType = "VARCHAR";
497         if (_testCreateTable(tableName, colType)) {
498             _testInsertSelect(colType, tableName, "", String JavaDoc.class, null, null);
499             _testInsertSelect(colType, tableName, "abcd", String JavaDoc.class, null, null);
500         }
501     }
502
503     public void testBigDecimal() {
504         _testCreateTable("JormBigDecimal", "DECIMAL");
505         String JavaDoc tableName = "JormBigDecimal";
506         String JavaDoc colType = "DECIMAL";
507         if (_testCreateTable(tableName, colType)) {
508             _testInsertSelect(colType, tableName, new BigDecimal JavaDoc(0.0), BigDecimal JavaDoc.class, null, null);
509             _testInsertSelect(colType, tableName, new BigDecimal JavaDoc("8764236557855635636.78966564"), BigDecimal JavaDoc.class, null, null);
510         }
511     }
512
513     public void testTableExistence() {
514         String JavaDoc tableName = "TableExist";
515         _testCreateTable(tableName, "INTEGER");
516         ResultSet JavaDoc rs = null;
517         boolean existtable = false;
518         try {
519             rs = conn.getMetaData().getTables(null, null, tableName, new String JavaDoc[]{"TABLE"});
520             while (rs.next() && !existtable) {
521                 existtable = tableName.equalsIgnoreCase(rs.getString(3));
522             }
523             if (existtable) {
524                 ok("Able to find an existing table");
525             } else {
526                 fail("Unable to found a table");
527             }
528         } catch(Exception JavaDoc e) {
529         } finally {
530             if (rs != null) {
531                 try {
532                     rs.close();
533                 } catch (SQLException JavaDoc e) {
534                     fail(e.getMessage());
535                 }
536             }
537         }
538     }
539
540     public void testSequence() {
541         String JavaDoc seqName = "TotoSeq";
542         Statement JavaDoc s = null;
543         try {
544             s = conn.createStatement();
545             s.execute("DROP SEQUENCE " + seqName);
546
547         } catch (SQLException JavaDoc e) {
548         } finally {
549             try {
550                 s.close();
551             } catch (SQLException JavaDoc e) {
552             }
553         }
554         try {
555             s = conn.createStatement();
556             s.execute(
557                     "CREATE SEQUENCE " + seqName);
558             ok("create sequence");
559         } catch (SQLException JavaDoc e) {
560             fail("create sequence: " + e.getMessage(), e);
561             return;
562         } finally {
563             try {
564                 s.close();
565             } catch (SQLException JavaDoc e) {
566             }
567         }
568         ResultSet JavaDoc rs = null;
569         boolean existtable = false;
570         try {
571             rs = conn.getMetaData().getTables(null, null, seqName, null);
572             while (rs.next() && !existtable) {
573                 existtable = seqName.equalsIgnoreCase(rs.getString(3));
574             }
575             if (existtable) {
576                 ok("Able to find an existing sequence");
577             } else {
578                 fail("Unable to found an existing sequence");
579             }
580         } catch(Exception JavaDoc e) {
581         } finally {
582             if (rs != null) {
583                 try {
584                     rs.close();
585                 } catch (SQLException JavaDoc e) {
586                     fail(e.getMessage());
587                 }
588             }
589         }
590     }
591
592     public static String JavaDoc readString(String JavaDoc invitation) throws Exception JavaDoc {
593         System.out.print(invitation);
594         System.out.flush();
595         StringBuffer JavaDoc reply_buffer = null;
596         char b = (char) System.in.read();
597         while (b > 0 && b != '\n' && Character.isWhitespace(b)) {
598             b = (char) System.in.read();
599         }
600         while (b > 0 && b != '\n' && b != 13) {
601             if (reply_buffer == null) {
602                 reply_buffer = new StringBuffer JavaDoc();
603             }
604             reply_buffer.append(b);
605             b = (char) System.in.read();
606         }
607         if (System.in.available() == 1) {
608             System.in.read();
609         }
610         if (reply_buffer != null) {
611             return reply_buffer.toString();
612         } else {
613             return "";
614         }
615     }
616
617     public void fail(String JavaDoc msg, Throwable JavaDoc t, String JavaDoc colType) {
618         fail(colType + ": " + msg, t);
619     }
620
621     public void fail(String JavaDoc msg, Throwable JavaDoc t) {
622         if (debug) {
623             fail(msg);
624             t.printStackTrace(out);
625         } else
626             fail(msg + t.getMessage());
627     }
628
629     public void fail(String JavaDoc msg, String JavaDoc colType) {
630         fail(colType + ": " + msg);
631     }
632
633     public void fail(String JavaDoc msg) {
634         out.println("**FAIL: " + msg);
635     }
636
637     public void ok(String JavaDoc msg, String JavaDoc colType) {
638         ok(colType + ": " + msg);
639     }
640
641     public void ok(String JavaDoc msg) {
642         if (debug)
643             out.println("OK: " + msg);
644     }
645
646     public void debug(String JavaDoc msg) {
647         if (debug)
648             out.println("DEBUG: " + msg);
649     }
650 }
651
Popular Tags