KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.springframework.jdbc.core.support;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import junit.framework.TestCase;
25 import org.easymock.MockControl;
26
27 import org.springframework.dao.DataAccessException;
28 import org.springframework.dao.IncorrectResultSizeDataAccessException;
29 import org.springframework.jdbc.LobRetrievalFailureException;
30 import org.springframework.jdbc.support.lob.LobCreator;
31 import org.springframework.jdbc.support.lob.LobHandler;
32
33 /**
34  * @author Alef Arendsen
35  */

36 public class LobSupportTests extends TestCase {
37     
38     public void testCreatingPreparedStatementCallback() throws SQLException JavaDoc {
39         // - return value should match
40
// - lob creator should be closed
41
// - set return value should be called
42
// - execute update should be called
43

44         MockControl lobHandlerControl = MockControl.createControl(LobHandler.class);
45         LobHandler handler = (LobHandler)lobHandlerControl.getMock();
46         
47         MockControl lobCreatorControl = MockControl.createControl(LobCreator.class);
48         LobCreator creator = (LobCreator)lobCreatorControl.getMock();
49         
50         MockControl psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
51         PreparedStatement JavaDoc ps = (PreparedStatement JavaDoc)psControl.getMock();
52         
53         handler.getLobCreator();
54         lobHandlerControl.setReturnValue(creator);
55         ps.executeUpdate();
56         psControl.setReturnValue(3);
57         creator.close();
58         
59         lobHandlerControl.replay();
60         lobCreatorControl.replay();
61         psControl.replay();
62         
63         class SetValuesCalled {
64             boolean b = false;
65         }
66         
67         final SetValuesCalled svc = new SetValuesCalled();
68         
69         AbstractLobCreatingPreparedStatementCallback psc =
70             new AbstractLobCreatingPreparedStatementCallback(handler) {
71             
72             protected void setValues(PreparedStatement JavaDoc ps, LobCreator lobCreator)
73                     throws SQLException JavaDoc, DataAccessException {
74                 svc.b = true;
75             }
76         };
77         
78         assertEquals(new Integer JavaDoc(3), psc.doInPreparedStatement(ps));
79         
80         lobHandlerControl.verify();
81         lobCreatorControl.verify();
82         psControl.verify();
83         assertTrue(svc.b);
84     }
85     
86     public void testAbstractLobStreamingResultSetExtractorNoRows() throws SQLException JavaDoc {
87         MockControl rsetControl = MockControl.createControl(ResultSet JavaDoc.class);
88         ResultSet JavaDoc rset = (ResultSet JavaDoc)rsetControl.getMock();
89         rset.next();
90         rsetControl.setReturnValue(false);
91         rsetControl.replay();
92         
93         AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(false);
94         try {
95             lobRse.extractData(rset);
96             fail("IncorrectResultSizeDataAccessException should have been thrown");
97         } catch (IncorrectResultSizeDataAccessException e) {
98             // expected
99
}
100     }
101     
102     public void testAbstractLobStreamingResultSetExtractorOneRow() throws SQLException JavaDoc {
103         MockControl rsetControl = MockControl.createControl(ResultSet JavaDoc.class);
104         ResultSet JavaDoc rset = (ResultSet JavaDoc)rsetControl.getMock();
105         rset.next();
106         rsetControl.setReturnValue(true);
107         // see if it's called
108
rset.clearWarnings();
109         rset.next();
110         rsetControl.setReturnValue(false);
111         rsetControl.replay();
112         
113         AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(false);
114         lobRse.extractData(rset);
115         rsetControl.verify();
116     }
117
118     public void testAbstractLobStreamingResultSetExtractorMultipleRows() throws SQLException JavaDoc {
119         MockControl rsetControl = MockControl.createControl(ResultSet JavaDoc.class);
120         ResultSet JavaDoc rset = (ResultSet JavaDoc)rsetControl.getMock();
121         rset.next();
122         rsetControl.setReturnValue(true);
123         // see if it's called
124
rset.clearWarnings();
125         rset.next();
126         rsetControl.setReturnValue(true);
127         rsetControl.replay();
128         
129         AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(false);
130         try {
131             lobRse.extractData(rset);
132             fail("IncorrectResultSizeDataAccessException should have been thrown");
133         } catch (IncorrectResultSizeDataAccessException e) {
134             // expected
135
}
136         rsetControl.verify();
137     }
138     
139     public void testAbstractLobStreamingResultSetExtractorCorrectException() throws SQLException JavaDoc {
140         MockControl rsetControl = MockControl.createControl(ResultSet JavaDoc.class);
141         ResultSet JavaDoc rset = (ResultSet JavaDoc)rsetControl.getMock();
142         rset.next();
143         rsetControl.setReturnValue(true);
144         rsetControl.replay();
145         
146         AbstractLobStreamingResultSetExtractor lobRse = getResultSetExtractor(true);
147         try {
148             lobRse.extractData(rset);
149             fail("LobRetrievalFailureException should have been thrown");
150         } catch (LobRetrievalFailureException e) {
151             // expected
152
}
153         rsetControl.verify();
154     }
155     
156     private AbstractLobStreamingResultSetExtractor getResultSetExtractor(final boolean ex) {
157         AbstractLobStreamingResultSetExtractor lobRse = new AbstractLobStreamingResultSetExtractor() {
158             protected void streamData(ResultSet JavaDoc rs) throws SQLException JavaDoc, IOException JavaDoc {
159                 if (ex) {
160                     throw new IOException JavaDoc();
161                 }
162                 else {
163                     rs.clearWarnings();
164                 }
165             }
166         };
167         return lobRse;
168     }
169 }
170
Popular Tags