1 16 17 package org.springframework.jdbc.object; 18 19 import java.sql.Types ; 20 21 import javax.sql.DataSource ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.dao.InvalidDataAccessApiUsageException; 26 import org.springframework.jdbc.core.JdbcTemplate; 27 import org.springframework.jdbc.core.SqlParameter; 28 import org.springframework.jdbc.datasource.DriverManagerDataSource; 29 30 33 public class RdbmsOperationTests extends TestCase { 34 35 public void testEmptySql() { 36 TestRdbmsOperation operation = new TestRdbmsOperation(); 37 try { 38 operation.compile(); 39 fail("Shouldn't allow compiling without sql statement"); 40 } 41 catch (InvalidDataAccessApiUsageException idaauex) { 42 } 44 } 45 46 public void testSetTypeAfterCompile() { 47 TestRdbmsOperation operation = new TestRdbmsOperation(); 48 operation.setDataSource(new DriverManagerDataSource()); 49 operation.setSql("select * from mytable"); 50 operation.compile(); 51 try { 52 operation.setTypes(new int[] {Types.INTEGER }); 53 fail("Shouldn't allow setting parameters after compile"); 54 } 55 catch (InvalidDataAccessApiUsageException idaauex) { 56 } 58 } 59 60 public void testDeclareParameterAfterCompile() { 61 TestRdbmsOperation operation = new TestRdbmsOperation(); 62 operation.setDataSource(new DriverManagerDataSource()); 63 operation.setSql("select * from mytable"); 64 operation.compile(); 65 try { 66 operation.declareParameter(new SqlParameter(Types.INTEGER)); 67 fail("Shouldn't allow setting parameters after compile"); 68 } 69 catch (InvalidDataAccessApiUsageException idaauex) { 70 } 72 } 73 74 public void testTooFewParameters() { 75 TestRdbmsOperation operation = new TestRdbmsOperation(); 76 operation.setSql("select * from mytable"); 77 operation.setTypes(new int[] { Types.INTEGER }); 78 try { 79 operation.validateParameters(null); 80 fail("Shouldn't validate without enough parameters"); 81 } 82 catch (InvalidDataAccessApiUsageException idaauex) { 83 } 85 } 86 87 public void testOperationConfiguredViaJdbcTemplateMustGetDataSource() throws Exception { 88 try { 89 TestRdbmsOperation operation = new TestRdbmsOperation(); 90 operation.setSql("foo"); 91 operation.compile(); 92 fail("Can't compile without providing a DataSource for the JdbcTemplate"); 93 } 94 catch (InvalidDataAccessApiUsageException ex) { 95 assertTrue(ex.getMessage().indexOf("ataSource") != -1); 98 } 99 } 100 101 public void testTooManyParameters() { 102 TestRdbmsOperation operation = new TestRdbmsOperation(); 103 operation.setSql("select * from mytable"); 104 try { 105 operation.validateParameters(new Object [] { new Integer (1), new Integer (2) }); 106 fail("Shouldn't validate with too many parameters"); 107 } 108 catch (InvalidDataAccessApiUsageException idaauex) { 109 } 111 } 112 113 public void testCompileTwice() { 114 TestRdbmsOperation operation = new TestRdbmsOperation(); 115 operation.setDataSource(new DriverManagerDataSource()); 116 operation.setSql("select * from mytable"); 117 operation.setTypes(null); 118 operation.compile(); 119 operation.compile(); 120 } 121 122 public void testEmptyDataSource() { 123 SqlOperation operation = new SqlOperation() { 124 }; 125 operation.setSql("select * from mytable"); 126 try { 127 operation.compile(); 128 fail("Shouldn't allow compiling without data source"); 129 } catch (InvalidDataAccessApiUsageException idaauex) { 130 } 132 } 133 134 public void testParameterPropagation() { 135 SqlOperation operation = new SqlOperation() { 136 }; 137 DataSource ds = new DriverManagerDataSource(); 138 operation.setDataSource(ds); 139 operation.setFetchSize(10); 140 operation.setMaxRows(20); 141 JdbcTemplate jt = operation.getJdbcTemplate(); 142 assertEquals(ds, jt.getDataSource()); 143 assertEquals(10, jt.getFetchSize()); 144 assertEquals(20, jt.getMaxRows()); 145 } 146 147 148 private static class TestRdbmsOperation extends RdbmsOperation { 149 150 protected void compileInternal() { 151 } 152 } 153 154 } 155 | Popular Tags |