KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > testlet > FailoverOn2BackendsTestLet


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.tools.testlet;
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.tools.components.ComponentInterface;
33
34 /**
35  * This class defines a FailoverOn2BackendsTestLet
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk</a>
38  * @version 1.0
39  */

40 public class FailoverOn2BackendsTestLet extends AbstractConnectionTestLet
41 {
42
43   /**
44    * Creates a new <code>FailoverOn2BackendsTestLet</code> object
45    *
46    * @param con connection to use for testing
47    */

48   public FailoverOn2BackendsTestLet(Connection JavaDoc con)
49   {
50     super(con);
51   }
52
53   /**
54    * @see org.objectweb.cjdbc.scenario.tools.testlet.AbstractTestLet#execute()
55    */

56   public void execute() throws Exception JavaDoc
57   {
58     
59     Object JavaDoc[] co = (Object JavaDoc[])config.get(LIST_FAILOVER_BACKENDS);
60     if(co ==null || co.length<2)
61       throw new Exception JavaDoc("Failover needs at least two backends");
62     
63     ComponentInterface hm1 = (ComponentInterface)co[0];
64     ComponentInterface hm2 = (ComponentInterface)co[1];
65     
66     Statement JavaDoc statement = jdbcConnection.createStatement();
67     ResultSet JavaDoc rs1 = statement.executeQuery("Select * from document");
68     assertNotNull("ResultSet is null", rs1);
69
70     // Drop a backend
71
hm1.release();
72
73     // Execute requests with same connection
74
Statement JavaDoc statement2 = jdbcConnection.createStatement();
75     ResultSet JavaDoc rs2 = statement2.executeQuery("Select * from document");
76     assertNotNull("ResultSet after failover is null", rs2);
77     rs1.last();
78     rs2.last();
79     assertTrue("Row numbers are different", rs1.getRow() == rs2.getRow());
80     rs1.first();
81     rs2.first();
82     while (rs1.next() & rs2.next())
83     {
84       assertTrue("Some result differs from expect result set", rs1.getString(
85           "id").equals(rs2.getString("id")));
86     }
87
88     // Drop other backend
89
hm2.release();
90
91     // Execute request
92
Statement JavaDoc statement3 = jdbcConnection.createStatement();
93     ResultSet JavaDoc rs3 = null;
94     try
95     {
96       rs3 = statement3.executeQuery("Select * from document");
97     }
98     catch (SQLException JavaDoc expected)
99     {
100       // expected cause no more backends.
101
}
102     assertNull("Should not be able to get a result set anymore", rs3);
103
104   }
105
106 }
107
Popular Tags