KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > TestExpectationSqlRow


1 package test.mockobjects;
2
3 import com.mockobjects.sql.ExpectationSqlRow;
4 import com.mockobjects.util.TestCaseMo;
5 import junit.framework.AssertionFailedError;
6
7 import java.sql.SQLException JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 public class TestExpectationSqlRow extends TestCaseMo {
12     private static final Class JavaDoc THIS = TestExpectationSqlRow.class;
13     private ExpectationSqlRow row = new ExpectationSqlRow("sql row");
14
15     public TestExpectationSqlRow(String JavaDoc name) {
16         super(name);
17     }
18
19     public void testIndexedValues() throws SQLException JavaDoc {
20         row.addExpectedIndexedValues(new Object JavaDoc[] { "one", "two" });
21
22         assertEquals("should have value", "one", row.get(1));
23         assertEquals("should have value", "two", row.get(2));
24         row.verify();
25     }
26
27     public void testConstructorIndexedValues() throws SQLException JavaDoc {
28         ExpectationSqlRow aRow =
29             new ExpectationSqlRow("row1", new Object JavaDoc[] { "one", "two" });
30
31         assertEquals("should have value", "one", aRow.get(1));
32         assertEquals("should have value", "two", aRow.get(2));
33         aRow.verify();
34     }
35
36     public void testNamedValues() throws SQLException JavaDoc {
37         row.addExpectedNamedValues(
38                 new String JavaDoc[] {"col1", "col2" },
39                 new Object JavaDoc[] {"one", "two" });
40         assertEquals("should have value", "two", row.get("col2"));
41         assertEquals("should have value", "one", row.get("col1"));
42         row.verify();
43     }
44
45     public void testConstructorNamedValues() throws SQLException JavaDoc {
46         ExpectationSqlRow aRow =
47             new ExpectationSqlRow("row1",
48                 new String JavaDoc[] {"col1", "col2" },
49                 new Object JavaDoc[] { "one", "two" });
50
51         assertEquals("should have value", "two", aRow.get("col2"));
52         assertEquals("should have value", "one", aRow.get("col1"));
53         aRow.verify();
54     }
55
56     public void testNamedValuesFromMap() throws SQLException JavaDoc {
57         Map JavaDoc values = new HashMap JavaDoc();
58         values.put("col1", "one");
59         values.put("col2", "two");
60
61         row.addExpectedNamedValues(values);
62         assertEquals("should have value", "two", row.get("col2"));
63         assertEquals("should have value", "one", row.get("col1"));
64         row.verify();
65     }
66
67     public void testNoValues() {
68         row.verify();
69     }
70
71     public void testFailInvalidIndex() throws SQLException JavaDoc {
72         row.addExpectedIndexedValues(new Object JavaDoc[] { "one" });
73         boolean assertionFailed = false;
74         try {
75             row.get(3);
76         } catch (AssertionFailedError ex) {
77             assertionFailed = true;
78         }
79         assertTrue("Should have failed", assertionFailed);
80     }
81
82     public void testFailInvalidKey() throws SQLException JavaDoc {
83         row.addExpectedNamedValues(new String JavaDoc[] {"col1" }, new Object JavaDoc[] {"one" });
84         boolean assertionFailed = false;
85         try {
86             row.get("col2");
87         } catch (AssertionFailedError ex) {
88             assertionFailed = true;
89         }
90         assertTrue("Should have failed", assertionFailed);
91     }
92
93     public void testFailEmptyRow() throws SQLException JavaDoc {
94         boolean assertionFailed = false;
95         try {
96             row.get(1);
97         } catch (AssertionFailedError ex) {
98             assertionFailed = true;
99         }
100         assertTrue("Should have failed", assertionFailed);
101     }
102
103     public void testIndexNotGot() throws SQLException JavaDoc {
104         row.addExpectedIndexedValues(new Object JavaDoc[] { "one" });
105         boolean assertionFailed = false;
106         try {
107             row.verify();
108         } catch (AssertionFailedError ex) {
109             assertionFailed = true;
110         }
111         assertTrue("Should have failed", assertionFailed);
112     }
113
114     public void testThrowExceptionOnGet() {
115         row.addExpectedIndexedValues(new Object JavaDoc[] {new SQLException JavaDoc("Mock SQL Excpeption")});
116         try {
117             row.get(1);
118             fail("Should have thrown exception");
119         } catch (SQLException JavaDoc expected) {
120         }
121     }
122 }
123
Popular Tags