DATACASS-282 - Polishing.
Encapsulate fields in BasicCassandraMappingContext, BasicCassandraPersistentEntity, BasicCassandraPersistentProperty, and refactor to immutable fields where possible.
This commit is contained in:
@@ -69,26 +69,26 @@ public class BasicCassandraMappingContext
|
||||
extends AbstractMappingContext<CassandraPersistentEntity<?>, CassandraPersistentProperty>
|
||||
implements CassandraMappingContext, ApplicationContextAware {
|
||||
|
||||
protected ApplicationContext context;
|
||||
|
||||
protected CassandraPersistentEntityMetadataVerifier verifier = new CompositeCassandraPersistentEntityMetadataVerifier();
|
||||
|
||||
protected ClassLoader beanClassLoader;
|
||||
|
||||
protected Mapping mapping = new Mapping();
|
||||
|
||||
// useful caches
|
||||
protected Map<Class<?>, CassandraPersistentEntity<?>> entitiesByType = new HashMap<>();
|
||||
protected Map<CqlIdentifier, Set<CassandraPersistentEntity<?>>> entitySetsByTableName = new HashMap<>();
|
||||
|
||||
protected Set<CassandraPersistentEntity<?>> primaryKeyEntities = new HashSet<>();
|
||||
protected Set<CassandraPersistentEntity<?>> userDefinedTypes = new HashSet<>();
|
||||
protected Set<CassandraPersistentEntity<?>> tableEntities = new HashSet<>();
|
||||
private CassandraPersistentEntityMetadataVerifier verifier = new CompositeCassandraPersistentEntityMetadataVerifier();
|
||||
|
||||
private CustomConversions customConversions;
|
||||
|
||||
private Mapping mapping = new Mapping();
|
||||
|
||||
private UserTypeResolver userTypeResolver;
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
// useful caches
|
||||
private final Map<Class<?>, CassandraPersistentEntity<?>> entitiesByType = new HashMap<>();
|
||||
private final Map<CqlIdentifier, Set<CassandraPersistentEntity<?>>> entitySetsByTableName = new HashMap<>();
|
||||
|
||||
private final Set<CassandraPersistentEntity<?>> primaryKeyEntities = new HashSet<>();
|
||||
private final Set<CassandraPersistentEntity<?>> userDefinedTypes = new HashSet<>();
|
||||
private final Set<CassandraPersistentEntity<?>> tableEntities = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Create a new {@link BasicCassandraMappingContext}.
|
||||
*/
|
||||
|
||||
@@ -52,22 +52,22 @@ import com.datastax.driver.core.UserType;
|
||||
public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T, CassandraPersistentProperty>
|
||||
implements CassandraPersistentEntity<T>, ApplicationContextAware {
|
||||
|
||||
protected static final CassandraPersistentEntityMetadataVerifier DEFAULT_VERIFIER = new CompositeCassandraPersistentEntityMetadataVerifier();
|
||||
private static final CassandraPersistentEntityMetadataVerifier DEFAULT_VERIFIER = new CompositeCassandraPersistentEntityMetadataVerifier();
|
||||
|
||||
private static final Optional<Comparator<CassandraPersistentProperty>> PROPERTY_COMPARATOR = Optional
|
||||
.of(CassandraPersistentPropertyComparator.INSTANCE);
|
||||
|
||||
protected ApplicationContext context;
|
||||
private CassandraPersistentEntityMetadataVerifier verifier = DEFAULT_VERIFIER;
|
||||
|
||||
protected Optional<Boolean> forceQuote = Optional.empty();
|
||||
private CassandraMappingContext mappingContext;
|
||||
|
||||
protected CassandraMappingContext mappingContext;
|
||||
private ApplicationContext context;
|
||||
|
||||
protected CassandraPersistentEntityMetadataVerifier verifier = DEFAULT_VERIFIER;
|
||||
private StandardEvaluationContext spelContext;
|
||||
|
||||
protected Optional<CqlIdentifier> tableName = Optional.empty();
|
||||
private Optional<Boolean> forceQuote = Optional.empty();
|
||||
|
||||
protected StandardEvaluationContext spelContext;
|
||||
private Optional<CqlIdentifier> tableName = Optional.empty();
|
||||
|
||||
/**
|
||||
* Create a new {@link BasicCassandraPersistentEntity} given {@link TypeInformation}.
|
||||
|
||||
@@ -63,26 +63,19 @@ import com.datastax.driver.core.UserType;
|
||||
public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentProperty<CassandraPersistentProperty>
|
||||
implements CassandraPersistentProperty, ApplicationContextAware {
|
||||
|
||||
protected ApplicationContext context;
|
||||
private final UserTypeResolver userTypeResolver;
|
||||
|
||||
private StandardEvaluationContext spelContext;
|
||||
|
||||
/**
|
||||
* Whether this property has been explicitly instructed to force quote column names.
|
||||
*/
|
||||
protected Boolean forceQuote;
|
||||
private Boolean forceQuote;
|
||||
|
||||
/**
|
||||
* An unmodifiable list of this property's column names.
|
||||
*/
|
||||
protected List<CqlIdentifier> columnNames;
|
||||
|
||||
/**
|
||||
* An unmodifiable list of this property's explicitly set column names.
|
||||
*/
|
||||
protected List<CqlIdentifier> explicitColumnNames;
|
||||
|
||||
protected StandardEvaluationContext spelContext;
|
||||
|
||||
private final UserTypeResolver userTypeResolver;
|
||||
private List<CqlIdentifier> columnNames;
|
||||
|
||||
/**
|
||||
* Create a new {@link BasicCassandraPersistentProperty}.
|
||||
@@ -125,7 +118,6 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
|
||||
Assert.notNull(context, "ApplicationContext must not be null");
|
||||
|
||||
this.context = context;
|
||||
spelContext = new StandardEvaluationContext();
|
||||
spelContext.addPropertyAccessor(new BeanFactoryAccessor());
|
||||
spelContext.setBeanResolver(new BeanFactoryResolver(context));
|
||||
@@ -459,7 +451,7 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
getName(), getOwner().getType().getName(), this.columnNames.size(), this.columnNames.size() == 1 ? "" : "s",
|
||||
columnNames.size()));
|
||||
|
||||
this.columnNames = this.explicitColumnNames = Collections.unmodifiableList(new ArrayList<>(columnNames));
|
||||
this.columnNames = Collections.unmodifiableList(new ArrayList<>(columnNames));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -403,7 +403,7 @@ public class BasicCassandraMappingContextUnitTests {
|
||||
@Test // DATACASS-172
|
||||
public void usesTypeShouldReportTypeUsageInMappedUdt() {
|
||||
|
||||
final UserType myTypeMock = mock(UserType.class, "mappedudt");
|
||||
UserType myTypeMock = mock(UserType.class, "mappedudt");
|
||||
when(myTypeMock.getTypeName()).thenReturn("mappedudt");
|
||||
|
||||
mappingContext.setUserTypeResolver(typeName -> myTypeMock);
|
||||
@@ -416,7 +416,7 @@ public class BasicCassandraMappingContextUnitTests {
|
||||
@Test // DATACASS-172
|
||||
public void usesTypeShouldReportTypeUsageInColumn() {
|
||||
|
||||
final UserType myTypeMock = mock(UserType.class, "mappedudt");
|
||||
UserType myTypeMock = mock(UserType.class, "mappedudt");
|
||||
when(myTypeMock.getTypeName()).thenReturn("mappedudt");
|
||||
|
||||
mappingContext.setUserTypeResolver(typeName -> myTypeMock);
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.cassandra.core.cql.CqlIdentifier;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
@@ -82,16 +83,19 @@ public class BasicCassandraPersistentEntityUnitTests {
|
||||
|
||||
@Test
|
||||
public void setForceQuoteCallsSetTableName() {
|
||||
|
||||
BasicCassandraPersistentEntity<Message> entitySpy = spy(
|
||||
new BasicCassandraPersistentEntity<>(ClassTypeInformation.from(Message.class)));
|
||||
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(entitySpy);
|
||||
|
||||
entitySpy.setTableName(CqlIdentifier.cqlId("Messages", false));
|
||||
|
||||
assertThat(entitySpy.forceQuote).isNotPresent();
|
||||
assertThat((Optional) dfa.getPropertyValue("forceQuote")).isNotPresent();
|
||||
|
||||
entitySpy.setForceQuote(true);
|
||||
|
||||
assertThat(entitySpy.forceQuote).contains(true);
|
||||
assertThat((Optional) dfa.getPropertyValue("forceQuote")).contains(true);
|
||||
|
||||
verify(entitySpy, times(2)).setTableName(isA(CqlIdentifier.class));
|
||||
}
|
||||
@@ -101,10 +105,11 @@ public class BasicCassandraPersistentEntityUnitTests {
|
||||
BasicCassandraPersistentEntity<Message> entitySpy = spy(
|
||||
new BasicCassandraPersistentEntity<>(ClassTypeInformation.from(Message.class)));
|
||||
|
||||
entitySpy.forceQuote = Optional.of(true);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(entitySpy);
|
||||
dfa.setPropertyValue("forceQuote", Optional.of(true));
|
||||
entitySpy.setForceQuote(true);
|
||||
|
||||
assertThat(entitySpy.forceQuote).contains(true);
|
||||
assertThat((Optional) dfa.getPropertyValue("forceQuote")).contains(true);
|
||||
|
||||
verify(entitySpy, never()).setTableName(isA(CqlIdentifier.class));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user