|
| 1 | +/* |
| 2 | + * Copyright 2018 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.mapping.model; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import org.springframework.data.annotation.Id; |
| 20 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 21 | + |
| 22 | +import java.time.LocalDateTime; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | + |
| 28 | +/** |
| 29 | + * Unit tests for the {@link NamingStrategy}. |
| 30 | + * |
| 31 | + * @author Kazuki Shimizu |
| 32 | + */ |
| 33 | +public class NamingStrategyUnitTests { |
| 34 | + |
| 35 | + private final NamingStrategy target = NamingStrategy.INSTANCE; |
| 36 | + private final JdbcMappingContext context = new JdbcMappingContext(target, mock(NamedParameterJdbcOperations.class), mock(ConversionCustomizer.class)); |
| 37 | + private final JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class); |
| 38 | + |
| 39 | + @Test |
| 40 | + public void getTableName() { |
| 41 | + assertThat(target.getTableName(persistentEntity.getType())) |
| 42 | + .isEqualTo("DummyEntity"); |
| 43 | + assertThat(target.getTableName(DummySubEntity.class)) |
| 44 | + .isEqualTo("dummy_sub_entity"); // DATAJDBC-106 |
| 45 | + } |
| 46 | + |
| 47 | + @Test // DATAJDBC-106 |
| 48 | + public void getTableNameWithTableAnnotation() { |
| 49 | + assertThat(target.getTableName(DummySubEntity.class)) |
| 50 | + .isEqualTo("dummy_sub_entity"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void getColumnName() { |
| 55 | + assertThat(target.getColumnName(persistentEntity.getPersistentProperty("id"))) |
| 56 | + .isEqualTo("id"); |
| 57 | + assertThat(target.getColumnName(persistentEntity.getPersistentProperty("createdAt"))) |
| 58 | + .isEqualTo("createdAt"); |
| 59 | + assertThat(target.getColumnName(persistentEntity.getPersistentProperty("dummySubEntities"))) |
| 60 | + .isEqualTo("dummySubEntities"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test // DATAJDBC-106 |
| 64 | + public void getColumnNameWithColumnAnnotation() { |
| 65 | + assertThat(target.getColumnName(persistentEntity.getPersistentProperty("name"))) |
| 66 | + .isEqualTo("dummy_name"); |
| 67 | + assertThat(target.getColumnName(persistentEntity.getPersistentProperty("lastUpdatedAt"))) |
| 68 | + .isEqualTo("dummy_last_updated_at"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void getReverseColumnName() { |
| 73 | + assertThat(target.getReverseColumnName(persistentEntity.getPersistentProperty("dummySubEntities"))) |
| 74 | + .isEqualTo("DummyEntity"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void getKeyColumn() { |
| 79 | + assertThat(target.getKeyColumn(persistentEntity.getPersistentProperty("dummySubEntities"))) |
| 80 | + .isEqualTo("DummyEntity_key"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void getSchema() { |
| 85 | + assertThat(target.getSchema()) |
| 86 | + .isEmpty(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void getQualifiedTableName() { |
| 91 | + assertThat(target.getQualifiedTableName(persistentEntity.getType())) |
| 92 | + .isEqualTo("DummyEntity"); |
| 93 | + |
| 94 | + NamingStrategy strategy = new NamingStrategy() { |
| 95 | + @Override |
| 96 | + public String getSchema() { |
| 97 | + return "schema"; |
| 98 | + } |
| 99 | + }; |
| 100 | + assertThat(strategy.getQualifiedTableName(persistentEntity.getType())) |
| 101 | + .isEqualTo("schema.DummyEntity"); |
| 102 | + } |
| 103 | + |
| 104 | + private static class DummyEntity { |
| 105 | + @Id |
| 106 | + private int id; |
| 107 | + @Column("dummy_name") |
| 108 | + private String name; |
| 109 | + private LocalDateTime createdAt; |
| 110 | + private LocalDateTime lastUpdatedAt; |
| 111 | + private List<DummySubEntity> dummySubEntities; |
| 112 | + @Column("dummy_last_updated_at") |
| 113 | + public LocalDateTime getLastUpdatedAt() { |
| 114 | + return LocalDateTime.now(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Table("dummy_sub_entity") |
| 119 | + private static class DummySubEntity { |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments