View Javadoc
1   /*
2    * Copyright 2002-2013 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;
19  import java.io.InputStream;
20  import java.io.InputStreamReader;
21  import java.sql.PreparedStatement;
22  import java.sql.SQLException;
23  import java.sql.Types;
24  
25  import org.junit.Before;
26  import org.junit.Rule;
27  import org.junit.Test;
28  import org.junit.rules.ExpectedException;
29  import org.mockito.ArgumentCaptor;
30  import org.mockito.Captor;
31  import org.mockito.MockitoAnnotations;
32  
33  import org.springframework.jdbc.support.lob.LobCreator;
34  import org.springframework.jdbc.support.lob.LobHandler;
35  
36  import static org.hamcrest.Matchers.*;
37  import static org.junit.Assert.*;
38  import static org.mockito.BDDMockito.*;
39  
40  /**
41   * Test cases for the sql lob value:
42   *
43   * BLOB:
44   *   1. Types.BLOB: setBlobAsBytes (byte[])
45   *   2. String: setBlobAsBytes (byte[])
46   *   3. else: IllegalArgumentException
47   *
48   * CLOB:
49   *   4. String or NULL: setClobAsString (String)
50   *   5. InputStream: setClobAsAsciiStream (InputStream)
51   *   6. Reader: setClobAsCharacterStream (Reader)
52   *   7. else: IllegalArgumentException
53   *
54   * @author Alef Arendsen
55   */
56  public class SqlLobValueTests  {
57  
58  	@Rule
59  	public ExpectedException thrown = ExpectedException.none();
60  
61  	private PreparedStatement preparedStatement;
62  	private LobHandler handler;
63  	private LobCreator creator;
64  
65  	@Captor
66  	private ArgumentCaptor<InputStream> inputStreamCaptor;
67  
68  	@Before
69  	public void setUp() {
70  		MockitoAnnotations.initMocks(this);
71  		preparedStatement = mock(PreparedStatement.class);
72  		handler = mock(LobHandler.class);
73  		creator = mock(LobCreator.class);
74  		given(handler.getLobCreator()).willReturn(creator);
75  	}
76  
77  	@Test
78  	public void test1() throws SQLException {
79  		byte[] testBytes = "Bla".getBytes();
80  		SqlLobValue lob = new SqlLobValue(testBytes, handler);
81  		lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
82  		verify(creator).setBlobAsBytes(preparedStatement, 1, testBytes);
83  	}
84  
85  	@Test
86  	public void test2() throws SQLException {
87  		String testString = "Bla";
88  		SqlLobValue lob = new SqlLobValue(testString, handler);
89  		lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
90  		verify(creator).setBlobAsBytes(preparedStatement, 1, testString.getBytes());
91  	}
92  
93  	@Test
94  	public void test3() throws SQLException {
95  		SqlLobValue lob = new SqlLobValue(new InputStreamReader(new ByteArrayInputStream("Bla".getBytes())), 12);
96  		thrown.expect(IllegalArgumentException.class);
97  		lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
98  	}
99  
100 	@Test
101 	public void test4() throws SQLException {
102 		String testContent = "Bla";
103 		SqlLobValue lob = new SqlLobValue(testContent, handler);
104 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
105 		verify(creator).setClobAsString(preparedStatement, 1, testContent);
106 	}
107 
108 	@Test
109 	public void test5() throws Exception {
110 		byte[] testContent = "Bla".getBytes();
111 		SqlLobValue lob = new SqlLobValue(new ByteArrayInputStream(testContent), 3, handler);
112 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
113 		verify(creator).setClobAsAsciiStream(eq(preparedStatement), eq(1), inputStreamCaptor.capture(), eq(3));
114 		byte[] bytes = new byte[3];
115 		inputStreamCaptor.getValue().read(bytes );
116 		assertThat(bytes, equalTo(testContent));
117 	}
118 
119 	@Test
120 	public void test6() throws SQLException {
121 		byte[] testContent = "Bla".getBytes();
122 		ByteArrayInputStream bais = new ByteArrayInputStream(testContent);
123 		InputStreamReader reader = new InputStreamReader(bais);
124 		SqlLobValue lob = new SqlLobValue(reader, 3, handler);
125 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
126 		verify(creator).setClobAsCharacterStream(eq(preparedStatement), eq(1), eq(reader), eq(3));
127 	}
128 
129 	@Test
130 	public void test7() throws SQLException {
131 		SqlLobValue lob = new SqlLobValue("bla".getBytes());
132 		thrown.expect(IllegalArgumentException.class);
133 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
134 	}
135 
136 	@Test
137 	public void testOtherConstructors() throws SQLException {
138 		// a bit BS, but we need to test them, as long as they don't throw exceptions
139 
140 		SqlLobValue lob = new SqlLobValue("bla");
141 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
142 
143 		try {
144 			lob = new SqlLobValue("bla".getBytes());
145 			lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
146 			fail("IllegalArgumentException should have been thrown");
147 		}
148 		catch (IllegalArgumentException e) {
149 			// expected
150 		}
151 
152 		lob = new SqlLobValue(new ByteArrayInputStream("bla".getBytes()), 3);
153 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
154 
155 		lob = new SqlLobValue(new InputStreamReader(new ByteArrayInputStream(
156 				"bla".getBytes())), 3);
157 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
158 
159 		// same for BLOB
160 		lob = new SqlLobValue("bla");
161 		lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
162 
163 		lob = new SqlLobValue("bla".getBytes());
164 		lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
165 
166 		lob = new SqlLobValue(new ByteArrayInputStream("bla".getBytes()), 3);
167 		lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
168 
169 		lob = new SqlLobValue(new InputStreamReader(new ByteArrayInputStream(
170 				"bla".getBytes())), 3);
171 
172 		try {
173 			lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
174 			fail("IllegalArgumentException should have been thrown");
175 		}
176 		catch (IllegalArgumentException e) {
177 			// expected
178 		}
179 	}
180 
181 	@Test
182 	public void testCorrectCleanup() throws SQLException {
183 		SqlLobValue lob = new SqlLobValue("Bla", handler);
184 		lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
185 		lob.cleanup();
186 		verify(creator).setClobAsString(preparedStatement, 1, "Bla");
187 		verify(creator).close();
188 	}
189 
190 	@Test
191 	public void testOtherSqlType() throws SQLException {
192 		SqlLobValue lob = new SqlLobValue("Bla", handler);
193 		thrown.expect(IllegalArgumentException.class);
194 		lob.setTypeValue(preparedStatement, 1, Types.SMALLINT, "test");
195 	}
196 
197 }