5 changed files with 250 additions and 17 deletions
@ -0,0 +1,73 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.repository.support; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*; |
||||||
|
import static org.mockito.Mockito.*; |
||||||
|
|
||||||
|
import java.lang.reflect.Method; |
||||||
|
import java.sql.ResultSet; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.data.jdbc.repository.query.Query; |
||||||
|
import org.springframework.data.projection.ProjectionFactory; |
||||||
|
import org.springframework.data.repository.core.RepositoryMetadata; |
||||||
|
import org.springframework.jdbc.core.RowMapper; |
||||||
|
|
||||||
|
/** |
||||||
|
* Unit tests for {@link JdbcQueryMethod}. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
*/ |
||||||
|
public class JdbcQueryMethodUnitTests { |
||||||
|
|
||||||
|
public static final String DUMMY_SELECT = "SELECT something"; |
||||||
|
|
||||||
|
@Test // DATAJDBC-165
|
||||||
|
public void returnsSqlStatement() throws NoSuchMethodException { |
||||||
|
|
||||||
|
RepositoryMetadata metadata = mock(RepositoryMetadata.class); |
||||||
|
when(metadata.getReturnedDomainClass(any(Method.class))).thenReturn((Class) String.class); |
||||||
|
|
||||||
|
JdbcQueryMethod queryMethod = new JdbcQueryMethod(JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"), |
||||||
|
metadata, mock(ProjectionFactory.class)); |
||||||
|
|
||||||
|
assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-165
|
||||||
|
public void returnsSpecifiedRowMapperClass() throws NoSuchMethodException { |
||||||
|
|
||||||
|
RepositoryMetadata metadata = mock(RepositoryMetadata.class); |
||||||
|
when(metadata.getReturnedDomainClass(any(Method.class))).thenReturn((Class) String.class); |
||||||
|
|
||||||
|
JdbcQueryMethod queryMethod = new JdbcQueryMethod(JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"), |
||||||
|
metadata, mock(ProjectionFactory.class)); |
||||||
|
|
||||||
|
assertThat(queryMethod.getRowMapperClass()).isEqualTo(CustomRowMapper.class); |
||||||
|
} |
||||||
|
|
||||||
|
@Query(value = DUMMY_SELECT, rowMapperClass = CustomRowMapper.class) |
||||||
|
private void queryMethod() {} |
||||||
|
|
||||||
|
private class CustomRowMapper implements RowMapper { |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object mapRow(ResultSet rs, int rowNum) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,111 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.repository.support; |
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext; |
||||||
|
import org.springframework.data.repository.query.DefaultParameters; |
||||||
|
import org.springframework.data.repository.query.Parameters; |
||||||
|
import org.springframework.jdbc.core.RowMapper; |
||||||
|
import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
||||||
|
|
||||||
|
import java.sql.ResultSet; |
||||||
|
|
||||||
|
import static org.mockito.Mockito.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Unit tests for {@link JdbcRepositoryQuery}. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
*/ |
||||||
|
public class JdbcRepositoryQueryUnitTests { |
||||||
|
|
||||||
|
JdbcQueryMethod queryMethod; |
||||||
|
JdbcMappingContext context; |
||||||
|
RowMapper defaultRowMapper; |
||||||
|
JdbcRepositoryQuery query; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setup() throws NoSuchMethodException { |
||||||
|
|
||||||
|
Parameters parameters = new DefaultParameters(JdbcRepositoryQueryUnitTests.class.getDeclaredMethod("dummyMethod")); |
||||||
|
queryMethod = mock(JdbcQueryMethod.class); |
||||||
|
when(queryMethod.getParameters()).thenReturn(parameters); |
||||||
|
|
||||||
|
context = mock(JdbcMappingContext.class, RETURNS_DEEP_STUBS); |
||||||
|
defaultRowMapper = mock(RowMapper.class); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-165
|
||||||
|
public void emptyQueryThrowsException() { |
||||||
|
|
||||||
|
when(queryMethod.getAnnotatedQuery()).thenReturn(null); |
||||||
|
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper); |
||||||
|
|
||||||
|
Assertions.assertThatExceptionOfType(IllegalStateException.class) //
|
||||||
|
.isThrownBy(() -> query.execute(new Object[]{})); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-165
|
||||||
|
public void defaultRowMapperIsUsedByDefault() { |
||||||
|
|
||||||
|
when(queryMethod.getAnnotatedQuery()).thenReturn("some sql statement"); |
||||||
|
when(queryMethod.getRowMapperClass()).thenReturn((Class) RowMapper.class); |
||||||
|
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper); |
||||||
|
|
||||||
|
query.execute(new Object[]{}); |
||||||
|
|
||||||
|
verify(context.getTemplate()).queryForObject(anyString(), any(SqlParameterSource.class), eq(defaultRowMapper)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-165
|
||||||
|
public void defaultRowMapperIsUsedForNull() { |
||||||
|
|
||||||
|
when(queryMethod.getAnnotatedQuery()).thenReturn("some sql statement"); |
||||||
|
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper); |
||||||
|
|
||||||
|
query.execute(new Object[]{}); |
||||||
|
|
||||||
|
verify(context.getTemplate()).queryForObject(anyString(), any(SqlParameterSource.class), eq(defaultRowMapper)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // DATAJDBC-165
|
||||||
|
public void customRowMapperIsUsedWhenSpecified() { |
||||||
|
|
||||||
|
when(queryMethod.getAnnotatedQuery()).thenReturn("some sql statement"); |
||||||
|
when(queryMethod.getRowMapperClass()).thenReturn((Class) CustomRowMapper.class); |
||||||
|
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper); |
||||||
|
|
||||||
|
query.execute(new Object[]{}); |
||||||
|
|
||||||
|
verify(context.getTemplate()).queryForObject(anyString(), any(SqlParameterSource.class), isA(CustomRowMapper.class)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* The whole purpose of this method is to easily generate a {@link DefaultParameters} instance during test setup. |
||||||
|
*/ |
||||||
|
private void dummyMethod() { |
||||||
|
} |
||||||
|
|
||||||
|
private static class CustomRowMapper implements RowMapper { |
||||||
|
@Override |
||||||
|
public Object mapRow(ResultSet rs, int rowNum) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue