KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > runtime > TestJdbcConnection


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
24 package org.objectweb.jorm.runtime;
25
26 import org.objectweb.jorm.naming.api.PBinder;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.util.monolog.api.BasicLevel;
29
30 import java.sql.Connection JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 /**
36  * @author S. Chassandes-Barrioz
37  */

38 public class TestJdbcConnection extends TestRuntimeHelper {
39
40     protected String JavaDoc getLoggerName() {
41         return null;
42     }
43
44     protected PBinder getBinder(String JavaDoc className) throws Exception JavaDoc {
45         return null;
46     }
47
48     public TestJdbcConnection(String JavaDoc s) throws Exception JavaDoc {
49         super(s);
50     }
51     public void testGetConnection() throws Exception JavaDoc {
52         mapper.closeConnection(mapper.getConnection());
53     }
54
55     public void testExistingTable() throws Exception JavaDoc {
56         Connection JavaDoc connection = (Connection JavaDoc) mapper.getConnection();
57         String JavaDoc name = "JT_EXISTING_TABLE";
58         try {
59             connection.createStatement().execute("DROP TABLE " + name);
60         } catch (SQLException JavaDoc e) {
61         }
62         logger.log(BasicLevel.DEBUG, "create the table: " + name);
63         connection.createStatement().execute(
64             "CREATE TABLE " + name + " (f1 INTEGER)");
65         ResultSet JavaDoc rs = null;
66         boolean existtable = false;
67         logger.log(BasicLevel.DEBUG, "Search the table: " + name);
68         ArrayList JavaDoc tables = new ArrayList JavaDoc(1);
69         try {
70             rs = connection.getMetaData()
71                     .getTables(null, null, null, null);
72             String JavaDoc current = null;
73             while (rs.next() && !existtable) {
74                 current = rs.getString(3);
75                 logger.log(BasicLevel.DEBUG, "found: " + current);
76                 logger.log(BasicLevel.DEBUG, "1: " + current);
77                 tables.add(current);
78                 existtable = name.equalsIgnoreCase(current);
79             }
80         } catch (SQLException JavaDoc e) {
81             throw new PException(e, "Pb while testing the existence of table " + name);
82         } finally {
83             if (rs != null) {
84                 try {
85                     rs.close();
86                 } catch (SQLException JavaDoc e) {
87                 }
88             }
89         }
90         connection.createStatement().execute("DROP TABLE " + name);
91         assertTrue("The table "+ name + " was not found in " + tables, existtable);
92     }
93
94
95 }
96
Popular Tags