001: package com.mockrunner.test.jdbc;
002:
003: import junit.framework.TestCase;
004:
005: import com.mockrunner.jdbc.StringValuesTable;
006:
007: /**
008: * Exercises the published contract for <code>StringValuesTable</code>
009: * instances.
010: *
011: * @author Erick G. Reid
012: */
013: public class StringValuesTableTest extends TestCase {
014: private StringValuesTable stringValuesTable;
015: private String tableName;
016: private String[] columnNames;
017: private String[][] stringMatrix;
018:
019: /**
020: * Set up the test fixture.
021: */
022: public void setUp() throws Exception {
023: super .setUp();
024: tableName = "initech-employees";
025: columnNames = new String[] { "id", "name", "address" };
026: stringMatrix = new String[][] {
027: new String[] { "1", "gibbons", "peter" },
028: new String[] { "2", "lumbergh", "bill" },
029: new String[] { "3", "waddams", "milton" }, };
030: stringValuesTable = new StringValuesTable(tableName,
031: columnNames, stringMatrix);
032: }
033:
034: /**
035: * Tear down the test fixture.
036: */
037: public void tearDown() throws Exception {
038: super .tearDown();
039: columnNames = null;
040: stringMatrix = null;
041: stringValuesTable = null;
042: }
043:
044: /**
045: * Ensures that the contructors published for <code>StringValuesTable</code>
046: * fulfill their contract.
047: *
048: * @throws Exception is an error occurs during testing.
049: */
050: public void testConstructors() throws Exception {
051: try {
052: stringValuesTable = new StringValuesTable(null,
053: columnNames, stringMatrix);
054: fail("an IllegalArgumentException should have thrown");
055: } catch (IllegalArgumentException expected) {
056: //expected exception
057: }
058: try {
059: stringValuesTable = new StringValuesTable("", columnNames,
060: stringMatrix);
061: fail("an IllegalArgumentException should have thrown");
062: } catch (IllegalArgumentException expected) {
063: //expected exception
064: }
065: try {
066: stringValuesTable = new StringValuesTable(" ", columnNames,
067: stringMatrix);
068: fail("an IllegalArgumentException should have thrown");
069: } catch (IllegalArgumentException expected) {
070: //expected exception
071: }
072: try {
073: stringValuesTable = new StringValuesTable(tableName,
074: columnNames, null);
075: fail("an IllegalArgumentException should have thrown");
076: } catch (IllegalArgumentException expected) {
077: //expected exception
078: }
079: try {
080: // test null column name:
081: //
082: stringValuesTable = new StringValuesTable(tableName,
083: new String[] { "id", null, "lastname" },
084: stringMatrix);
085: fail("an IllegalArgumentException should have thrown");
086: } catch (IllegalArgumentException expected) {
087: //expected exception
088: }
089: try {
090: // test invalid number of columns:
091: //
092: stringValuesTable = new StringValuesTable(tableName,
093: new String[] { "id", "lastname" }, stringMatrix);
094: fail("an IllegalArgumentException should have thrown");
095: } catch (IllegalArgumentException expected) {
096: //expected exception
097: }
098: try {
099: // test duplicate columns:
100: //
101: stringValuesTable = new StringValuesTable(tableName,
102: new String[] { "id", "id", "lastname" },
103: stringMatrix);
104: fail("an IllegalArgumentException should have thrown");
105: } catch (IllegalArgumentException expected) {
106: //expected exception
107: }
108: try {
109: // test invalid number of columns:
110: //
111: stringValuesTable = new StringValuesTable(
112: tableName,
113: columnNames,
114: new String[][] {
115: new String[] { "1", "gibbons", "peter" },
116: new String[] { "2", "lumbergh", },
117: new String[] { "3", "waddams", "milton" }, });
118: fail("an IllegalArgumentException should have thrown");
119: } catch (IllegalArgumentException expected) {
120: //expected exception
121: }
122: try {
123: // test invalid number of columns:
124: //
125: stringValuesTable = new StringValuesTable(
126: tableName,
127: null,
128: new String[][] {
129: new String[] { "1", "gibbons", "peter" },
130: new String[] { "2", "lumbergh", },
131: new String[] { "3", "waddams", "milton" }, });
132: fail("an IllegalArgumentException should have thrown");
133: } catch (IllegalArgumentException expected) {
134: //expected exception
135: }
136: try {
137: // test null matrix element:
138: //
139: stringValuesTable = new StringValuesTable(
140: tableName,
141: columnNames,
142: new String[][] {
143: new String[] { "1", "gibbons", "peter" },
144: new String[] { "2", "lumbergh", null },
145: new String[] { "3", "waddams", "milton" }, });
146: fail("an IllegalArgumentException should have thrown");
147: } catch (IllegalArgumentException expected) {
148: //expected exception
149: }
150: try {
151: // test invalid number of columns:
152: //
153: stringValuesTable = new StringValuesTable(
154: tableName,
155: null,
156: new String[][] {
157: new String[] { "1", "gibbons", "peter" },
158: new String[] { "2", "lumbergh", null },
159: new String[] { "3", "waddams", "milton" }, });
160: fail("an IllegalArgumentException should have thrown");
161: } catch (IllegalArgumentException expected) {
162: //expected exception
163: }
164: try {
165: // test violate minimum matrix size constraint:
166: //
167: stringValuesTable = new StringValuesTable(tableName, null,
168: new String[0][0]);
169: fail("an IllegalArgumentException should have thrown");
170: } catch (IllegalArgumentException expected) {
171: //expected exception
172: }
173: }
174:
175: /**
176: * Ensures that published contract for
177: * <code>String[] getColumn(String)</code> is upheld.
178: *
179: * @throws Exception is an error occurs during testing.
180: */
181: public void testGetColumnByName() throws Exception {
182: try {
183: stringValuesTable.getColumn(null);
184: fail("an IllegalArgumentException should have thrown");
185: } catch (IllegalArgumentException expected) {
186: }
187: try {
188: stringValuesTable.getColumn("");
189: fail("an IllegalArgumentException should have thrown");
190: } catch (IllegalArgumentException expected) {
191: //expected exception
192: }
193: // assert expected values from valid calls:
194: //
195: for (int column = 0; column < columnNames.length; column++) {
196: String[] values = stringValuesTable
197: .getColumn(columnNames[column]);
198: for (int row = 0; row < values.length; row++) {
199: assertEquals(values[row], stringMatrix[row][column]);
200: }
201: }
202: // exercise tables with no columns:
203: //
204: stringValuesTable = new StringValuesTable(tableName, null,
205: stringMatrix);
206: try {
207: stringValuesTable.getColumn("id");
208: fail("an IllegalArgumentException should have thrown");
209: } catch (IllegalArgumentException expected) {
210: //expected exception
211: }
212: stringValuesTable = new StringValuesTable(tableName,
213: stringMatrix);
214: try {
215: stringValuesTable.getColumn("id");
216: fail("an IllegalArgumentException should have thrown");
217: } catch (IllegalArgumentException expected) {
218: }
219: }
220:
221: /**
222: * Ensures that published contract for <code>String[] getColumn(int)</code>
223: * is upheld.
224: *
225: * @throws Exception is an error occurs during testing.
226: */
227: public void testGetColumnByNumber() throws Exception {
228: try {
229: stringValuesTable.getColumn(0);
230: fail("an IllegalArgumentException should have thrown");
231: } catch (IllegalArgumentException expected) {
232: //expected exception
233: }
234: try {
235: stringValuesTable.getColumn(-1);
236: fail("an IllegalArgumentException should have thrown");
237: } catch (IllegalArgumentException expected) {
238: //expected exception
239: }
240: try {
241: stringValuesTable.getColumn(columnNames.length + 1);
242: fail("an IllegalArgumentException should have thrown");
243: } catch (IllegalArgumentException expected) {
244: //expected exception
245: }
246: // assert expected values from valid calls:
247: //
248: for (int column = 1; column <= columnNames.length; column++) {
249: String[] values = stringValuesTable.getColumn(column);
250: for (int row = 0; row < values.length; row++) {
251: assertEquals(values[row], stringMatrix[row][column - 1]);
252: }
253: }
254: }
255:
256: /**
257: * Ensures that published contract for
258: * <code>String[] getColumnNames()</code> is upheld.
259: *
260: * @throws Exception is an error occurs during testing.
261: */
262: public void testGetColumnNames() throws Exception {
263: String[] columnNames = stringValuesTable.getColumnNames();
264: assertEquals(columnNames.length, columnNames.length);
265: for (int i = 0; i < columnNames.length; i++) {
266: assertEquals(columnNames[i], columnNames[i]);
267: }
268: // exercise tables with no columns:
269: //
270: stringValuesTable = new StringValuesTable(tableName, null,
271: stringMatrix);
272: assertTrue(stringValuesTable.getColumnNames().length == 0);
273: stringValuesTable = new StringValuesTable(tableName,
274: stringMatrix);
275: assertTrue(stringValuesTable.getColumnNames().length == 0);
276: }
277:
278: /**
279: * Ensures that published contract for
280: * <code>String getItem(int, String)</code> is upheld.
281: *
282: * @throws Exception is an error occurs during testing.
283: */
284: public void testGetItemColumnName() throws Exception {
285: try {
286: stringValuesTable.getItem(1, null);
287: fail("an IllegalArgumentException should have thrown");
288: } catch (IllegalArgumentException expected) {
289: //expected exception
290: }
291: try {
292: stringValuesTable.getItem(1, "");
293: fail("an IllegalArgumentException should have thrown");
294: } catch (IllegalArgumentException expected) {
295: //expected exception
296: }
297: try {
298: stringValuesTable.getItem(-1, columnNames[0]);
299: fail("an IllegalArgumentException should have thrown");
300: } catch (IllegalArgumentException expected) {
301: //expected exception
302: }
303: // assert expected values from valid calls:
304: //
305: for (int row = 1; row <= stringMatrix.length; row++) {
306: for (int column = 1; column <= columnNames.length; column++) {
307: assertEquals(stringMatrix[row - 1][column - 1],
308: stringValuesTable.getItem(row,
309: columnNames[column - 1]));
310: }
311: }
312: }
313:
314: /**
315: * Ensures that published contract for <code>String getItem(int, int)</code>
316: * is upheld.
317: *
318: * @throws Exception is an error occurs during testing.
319: */
320: public void testGetItemColumnNumber() throws Exception {
321: try {
322: stringValuesTable.getItem(1, -1);
323: fail("an IllegalArgumentException should have thrown");
324: } catch (IllegalArgumentException expected) {
325: //expected exception
326: }
327: try {
328: stringValuesTable.getItem(1, 0);
329: fail("an IllegalArgumentException should have thrown");
330: } catch (IllegalArgumentException expected) {
331: //expected exception
332: }
333: try {
334: stringValuesTable.getItem(-1, 1);
335: fail("an IllegalArgumentException should have thrown");
336: } catch (IllegalArgumentException expected) {
337: //expected exception
338: }
339: // assert expected values from valid calls:
340: //
341: for (int row = 1; row <= stringMatrix.length; row++) {
342: for (int column = 1; column <= columnNames.length; column++) {
343: assertEquals(stringMatrix[row - 1][column - 1],
344: stringValuesTable.getItem(row, column));
345: }
346: }
347: }
348:
349: /**
350: * Ensures that published contract for <code>String getName()</code> is
351: * upheld.
352: *
353: * @throws Exception is an error occurs during testing.
354: */
355: public void testGetName() throws Exception {
356: assertEquals(tableName, stringValuesTable.getName());
357: }
358:
359: /**
360: * Ensures that published contract for <code>int getNumberOfColumns()</code>
361: * is upheld.
362: *
363: * @throws Exception is an error occurs during testing.
364: */
365: public void testGetNumberOfColumns() throws Exception {
366: assertEquals(columnNames.length, stringValuesTable
367: .getNumberOfColumns());
368: stringValuesTable = new StringValuesTable(tableName, null,
369: stringMatrix);
370: assertEquals(stringMatrix[0].length, stringValuesTable
371: .getNumberOfColumns());
372: stringValuesTable = new StringValuesTable(tableName,
373: stringMatrix);
374: assertEquals(stringMatrix[0].length, stringValuesTable
375: .getNumberOfColumns());
376: }
377:
378: /**
379: * Ensures that published contract for <code>int getNumberOfRows()</code>
380: * is upheld.
381: *
382: * @throws Exception is an error occurs during testing.
383: */
384: public void testGetNumberOfRows() throws Exception {
385: assertEquals(stringMatrix.length, stringValuesTable
386: .getNumberOfRows());
387: }
388:
389: /**
390: * Ensures that published contract for <code>String[] getRow(int)</code>
391: * is upheld.
392: *
393: * @throws Exception is an error occurs during testing.
394: */
395: public void testGetRow() throws Exception {
396: try {
397: stringValuesTable.getRow(0);
398: fail("an IllegalArgumentException should have thrown");
399: } catch (IllegalArgumentException expected) {
400: //expected exception
401: }
402: try {
403: stringValuesTable.getRow(-1);
404: fail("an IllegalArgumentException should have thrown");
405: } catch (IllegalArgumentException expected) {
406: //expected exception
407: }
408: try {
409: stringValuesTable.getColumn(stringMatrix.length + 1);
410: fail("an IllegalArgumentException should have thrown");
411: } catch (IllegalArgumentException expected) {
412: //expected exception
413: }
414: // assert expected values from valid calls:
415: //
416: for (int row = 1; row <= stringMatrix.length; row++) {
417: String[] values = stringValuesTable.getRow(row);
418: for (int column = 0; column < values.length; column++) {
419: assertEquals(stringMatrix[row - 1][column],
420: values[column]);
421: }
422: }
423: }
424:
425: /**
426: * Ensures that published contract for
427: * <code>boolean isValidColumnName(String)</code> is upheld.
428: *
429: * @throws Exception is an error occurs during testing.
430: */
431: public void testIsValidColumnName() throws Exception {
432: assertFalse(stringValuesTable.isValidColumnName(null));
433: assertFalse(stringValuesTable.isValidColumnName(""));
434: assertFalse(stringValuesTable.isValidColumnName(columnNames[0]
435: + "foo"));
436: for (int ii = 0; ii < columnNames.length; ii++) {
437: assertTrue(stringValuesTable
438: .isValidColumnName(columnNames[ii]));
439: }
440: }
441:
442: /**
443: * Ensures that published contract for
444: * <code>boolean isValidColumnNumber(int)</code> is upheld.
445: *
446: * @throws Exception is an error occurs during testing.
447: */
448: public void testIsValidColumnNumber() throws Exception {
449: assertFalse(stringValuesTable.isValidColumnNumber(0));
450: assertFalse(stringValuesTable.isValidColumnNumber(-1));
451: assertFalse(stringValuesTable
452: .isValidColumnNumber(columnNames.length + 1));
453: for (int ii = 1; ii <= stringValuesTable.getColumnNames().length; ii++) {
454: assertTrue(stringValuesTable.isValidColumnNumber(ii));
455: }
456: }
457:
458: /**
459: * Ensures that published contract for
460: * <code>boolean isValidRowNumber(int)</code> is upheld.
461: *
462: * @throws Exception is an error occurs during testing.
463: */
464: public void testIsValidRowNumber() throws Exception {
465: assertFalse(stringValuesTable.isValidRowNumber(0));
466: assertFalse(stringValuesTable.isValidRowNumber(-1));
467: assertFalse(stringValuesTable
468: .isValidRowNumber(stringMatrix.length + 1));
469: for (int ii = 1; ii <= stringValuesTable.getNumberOfRows(); ii++) {
470: assertTrue(stringValuesTable.isValidRowNumber(ii));
471: }
472: }
473: }
|