KickJava   Java API By Example, From Geeks To Geeks.

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


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
30 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility;
31
32 /**
33  * This test executes in order: a select, an update and a select on the update
34  * and checks the result set are as expected
35  *
36  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
37  * @version 1.0
38  */

39 public class UpdateTestLet extends AbstractConnectionTestLet
40 {
41   
42   private int totalExecute;
43   
44   /**
45    * Creates a new <code>UpdateTestLet</code> object
46    *
47    * @param con connection to use for testing
48    */

49   public UpdateTestLet(Connection JavaDoc con)
50   {
51     super(con);
52     config.put(TABLE_NAME, "PRODUCT");
53     config.put(COLUMN_NAME, "NAME");
54     config.put(UPDATED_COLUMN_VALUE, "horizontalTest");
55     config.put(SELECTED_COLUMNS, "*");
56     config.put(USE_PREPARED_STATEMENT, "false");
57     config.put(USE_TRANSACTIONS,"false");
58   }
59
60   /**
61    * @see org.objectweb.cjdbc.scenario.tools.testlet.AbstractTestLet#execute()
62    */

63   public void execute() throws Exception JavaDoc
64   {
65     String JavaDoc selectQuery = "Select " + config.get(SELECTED_COLUMNS) + " from "
66         + config.get(TABLE_NAME);
67     String JavaDoc updateQuery = "update " + config.get(TABLE_NAME) + " set "
68         + config.get(COLUMN_NAME) + "='" + config.get(UPDATED_COLUMN_VALUE)+(totalExecute++)
69         + "'";
70     boolean usePrepared = Boolean.valueOf((String JavaDoc)config.get(USE_PREPARED_STATEMENT)).booleanValue();
71     
72     ResultSet JavaDoc rs1,rs2;
73     if(usePrepared)
74     {
75       if(useTransaction())
76         jdbcConnection.setAutoCommit(false);
77       rs1 = jdbcConnection.prepareStatement(selectQuery).executeQuery();
78       jdbcConnection.prepareStatement(updateQuery).executeUpdate();
79       rs2 = jdbcConnection.prepareStatement(selectQuery).executeQuery();
80       if(useTransaction())
81         jdbcConnection.commit();
82     }
83     else
84     {
85       if(useTransaction())
86         jdbcConnection.setAutoCommit(false);
87       rs1 = jdbcConnection.createStatement().executeQuery(selectQuery);
88       jdbcConnection.createStatement().executeUpdate(updateQuery);
89       rs2 = jdbcConnection.createStatement().executeQuery(selectQuery);
90       if(useTransaction())
91         jdbcConnection.commit();
92     }
93     
94     assertNotNull("ResultSet before update is null", rs1);
95     assertNotNull("ResultSet after update is null", rs2);
96     assertFalse(ScenarioUtility.checkEquals(rs1, rs2));
97   }
98 }
Popular Tags