Browse Source

DATAJDBC-107 - Polishing.

Code formatting.
Added @author tag.
Tiny code simplifications.
pull/13/head
Jens Schauder 8 years ago
parent
commit
fe5ae93fd0
  1. 8
      src/main/java/org/springframework/data/jdbc/mapping/model/BasicJdbcPersistentProperty.java
  2. 3
      src/main/java/org/springframework/data/jdbc/mapping/model/JdbcMappingContext.java
  3. 1
      src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentEntityImpl.java
  4. 4
      src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java
  5. 19
      src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java
  6. 3
      src/test/java/org/springframework/data/jdbc/core/SqlGeneratorContextBasedNamingStrategyUnitTests.java
  7. 62
      src/test/java/org/springframework/data/jdbc/core/SqlGeneratorFixedNamingStrategyUnitTests.java
  8. 13
      src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java
  9. 9
      src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java

8
src/main/java/org/springframework/data/jdbc/mapping/model/BasicJdbcPersistentProperty.java

@ -26,12 +26,14 @@ import org.springframework.data.mapping.PersistentEntity; @@ -26,12 +26,14 @@ import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Meta data about a property to be used by repository implementations.
*
* @author Jens Schauder
* @author Greg Turnquist
* @since 2.0
*/
public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProperty<JdbcPersistentProperty>
@ -52,11 +54,15 @@ public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProper @@ -52,11 +54,15 @@ public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProper
* @param property must not be {@literal null}.
* @param owner must not be {@literal null}.
* @param simpleTypeHolder must not be {@literal null}.
* @param context
* @param context must not be {@literal null}
*/
public BasicJdbcPersistentProperty(Property property, PersistentEntity<?, JdbcPersistentProperty> owner,
SimpleTypeHolder simpleTypeHolder, JdbcMappingContext context) {
super(property, owner, simpleTypeHolder);
Assert.notNull(context, "context must not be null.");
this.context = context;
}

3
src/main/java/org/springframework/data/jdbc/mapping/model/JdbcMappingContext.java

@ -38,6 +38,7 @@ import org.springframework.data.util.TypeInformation; @@ -38,6 +38,7 @@ import org.springframework.data.util.TypeInformation;
* {@link MappingContext} implementation for JDBC.
*
* @author Jens Schauder
* @author Greg Turnquist
* @since 2.0
*/
public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEntity<?>, JdbcPersistentProperty> {
@ -63,8 +64,6 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt @@ -63,8 +64,6 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
Class<?> currentType = path == null ? rootType : PropertyPaths.getLeafType(path);
JdbcPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(currentType);
String rootPrefix = path == null ? "" : path.toDotPath() + ".";
for (JdbcPersistentProperty property : persistentEntity) {
if (property.isEntity()) {

1
src/main/java/org/springframework/data/jdbc/mapping/model/JdbcPersistentEntityImpl.java

@ -24,6 +24,7 @@ import org.springframework.data.util.TypeInformation; @@ -24,6 +24,7 @@ import org.springframework.data.util.TypeInformation;
* Meta data a repository might need for implementing persistence operations for instances of type {@code T}
*
* @author Jens Schauder
* @author Greg Turnquist
* @since 2.0
*/
class JdbcPersistentEntityImpl<T> extends BasicPersistentEntity<T, JdbcPersistentProperty>

4
src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java

@ -31,6 +31,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; @@ -31,6 +31,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
/**
* @author Jens Schauder
* @author Greg Turnquist
* @since 2.0
*/
public class JdbcRepositoryFactory extends RepositoryFactorySupport {
@ -39,7 +40,8 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport { @@ -39,7 +40,8 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
private final NamedParameterJdbcOperations jdbcOperations;
private final ApplicationEventPublisher publisher;
public JdbcRepositoryFactory(NamedParameterJdbcOperations namedParameterJdbcOperations, ApplicationEventPublisher publisher, NamingStrategy namingStrategy) {
public JdbcRepositoryFactory(NamedParameterJdbcOperations namedParameterJdbcOperations,
ApplicationEventPublisher publisher, NamingStrategy namingStrategy) {
this.jdbcOperations = namedParameterJdbcOperations;
this.publisher = publisher;

19
src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java

@ -38,6 +38,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; @@ -38,6 +38,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
* repository factories via Spring configuration.
*
* @author Jens Schauder
* @author Greg Turnquist
* @since 2.0
*/
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> //
@ -47,8 +48,6 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend @@ -47,8 +48,6 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
"No unique NamedParameterJdbcOperation could be found, " //
+ "nor JdbcOperations or DataSource to construct one from.";
private static final String NO_NAMING_STRATEGY_ERROR_MESSAGE = "No unique NamingStrategy could be found.";
private static final String NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME = "namedParameterJdbcTemplate";
private static final String JDBC_OPERATIONS_BEAN_NAME = "jdbcTemplate";
private static final String DATA_SOURCE_BEAN_NAME = "dataSource";
@ -67,23 +66,21 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend @@ -67,23 +66,21 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher, findOrCreateNamingStrategy());
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher,
findOrCreateNamingStrategy());
}
private NamedParameterJdbcOperations findOrCreateJdbcOperations() {
return Optionals
.firstNonEmpty( //
this::getNamedParameterJdbcOperations, //
() -> getJdbcOperations().map(NamedParameterJdbcTemplate::new), //
() -> getDataSource().map(NamedParameterJdbcTemplate::new)) //
return Optionals.firstNonEmpty( //
this::getNamedParameterJdbcOperations, //
() -> getJdbcOperations().map(NamedParameterJdbcTemplate::new), //
() -> getDataSource().map(NamedParameterJdbcTemplate::new)) //
.orElseThrow(() -> new IllegalStateException(NO_NAMED_PARAMETER_JDBC_OPERATION_ERROR_MESSAGE));
}
private NamingStrategy findOrCreateNamingStrategy() {
return getNamingStrategy()
.orElse(new DefaultNamingStrategy());
return getNamingStrategy().orElse(new DefaultNamingStrategy());
}
private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() {

3
src/test/java/org/springframework/data/jdbc/core/SqlGeneratorContextBasedNamingStrategyUnitTests.java

@ -191,6 +191,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests { @@ -191,6 +191,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests {
return new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
}
@SuppressWarnings("unused")
static class DummyEntity {
@Id Long id;
@ -198,6 +199,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests { @@ -198,6 +199,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests {
ReferencedEntity ref;
}
@SuppressWarnings("unused")
static class ReferencedEntity {
@Id Long l1id;
@ -205,6 +207,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests { @@ -205,6 +207,7 @@ public class SqlGeneratorContextBasedNamingStrategyUnitTests {
SecondLevelReferencedEntity further;
}
@SuppressWarnings("unused")
static class SecondLevelReferencedEntity {
@Id Long l2id;

62
src/test/java/org/springframework/data/jdbc/core/SqlGeneratorFixedNamingStrategyUnitTests.java

@ -28,10 +28,8 @@ import org.springframework.data.jdbc.mapping.model.NamingStrategy; @@ -28,10 +28,8 @@ import org.springframework.data.jdbc.mapping.model.NamingStrategy;
import org.springframework.data.mapping.PropertyPath;
/**
* Unit tests to a fixed {@link NamingStrategy} implementation containing a hard wired schema, table, and property prefix.
*
* NOTE: Due to the need to verify SQL generation and {@link SqlGenerator}'s package-private status suggests
* this unit test exist in this package, not {@literal org.springframework.data.jdbc.mappings.model}.
* Unit tests the {@link SqlGenerator} with a fixed {@link NamingStrategy} implementation containing a hard wired
* schema, table, and property prefix.
*
* @author Greg Turnquist
*/
@ -61,7 +59,7 @@ public class SqlGeneratorFixedNamingStrategyUnitTests { @@ -61,7 +59,7 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
public String getTableName(Class<?> type) {
return type.getSimpleName().toUpperCase();
}
@Override
public String getColumnName(JdbcPersistentProperty property) {
return property.getName().toLowerCase();
@ -77,12 +75,14 @@ public class SqlGeneratorFixedNamingStrategyUnitTests { @@ -77,12 +75,14 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(sql) //
.startsWith("SELECT") //
.contains("FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_id AS FixedCustomPropertyPrefix_id,") //
.contains("FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_name AS FixedCustomPropertyPrefix_name,") //
.contains("ref.FixedCustomPropertyPrefix_l1id AS ref_FixedCustomPropertyPrefix_l1id") //
.contains("ref.FixedCustomPropertyPrefix_content AS ref_FixedCustomPropertyPrefix_content") //
.contains("FROM FixedCustomSchema.FixedCustomTablePrefix_DummyEntity");
.startsWith("SELECT") //
.contains(
"FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_id AS FixedCustomPropertyPrefix_id,") //
.contains(
"FixedCustomSchema.FixedCustomTablePrefix_DummyEntity.FixedCustomPropertyPrefix_name AS FixedCustomPropertyPrefix_name,") //
.contains("ref.FixedCustomPropertyPrefix_l1id AS ref_FixedCustomPropertyPrefix_l1id") //
.contains("ref.FixedCustomPropertyPrefix_content AS ref_FixedCustomPropertyPrefix_content") //
.contains("FROM FixedCustomSchema.FixedCustomTablePrefix_DummyEntity");
softAssertions.assertAll();
}
@ -95,12 +95,12 @@ public class SqlGeneratorFixedNamingStrategyUnitTests { @@ -95,12 +95,12 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(sql) //
.startsWith("SELECT") //
.contains("DUMMYENTITY.id AS id,") //
.contains("DUMMYENTITY.name AS name,") //
.contains("ref.l1id AS ref_l1id") //
.contains("ref.content AS ref_content") //
.contains("FROM DUMMYENTITY");
.startsWith("SELECT") //
.contains("DUMMYENTITY.id AS id,") //
.contains("DUMMYENTITY.name AS name,") //
.contains("ref.l1id AS ref_l1id") //
.contains("ref.content AS ref_content") //
.contains("FROM DUMMYENTITY");
softAssertions.assertAll();
}
@ -111,8 +111,8 @@ public class SqlGeneratorFixedNamingStrategyUnitTests { @@ -111,8 +111,8 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref", DummyEntity.class));
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId");
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId");
}
@Test // DATAJDBC-107
@ -122,12 +122,10 @@ public class SqlGeneratorFixedNamingStrategyUnitTests { @@ -122,12 +122,10 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteByPath(PropertyPath.from("ref.further", DummyEntity.class));
assertThat(sql).isEqualTo(
"DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN " +
"(SELECT FixedCustomPropertyPrefix_l1id " +
"FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId)");
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity "
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN "
+ "(SELECT FixedCustomPropertyPrefix_l1id " + "FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity = :rootId)");
}
@Test // DATAJDBC-107
@ -147,8 +145,8 @@ public class SqlGeneratorFixedNamingStrategyUnitTests { @@ -147,8 +145,8 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref", DummyEntity.class));
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL");
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL");
}
@Test // DATAJDBC-107
@ -158,12 +156,10 @@ public class SqlGeneratorFixedNamingStrategyUnitTests { @@ -158,12 +156,10 @@ public class SqlGeneratorFixedNamingStrategyUnitTests {
String sql = sqlGenerator.createDeleteAllSql(PropertyPath.from("ref.further", DummyEntity.class));
assertThat(sql).isEqualTo(
"DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN " +
"(SELECT FixedCustomPropertyPrefix_l1id " +
"FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity " +
"WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL)");
assertThat(sql).isEqualTo("DELETE FROM FixedCustomSchema.FixedCustomTablePrefix_SecondLevelReferencedEntity "
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity IN "
+ "(SELECT FixedCustomPropertyPrefix_l1id " + "FROM FixedCustomSchema.FixedCustomTablePrefix_ReferencedEntity "
+ "WHERE FixedCustomSchema.FixedCustomTablePrefix_DummyEntity IS NOT NULL)");
}
/**

13
src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java

@ -31,20 +31,18 @@ import org.springframework.data.mapping.PropertyPath; @@ -31,20 +31,18 @@ import org.springframework.data.mapping.PropertyPath;
* Unit tests for the {@link SqlGenerator}.
*
* @author Jens Schauder
* @author Greg Turnquist
*/
public class SqlGeneratorUnitTests {
private NamingStrategy namingStrategy;
private JdbcMappingContext context;
private JdbcPersistentEntity<?> persistentEntity;
private SqlGenerator sqlGenerator;
@Before
public void setUp() {
this.namingStrategy = new DefaultNamingStrategy();
this.context = new JdbcMappingContext(namingStrategy);
this.persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
NamingStrategy namingStrategy = new DefaultNamingStrategy();
JdbcMappingContext context = new JdbcMappingContext(namingStrategy);
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
this.sqlGenerator = new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
}
@ -105,6 +103,7 @@ public class SqlGeneratorUnitTests { @@ -105,6 +103,7 @@ public class SqlGeneratorUnitTests {
"DELETE FROM SecondLevelReferencedEntity WHERE ReferencedEntity IN (SELECT l1id FROM ReferencedEntity WHERE DummyEntity IS NOT NULL)");
}
@SuppressWarnings("unused")
static class DummyEntity {
@Id Long id;
@ -112,6 +111,7 @@ public class SqlGeneratorUnitTests { @@ -112,6 +111,7 @@ public class SqlGeneratorUnitTests {
ReferencedEntity ref;
}
@SuppressWarnings("unused")
static class ReferencedEntity {
@Id Long l1id;
@ -119,6 +119,7 @@ public class SqlGeneratorUnitTests { @@ -119,6 +119,7 @@ public class SqlGeneratorUnitTests {
SecondLevelReferencedEntity further;
}
@SuppressWarnings("unused")
static class SecondLevelReferencedEntity {
@Id Long l2id;

9
src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIdGenerationIntegrationTests.java

@ -47,6 +47,7 @@ import org.springframework.test.context.junit4.rules.SpringMethodRule; @@ -47,6 +47,7 @@ import org.springframework.test.context.junit4.rules.SpringMethodRule;
* Testing special cases for id generation with {@link SimpleJdbcRepository}.
*
* @author Jens Schauder
* @author Greg Turnquist
*/
@ContextConfiguration
@EnableJdbcRepositories(considerNestedRepositories = true)
@ -131,8 +132,8 @@ public class JdbcRepositoryIdGenerationIntegrationTests { @@ -131,8 +132,8 @@ public class JdbcRepositoryIdGenerationIntegrationTests {
}
/**
* {@link NamingStrategy} that harmlessly uppercases the table name,
* demonstrating how to inject one while not breaking existing SQL operations.
* {@link NamingStrategy} that harmlessly uppercases the table name, demonstrating how to inject one while not
* breaking existing SQL operations.
*/
@Bean
NamingStrategy namingStrategy() {
@ -152,8 +153,8 @@ public class JdbcRepositoryIdGenerationIntegrationTests { @@ -152,8 +153,8 @@ public class JdbcRepositoryIdGenerationIntegrationTests {
@Bean
ReadOnlyIdEntityRepository readOnlyIdRepository(DataSource db, NamingStrategy namingStrategy) {
return new JdbcRepositoryFactory(new NamedParameterJdbcTemplate(db), mock(ApplicationEventPublisher.class), namingStrategy)
.getRepository(ReadOnlyIdEntityRepository.class);
return new JdbcRepositoryFactory(new NamedParameterJdbcTemplate(db), mock(ApplicationEventPublisher.class),
namingStrategy).getRepository(ReadOnlyIdEntityRepository.class);
}
@Bean

Loading…
Cancel
Save