KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.SecureRandom JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
35 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility;
36
37 /**
38  * This class defines a PreparedStatementRecoveryTestLet
39  *
40  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
41  * @version 1.0
42  */

43 public class PreparedStatementRecoveryTestLet extends AbstractVdbTestLet
44 {
45
46   SecureRandom JavaDoc rand = new SecureRandom JavaDoc();
47
48   int numberOfUpdates;
49
50   /**
51    * Creates a new <code>PreparedStatementRecoveryTestLet</code> object
52    *
53    * @param vdb virtual database object
54    */

55   public PreparedStatementRecoveryTestLet(VirtualDatabase vdb)
56   {
57     super(vdb);
58     config.put(NUMBER_OF_UPDATES, "50");
59     config.put(USE_OPTIMIZED_STATEMENT, Boolean.TRUE);
60
61   }
62
63   /**
64    * @see org.objectweb.cjdbc.scenario.tools.testlet.AbstractTestLet#execute()
65    */

66   public void execute() throws Exception JavaDoc
67   {
68
69     if (vdb.getAllBackendNames().size() < 2)
70       throw new TestLetException("This testlet needs at least two backends");
71
72     Properties JavaDoc props = new Properties JavaDoc();
73     boolean use = ((Boolean JavaDoc) config.get(USE_OPTIMIZED_STATEMENT))
74         .booleanValue();
75     props.put("driverProcessed", "" + !use);
76
77     Connection JavaDoc con = getCJDBCConnection(props);
78
79     int numberOfUpdates = Integer.parseInt((String JavaDoc) config
80         .get(NUMBER_OF_UPDATES));
81     System.out.println("number of updates:" + numberOfUpdates);
82
83     String JavaDoc create = "create table setget (col1 varchar(10), col2 varchar(10),col3 varchar(10),col4 varchar(10),col5 varchar(10),col6 varchar(10),col7 varchar(10),col8 varchar(10), col9 varchar)";
84     String JavaDoc insert = "insert into setget values(?,?,?,?,?,?,?,?,?)";
85     con.createStatement().executeUpdate(create);
86
87     //Disable backend
88
String JavaDoc checkpoint = "checkpoint" + System.currentTimeMillis();
89     String JavaDoc backendName = getBackend(0).getName();
90     vdb.disableBackendWithCheckpoint(backendName);
91
92     // Execute random queries
93
PreparedStatement JavaDoc ps = con.prepareStatement(insert);
94     for (int i = 0; i < numberOfUpdates; i++)
95       doExecuteRandomPStatement(ps, 9);
96     
97     con.close();
98
99     // Enable backend
100
vdb.enableBackendFromCheckpoint(backendName);
101 //
102
// ArrayList resultRecovery = ScenarioUtility.getSingleQueryResult(
103
// "select * from RECOVERY",
104
// getDatabaseConnection("jdbc:hsqldb:hsql://localhost:9003"));
105
// ScenarioUtility.displayResultOnScreen(resultRecovery);
106

107     // Get original result
108
String JavaDoc query = "select * from setget";
109     ArrayList JavaDoc result1 = ScenarioUtility.getSingleQueryResult(query,
110         getBackendConnection(0));
111     ScenarioUtility.displayResultOnScreen(result1);
112     // Get new result after recovery
113
ArrayList JavaDoc result2 = ScenarioUtility.getSingleQueryResult(query,
114         getBackendConnection(1));
115     ScenarioUtility.displayResultOnScreen(result2);
116
117     assertEquals(result1, result2);
118   }
119
120   private void doExecuteRandomPStatement(PreparedStatement JavaDoc ps, int size)
121       throws SQLException JavaDoc
122   {
123     for (int i = 0; i < size; i++)
124       doSetRandomParameter(i + 1, ps);
125
126     int result = ps.executeUpdate();
127     assertTrue("Updated columns returned false value", result == 1);
128   }
129
130   private void doSetRandomParameter(int index, PreparedStatement JavaDoc ps)
131       throws SQLException JavaDoc
132   {
133     int type = rand.nextInt(12);
134     if (type == 0)
135       ps.setBoolean(index, rand.nextBoolean());
136     else if (type == 1)
137       ps.setByte(index, (byte) rand.nextInt());
138     else if (type == 2)
139     {
140       byte[] b = new byte[10];
141       rand.nextBytes(b);
142       ps.setBytes(index, b);
143     }
144     else if (type == 3)
145       ps.setDate(index, new java.sql.Date JavaDoc(2004, 10, 10));
146     else if (type == 4)
147       ps.setDouble(index, rand.nextDouble());
148     else if (type == 5)
149       ps.setFloat(index, rand.nextFloat());
150     else if (type == 6)
151       ps.setInt(index, rand.nextInt(10));
152     else if (type == 7)
153       ps.setLong(index, rand.nextLong());
154     else if (type == 8)
155       ps.setNull(index, java.sql.Types.VARCHAR);
156     else if (type == 9)
157       ps.setObject(index, new String JavaDoc("niko"));
158     else if (type == 10)
159       ps.setShort(index, (short) rand.nextInt(10));
160     else if (type == 11)
161       ps.setString(index, "niko");
162     //else if (type == 12)
163
// ps.setURL(index, new URL("http://www.google.com"));
164
}
165
166 }
Popular Tags