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 ; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 11 public class TestExpectationSqlRow extends TestCaseMo { 12 private static final Class THIS = TestExpectationSqlRow.class; 13 private ExpectationSqlRow row = new ExpectationSqlRow("sql row"); 14 15 public TestExpectationSqlRow(String name) { 16 super(name); 17 } 18 19 public void testIndexedValues() throws SQLException { 20 row.addExpectedIndexedValues(new Object [] { "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 { 28 ExpectationSqlRow aRow = 29 new ExpectationSqlRow("row1", new Object [] { "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 { 37 row.addExpectedNamedValues( 38 new String [] {"col1", "col2" }, 39 new Object [] {"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 { 46 ExpectationSqlRow aRow = 47 new ExpectationSqlRow("row1", 48 new String [] {"col1", "col2" }, 49 new Object [] { "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 { 57 Map values = new HashMap (); 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 { 72 row.addExpectedIndexedValues(new Object [] { "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 { 83 row.addExpectedNamedValues(new String [] {"col1" }, new Object [] {"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 { 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 { 104 row.addExpectedIndexedValues(new Object [] { "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 [] {new SQLException ("Mock SQL Excpeption")}); 116 try { 117 row.get(1); 118 fail("Should have thrown exception"); 119 } catch (SQLException expected) { 120 } 121 } 122 } 123 | Popular Tags |