KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > RemoteDSUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.test;
23
24 import java.net.URL JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.ResultSetMetaData JavaDoc;
30
31 import junit.framework.Test;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.sql.DataSource JavaDoc;
34
35 import org.jboss.test.JBossTestCase;
36
37 /** Tests of remote access to a jdbc datasource.
38  *
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 37406 $
41  */

42 public class RemoteDSUnitTestCase extends JBossTestCase
43 {
44    public RemoteDSUnitTestCase (String JavaDoc name)
45    {
46       super(name);
47    }
48
49    
50    public void testStatement() throws Exception JavaDoc
51    {
52       log.info("+++ testStatement");
53       InitialContext JavaDoc ctx = super.getInitialContext();
54       DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("RemoteDS");
55       Connection JavaDoc conn = ds.getConnection("sa", "");
56       DatabaseMetaData JavaDoc dmd = conn.getMetaData();
57       log.info(dmd);
58       Statement JavaDoc stmt = conn.createStatement();
59       // Create a table
60
stmt.executeUpdate("CREATE TABLE COFFEES " +
61          "(NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, SALES INTEGER, TOTAL INTEGER)");
62       // Add some data
63
stmt.executeUpdate("INSERT INTO COFFEES VALUES ('Colombian', 100, 7.99, 0, 0)");
64       stmt.executeUpdate("INSERT INTO COFFEES VALUES ('FrenchRoast', 101, 8.99, 0, 0)");
65       stmt.executeUpdate("INSERT INTO COFFEES VALUES ('JavaBean', 102, 6.99, 0, 0)");
66       // Query the data
67
ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM COFFEES where NAME = 'Colombian'");
68       ResultSetMetaData JavaDoc rsmd = rs.getMetaData();
69       assertTrue("ResultSetMetaData.getColumnCount == 5",
70          rsmd.getColumnCount() == 5);
71       int nameIndex = rs.findColumn("NAME");
72       String JavaDoc cname = rsmd.getColumnName(nameIndex);
73       assertTrue("NAME column maps", cname.equalsIgnoreCase("NAME"));
74       
75       assertTrue("ResultSet.next == true",
76          rs.next() == true );
77       String JavaDoc name = rs.getString("NAME");
78       assertTrue("name == Colombian", name.equals("Colombian"));
79       int id = rs.getInt("SUP_ID");
80       assertTrue("id == 100", id == 100);
81       float price = rs.getFloat("PRICE");
82       int iprice = Math.round(100 * price);
83       log.info("iprice = "+iprice);
84       assertTrue("price == 7.99", 799 == iprice);
85       int sales = rs.getInt("SALES");
86       assertTrue("sales == 0", sales == 0);
87       int total = rs.getInt("TOTAL");
88       assertTrue("total == 0", total == 0);
89       rs.close();
90
91       // Drop the table
92
stmt.executeUpdate("DROP TABLE COFFEES");
93       stmt.close();
94       conn.close();
95    }
96
97    public static Test suite() throws Exception JavaDoc
98    {
99       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
100       URL JavaDoc resURL = loader.getResource("jca/remote-jdbc/remote-ds.xml");
101       return getDeploySetup(RemoteDSUnitTestCase.class, resURL.toString());
102    }
103 }
104
Popular Tags