|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -41,6 +41,7 @@ import static org.mockito.BDDMockito.*; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Phillip Webb |
|
|
|
* @author Phillip Webb |
|
|
|
|
|
|
|
* @author Rob Winch |
|
|
|
* @since 19.12.2004 |
|
|
|
* @since 19.12.2004 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class JdbcTemplateQueryTests { |
|
|
|
public class JdbcTemplateQueryTests { |
|
|
|
@ -49,13 +50,20 @@ public class JdbcTemplateQueryTests { |
|
|
|
public ExpectedException thrown = ExpectedException.none(); |
|
|
|
public ExpectedException thrown = ExpectedException.none(); |
|
|
|
|
|
|
|
|
|
|
|
private Connection connection; |
|
|
|
private Connection connection; |
|
|
|
|
|
|
|
|
|
|
|
private DataSource dataSource; |
|
|
|
private DataSource dataSource; |
|
|
|
|
|
|
|
|
|
|
|
private Statement statement; |
|
|
|
private Statement statement; |
|
|
|
|
|
|
|
|
|
|
|
private PreparedStatement preparedStatement; |
|
|
|
private PreparedStatement preparedStatement; |
|
|
|
|
|
|
|
|
|
|
|
private ResultSet resultSet; |
|
|
|
private ResultSet resultSet; |
|
|
|
|
|
|
|
|
|
|
|
private ResultSetMetaData resultSetMetaData; |
|
|
|
private ResultSetMetaData resultSetMetaData; |
|
|
|
|
|
|
|
|
|
|
|
private JdbcTemplate template; |
|
|
|
private JdbcTemplate template; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setUp() throws Exception { |
|
|
|
public void setUp() throws Exception { |
|
|
|
this.connection = mock(Connection.class); |
|
|
|
this.connection = mock(Connection.class); |
|
|
|
@ -75,6 +83,7 @@ public class JdbcTemplateQueryTests { |
|
|
|
given(this.statement.executeQuery(anyString())).willReturn(this.resultSet); |
|
|
|
given(this.statement.executeQuery(anyString())).willReturn(this.resultSet); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testQueryForList() throws Exception { |
|
|
|
public void testQueryForList() throws Exception { |
|
|
|
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3"; |
|
|
|
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3"; |
|
|
|
@ -219,7 +228,18 @@ public class JdbcTemplateQueryTests { |
|
|
|
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; |
|
|
|
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; |
|
|
|
given(this.resultSet.next()).willReturn(true, false); |
|
|
|
given(this.resultSet.next()).willReturn(true, false); |
|
|
|
given(this.resultSet.getInt(1)).willReturn(22); |
|
|
|
given(this.resultSet.getInt(1)).willReturn(22); |
|
|
|
int i = this.template.queryForObject(sql,Integer.class).intValue(); |
|
|
|
int i = this.template.queryForObject(sql, Integer.class).intValue(); |
|
|
|
|
|
|
|
assertEquals("Return of an int", 22, i); |
|
|
|
|
|
|
|
verify(this.resultSet).close(); |
|
|
|
|
|
|
|
verify(this.statement).close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testQueryForIntPrimitive() throws Exception { |
|
|
|
|
|
|
|
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; |
|
|
|
|
|
|
|
given(this.resultSet.next()).willReturn(true, false); |
|
|
|
|
|
|
|
given(this.resultSet.getInt(1)).willReturn(22); |
|
|
|
|
|
|
|
int i = this.template.queryForObject(sql, int.class); |
|
|
|
assertEquals("Return of an int", 22, i); |
|
|
|
assertEquals("Return of an int", 22, i); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.resultSet).close(); |
|
|
|
verify(this.statement).close(); |
|
|
|
verify(this.statement).close(); |
|
|
|
@ -236,6 +256,17 @@ public class JdbcTemplateQueryTests { |
|
|
|
verify(this.statement).close(); |
|
|
|
verify(this.statement).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testQueryForLongPrimitive() throws Exception { |
|
|
|
|
|
|
|
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3"; |
|
|
|
|
|
|
|
given(this.resultSet.next()).willReturn(true, false); |
|
|
|
|
|
|
|
given(this.resultSet.getLong(1)).willReturn(87L); |
|
|
|
|
|
|
|
long l = this.template.queryForObject(sql, long.class); |
|
|
|
|
|
|
|
assertEquals("Return of a long", 87, l); |
|
|
|
|
|
|
|
verify(this.resultSet).close(); |
|
|
|
|
|
|
|
verify(this.statement).close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testQueryForListWithArgs() throws Exception { |
|
|
|
public void testQueryForListWithArgs() throws Exception { |
|
|
|
doTestQueryForListWithArgs("SELECT AGE FROM CUSTMR WHERE ID < ?"); |
|
|
|
doTestQueryForListWithArgs("SELECT AGE FROM CUSTMR WHERE ID < ?"); |
|
|
|
|