View Javadoc
1   package com.github.davidmoten.rx.jdbc;
2   
3   import static com.github.davidmoten.rx.jdbc.Util.autoMap;
4   import static com.github.davidmoten.rx.jdbc.Util.closeQuietly;
5   import static com.github.davidmoten.rx.jdbc.Util.closeQuietlyIfAutoCommit;
6   import static org.easymock.EasyMock.createMock;
7   import static org.easymock.EasyMock.expect;
8   import static org.easymock.EasyMock.expectLastCall;
9   import static org.easymock.EasyMock.replay;
10  import static org.easymock.EasyMock.verify;
11  import static org.junit.Assert.assertEquals;
12  
13  import java.io.ByteArrayInputStream;
14  import java.io.IOException;
15  import java.io.Reader;
16  import java.io.StringReader;
17  import java.math.BigDecimal;
18  import java.math.BigInteger;
19  import java.sql.Blob;
20  import java.sql.Clob;
21  import java.sql.Connection;
22  import java.sql.ResultSet;
23  import java.sql.ResultSetMetaData;
24  import java.sql.SQLException;
25  import java.sql.Types;
26  import java.util.Arrays;
27  
28  import org.easymock.EasyMock;
29  import org.junit.Test;
30  import org.mockito.Mockito;
31  import org.mockito.invocation.InvocationOnMock;
32  import org.mockito.stubbing.Answer;
33  
34  public class UtilTest {
35  
36      @Test
37      public void obtainCoverageOfPrivateConstructor() {
38          TestingUtil.instantiateUsingPrivateConstructor(Util.class);
39      }
40  
41      @Test
42      public void testAutoMapOfUtilDateToSqlDate() {
43          assertEquals(new java.sql.Date(1), autoMap(new java.util.Date(1), java.sql.Date.class));
44      }
45  
46      @Test
47      public void testAutoMapOfSqlDateToUtilDate() {
48          assertEquals(new java.util.Date(1), autoMap(new java.sql.Date(1), java.util.Date.class));
49      }
50  
51      @Test
52      public void testAutoMapOfSqlDateToLong() {
53          assertEquals(1L, autoMap(new java.sql.Date(1), Long.class));
54      }
55  
56      @Test
57      public void testAutoMapOfSqlDateToBigInteger() {
58          assertEquals(BigInteger.ONE, autoMap(new java.sql.Date(1), BigInteger.class));
59      }
60  
61      private static class Simple {
62      }
63  
64      @Test
65      public void testAutoMapOfSqlDateToSimple() {
66          assertEquals(new java.sql.Date(1), autoMap(new java.sql.Date(1), Simple.class));
67      }
68  
69      @Test
70      public void testAutoMapOfSqlTimestampToUtilDate() {
71          assertEquals(new java.util.Date(1),
72                  autoMap(new java.sql.Timestamp(1), java.util.Date.class));
73      }
74  
75      @Test
76      public void testAutoMapOfSqlTimestampToLong() {
77          assertEquals(1L, autoMap(new java.sql.Timestamp(1), Long.class));
78      }
79  
80      @Test
81      public void testAutoMapOfSqlTimestampToBigInteger() {
82          assertEquals(BigInteger.ONE, autoMap(new java.sql.Timestamp(1), BigInteger.class));
83      }
84  
85      @Test
86      public void testAutoMapOfSqlTimestampToSimple() {
87          assertEquals(new java.sql.Timestamp(1), autoMap(new java.sql.Timestamp(1), Simple.class));
88      }
89  
90      @Test
91      public void testAutoMapOfSqlTimeToUtilDate() {
92          assertEquals(new java.util.Date(1), autoMap(new java.sql.Time(1), java.util.Date.class));
93      }
94  
95      @Test
96      public void testAutoMapOfSqlTimeToLong() {
97          assertEquals(1L, autoMap(new java.sql.Time(1), Long.class));
98      }
99  
100     @Test
101     public void testAutoMapOfSqlTimeToBigInteger() {
102         assertEquals(BigInteger.ONE, autoMap(new java.sql.Time(1), BigInteger.class));
103     }
104 
105     @Test
106     public void testAutoMapOfSqlTimeToSimple() {
107         assertEquals(new java.sql.Time(1), autoMap(new java.sql.Time(1), Simple.class));
108     }
109 
110     @Test
111     public void testAutoMapBlobToByteArray() throws SQLException {
112         Blob blob = EasyMock.createMock(Blob.class);
113         byte[] b = "hello there".getBytes();
114         expect(blob.getBinaryStream()).andReturn(new ByteArrayInputStream(b)).once();
115         blob.free();
116         EasyMock.expectLastCall().once();
117         replay(blob);
118         Object bytes = autoMap(blob, byte[].class);
119         Arrays.equals(b, (byte[]) bytes);
120         verify(blob);
121     }
122 
123     @Test
124     public void testAutoMapBlobToSimple() throws SQLException {
125         Blob blob = EasyMock.createMock(Blob.class);
126         replay(blob);
127         Object bytes = autoMap(blob, Simple.class);
128         assertEquals(bytes, blob);
129         verify(blob);
130     }
131 
132     @Test
133     public void testAutoMapClobToByteArray() throws SQLException {
134         Clob clob = EasyMock.createMock(Clob.class);
135         String s = "hello there";
136         expect(clob.getCharacterStream()).andReturn(new StringReader(s)).once();
137         clob.free();
138         EasyMock.expectLastCall().once();
139         replay(clob);
140         Object string = autoMap(clob, String.class);
141         assertEquals(s, string);
142         verify(clob);
143     }
144 
145     @Test
146     public void testAutoMapClobToSimple() throws SQLException {
147         Clob clob = EasyMock.createMock(Clob.class);
148         replay(clob);
149         Object result = autoMap(clob, Simple.class);
150         assertEquals(clob, result);
151         verify(clob);
152     }
153 
154     @Test
155     public void testAutoMapBigIntegerToLong() {
156         assertEquals(1L, autoMap(BigInteger.ONE, Long.class));
157     }
158 
159     @Test
160     public void testAutoMapBigIntegerToDouble() {
161         assertEquals(1.0, (Double) autoMap(BigInteger.ONE, Double.class), 0.00001);
162     }
163 
164     @Test
165     public void testAutoMapBigIntegerToFloat() {
166         assertEquals(1.0, (Float) autoMap(BigInteger.ONE, Float.class), 0.00001);
167     }
168 
169     @Test
170     public void testAutoMapBigIntegerToShort() {
171         assertEquals((short) 1, autoMap(BigInteger.ONE, Short.class));
172     }
173 
174     @Test
175     public void testAutoMapBigIntegerToInteger() {
176         assertEquals(1, autoMap(BigInteger.ONE, Integer.class));
177     }
178 
179     @Test
180     public void testAutoMapBigDecimalToDouble() {
181         assertEquals(1.0, (Double) autoMap(BigDecimal.ONE, Double.class), 0.00001);
182     }
183 
184     @Test
185     public void testAutoMapBigDecimalToFloat() {
186         assertEquals(1.0, (Float) autoMap(BigDecimal.ONE, Float.class), 0.00001);
187     }
188 
189     @Test
190     public void testAutoMapBigDecimalToShort() {
191         assertEquals((short) 1, autoMap(BigDecimal.ONE, Short.class));
192     }
193 
194     @Test
195     public void testAutoMapBigDecimalToInteger() {
196         assertEquals(1, autoMap(BigDecimal.ONE, Integer.class));
197     }
198 
199     @Test
200     public void testAutoMapBigDecimalToLong() {
201         assertEquals(1L, autoMap(BigDecimal.ONE, Long.class));
202     }
203 
204     @Test
205     public void testAutoMapOfBigIntegerToSimple() {
206         assertEquals(BigInteger.ONE, autoMap(BigInteger.ONE, Simple.class));
207     }
208 
209     @Test
210     public void testAutoMapOfBigDecimalToSimple() {
211         assertEquals(BigDecimal.ONE, autoMap(BigDecimal.ONE, Simple.class));
212     }
213 
214     @Test
215     public void testAutoMapOfDoubleToBigDecimal() {
216         assertEquals(BigDecimal.ONE.doubleValue(),
217                 ((BigDecimal) autoMap(1.0, BigDecimal.class)).doubleValue(), 0.0001);
218     }
219 
220     @Test
221     public void testAutoMapOfIntegerToBigDecimal() {
222         assertEquals(BigDecimal.ONE.doubleValue(),
223                 ((BigDecimal) autoMap(1, BigDecimal.class)).doubleValue(), 0.0001);
224     }
225 
226     @Test
227     public void testAutoMapOfDoubleToBigInteger() {
228         assertEquals(1.2, (Double) autoMap(1.2, BigInteger.class), 0.0001);
229     }
230 
231     @Test
232     public void testAutoMapOfShortToBigInteger() {
233         assertEquals(BigInteger.ONE, autoMap((short) 1, BigInteger.class));
234     }
235 
236     @Test
237     public void testAutoMapOfIntegerToBigInteger() {
238         assertEquals(BigInteger.ONE, autoMap(1, BigInteger.class));
239     }
240 
241     @Test
242     public void testAutoMapOfNumberToBigInteger() {
243         assertEquals(BigInteger.ONE, autoMap(1L, BigInteger.class));
244     }
245 
246     @Test
247     public void testAutoMapOfNumberToInteger() {
248         assertEquals(1, autoMap(1L, Integer.class));
249     }
250 
251     @Test
252     public void testAutoMapOfNumberToShort() {
253         assertEquals((short) 1, autoMap(1L, Short.class));
254     }
255 
256     @Test
257     public void testAutoMapOfNumberToDouble() {
258         assertEquals(1.0, (Double) autoMap(1L, Double.class), 0.00001);
259     }
260 
261     @Test
262     public void testAutoMapOfNumberToLong() {
263         assertEquals(1L, (long) (Long) autoMap(1, Long.class));
264     }
265 
266     @Test
267     public void testAutoMapOfLongToFloat() {
268         assertEquals(1.0f, (Float) autoMap(1L, Float.class), 0.00001);
269     }
270 
271     @Test
272     public void testClose1() throws SQLException {
273         Connection con = createMock(Connection.class);
274         expect(con.isClosed()).andThrow(new SQLException());
275         replay(con);
276         closeQuietly(con);
277         verify(con);
278     }
279 
280     @Test(expected = RuntimeException.class)
281     public void testClose2() throws SQLException {
282         Connection con = createMock(Connection.class);
283         expect(con.isClosed()).andThrow(new SQLException());
284         replay(con);
285         closeQuietlyIfAutoCommit(con);
286         verify(con);
287     }
288 
289     @Test(expected = RuntimeException.class)
290     public void testCommit() throws SQLException {
291         Connection con = createMock(Connection.class);
292         con.commit();
293         expectLastCall().andThrow(new SQLException());
294         replay(con);
295         Util.commit(con);
296         verify(con);
297     }
298 
299     @Test
300     public void testCommitNullDoesNothing() {
301         Util.commit(null);
302     }
303 
304     @Test
305     public void testRollbackNullDoesNothing() {
306         Util.rollback(null);
307     }
308 
309     @Test(expected = RuntimeException.class)
310     public void testRollback() throws SQLException {
311         Connection con = createMock(Connection.class);
312         con.rollback();
313         expectLastCall().andThrow(new SQLException());
314         replay(con);
315         Util.rollback(con);
316         verify(con);
317     }
318 
319     @Test
320     public void testMapObjectExistingClob() throws SQLException, IOException {
321 
322         final Clob mock = Mockito.mock(Clob.class);
323         Mockito.when(mock.getCharacterStream()).thenAnswer(new Answer<Reader>() {
324             @Override
325             public Reader answer(InvocationOnMock invocationOnMock) throws Throwable {
326                 return new StringReader("test");
327             }
328         });
329 
330         final ResultSetMetaData metaData = Mockito.mock(ResultSetMetaData.class);
331         Mockito.when(metaData.getColumnType(1)).thenAnswer(new Answer<Integer>() {
332             @Override
333             public Integer answer(InvocationOnMock invocationOnMock) throws Throwable {
334                 return Types.CLOB;
335             }
336         });
337 
338         ResultSet resultSet = Mockito.mock(ResultSet.class);
339 
340         Mockito.when(resultSet.getObject(1)).thenAnswer(new Answer<Clob>() {
341             @Override
342             public Clob answer(InvocationOnMock invocationOnMock) throws Throwable {
343                 return mock;
344             }
345         });
346 
347         Mockito.when(resultSet.getClob(1)).thenAnswer(new Answer<Clob>() {
348             @Override
349             public Clob answer(InvocationOnMock invocationOnMock) throws Throwable {
350                 return mock;
351             }
352         });
353 
354         Mockito.when(resultSet.getMetaData()).thenAnswer(new Answer<ResultSetMetaData>() {
355             @Override
356             public ResultSetMetaData answer(InvocationOnMock invocationOnMock) throws Throwable {
357                 return metaData;
358             }
359         });
360 
361         assertEquals(Util.mapObject(resultSet, String.class, 1), "test");
362 
363     }
364 
365     @Test
366     public void testMapObjectNullClob() throws SQLException, IOException {
367 
368         final ResultSetMetaData metaData = Mockito.mock(ResultSetMetaData.class);
369         Mockito.when(metaData.getColumnType(1)).thenAnswer(new Answer<Integer>() {
370             @Override
371             public Integer answer(InvocationOnMock invocationOnMock) throws Throwable {
372                 return Types.CLOB;
373             }
374         });
375 
376         ResultSet resultSet = Mockito.mock(ResultSet.class);
377 
378         Mockito.when(resultSet.getObject(1)).thenAnswer(new Answer<Clob>() {
379             @Override
380             public Clob answer(InvocationOnMock invocationOnMock) throws Throwable {
381                 return null;
382             }
383         });
384 
385         Mockito.when(resultSet.getClob(1)).thenAnswer(new Answer<Clob>() {
386             @Override
387             public Clob answer(InvocationOnMock invocationOnMock) throws Throwable {
388                 return null;
389             }
390         });
391 
392         Mockito.when(resultSet.getMetaData()).thenAnswer(new Answer<ResultSetMetaData>() {
393             @Override
394             public ResultSetMetaData answer(InvocationOnMock invocationOnMock) throws Throwable {
395                 return metaData;
396             }
397         });
398 
399         assertEquals(Util.mapObject(resultSet, String.class, 1), null);
400 
401     }
402 
403     @Test
404     public void testCamelCaseToUnderscore() {
405         assertEquals("a", Util.camelCaseToUnderscore("a"));
406     }
407 
408     @Test
409     public void testCamelCaseToUnderscore2() {
410         assertEquals("ab", Util.camelCaseToUnderscore("ab"));
411     }
412 
413     @Test
414     public void testCamelCaseToUnderscore3() {
415         assertEquals("a_B", Util.camelCaseToUnderscore("aB"));
416     }
417 
418     @Test
419     public void testCamelCaseToUnderscore4() {
420         assertEquals("a_BC", Util.camelCaseToUnderscore("aBC"));
421     }
422 
423     @Test
424     public void testCamelCaseToUnderscore5() {
425         assertEquals("a_Before_Count", Util.camelCaseToUnderscore("aBeforeCount"));
426     }
427 
428     @Test
429     public void testCamelCaseToUnderscore6() {
430         assertEquals("", Util.camelCaseToUnderscore(""));
431     }
432 
433     @Test
434     public void testCountOfQuestionMarks() {
435         assertEquals(2,
436                 Util.countQuestionMarkParameters("select name from blah where a <? and b = ?"));
437     }
438 
439     @Test
440     public void testCountOfQuestionMarksIgnoresTextInQuotes() {
441         assertEquals(1,
442                 Util.countQuestionMarkParameters("select name from blah where a < '?' and b = ?"));
443     }
444 
445     @Test
446     public void testCountOfNoQuestionMarks() {
447         assertEquals(0, Util.countQuestionMarkParameters("boo"));
448     }
449 
450     @Test
451     public void testCountOfNoQuestionMarksReturnsZeroWhenEmptyString() {
452         assertEquals(0, Util.countQuestionMarkParameters(""));
453     }
454 
455     @Test
456     public void testCountOfNoQuestionMarksSkipsNestedUnclosedQuotes() {
457         assertEquals(
458                 1,
459                 Util.countQuestionMarkParameters("select name from person where name > '\"?' and name > ?"));
460     }
461 }