Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Fixing a test used for performance reasons.

Formatting a test.
Removing public modifier.
Separating test methods from infrastructure.

Original pull request #1863
See #1856
  • Loading branch information
schauder authored and mp911de committed Sep 4, 2024
1 parent b006923 commit 4221840
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOpera

this.query = queryMethod.getRequiredQuery();
this.spelEvaluator = queryContext.parse(query, getQueryMethod().getParameters());
this.containsSpelExpressions = !this.spelEvaluator.getQueryString().equals(queryContext);
this.containsSpelExpressions = !this.spelEvaluator.getQueryString().equals(query);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;
Expand Down Expand Up @@ -330,14 +329,13 @@ void appliesConverterToIterable() {
@Test // GH-1323
void queryByListOfTuples() {

String[][] tuples = {new String[]{"Albert", "Einstein"}, new String[]{"Richard", "Feynman"}};
String[][] tuples = { new String[] { "Albert", "Einstein" }, new String[] { "Richard", "Feynman" } };

SqlParameterSource parameterSource = forMethod("findByListOfTuples", List.class) //
.withArguments(Arrays.asList(tuples))
.withArguments(Arrays.asList(tuples)) //
.extractParameterSource();

assertThat(parameterSource.getValue("tuples"))
.asInstanceOf(LIST)
assertThat(parameterSource.getValue("tuples")).asInstanceOf(LIST) //
.containsExactly(tuples);

assertThat(parameterSource.getSqlType("tuples")).isEqualTo(JdbcUtil.TYPE_UNKNOWN.getVendorTypeNumber());
Expand All @@ -348,12 +346,38 @@ void queryByListOfConvertableTuples() {

SqlParameterSource parameterSource = forMethod("findByListOfTuples", List.class) //
.withCustomConverters(DirectionToIntegerConverter.INSTANCE) //
.withArguments(Arrays.asList(new Object[]{Direction.LEFT, "Einstein"}, new Object[]{Direction.RIGHT, "Feynman"}))
.withArguments(
Arrays.asList(new Object[] { Direction.LEFT, "Einstein" }, new Object[] { Direction.RIGHT, "Feynman" }))
.extractParameterSource();

assertThat(parameterSource.getValue("tuples"))
.asInstanceOf(LIST)
.containsExactly(new Object[][]{new Object[]{-1, "Einstein"}, new Object[]{1, "Feynman"}});
assertThat(parameterSource.getValue("tuples")).asInstanceOf(LIST) //
.containsExactly(new Object[][] { new Object[] { -1, "Einstein" }, new Object[] { 1, "Feynman" } });
}

@Test // GH-619
void spelCanBeUsedInsideQueries() {

JdbcQueryMethod queryMethod = createMethod("findBySpelExpression", Object.class);

List<EvaluationContextExtension> list = new ArrayList<>();
list.add(new MyEvaluationContextProvider());
QueryMethodEvaluationContextProvider evaluationContextProviderImpl = new ExtensionAwareQueryMethodEvaluationContextProvider(
list);

StringBasedJdbcQuery sut = new StringBasedJdbcQuery(queryMethod, operations, defaultRowMapper, converter,
evaluationContextProviderImpl);

ArgumentCaptor<SqlParameterSource> paramSource = ArgumentCaptor.forClass(SqlParameterSource.class);
ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class);

sut.execute(new Object[] { "myValue" });

verify(this.operations).queryForObject(query.capture(), paramSource.capture(), any(RowMapper.class));

assertThat(query.getValue())
.isEqualTo("SELECT * FROM table WHERE c = :__$synthetic$__1 AND c2 = :__$synthetic$__2");
assertThat(paramSource.getValue().getValue("__$synthetic$__1")).isEqualTo("test-value1");
assertThat(paramSource.getValue().getValue("__$synthetic$__2")).isEqualTo("test-value2");
}

QueryFixture forMethod(String name, Class... paramTypes) {
Expand Down Expand Up @@ -486,32 +510,6 @@ interface MyRepository extends Repository<Object, Long> {
Object findByListOfTuples(@Param("tuples") List<Object[]> tuples);
}

@Test // GH-619
public void spelCanBeUsedInsideQueries() {

JdbcQueryMethod queryMethod = createMethod("findBySpelExpression", Object.class);

List<EvaluationContextExtension> list = new ArrayList<>();
list.add(new MyEvaluationContextProvider());
QueryMethodEvaluationContextProvider evaluationContextProviderImpl = new ExtensionAwareQueryMethodEvaluationContextProvider(
list);

StringBasedJdbcQuery sut = new StringBasedJdbcQuery(queryMethod, operations, defaultRowMapper, converter,
evaluationContextProviderImpl);

ArgumentCaptor<SqlParameterSource> paramSource = ArgumentCaptor.forClass(SqlParameterSource.class);
ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class);

sut.execute(new Object[] { "myValue" });

verify(this.operations).queryForObject(query.capture(), paramSource.capture(), any(RowMapper.class));

assertThat(query.getValue())
.isEqualTo("SELECT * FROM table WHERE c = :__$synthetic$__1 AND c2 = :__$synthetic$__2");
assertThat(paramSource.getValue().getValue("__$synthetic$__1")).isEqualTo("test-value1");
assertThat(paramSource.getValue().getValue("__$synthetic$__2")).isEqualTo("test-value2");
}

private static class CustomRowMapper implements RowMapper<Object> {

@Override
Expand Down

0 comments on commit 4221840

Please sign in to comment.