001: package com.mockrunner.test.jdbc;
002:
003: import java.util.Arrays;
004:
005: import junit.framework.TestCase;
006:
007: import com.mockrunner.jdbc.ArrayResultSetFactory;
008: import com.mockrunner.jdbc.StringValuesTable;
009: import com.mockrunner.mock.jdbc.MockResultSet;
010:
011: /**
012: * Exercises the published contract for <code>ArrayResultSetFactory</code>
013: * instances.
014: *
015: * @author Erick G. Reid
016: */
017: public class ArrayResultSetFactoryTest extends TestCase {
018: private ArrayResultSetFactory arrayResultSetFactory;
019: private StringValuesTable stringTable;
020: private String[] columnNames;
021: private String[][] stringMatrix;
022:
023: /**
024: * Set up the test fixture.
025: */
026: public void setUp() throws Exception {
027: super .setUp();
028: columnNames = new String[] { "id", "name", "address" };
029: stringMatrix = new String[][] {
030: new String[] { "1", "moe", "123 main" },
031: new String[] { "2", "larry", "123 main" },
032: new String[] { "3", "curly", "123 main" }, };
033: stringTable = new StringValuesTable("table", columnNames,
034: stringMatrix);
035: arrayResultSetFactory = new ArrayResultSetFactory(columnNames,
036: stringMatrix);
037: }
038:
039: /**
040: * Tear down the test fixture.
041: */
042: public void tearDown() throws Exception {
043: super .tearDown();
044: arrayResultSetFactory = null;
045: }
046:
047: /**
048: * Ensures that the contructors published for
049: * <code>ArrayResultSetFactory</code> fulfill their contract.
050: *
051: * @throws Exception is an error occurs during testing.
052: */
053: public void testConstructors() throws Exception {
054: try {
055: arrayResultSetFactory = new ArrayResultSetFactory(
056: (StringValuesTable) null);
057: fail("an IllegalArgumentException should have thrown");
058: } catch (IllegalArgumentException expected) {
059: //expected exception
060: }
061: try {
062: arrayResultSetFactory = new ArrayResultSetFactory(
063: (String[][]) null);
064: fail("an IllegalArgumentException should have thrown");
065: } catch (IllegalArgumentException expected) {
066: //expected exception
067: }
068: try {
069: arrayResultSetFactory = new ArrayResultSetFactory(null,
070: null);
071: fail("an IllegalArgumentException should have thrown");
072: } catch (IllegalArgumentException expected) {
073: //expected exception
074: }
075: try {
076: arrayResultSetFactory = new ArrayResultSetFactory(
077: columnNames, null);
078: fail("an IllegalArgumentException should have thrown");
079: } catch (IllegalArgumentException expected) {
080: //expected exception
081: }
082: try {
083: arrayResultSetFactory = new ArrayResultSetFactory(
084: new String[] { "", "", null }, stringMatrix);
085: fail("an IllegalArgumentException should have thrown");
086: } catch (IllegalArgumentException expected) {
087: //expected exception
088: }
089: try {
090: arrayResultSetFactory = new ArrayResultSetFactory(
091: columnNames, new String[][] { null,
092: new String[] { "", "", "" } });
093: fail("an IllegalArgumentException should have thrown");
094: } catch (IllegalArgumentException expected) {
095: //expected exception
096: }
097: try {
098: arrayResultSetFactory = new ArrayResultSetFactory(
099: columnNames, new String[][] { new String[] { "",
100: null, "" } });
101: fail("an IllegalArgumentException should have thrown");
102: } catch (IllegalArgumentException expected) {
103: //expected exception
104: }
105: try {
106: arrayResultSetFactory = new ArrayResultSetFactory(
107: columnNames, new String[][] {
108: new String[] { "", "" },
109: new String[] { "", "", "" } });
110: fail("an IllegalArgumentException should have thrown");
111: } catch (IllegalArgumentException expected) {
112: //expected exception
113: }
114: try {
115: arrayResultSetFactory = new ArrayResultSetFactory(
116: columnNames, new String[][] {
117: new String[] { "", "", "" },
118: new String[] { "", "" } });
119: fail("an IllegalArgumentException should have thrown");
120: } catch (IllegalArgumentException expected) {
121: //expected exception
122: }
123: arrayResultSetFactory = new ArrayResultSetFactory(stringTable);
124: arrayResultSetFactory = new ArrayResultSetFactory(stringMatrix);
125: arrayResultSetFactory = new ArrayResultSetFactory(columnNames,
126: stringMatrix);
127: }
128:
129: /**
130: * Ensures that <code>MockResultSet create(id)</code> fulfills its published contract.
131: *
132: * @throws Exception is an error occurs during testing.
133: */
134: public void testCreate() throws Exception {
135: MockResultSet mockResultSet = null;
136: try {
137: mockResultSet = arrayResultSetFactory.create(null);
138: fail("an IllegalArgumentException should have thrown");
139: } catch (IllegalArgumentException expected) {
140: //expected exception
141: }
142: mockResultSet = arrayResultSetFactory.create("");
143: assertEquals(columnNames.length, mockResultSet.getColumnCount());
144: for (int ii = 0; ii < columnNames.length; ii++) {
145: assertNotNull(mockResultSet.getColumn(columnNames[ii]));
146: }
147: for (int ii = 0; ii < stringMatrix.length; ii++) {
148: assertEquals(Arrays.asList(stringMatrix[ii]), mockResultSet
149: .getRow(ii + 1));
150: }
151: }
152: }
|