1 16 17 package org.springframework.jdbc.support; 18 19 import java.sql.Connection ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.sql.Statement ; 23 24 import javax.sql.DataSource ; 25 26 import junit.framework.TestCase; 27 import org.easymock.MockControl; 28 29 import org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer; 30 import org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer; 31 import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer; 32 import org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer; 33 34 38 public class DataFieldMaxValueIncrementerTests extends TestCase { 39 40 public void testHsqlMaxValueIncrementer() throws SQLException { 41 MockControl dsControl = MockControl.createControl(DataSource .class); 42 DataSource ds = (DataSource ) dsControl.getMock(); 43 MockControl conControl = MockControl.createControl(Connection .class); 44 Connection con = (Connection ) conControl.getMock(); 45 MockControl stmtControl = MockControl.createControl(Statement .class); 46 Statement stmt = (Statement ) stmtControl.getMock(); 47 MockControl rsControl = MockControl.createControl(ResultSet .class); 48 ResultSet rs = (ResultSet ) rsControl.getMock(); 49 50 ds.getConnection(); 51 dsControl.setReturnValue(con, 2); 52 con.createStatement(); 53 conControl.setReturnValue(stmt, 2); 54 stmt.executeUpdate("insert into myseq values(null)"); 55 stmtControl.setReturnValue(1, 6); 56 stmt.executeQuery("select max(identity()) from myseq"); 57 stmtControl.setReturnValue(rs, 6); 58 rs.next(); 59 rsControl.setReturnValue(true, 6); 60 for (long i = 0; i < 6; i++) { 61 rs.getLong(1); 62 rsControl.setReturnValue(i); 63 } 64 rs.close(); 65 rsControl.setVoidCallable(6); 66 stmt.executeUpdate("delete from myseq where seq < 2"); 67 stmtControl.setReturnValue(1); 68 stmt.executeUpdate("delete from myseq where seq < 5"); 69 stmtControl.setReturnValue(1); 70 stmt.close(); 71 stmtControl.setVoidCallable(2); 72 con.close(); 73 conControl.setVoidCallable(2); 74 75 dsControl.replay(); 76 conControl.replay(); 77 stmtControl.replay(); 78 rsControl.replay(); 79 80 HsqlMaxValueIncrementer incrementer = new HsqlMaxValueIncrementer(); 81 incrementer.setDataSource(ds); 82 incrementer.setIncrementerName("myseq"); 83 incrementer.setColumnName("seq"); 84 incrementer.setCacheSize(3); 85 incrementer.setPaddingLength(3); 86 incrementer.afterPropertiesSet(); 87 88 assertEquals(0, incrementer.nextIntValue()); 89 assertEquals(1, incrementer.nextLongValue()); 90 assertEquals("002", incrementer.nextStringValue()); 91 assertEquals(3, incrementer.nextIntValue()); 92 assertEquals(4, incrementer.nextLongValue()); 93 94 dsControl.verify(); 95 conControl.verify(); 96 stmtControl.verify(); 97 rsControl.verify(); 98 } 99 100 public void testMySQLMaxValueIncrementer() throws SQLException { 101 MockControl dsControl = MockControl.createControl(DataSource .class); 102 DataSource ds = (DataSource ) dsControl.getMock(); 103 MockControl conControl = MockControl.createControl(Connection .class); 104 Connection con = (Connection ) conControl.getMock(); 105 MockControl stmtControl = MockControl.createControl(Statement .class); 106 Statement stmt = (Statement ) stmtControl.getMock(); 107 MockControl rsControl = MockControl.createControl(ResultSet .class); 108 ResultSet rs = (ResultSet ) rsControl.getMock(); 109 110 ds.getConnection(); 111 dsControl.setReturnValue(con, 2); 112 con.createStatement(); 113 conControl.setReturnValue(stmt, 2); 114 stmt.executeUpdate("update myseq set seq = last_insert_id(seq + 2)"); 115 stmtControl.setReturnValue(1, 2); 116 stmt.executeQuery("select last_insert_id()"); 117 stmtControl.setReturnValue(rs, 2); 118 rs.next(); 119 rsControl.setReturnValue(true, 2); 120 rs.getLong(1); 121 rsControl.setReturnValue(2); 122 rs.getLong(1); 123 rsControl.setReturnValue(4); 124 rs.close(); 125 rsControl.setVoidCallable(2); 126 stmt.close(); 127 stmtControl.setVoidCallable(2); 128 con.close(); 129 conControl.setVoidCallable(2); 130 131 dsControl.replay(); 132 conControl.replay(); 133 stmtControl.replay(); 134 rsControl.replay(); 135 136 MySQLMaxValueIncrementer incrementer = new MySQLMaxValueIncrementer(); 137 incrementer.setDataSource(ds); 138 incrementer.setIncrementerName("myseq"); 139 incrementer.setColumnName("seq"); 140 incrementer.setCacheSize(2); 141 incrementer.setPaddingLength(1); 142 incrementer.afterPropertiesSet(); 143 144 assertEquals(1, incrementer.nextIntValue()); 145 assertEquals(2, incrementer.nextLongValue()); 146 assertEquals("3", incrementer.nextStringValue()); 147 assertEquals(4, incrementer.nextLongValue()); 148 149 dsControl.verify(); 150 conControl.verify(); 151 stmtControl.verify(); 152 rsControl.verify(); 153 } 154 155 public void testPostgreSQLSequenceMaxValueIncrementer() throws SQLException { 156 MockControl dsControl = MockControl.createControl(DataSource .class); 157 DataSource ds = (DataSource ) dsControl.getMock(); 158 MockControl conControl = MockControl.createControl(Connection .class); 159 Connection con = (Connection ) conControl.getMock(); 160 MockControl stmtControl = MockControl.createControl(Statement .class); 161 Statement stmt = (Statement ) stmtControl.getMock(); 162 MockControl rsControl = MockControl.createControl(ResultSet .class); 163 ResultSet rs = (ResultSet ) rsControl.getMock(); 164 165 ds.getConnection(); 166 dsControl.setReturnValue(con, 2); 167 con.createStatement(); 168 conControl.setReturnValue(stmt, 2); 169 stmt.executeQuery("select nextval('myseq')"); 170 stmtControl.setReturnValue(rs, 2); 171 rs.next(); 172 rsControl.setReturnValue(true, 2); 173 rs.getLong(1); 174 rsControl.setReturnValue(10); 175 rs.getLong(1); 176 rsControl.setReturnValue(12); 177 rs.close(); 178 rsControl.setVoidCallable(2); 179 stmt.close(); 180 stmtControl.setVoidCallable(2); 181 con.close(); 182 conControl.setVoidCallable(2); 183 184 dsControl.replay(); 185 conControl.replay(); 186 stmtControl.replay(); 187 rsControl.replay(); 188 189 PostgreSQLSequenceMaxValueIncrementer incrementer = new PostgreSQLSequenceMaxValueIncrementer(); 190 incrementer.setDataSource(ds); 191 incrementer.setIncrementerName("myseq"); 192 incrementer.setPaddingLength(5); 193 incrementer.afterPropertiesSet(); 194 195 assertEquals("00010", incrementer.nextStringValue()); 196 assertEquals(12, incrementer.nextIntValue()); 197 198 dsControl.verify(); 199 conControl.verify(); 200 stmtControl.verify(); 201 rsControl.verify(); 202 } 203 204 public void testOracleSequenceMaxValueIncrementer() throws SQLException { 205 MockControl dsControl = MockControl.createControl(DataSource .class); 206 DataSource ds = (DataSource ) dsControl.getMock(); 207 MockControl conControl = MockControl.createControl(Connection .class); 208 Connection con = (Connection ) conControl.getMock(); 209 MockControl stmtControl = MockControl.createControl(Statement .class); 210 Statement stmt = (Statement ) stmtControl.getMock(); 211 MockControl rsControl = MockControl.createControl(ResultSet .class); 212 ResultSet rs = (ResultSet ) rsControl.getMock(); 213 214 ds.getConnection(); 215 dsControl.setReturnValue(con, 2); 216 con.createStatement(); 217 conControl.setReturnValue(stmt, 2); 218 stmt.executeQuery("select myseq.nextval from dual"); 219 stmtControl.setReturnValue(rs, 2); 220 rs.next(); 221 rsControl.setReturnValue(true, 2); 222 rs.getLong(1); 223 rsControl.setReturnValue(10); 224 rs.getLong(1); 225 rsControl.setReturnValue(12); 226 rs.close(); 227 rsControl.setVoidCallable(2); 228 stmt.close(); 229 stmtControl.setVoidCallable(2); 230 con.close(); 231 conControl.setVoidCallable(2); 232 233 dsControl.replay(); 234 conControl.replay(); 235 stmtControl.replay(); 236 rsControl.replay(); 237 238 OracleSequenceMaxValueIncrementer incrementer = new OracleSequenceMaxValueIncrementer(); 239 incrementer.setDataSource(ds); 240 incrementer.setIncrementerName("myseq"); 241 incrementer.setPaddingLength(2); 242 incrementer.afterPropertiesSet(); 243 244 assertEquals(10, incrementer.nextLongValue()); 245 assertEquals("12", incrementer.nextStringValue()); 246 247 dsControl.verify(); 248 conControl.verify(); 249 stmtControl.verify(); 250 rsControl.verify(); 251 } 252 253 } 254 | Popular Tags |