1 16 package org.springframework.jdbc.core.support; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.InputStreamReader ; 20 import java.sql.PreparedStatement ; 21 import java.sql.SQLException ; 22 import java.sql.Types ; 23 import java.util.Arrays ; 24 import java.util.Date ; 25 26 import junit.framework.TestCase; 27 import org.easymock.ArgumentsMatcher; 28 import org.easymock.MockControl; 29 30 import org.springframework.jdbc.support.lob.LobCreator; 31 import org.springframework.jdbc.support.lob.LobHandler; 32 33 49 public class SqlLobValueTests extends TestCase { 50 51 private MockControl psControl; 52 private PreparedStatement ps; 53 54 private MockControl lobHandlerControl; 55 private LobHandler handler; 56 57 private MockControl lobCreatorControl; 58 private LobCreator creator; 59 60 public void setUp() { 61 psControl = MockControl.createControl(PreparedStatement .class); 63 ps = (PreparedStatement ) psControl.getMock(); 64 65 lobHandlerControl = MockControl.createControl(LobHandler.class); 67 handler = (LobHandler) lobHandlerControl.getMock(); 68 69 lobCreatorControl = MockControl.createControl(LobCreator.class); 71 creator = (LobCreator) lobCreatorControl.getMock(); 72 73 handler.getLobCreator(); 75 lobHandlerControl.setReturnValue(creator); 76 } 77 78 private void replay() { 79 psControl.replay(); 80 lobHandlerControl.replay(); 81 lobCreatorControl.replay(); 82 } 83 84 public void test1() throws SQLException { 85 byte[] testBytes = "Bla".getBytes(); 86 creator.setBlobAsBytes(ps, 1, testBytes); 87 replay(); 88 SqlLobValue lob = new SqlLobValue(testBytes, handler); 89 lob.setTypeValue(ps, 1, Types.BLOB, "test"); 90 lobHandlerControl.verify(); 91 lobCreatorControl.verify(); 92 } 93 94 public void test2() throws SQLException { 95 String testString = "Bla"; 96 97 creator.setBlobAsBytes(ps, 1, testString.getBytes()); 98 lobCreatorControl.setMatcher(new ArgumentsMatcher() { 100 public boolean matches(Object [] arg0, Object [] arg1) { 101 byte[] one = (byte[]) arg0[2]; 102 byte[] two = (byte[]) arg1[2]; 103 return Arrays.equals(one, two); 104 } 105 public String toString(Object [] arg0) { 106 return "bla"; 107 } 108 }); 109 110 replay(); 111 112 SqlLobValue lob = new SqlLobValue(testString, handler); 113 lob.setTypeValue(ps, 1, Types.BLOB, "test"); 114 lobHandlerControl.verify(); 115 lobCreatorControl.verify(); 116 117 } 118 119 public void test3() 120 throws SQLException { 121 122 Date testContent = new Date (); 123 124 SqlLobValue lob = 125 new SqlLobValue(new InputStreamReader (new ByteArrayInputStream ("Bla".getBytes())), 12); 126 try { 127 lob.setTypeValue(ps, 1, Types.BLOB, "test"); 128 fail("IllegalArgumentException should have been thrown"); 129 } 130 catch (IllegalArgumentException e) { 131 } 133 } 134 135 public void test4() throws SQLException { 136 String testContent = "Bla"; 137 creator.setClobAsString(ps, 1, testContent); 138 139 replay(); 140 141 SqlLobValue lob = new SqlLobValue(testContent, handler); 142 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 143 lobHandlerControl.verify(); 144 lobCreatorControl.verify(); 145 } 146 147 public void test5() throws SQLException { 148 byte[] testContent = "Bla".getBytes(); 149 ByteArrayInputStream bais = new ByteArrayInputStream (testContent); 150 creator.setClobAsAsciiStream(ps, 1, bais, 3); 151 lobCreatorControl.setMatcher(new ArgumentsMatcher() { 152 public boolean matches(Object [] arg0, Object [] arg1) { 153 return true; 155 } 156 public String toString(Object [] arg0) { 157 return null; 158 } 159 }); 160 161 replay(); 162 163 SqlLobValue lob = new SqlLobValue(new ByteArrayInputStream (testContent), 3, handler); 164 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 165 lobHandlerControl.verify(); 166 lobCreatorControl.verify(); 167 } 168 169 public void test6()throws SQLException { 170 byte[] testContent = "Bla".getBytes(); 171 ByteArrayInputStream bais = new ByteArrayInputStream (testContent); 172 InputStreamReader reader = new InputStreamReader (bais); 173 creator.setClobAsCharacterStream(ps, 1, reader, 3); 174 lobCreatorControl.setMatcher(new ArgumentsMatcher() { 175 public boolean matches(Object [] arg0, Object [] arg1) { 176 return true; 178 } 179 public String toString(Object [] arg0) { 180 return null; 181 } 182 }); 183 184 replay(); 185 186 SqlLobValue lob = new SqlLobValue(reader, 3, handler); 187 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 188 lobHandlerControl.verify(); 189 lobCreatorControl.verify(); 190 191 } 192 193 public void test7() throws SQLException { 194 Date testContent = new Date (); 195 196 SqlLobValue lob = new SqlLobValue("bla".getBytes()); 197 try { 198 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 199 fail("IllegalArgumentException should have been thrown"); 200 } 201 catch (IllegalArgumentException e) { 202 } 204 } 205 206 public void testOtherConstructors() throws SQLException { 207 209 SqlLobValue lob = new SqlLobValue("bla"); 210 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 211 212 try { 213 lob = new SqlLobValue("bla".getBytes()); 214 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 215 fail("IllegalArgumentException should have been thrown"); 216 } 217 catch (IllegalArgumentException e) { 218 } 220 221 lob = new SqlLobValue(new ByteArrayInputStream ("bla".getBytes()), 3); 222 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 223 224 lob = new SqlLobValue(new InputStreamReader ( 225 new ByteArrayInputStream ("bla".getBytes())), 3); 226 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 227 228 lob = new SqlLobValue("bla"); 230 lob.setTypeValue(ps, 1, Types.BLOB, "test"); 231 232 lob = new SqlLobValue("bla".getBytes()); 233 lob.setTypeValue(ps, 1, Types.BLOB, "test"); 234 235 lob = new SqlLobValue(new ByteArrayInputStream ("bla".getBytes()), 3); 236 lob.setTypeValue(ps, 1, Types.BLOB, "test"); 237 238 lob = new SqlLobValue(new InputStreamReader ( 239 new ByteArrayInputStream ("bla".getBytes())), 3); 240 241 try { 242 lob.setTypeValue(ps, 1, Types.BLOB, "test"); 243 fail("IllegalArgumentException should have been thrown"); 244 } 245 catch (IllegalArgumentException e) { 246 } 248 } 249 250 public void testCorrectCleanup() throws SQLException { 251 creator.setClobAsString(ps, 1, "Bla"); 252 creator.close(); 253 254 replay(); 255 SqlLobValue lob = new SqlLobValue("Bla", handler); 256 lob.setTypeValue(ps, 1, Types.CLOB, "test"); 257 lob.cleanup(); 258 259 lobCreatorControl.verify(); 260 } 261 262 public void testOtherSqlType() throws SQLException { 263 replay(); 264 SqlLobValue lob = new SqlLobValue("Bla", handler); 265 try { 266 lob.setTypeValue(ps, 1, Types.SMALLINT, "test"); 267 fail("IllegalArgumentException should have been thrown"); 268 } 269 catch (IllegalArgumentException e) { 270 } 272 } 273 274 } 275 | Popular Tags |