|
| 1 | +/* |
| 2 | + * Copyright 2017 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.data.jdbc.core; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import lombok.RequiredArgsConstructor; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | +import org.mockito.ArgumentCaptor; |
| 27 | +import org.springframework.data.annotation.Id; |
| 28 | +import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy; |
| 29 | +import org.springframework.data.jdbc.mapping.model.JdbcMappingContext; |
| 30 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 31 | +import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
| 32 | +import org.springframework.jdbc.support.KeyHolder; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Jens Schauder |
| 36 | + */ |
| 37 | +public class DefaultDataAccessStrategyUnitTests { |
| 38 | + |
| 39 | + public static final long ID_FROM_ADDITIONAL_VALUES = 23L; |
| 40 | + public static final long ORIGINAL_ID = 4711L; |
| 41 | + |
| 42 | + JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy()); |
| 43 | + NamedParameterJdbcOperations jdbcOperations = mock(NamedParameterJdbcOperations.class); |
| 44 | + HashMap<String, Object> additionalParameters = new HashMap<>(); |
| 45 | + ArgumentCaptor<SqlParameterSource> captor = ArgumentCaptor.forClass(SqlParameterSource.class); |
| 46 | + |
| 47 | + DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy( // |
| 48 | + new SqlGeneratorSource(context), // |
| 49 | + jdbcOperations, // |
| 50 | + context // |
| 51 | + ); |
| 52 | + |
| 53 | + @Test // DATAJDBC-146 |
| 54 | + public void additionalParameterForIdDoesNotLeadToDuplicateParameters() { |
| 55 | + |
| 56 | + additionalParameters.put("id", ID_FROM_ADDITIONAL_VALUES); |
| 57 | + |
| 58 | + accessStrategy.insert(new DummyEntity(ORIGINAL_ID), DummyEntity.class, additionalParameters); |
| 59 | + |
| 60 | + verify(jdbcOperations).update(eq("insert into DummyEntity (id) values (:id)"), captor.capture(), |
| 61 | + any(KeyHolder.class)); |
| 62 | + assertThat(captor.getValue().getValue("id")).isEqualTo(ID_FROM_ADDITIONAL_VALUES); |
| 63 | + } |
| 64 | + |
| 65 | + @Test // DATAJDBC-146 |
| 66 | + public void additionalParametersGetAddedToStatement() { |
| 67 | + |
| 68 | + additionalParameters.put("reference", ID_FROM_ADDITIONAL_VALUES); |
| 69 | + |
| 70 | + accessStrategy.insert(new DummyEntity(ORIGINAL_ID), DummyEntity.class, additionalParameters); |
| 71 | + |
| 72 | + verify(jdbcOperations).update(eq("insert into DummyEntity (id, reference) values (:id, :reference)"), |
| 73 | + captor.capture(), any(KeyHolder.class)); |
| 74 | + assertThat(captor.getValue().getValue("id")).isEqualTo(ORIGINAL_ID); |
| 75 | + } |
| 76 | + |
| 77 | + @RequiredArgsConstructor |
| 78 | + private static class DummyEntity { |
| 79 | + |
| 80 | + @Id private final Long id; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments