KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > basic > StartStopDatabaseScenario


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.basic;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import org.objectweb.cjdbc.scenario.templates.Template;
35 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface;
36 import org.objectweb.cjdbc.scenario.tools.components.backend.DatabaseManager;
37
38 /**
39  * This class defines a StartStopHsqlScenario
40  *
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @version 1.0
43  */

44 public class StartStopDatabaseScenario extends Template
45 {
46
47   DatabaseManager hm;
48   ComponentInterface hsql1;
49   String JavaDoc port1 = "9011";
50   String JavaDoc port2 = "9012";
51   String JavaDoc port3 = "9013";
52
53   /**
54    * @see junit.framework.TestCase#setUp()
55    */

56   protected void setUp()
57   {
58
59   }
60
61   /**
62    * @see junit.framework.TestCase#tearDown()
63    */

64   protected void tearDown()
65   {
66
67   }
68
69   /**
70    * testStart
71    */

72   public void testStart()
73   {
74     hm = new DatabaseManager();
75     try
76     {
77       hsql1 = hm.start(port1);
78       assertTrue("Hypersonic was not started", hm.isStarted(port1));
79     }
80     catch (Exception JavaDoc e)
81     {
82       fail("Could not start hsql.");
83     }
84     hm.stop(hsql1);
85   }
86
87   /**
88    * testStartStop
89    */

90   public void testStartStop()
91   {
92     hm = new DatabaseManager();
93
94     try
95     {
96       hsql1 = hm.start(port2);
97       hm.stop(hsql1);
98     }
99     catch (Exception JavaDoc e)
100     {
101       fail("Could not control hsql");
102     }
103   }
104
105   /**
106    * Start Stop Connect Test
107    */

108   public void testStartStopConnect()
109   {
110     hm = new DatabaseManager();
111     Connection JavaDoc con = null;
112     Statement JavaDoc statement = null;
113     ResultSet JavaDoc rs = null;
114
115     try
116     {
117       hsql1 = hm.start(port3);
118       hm.loaddatabase(port3);
119     }
120     catch (Exception JavaDoc e)
121     {
122       e.printStackTrace();
123       hm.stop(hsql1);
124       fail("Could not start hsql.");
125     }
126     try
127     {
128       Class.forName("org.hsqldb.jdbcDriver");
129     }
130     catch (ClassNotFoundException JavaDoc e)
131     {
132       hm.stop(hsql1);
133       fail("Could not find hsql driver");
134     }
135     try
136     {
137       Properties JavaDoc props = new Properties JavaDoc();
138       props.put("user", "test");
139       props.put("password", "");
140       con = DriverManager.getConnection(
141           "jdbc:hsqldb:hsql://localhost:" + port3, props);
142     }
143     catch (SQLException JavaDoc e1)
144     {
145       hm.stop(hsql1);
146       e1.printStackTrace();
147       fail("Could not get connection to hsql");
148     }
149     try
150     {
151       statement = con.createStatement();
152       statement.setFetchSize(5);
153     }
154     catch (SQLException JavaDoc e2)
155     {
156       hm.stop(hsql1);
157       fail("could not prepare statement");
158     }
159     try
160     {
161
162       rs = statement.executeQuery("select * from document");
163     }
164     catch (SQLException JavaDoc e3)
165     {
166       hm.stop(hsql1);
167       System.out.println(e3.getMessage());
168       e3.printStackTrace();
169       fail("Could not get result set");
170     }
171     catch (Exception JavaDoc e)
172     {
173       e.printStackTrace();
174     }
175     try
176     {
177       while (rs.next())
178       {
179         rs.getString("ID");
180       }
181     }
182     catch (Exception JavaDoc e)
183     {
184       hm.stop(hsql1);
185       e.printStackTrace();
186       fail("Could not access result set");
187     }
188     hm.stop(hsql1);
189   }
190
191   /**
192    * Stupid test for new hsqldb
193    *
194    * @throws Exception if fails
195    */

196   public void testNewHsqldbRelease() throws Exception JavaDoc
197   {
198     hm = new DatabaseManager();
199     hsql1 = hm.start(port1);
200     hm.loaddatabase(port1);
201     Connection JavaDoc con = getHypersonicConnection(Integer.parseInt(port1));
202     Statement JavaDoc stm = con.createStatement();
203     //stm.executeUpdate("drop table test");
204
stm
205             .executeUpdate("create table test (id int,atime timestamp default current_timestamp)");
206      int row = stm.executeUpdate("insert into test (id) values (1)");
207      System.out.println(row);
208      hm.stop(hsql1);
209   }
210
211 }
212
Popular Tags