|
| 1 | +/* |
| 2 | + * Copyright 2017-2020 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 | + * https://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 org.junit.ClassRule; |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.context.ApplicationEventPublisher; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.context.annotation.Import; |
| 26 | +import org.springframework.data.annotation.Id; |
| 27 | +import org.springframework.data.jdbc.core.convert.DataAccessStrategy; |
| 28 | +import org.springframework.data.jdbc.core.convert.JdbcConverter; |
| 29 | +import org.springframework.data.jdbc.testing.TestConfiguration; |
| 30 | +import org.springframework.data.relational.core.mapping.NamingStrategy; |
| 31 | +import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
| 32 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 33 | +import org.springframework.test.context.ContextConfiguration; |
| 34 | +import org.springframework.test.context.junit4.rules.SpringClassRule; |
| 35 | +import org.springframework.test.context.junit4.rules.SpringMethodRule; |
| 36 | +import org.springframework.transaction.annotation.Transactional; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.*; |
| 39 | + |
| 40 | +/** |
| 41 | + * Integration tests for {@link JdbcAggregateTemplate} using an entity mapped with an explicite schema. |
| 42 | + * |
| 43 | + * @author Jens Schauder |
| 44 | + */ |
| 45 | +@ContextConfiguration |
| 46 | +@Transactional |
| 47 | +public class JdbcAggregateTemplateSchemaIntegrationTests { |
| 48 | + |
| 49 | + @ClassRule public static final SpringClassRule classRule = new SpringClassRule(); |
| 50 | + @Rule public SpringMethodRule methodRule = new SpringMethodRule(); |
| 51 | + |
| 52 | + @Autowired JdbcAggregateOperations template; |
| 53 | + @Autowired NamedParameterJdbcOperations jdbcTemplate; |
| 54 | + |
| 55 | + |
| 56 | + @Test |
| 57 | + public void insertFindUpdateDelete() { |
| 58 | + |
| 59 | + DummyEntity entity = new DummyEntity(); |
| 60 | + entity.name = "Alfred"; |
| 61 | + entity.reference = new Referenced(); |
| 62 | + entity.reference.name = "Peter"; |
| 63 | + |
| 64 | + template.save(entity); |
| 65 | + |
| 66 | + DummyEntity reloaded = template.findById(entity.id, DummyEntity.class); |
| 67 | + |
| 68 | + assertThat(reloaded).isNotNull(); |
| 69 | + |
| 70 | + reloaded.name += " E. Neumann"; |
| 71 | + |
| 72 | + template.save(reloaded); |
| 73 | + |
| 74 | + template.deleteById(reloaded.id, DummyEntity.class); |
| 75 | + } |
| 76 | + |
| 77 | + static class DummyEntity { |
| 78 | + |
| 79 | + @Id Long id; |
| 80 | + String name; |
| 81 | + Referenced reference; |
| 82 | + } |
| 83 | + |
| 84 | + static class Referenced { |
| 85 | + String name; |
| 86 | + } |
| 87 | + |
| 88 | + @Configuration |
| 89 | + @Import(TestConfiguration.class) |
| 90 | + static class Config { |
| 91 | + |
| 92 | + @Bean |
| 93 | + Class<?> testClass() { |
| 94 | + return JdbcAggregateTemplateSchemaIntegrationTests.class; |
| 95 | + } |
| 96 | + |
| 97 | + @Bean |
| 98 | + JdbcAggregateOperations operations(ApplicationEventPublisher publisher, RelationalMappingContext context, |
| 99 | + DataAccessStrategy dataAccessStrategy, JdbcConverter converter) { |
| 100 | + return new JdbcAggregateTemplate(publisher, context, converter, dataAccessStrategy); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + NamingStrategy namingStrategy() { |
| 105 | + return new NamingStrategy() { |
| 106 | + @Override |
| 107 | + public String getSchema() { |
| 108 | + return "other"; |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments