DATAJDBC-106 - Polishing.
Formatting and Javadoc. Original pull request: #65
This commit is contained in:
@@ -22,13 +22,13 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* The annotation to configure a mapping database column.
|
||||
* The annotation to configure the mapping from an attribute to a database column.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @since 1.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@Target({ ElementType.FIELD, ElementType.METHOD })
|
||||
@Documented
|
||||
public @interface Column {
|
||||
|
||||
|
||||
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.model;
|
||||
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
|
||||
/**
|
||||
* Interface and default implementation of a naming strategy. Defaults to no schema, table name based on {@link Class}
|
||||
* and column name based on {@link JdbcPersistentProperty}.
|
||||
@@ -28,6 +28,8 @@ import java.util.Optional;
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Michael Simons
|
||||
* @author Kazuki Shimizu
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface NamingStrategy {
|
||||
@@ -52,20 +54,22 @@ public interface NamingStrategy {
|
||||
* Look up the {@link Class}'s simple name or {@link Table#value()}.
|
||||
*/
|
||||
default String getTableName(Class<?> type) {
|
||||
|
||||
Table table = AnnotatedElementUtils.findMergedAnnotation(type, Table.class);
|
||||
return Optional.ofNullable(table)//
|
||||
.map(Table::value)//
|
||||
.orElse(type.getSimpleName());
|
||||
.map(Table::value)//
|
||||
.orElse(type.getSimpleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up the {@link JdbcPersistentProperty}'s name or {@link Column#value()}.
|
||||
*/
|
||||
default String getColumnName(JdbcPersistentProperty property) {
|
||||
|
||||
Column column = property.findAnnotation(Column.class);
|
||||
return Optional.ofNullable(column)//
|
||||
.map(Column::value)//
|
||||
.orElse(property.getName());
|
||||
.map(Column::value)//
|
||||
.orElse(property.getName());
|
||||
}
|
||||
|
||||
default String getQualifiedTableName(Class<?> type) {
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* The annotation to configure a mapping database table.
|
||||
* The annotation to configure the mapping from a class to a database table.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @since 1.0
|
||||
|
||||
@@ -15,15 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.jdbc.mapping.model;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link NamingStrategy}.
|
||||
@@ -33,63 +33,65 @@ import static org.mockito.Mockito.mock;
|
||||
public class NamingStrategyUnitTests {
|
||||
|
||||
private final NamingStrategy target = NamingStrategy.INSTANCE;
|
||||
private final JdbcMappingContext context = new JdbcMappingContext(target, mock(NamedParameterJdbcOperations.class), mock(ConversionCustomizer.class));
|
||||
private final JdbcMappingContext context = new JdbcMappingContext(
|
||||
target,
|
||||
mock(NamedParameterJdbcOperations.class),
|
||||
mock(ConversionCustomizer.class));
|
||||
private final JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
|
||||
|
||||
@Test
|
||||
public void getTableName() {
|
||||
assertThat(target.getTableName(persistentEntity.getType()))
|
||||
.isEqualTo("DummyEntity");
|
||||
assertThat(target.getTableName(DummySubEntity.class))
|
||||
.isEqualTo("dummy_sub_entity"); // DATAJDBC-106
|
||||
|
||||
assertThat(target.getTableName(persistentEntity.getType())).isEqualTo("DummyEntity");
|
||||
assertThat(target.getTableName(DummySubEntity.class)).isEqualTo("dummy_sub_entity"); // DATAJDBC-106
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-106
|
||||
public void getTableNameWithTableAnnotation() {
|
||||
assertThat(target.getTableName(DummySubEntity.class))
|
||||
.isEqualTo("dummy_sub_entity");
|
||||
|
||||
assertThat(target.getTableName(DummySubEntity.class)).isEqualTo("dummy_sub_entity");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumnName() {
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("id")))
|
||||
.isEqualTo("id");
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("createdAt")))
|
||||
.isEqualTo("createdAt");
|
||||
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("id"))).isEqualTo("id");
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("createdAt"))).isEqualTo("createdAt");
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("dummySubEntities")))
|
||||
.isEqualTo("dummySubEntities");
|
||||
}
|
||||
|
||||
@Test // DATAJDBC-106
|
||||
public void getColumnNameWithColumnAnnotation() {
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("name")))
|
||||
.isEqualTo("dummy_name");
|
||||
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("name"))).isEqualTo("dummy_name");
|
||||
assertThat(target.getColumnName(persistentEntity.getPersistentProperty("lastUpdatedAt")))
|
||||
.isEqualTo("dummy_last_updated_at");
|
||||
.isEqualTo("dummy_last_updated_at");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReverseColumnName() {
|
||||
|
||||
assertThat(target.getReverseColumnName(persistentEntity.getPersistentProperty("dummySubEntities")))
|
||||
.isEqualTo("DummyEntity");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getKeyColumn() {
|
||||
|
||||
assertThat(target.getKeyColumn(persistentEntity.getPersistentProperty("dummySubEntities")))
|
||||
.isEqualTo("DummyEntity_key");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSchema() {
|
||||
assertThat(target.getSchema())
|
||||
.isEmpty();
|
||||
assertThat(target.getSchema()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getQualifiedTableName() {
|
||||
assertThat(target.getQualifiedTableName(persistentEntity.getType()))
|
||||
.isEqualTo("DummyEntity");
|
||||
|
||||
assertThat(target.getQualifiedTableName(persistentEntity.getType())).isEqualTo("DummyEntity");
|
||||
|
||||
NamingStrategy strategy = new NamingStrategy() {
|
||||
@Override
|
||||
@@ -97,18 +99,18 @@ public class NamingStrategyUnitTests {
|
||||
return "schema";
|
||||
}
|
||||
};
|
||||
assertThat(strategy.getQualifiedTableName(persistentEntity.getType()))
|
||||
.isEqualTo("schema.DummyEntity");
|
||||
|
||||
assertThat(strategy.getQualifiedTableName(persistentEntity.getType())).isEqualTo("schema.DummyEntity");
|
||||
}
|
||||
|
||||
private static class DummyEntity {
|
||||
@Id
|
||||
private int id;
|
||||
@Column("dummy_name")
|
||||
private String name;
|
||||
|
||||
@Id private int id;
|
||||
@Column("dummy_name") private String name;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime lastUpdatedAt;
|
||||
private List<DummySubEntity> dummySubEntities;
|
||||
|
||||
@Column("dummy_last_updated_at")
|
||||
public LocalDateTime getLastUpdatedAt() {
|
||||
return LocalDateTime.now();
|
||||
@@ -116,7 +118,5 @@ public class NamingStrategyUnitTests {
|
||||
}
|
||||
|
||||
@Table("dummy_sub_entity")
|
||||
private static class DummySubEntity {
|
||||
}
|
||||
|
||||
private static class DummySubEntity {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user