KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > SqlLobValueTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.jdbc.core.support;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Types JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Date JavaDoc;
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 /**
34  * Test cases for the sql lob value:
35  *
36  * BLOB:
37  * 1. Types.BLOB: setBlobAsBytes (byte[])
38  * 2. String: setBlobAsBytes (byte[])
39  * 3. else: IllegalArgumentException
40  *
41  * CLOB:
42  * 4. String or NULL: setClobAsString (String)
43  * 5. InputStream: setClobAsAsciiStream (InputStream)
44  * 6. Reader: setClobAsCharacterStream (Reader)
45  * 7. else: IllegalArgumentException
46  *
47  * @author Alef Arendsen
48  */

49 public class SqlLobValueTests extends TestCase {
50     
51     private MockControl psControl;
52     private PreparedStatement JavaDoc 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         // create preparedstatement
62
psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
63         ps = (PreparedStatement JavaDoc) psControl.getMock();
64         
65         // create handler controler
66
lobHandlerControl = MockControl.createControl(LobHandler.class);
67         handler = (LobHandler) lobHandlerControl.getMock();
68         
69         // create creator control
70
lobCreatorControl = MockControl.createControl(LobCreator.class);
71         creator = (LobCreator) lobCreatorControl.getMock();
72         
73         // set initial state
74
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 JavaDoc {
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 JavaDoc {
95         String JavaDoc testString = "Bla";
96         
97         creator.setBlobAsBytes(ps, 1, testString.getBytes());
98         // set a matcher to match the byte array!
99
lobCreatorControl.setMatcher(new ArgumentsMatcher() {
100             public boolean matches(Object JavaDoc[] arg0, Object JavaDoc[] arg1) {
101                 byte[] one = (byte[]) arg0[2];
102                 byte[] two = (byte[]) arg1[2];
103                 return Arrays.equals(one, two);
104             }
105             public String JavaDoc toString(Object JavaDoc[] 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 JavaDoc {
121         
122         Date JavaDoc testContent = new Date JavaDoc();
123         
124         SqlLobValue lob =
125             new SqlLobValue(new InputStreamReader JavaDoc(new ByteArrayInputStream JavaDoc("Bla".getBytes())), 12);
126         try {
127             lob.setTypeValue(ps, 1, Types.BLOB, "test");
128             fail("IllegalArgumentException should have been thrown");
129         }
130         catch (IllegalArgumentException JavaDoc e) {
131             // expected
132
}
133     }
134     
135     public void test4() throws SQLException JavaDoc {
136         String JavaDoc 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 JavaDoc {
148         byte[] testContent = "Bla".getBytes();
149         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(testContent);
150         creator.setClobAsAsciiStream(ps, 1, bais, 3);
151         lobCreatorControl.setMatcher(new ArgumentsMatcher() {
152             public boolean matches(Object JavaDoc[] arg0, Object JavaDoc[] arg1) {
153                 // for now, match always
154
return true;
155             }
156             public String JavaDoc toString(Object JavaDoc[] arg0) {
157                 return null;
158             }
159         });
160         
161         replay();
162         
163         SqlLobValue lob = new SqlLobValue(new ByteArrayInputStream JavaDoc(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 JavaDoc {
170         byte[] testContent = "Bla".getBytes();
171         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(testContent);
172         InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(bais);
173         creator.setClobAsCharacterStream(ps, 1, reader, 3);
174         lobCreatorControl.setMatcher(new ArgumentsMatcher() {
175             public boolean matches(Object JavaDoc[] arg0, Object JavaDoc[] arg1) {
176                 // for now, match always
177
return true;
178             }
179             public String JavaDoc toString(Object JavaDoc[] 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 JavaDoc {
194         Date JavaDoc testContent = new Date JavaDoc();
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 JavaDoc e) {
202             // expected
203
}
204     }
205     
206     public void testOtherConstructors() throws SQLException JavaDoc {
207         // a bit BS, but we need to test them, as long as they don't throw exceptions
208

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 JavaDoc e) {
218             // expected
219
}
220
221         lob = new SqlLobValue(new ByteArrayInputStream JavaDoc("bla".getBytes()), 3);
222         lob.setTypeValue(ps, 1, Types.CLOB, "test");
223         
224         lob = new SqlLobValue(new InputStreamReader JavaDoc(
225                 new ByteArrayInputStream JavaDoc("bla".getBytes())), 3);
226         lob.setTypeValue(ps, 1, Types.CLOB, "test");
227         
228         // same for BLOB
229
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 JavaDoc("bla".getBytes()), 3);
236         lob.setTypeValue(ps, 1, Types.BLOB, "test");
237         
238         lob = new SqlLobValue(new InputStreamReader JavaDoc(
239                 new ByteArrayInputStream JavaDoc("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 JavaDoc e) {
246             // expected
247
}
248     }
249     
250     public void testCorrectCleanup() throws SQLException JavaDoc {
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 JavaDoc {
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 JavaDoc e) {
270             // expected
271
}
272     }
273
274 }
275
Popular Tags