001: package net.sourceforge.jtds.test;
002:
003: import java.sql.*;
004:
005: /**
006: * Some simple tests just to make sure everything is working properly.
007: *
008: * @created 9 August 2001
009: * @version 1.0
010: */
011:
012: public class SanityTest extends TestBase {
013: public SanityTest(String name) {
014: super (name);
015: }
016:
017: /**
018: * A simple test to make sure everything seems to be OK
019: */
020: public void testSanity() throws Exception {
021: Statement stmt = con.createStatement();
022: makeTestTables(stmt);
023: makeObjects(stmt, 5);
024: stmt.close();
025: }
026:
027: /**
028: * Basic test of cursor mechanisms.
029: */
030: public void testCursorStatements() throws Exception {
031: Statement stmt = con.createStatement();
032: makeTestTables(stmt);
033: makeObjects(stmt, 5);
034:
035: ResultSet rs;
036:
037: assertEquals("Expected an update count", false, stmt
038: .execute("DECLARE cursor1 SCROLL CURSOR FOR"
039: + "\nSELECT * FROM #test"));
040:
041: assertEquals("Expected an update count", false, stmt
042: .execute("OPEN cursor1"));
043:
044: rs = stmt.executeQuery("FETCH LAST FROM cursor1");
045: dump(rs);
046: rs.close();
047:
048: rs = stmt.executeQuery("FETCH FIRST FROM cursor1");
049: dump(rs);
050: rs.close();
051:
052: stmt.execute("CLOSE cursor1");
053: stmt.execute("DEALLOCATE cursor1");
054: stmt.close();
055: }
056:
057: public void testCursorRSCreate() throws Exception {
058: Statement stmt = con.createStatement(
059: ResultSet.TYPE_SCROLL_INSENSITIVE,
060: ResultSet.CONCUR_READ_ONLY);
061:
062: makeTestTables(stmt);
063: makeObjects(stmt, 5);
064:
065: ResultSet rs = stmt
066: .executeQuery("Select * from #test order by 1");
067:
068: // Move to last row (5)
069: rs.last();
070: assertEquals(5, rs.getRow());
071: assertEquals(4, rs.getInt(1));
072: assertEquals(false, rs.isBeforeFirst());
073: assertEquals(false, rs.isFirst());
074: assertEquals(true, rs.isLast());
075: assertEquals(false, rs.isAfterLast());
076:
077: // Move before first row
078: rs.beforeFirst();
079: assertEquals(0, rs.getRow());
080:
081: try {
082: rs.getInt(1);
083: fail("There should be no current row.");
084: } catch (SQLException ex) {
085: // This is ok, there's no row
086: }
087:
088: assertEquals(true, rs.isBeforeFirst());
089: assertEquals(false, rs.isFirst());
090: assertEquals(false, rs.isLast());
091: assertEquals(false, rs.isAfterLast());
092:
093: // Try to move 3 rows ahead (should end up on row 3 -- the spec,
094: // says that relative(1) is identical to next() and relative(-1) is
095: // identical to previous(), but the Javadoc says that they are
096: // different). Weird stuff...
097: rs.relative(3);
098: assertEquals(3, rs.getRow());
099: assertEquals(2, rs.getInt(1));
100: assertEquals(false, rs.isBeforeFirst());
101: assertEquals(false, rs.isFirst());
102: assertEquals(false, rs.isLast());
103: assertEquals(false, rs.isAfterLast());
104:
105: // Move after last row
106: rs.afterLast();
107: assertEquals(0, rs.getRow());
108:
109: try {
110: rs.getInt(1);
111: fail("There should be no current row.");
112: } catch (SQLException ex) {
113: // This is ok, there's no row
114: }
115:
116: assertEquals(false, rs.isBeforeFirst());
117: assertEquals(false, rs.isFirst());
118: assertEquals(false, rs.isLast());
119: assertEquals(true, rs.isAfterLast());
120:
121: // Move to first row
122: rs.first();
123: assertEquals(1, rs.getRow());
124: assertEquals(0, rs.getInt(1));
125: assertEquals(false, rs.isBeforeFirst());
126: assertEquals(true, rs.isFirst());
127: assertEquals(false, rs.isLast());
128: assertEquals(false, rs.isAfterLast());
129:
130: // Move to row 4
131: rs.absolute(4);
132: assertEquals(4, rs.getRow());
133: assertEquals(3, rs.getInt(1));
134: assertEquals(false, rs.isBeforeFirst());
135: assertEquals(false, rs.isFirst());
136: assertEquals(false, rs.isLast());
137: assertEquals(false, rs.isAfterLast());
138:
139: // Move 2 rows back
140: rs.relative(-2);
141: assertEquals(2, rs.getRow());
142: assertEquals(1, rs.getInt(1));
143: assertEquals(false, rs.isBeforeFirst());
144: assertEquals(false, rs.isFirst());
145: assertEquals(false, rs.isLast());
146: assertEquals(false, rs.isAfterLast());
147:
148: rs.close();
149: stmt.close();
150: }
151:
152: public void testCursorRSScroll() throws Exception {
153: Statement stmt = con.createStatement(
154: ResultSet.TYPE_SCROLL_INSENSITIVE,
155: ResultSet.CONCUR_READ_ONLY);
156:
157: makeTestTables(stmt);
158: makeObjects(stmt, 5);
159:
160: ResultSet rs = stmt.executeQuery("Select * from #test");
161:
162: while (rs.next())
163: ;
164:
165: rs.close();
166: stmt.close();
167: }
168:
169: /*
170: * Check that image fields that have once been set to a non
171: * null value return null when updated to null.
172: * Fix bug [1774322] Sybase nulled text fields return not null.
173: */
174: public void testNullImage() throws Exception {
175: Statement stmt = con.createStatement();
176: stmt
177: .execute("CREATE TABLE #TEST (id int primary key not null, img image null)");
178: stmt.executeUpdate("INSERT INTO #TEST VALUES (1, null)");
179: ResultSet rs = stmt.executeQuery("SELECT * FROM #TEST");
180: rs.next();
181: assertTrue(rs.getBytes(2) == null);
182: stmt
183: .executeUpdate("UPDATE #TEST SET img = '0x0123' WHERE id = 1");
184: rs = stmt.executeQuery("SELECT * FROM #TEST");
185: rs.next();
186: assertTrue(rs.getBytes(2) != null);
187: stmt.executeUpdate("UPDATE #TEST SET img = null WHERE id = 1");
188: rs = stmt.executeQuery("SELECT * FROM #TEST");
189: rs.next();
190: assertTrue(rs.getBytes(2) == null);
191: stmt.close();
192: }
193:
194: /*
195: * Check that text fields that have once been set to a non
196: * null value return null when updated to null.
197: * Fix bug [1774322] Sybase nulled text fields return not null.
198: */
199: public void testNullText() throws Exception {
200: Statement stmt = con.createStatement();
201: stmt
202: .execute("CREATE TABLE #TEST (id int primary key not null, txt text null)");
203: stmt.executeUpdate("INSERT INTO #TEST VALUES (1, null)");
204: ResultSet rs = stmt.executeQuery("SELECT * FROM #TEST");
205: rs.next();
206: assertTrue(rs.getString(2) == null);
207: stmt.executeUpdate("UPDATE #TEST SET txt = ' ' WHERE id = 1");
208: rs = stmt.executeQuery("SELECT * FROM #TEST");
209: rs.next();
210: assertTrue(rs.getString(2) != null);
211: stmt.executeUpdate("UPDATE #TEST SET txt = null WHERE id = 1");
212: rs = stmt.executeQuery("SELECT * FROM #TEST");
213: rs.next();
214: assertTrue(rs.getString(2) == null);
215: stmt.close();
216: }
217:
218: public static void main(String[] args) {
219: junit.textui.TestRunner.run(SanityTest.class);
220: }
221: }
|