1 16 17 package org.springframework.jdbc.core.support; 18 19 import java.io.IOException ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 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 36 public class LobSupportTests extends TestCase { 37 38 public void testCreatingPreparedStatementCallback() throws SQLException { 39 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 .class); 51 PreparedStatement ps = (PreparedStatement )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 ps, LobCreator lobCreator) 73 throws SQLException , DataAccessException { 74 svc.b = true; 75 } 76 }; 77 78 assertEquals(new Integer (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 { 87 MockControl rsetControl = MockControl.createControl(ResultSet .class); 88 ResultSet rset = (ResultSet )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 } 100 } 101 102 public void testAbstractLobStreamingResultSetExtractorOneRow() throws SQLException { 103 MockControl rsetControl = MockControl.createControl(ResultSet .class); 104 ResultSet rset = (ResultSet )rsetControl.getMock(); 105 rset.next(); 106 rsetControl.setReturnValue(true); 107 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 { 119 MockControl rsetControl = MockControl.createControl(ResultSet .class); 120 ResultSet rset = (ResultSet )rsetControl.getMock(); 121 rset.next(); 122 rsetControl.setReturnValue(true); 123 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 } 136 rsetControl.verify(); 137 } 138 139 public void testAbstractLobStreamingResultSetExtractorCorrectException() throws SQLException { 140 MockControl rsetControl = MockControl.createControl(ResultSet .class); 141 ResultSet rset = (ResultSet )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 } 153 rsetControl.verify(); 154 } 155 156 private AbstractLobStreamingResultSetExtractor getResultSetExtractor(final boolean ex) { 157 AbstractLobStreamingResultSetExtractor lobRse = new AbstractLobStreamingResultSetExtractor() { 158 protected void streamData(ResultSet rs) throws SQLException , IOException { 159 if (ex) { 160 throw new IOException (); 161 } 162 else { 163 rs.clearWarnings(); 164 } 165 } 166 }; 167 return lobRse; 168 } 169 } 170 | Popular Tags |