001: /*
002: * To change this template, choose Tools | Templates
003: * and open the template in the editor.
004: */
005:
006: package com.sun.data.provider.impl;
007:
008: import com.sun.data.provider.FieldKey;
009: import com.sun.sql.rowset.CachedRowSetXImpl;
010: import java.io.File;
011: import junit.framework.TestCase;
012:
013: import java.sql.Connection;
014: import java.sql.DriverManager;
015: import java.sql.PreparedStatement;
016: import java.sql.SQLException;
017: import java.util.logging.Level;
018: import java.util.logging.Logger;
019: import junit.framework.TestCase;
020:
021: /**
022: * This class makes sure that the rowset behaves correctly around what should
023: * happen if the rowset is executed, not executed, modified, etc.
024: *
025: * @author David
026: */
027: public class CachedRowSetDataProviderTest extends TestCase {
028:
029: private static final Logger LOGGER = Logger
030: .getLogger(CachedRowSetDataProviderTest.class.getName());
031:
032: private static final String DBURL = "jdbc:derby:mydb;create=true";
033: private static final String TABLENAME = "mytable";
034: private static final String IDNAME = "id";
035: private static final String COL1NAME = "col1";
036: private static final String COL2NAME = "col2";
037:
038: private static final int NUMROWS = 10;
039:
040: public CachedRowSetDataProviderTest(String testName) {
041: super (testName);
042: }
043:
044: protected void setUp() throws Exception {
045: try {
046: super .setUp();
047:
048: // Comment this out to turn off debugging
049: LOGGER.setLevel(Level.FINE);
050:
051: initDatabase();
052: } catch (SQLException sqle) {
053: reportSQLException(sqle);
054: } catch (Throwable t) {
055: LOGGER.log(Level.SEVERE, "Failed to set up test", t);
056: throw new Exception(t);
057: }
058: }
059:
060: private void reportSQLException(SQLException sqle) {
061: LOGGER.log(Level.SEVERE, null, sqle);
062:
063: if (sqle.getNextException() != null) {
064: reportSQLException(sqle.getNextException());
065: }
066: }
067:
068: protected void tearDown() throws Exception {
069: super .tearDown();
070:
071: // Remove the test database so it's not left lying around
072: String userdir = System.getProperty("user.dir");
073: File dbdir = new File(userdir + "/" + "mydb");
074:
075: LOGGER.log(Level.INFO, "userdir is " + userdir);
076:
077: deleteRecursively(dbdir);
078:
079: File logfile = new File(userdir + "/derby.log");
080: if (logfile.exists()) {
081: logfile.delete();
082: }
083: }
084:
085: private void deleteRecursively(File file) throws Exception {
086: if (!file.exists()) {
087: return;
088: }
089:
090: if (!file.isDirectory()) {
091: file.delete();
092: return;
093: }
094:
095: File[] children = file.listFiles();
096:
097: for (int i = 0; i < children.length; i++) {
098: deleteRecursively(children[i]);
099: }
100:
101: file.delete();
102: }
103:
104: private void initDatabase() throws Exception {
105: Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
106: Connection conn = DriverManager.getConnection(DBURL);
107:
108: try {
109: conn.prepareStatement("DROP TABLE " + TABLENAME).execute();
110: } catch (SQLException sqle) {
111: LOGGER.log(Level.FINE, null, sqle);
112: }
113:
114: String create = "CREATE TABLE " + TABLENAME + "(" + IDNAME
115: + " int primary key, " + COL1NAME + " varchar(255), "
116: + COL2NAME + " varchar(255))";
117:
118: conn.prepareStatement(create).execute();
119:
120: PreparedStatement insert = conn.prepareStatement("INSERT INTO "
121: + TABLENAME + " VALUES(?, ?, ?)");
122:
123: for (int i = 1; i <= NUMROWS; i++) {
124: insert.setInt(1, i);
125: insert.setString(2, "col1_" + i);
126: insert.setString(3, "col2_" + i);
127:
128: insert.execute();
129: }
130: }
131:
132: /**
133: * Make sure the data provider detects a change to the rowset command
134: *
135: * @throws java.lang.Exception
136: */
137: public void testCommandChange() throws Exception {
138: CachedRowSetXImpl rowset = new CachedRowSetXImpl();
139:
140: CachedRowSetDataProvider provider = new CachedRowSetDataProvider();
141: provider.setCachedRowSet(rowset);
142:
143: rowset.setUrl("jdbc:derby:mydb;create=true");
144:
145: /**
146: * Select only one row and one extra column
147: */
148: rowset.setCommand("SELECT " + IDNAME + ", " + COL1NAME
149: + " FROM " + TABLENAME + " WHERE " + IDNAME + " = 2");
150:
151: rowset.setTableName(TABLENAME);
152:
153: checkRows(provider, 1, 2);
154:
155: rowset.setCommand("SELECT " + IDNAME + " FROM " + TABLENAME);
156:
157: checkRows(provider, NUMROWS, 1);
158:
159: provider.close();
160: }
161:
162: private void checkRows(CachedRowSetDataProvider provider,
163: int expectedRows, int expectedFields) {
164: int numrows = provider.getRowCount();
165: assert (numrows == expectedRows);
166:
167: FieldKey[] keys = provider.getFieldKeys();
168: int numkeys = keys.length;
169:
170: assert (numkeys == expectedFields);
171:
172: provider.cursorFirst();
173:
174: for (int i = 0; i < numrows; i++) {
175: for (int j = 0; j < numkeys; j++) {
176: Object value = provider.getValue(keys[j]);
177: assert (value != null);
178: }
179: }
180: }
181:
182: }
|