KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > driver > Raidb1ChangeCatalogScenario


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): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.raidb1.driver;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31
32 import org.objectweb.cjdbc.scenario.templates.SimpleRaidb1Template;
33
34 /**
35  * Catalogs scenario
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modryzk </a>
38  * @version 1.0
39  */

40 public class Raidb1ChangeCatalogScenario extends SimpleRaidb1Template
41 {
42
43   /**
44    * Test changing of catalogs from connection
45    *
46    * @throws Exception if something's wrong
47    */

48   public void testChangeCatalog() throws Exception JavaDoc
49   {
50     Connection JavaDoc con = getCJDBCConnection("25322", "myDB1");
51     con.setCatalog("myDB1");
52     Statement JavaDoc statement = con.createStatement();
53     ResultSet JavaDoc rs = statement.executeQuery("Select * from document");
54     assertNotNull("ResultSet is null", rs);
55     statement.close();
56     int count1 = controller.getVirtualDatabase("myDB1").getRequestManager()
57         .getScheduler().getNumberRead();
58     con.setCatalog("myDB2");
59     statement = con.createStatement();
60     rs = statement.executeQuery("Select * from document");
61     assertNotNull("ResultSet is null", rs);
62     statement.close();
63     int count2 = controller.getVirtualDatabase("myDB2").getRequestManager()
64         .getScheduler().getNumberRead();
65     assertTrue("Expected only one request on catalog myDB1,was:" + count1,
66         count1 == 1);
67     assertTrue("Expected only one request on catalog myDB2,was:" + count2,
68         count2 == 1);
69     assertTrue("Expected same number of requests on both catalogs",
70         count2 == count1);
71   }
72
73   /**
74    * try to change catalog without a proper login
75    *
76    * @throws Exception if fails
77    */

78   public void testChangeCatalogWithWrongUser() throws Exception JavaDoc
79   {
80     Exception JavaDoc exception = null;
81     Connection JavaDoc con = null;
82     try
83     {
84       con = getCJDBCConnection("25322", "myDB1", "user2", "");
85     }
86     catch (Exception JavaDoc e)
87     {
88       exception = e;
89       e.printStackTrace();
90     }
91     assertNull("The user should be properly authenticated.", exception);
92     try
93     {
94       con.setCatalog("myDB2");
95     }
96     catch (SQLException JavaDoc sql)
97     {
98       //expected since not authentified
99
exception = sql;
100     }
101     assertNotNull("Changing of catalog should have thrown an exception",
102         exception);
103   }
104
105   /**
106    * @see junit.framework.TestCase#setUp()
107    */

108   protected void setUp()
109   {
110     super.setUp();
111     try
112     {
113     cm.loadVirtualDatabases(controller, "myDB1",
114         "hsqldb-raidb1-with2virtualdatabases.xml");
115     cm.loadVirtualDatabases(controller, "myDB2",
116         "hsqldb-raidb1-with2virtualdatabases.xml");
117     mainVdb = controller.getVirtualDatabase("myDB1");
118     mainVdb.enableAllBackends();
119     mainVdb = controller.getVirtualDatabase("myDB2");
120     mainVdb.enableAllBackends();
121     }
122     catch(Exception JavaDoc e)
123     {
124       e.printStackTrace();
125     }
126   }
127 }
128
Popular Tags