001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.sql.tests.internal.rowset;
019:
020: import java.sql.SQLException;
021:
022: import javax.sql.RowSetMetaData;
023: import javax.sql.rowset.RowSetMetaDataImpl;
024: import javax.sql.rowset.spi.SyncResolver;
025:
026: import org.apache.harmony.sql.internal.rowset.CachedRow;
027: import org.apache.harmony.sql.internal.rowset.SyncResolverImpl;
028:
029: public class SyncResolverTest extends CachedRowSetTestCase {
030: @Override
031: public void setUp() throws Exception {
032: super .setUp();
033: }
034:
035: @Override
036: public void tearDown() throws Exception {
037: super .tearDown();
038: }
039:
040: public void testNotSupportMethods() throws Exception {
041: /*
042: * TODO uncomment below fragment code when Harmony support detect
043: * conflict, so the test can run on both RI and Harmony
044: */
045: // CachedRowSet copy = crset.createCopy();
046: //
047: // copy.absolute(3);
048: // crset.absolute(3);
049: //
050: // copy.updateString(2, "updated");
051: // assertEquals("updated", copy.getString(2));
052: // assertEquals("test3", crset.getString(2));
053: //
054: // copy.updateRow();
055: // copy.acceptChanges();
056: //
057: // assertEquals(copy.getString(2), "updated");
058: // assertEquals(crset.getString(2), "test3");
059: //
060: // crset.updateString(2, "again");
061: //
062: // assertEquals(copy.getString(2), "updated");
063: // assertEquals(crset.getString(2), "again");
064: //
065: // crset.updateRow();
066: //
067: // SyncProviderException ex = null;
068: // try {
069: // crset.acceptChanges(conn);
070: // } catch (SyncProviderException e) {
071: // ex = e;
072: // }
073: //
074: // SyncResolver resolver = ex.getSyncResolver();
075: SyncResolver resolver = new SyncResolverImpl(null);
076:
077: try {
078: resolver.absolute(1);
079: fail("Should throw UnsupportedOperationException");
080: } catch (UnsupportedOperationException e) {
081: // expected
082: }
083: try {
084: resolver.relative(1);
085: fail("Should throw UnsupportedOperationException");
086: } catch (UnsupportedOperationException e) {
087: // expected
088: }
089: try {
090: resolver.next();
091: fail("Should throw UnsupportedOperationException");
092: } catch (UnsupportedOperationException e) {
093: // expected
094: }
095: try {
096: resolver.previous();
097: fail("Should throw UnsupportedOperationException");
098: } catch (UnsupportedOperationException e) {
099: // expected
100: }
101: try {
102: resolver.isAfterLast();
103: fail("Should throw UnsupportedOperationException");
104: } catch (UnsupportedOperationException e) {
105: // expected
106: }
107: try {
108: resolver.isBeforeFirst();
109: fail("Should throw UnsupportedOperationException");
110: } catch (UnsupportedOperationException e) {
111: // expected
112: }
113: try {
114: resolver.isFirst();
115: fail("Should throw UnsupportedOperationException");
116: } catch (UnsupportedOperationException e) {
117: // expected
118: }
119: try {
120: resolver.isLast();
121: fail("Should throw UnsupportedOperationException");
122: } catch (UnsupportedOperationException e) {
123: // expected
124: }
125: try {
126: resolver.getMetaData();
127: fail("Should throw UnsupportedOperationException");
128: } catch (UnsupportedOperationException e) {
129: // expected
130: }
131:
132: try {
133: resolver.getString(2);
134: fail("Should throw UnsupportedOperationException");
135: } catch (UnsupportedOperationException e) {
136: // expected
137: }
138:
139: try {
140: resolver.getCursorName();
141: fail("Should throw UnsupportedOperationException");
142: } catch (UnsupportedOperationException e) {
143: // expected
144: }
145:
146: try {
147: resolver.updateString(2, "hello");
148: fail("Should throw UnsupportedOperationException");
149: } catch (UnsupportedOperationException e) {
150: // expected
151: }
152:
153: try {
154: resolver.rowDeleted();
155: fail("Should throw UnsupportedOperationException");
156: } catch (UnsupportedOperationException e) {
157: // expected
158: }
159:
160: try {
161: resolver.rowInserted();
162: fail("Should throw UnsupportedOperationException");
163: } catch (UnsupportedOperationException e) {
164: // expected
165: }
166:
167: try {
168: resolver.rowUpdated();
169: fail("Should throw UnsupportedOperationException");
170: } catch (UnsupportedOperationException e) {
171: // expected
172: }
173:
174: try {
175: resolver.getWarnings();
176: fail("Should throw UnsupportedOperationException");
177: } catch (UnsupportedOperationException e) {
178: // expected
179: }
180:
181: try {
182: resolver.getStatement();
183: fail("Should throw UnsupportedOperationException");
184: } catch (UnsupportedOperationException e) {
185: // expected
186: }
187:
188: try {
189: resolver.findColumn("ID");
190: fail("Should throw UnsupportedOperationException");
191: } catch (UnsupportedOperationException e) {
192: // expected
193: }
194:
195: try {
196: resolver.wasNull();
197: fail("Should throw UnsupportedOperationException");
198: } catch (UnsupportedOperationException e) {
199: // expected
200: }
201:
202: try {
203: resolver.moveToCurrentRow();
204: fail("Should throw UnsupportedOperationException");
205: } catch (UnsupportedOperationException e) {
206: // expected
207: }
208:
209: try {
210: resolver.moveToInsertRow();
211: fail("Should throw UnsupportedOperationException");
212: } catch (UnsupportedOperationException e) {
213: // expected
214: }
215:
216: try {
217: resolver.refreshRow();
218: fail("Should throw UnsupportedOperationException");
219: } catch (UnsupportedOperationException e) {
220: // expected
221: }
222:
223: try {
224: resolver.execute();
225: fail("Should throw UnsupportedOperationException");
226: } catch (UnsupportedOperationException e) {
227: // expected
228: }
229:
230: try {
231: resolver.clearWarnings();
232: fail("Should throw UnsupportedOperationException");
233: } catch (UnsupportedOperationException e) {
234: // expected
235: }
236:
237: try {
238: resolver.deleteRow();
239: fail("Should throw UnsupportedOperationException");
240: } catch (UnsupportedOperationException e) {
241: // expected
242: }
243:
244: try {
245: resolver.insertRow();
246: fail("Should throw UnsupportedOperationException");
247: } catch (UnsupportedOperationException e) {
248: // expected
249: }
250:
251: try {
252: resolver.updateRow();
253: fail("Should throw UnsupportedOperationException");
254: } catch (UnsupportedOperationException e) {
255: // expected
256: }
257:
258: try {
259: resolver.cancelRowUpdates();
260: fail("Should throw UnsupportedOperationException");
261: } catch (UnsupportedOperationException e) {
262: // expected
263: }
264:
265: try {
266: resolver.close();
267: fail("Should throw UnsupportedOperationException");
268: } catch (UnsupportedOperationException e) {
269: // expected
270: }
271: }
272:
273: public void testGetConflictValue() throws Exception {
274:
275: RowSetMetaData metadata = new RowSetMetaDataImpl();
276: metadata.setColumnCount(DEFAULT_COLUMN_COUNT);
277:
278: SyncResolverImpl resolver = new SyncResolverImpl(metadata);
279: resolver.addConflictRow(new CachedRow(
280: new Object[DEFAULT_COLUMN_COUNT]), 1,
281: SyncResolver.INSERT_ROW_CONFLICT);
282:
283: resolver.addConflictRow(new CachedRow(
284: new Object[DEFAULT_COLUMN_COUNT]), 2,
285: SyncResolver.INSERT_ROW_CONFLICT);
286:
287: // before call nextConflict
288: try {
289: resolver.getConflictValue(1);
290: fail("Should throw SQLException");
291: } catch (SQLException e) {
292: // expected, Invalid cursor position
293: }
294:
295: try {
296: resolver.getConflictValue(-1);
297: fail("Should throw SQLException");
298: } catch (SQLException e) {
299: // expected, Invalid column index
300: }
301: try {
302: resolver.getConflictValue("not exist");
303: fail("Should throw SQLException");
304: } catch (SQLException e) {
305: // expected, Invalid column name
306: }
307:
308: assertTrue(resolver.nextConflict());
309:
310: for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
311: assertNull(resolver.getConflictValue(i));
312: }
313:
314: assertTrue(resolver.nextConflict());
315:
316: for (int i = 1; i <= DEFAULT_COLUMN_COUNT; ++i) {
317: assertNull(resolver.getConflictValue(i));
318: }
319:
320: assertFalse(resolver.nextConflict());
321: assertEquals(0, resolver.getRow());
322:
323: /*
324: * ri throw SQLException after call nextConflict again, it's not
325: * reasonable
326: */
327: try {
328: resolver.getConflictValue(1);
329: fail("Should throw SQLException");
330: } catch (SQLException e) {
331: // expected Invalid cursor position
332: }
333:
334: }
335:
336: public void testNextPreviousConflict() throws Exception {
337:
338: RowSetMetaData metadata = new RowSetMetaDataImpl();
339: metadata.setColumnCount(DEFAULT_COLUMN_COUNT);
340:
341: SyncResolverImpl resolver = new SyncResolverImpl(metadata);
342: resolver.addConflictRow(new CachedRow(
343: new Object[DEFAULT_COLUMN_COUNT]), 1,
344: SyncResolver.INSERT_ROW_CONFLICT);
345:
346: resolver.addConflictRow(new CachedRow(
347: new Object[DEFAULT_COLUMN_COUNT]), 2,
348: SyncResolver.INSERT_ROW_CONFLICT);
349:
350: assertTrue(resolver.nextConflict());
351: assertTrue(resolver.nextConflict());
352: assertFalse(resolver.nextConflict());
353: assertFalse(resolver.nextConflict());
354:
355: assertTrue(resolver.previousConflict());
356: assertTrue(resolver.previousConflict());
357: assertFalse(resolver.previousConflict());
358: assertFalse(resolver.previousConflict());
359: }
360:
361: public void testGetStatus() throws Exception {
362:
363: RowSetMetaData metadata = new RowSetMetaDataImpl();
364: metadata.setColumnCount(DEFAULT_COLUMN_COUNT);
365:
366: SyncResolverImpl resolver = new SyncResolverImpl(metadata);
367: resolver.addConflictRow(new CachedRow(
368: new Object[DEFAULT_COLUMN_COUNT]), 1,
369: SyncResolver.INSERT_ROW_CONFLICT);
370:
371: resolver.addConflictRow(new CachedRow(
372: new Object[DEFAULT_COLUMN_COUNT]), 2,
373: SyncResolver.INSERT_ROW_CONFLICT);
374:
375: try {
376: resolver.getStatus();
377: fail("Should throw NullPointerException");
378: } catch (NullPointerException e) {
379: // expected
380: }
381:
382: assertTrue(resolver.nextConflict());
383: assertEquals(SyncResolver.INSERT_ROW_CONFLICT, resolver
384: .getStatus());
385: assertTrue(resolver.nextConflict());
386: assertEquals(SyncResolver.INSERT_ROW_CONFLICT, resolver
387: .getStatus());
388: assertFalse(resolver.nextConflict());
389:
390: try {
391: resolver.getStatus();
392: fail("Should throw NullPointerException");
393: } catch (NullPointerException e) {
394: // expected
395: }
396:
397: }
398: }
|