@@ -15,12 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.mapping;
|
||||
|
||||
import static org.springframework.cassandra.core.cql.CqlIdentifier.cqlId;
|
||||
import static org.springframework.cassandra.core.cql.CqlIdentifier.*;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -77,17 +76,17 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
/**
|
||||
* Creates a new {@link BasicCassandraPersistentProperty}.
|
||||
*
|
||||
* @param field
|
||||
* @param propertyDescriptor
|
||||
* @param owner
|
||||
* @param simpleTypeHolder
|
||||
* @param field the actual {@link Field} in the domain entity corresponding to this persistent entity.
|
||||
* @param propertyDescriptor a {@link PropertyDescriptor} for the corresponding property in the domain entity.
|
||||
* @param owner the containing object or {@link CassandraPersistentEntity} of this persistent property.
|
||||
* @param simpleTypeHolder mapping of Java [simple|wrapper] types to Cassandra data types.
|
||||
*/
|
||||
public BasicCassandraPersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
|
||||
CassandraPersistentEntity<?> owner, CassandraSimpleTypeHolder simpleTypeHolder) {
|
||||
|
||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
||||
|
||||
if (owner != null && owner.getApplicationContext() != null) {
|
||||
if (owner.getApplicationContext() != null) {
|
||||
setApplicationContext(owner.getApplicationContext());
|
||||
}
|
||||
}
|
||||
@@ -111,30 +110,23 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
|
||||
@Override
|
||||
public boolean isCompositePrimaryKey() {
|
||||
return AnnotatedElementUtils.findMergedAnnotation(getType(), PrimaryKeyClass.class) != null;
|
||||
return (AnnotatedElementUtils.findMergedAnnotation(getType(), PrimaryKeyClass.class) != null);
|
||||
}
|
||||
|
||||
public Class<?> getCompositePrimaryKeyType() {
|
||||
if (!isCompositePrimaryKey()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getType();
|
||||
return (isCompositePrimaryKey() ? getType() : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeInformation<?> getCompositePrimaryKeyTypeInformation() {
|
||||
if (!isCompositePrimaryKey()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ClassTypeInformation.from(getCompositePrimaryKeyType());
|
||||
return (isCompositePrimaryKey() ? ClassTypeInformation.from(getCompositePrimaryKeyType()) : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CqlIdentifier getColumnName() {
|
||||
|
||||
List<CqlIdentifier> columnNames = getColumnNames();
|
||||
|
||||
if (columnNames.size() != 1) {
|
||||
throw new IllegalStateException("property does not have a single column mapping");
|
||||
}
|
||||
@@ -147,20 +139,21 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
|
||||
PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);
|
||||
|
||||
return primaryKeyColumn == null ? null : primaryKeyColumn.ordering();
|
||||
return (primaryKeyColumn != null ? primaryKeyColumn.ordering() : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataType getDataType() {
|
||||
|
||||
CassandraType cassandraType = findAnnotation(CassandraType.class);
|
||||
|
||||
if (cassandraType != null) {
|
||||
return getDataTypeFor(cassandraType);
|
||||
}
|
||||
|
||||
if (isMap()) {
|
||||
|
||||
List<TypeInformation<?>> args = getTypeInformation().getTypeArguments();
|
||||
|
||||
ensureTypeArguments(args.size(), 2);
|
||||
|
||||
return DataType.map(getDataTypeFor(args.get(0).getType()), getDataTypeFor(args.get(1).getType()));
|
||||
@@ -174,42 +167,40 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
if (Set.class.isAssignableFrom(getType())) {
|
||||
return DataType.set(getDataTypeFor(args.get(0).getType()));
|
||||
}
|
||||
|
||||
if (List.class.isAssignableFrom(getType())) {
|
||||
return DataType.list(getDataTypeFor(args.get(0).getType()));
|
||||
}
|
||||
}
|
||||
|
||||
DataType dataType = CassandraSimpleTypeHolder.getDataTypeFor(getType());
|
||||
|
||||
if (dataType == null) {
|
||||
throw new InvalidDataAccessApiUsageException(
|
||||
String
|
||||
.format(
|
||||
"unknown type for property [%s], type [%s] in entity [%s]; only primitive types and collections or maps of primitive types are allowed",
|
||||
getName(), getType(), getOwner().getName()));
|
||||
throw new InvalidDataAccessApiUsageException(String.format(
|
||||
"unknown type for property [%s], type [%s] in entity [%s]; only primitive types and collections or maps of primitive types are allowed",
|
||||
getName(), getType(), getOwner().getName()));
|
||||
}
|
||||
|
||||
return dataType;
|
||||
}
|
||||
|
||||
private DataType getDataTypeFor(CassandraType annotation) {
|
||||
|
||||
DataType.Name type = annotation.type();
|
||||
|
||||
switch (type) {
|
||||
|
||||
case MAP:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 2);
|
||||
return DataType.map(getDataTypeFor(annotation.typeArguments()[0]),
|
||||
case MAP:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 2);
|
||||
return DataType.map(getDataTypeFor(annotation.typeArguments()[0]),
|
||||
getDataTypeFor(annotation.typeArguments()[1]));
|
||||
|
||||
case LIST:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 1);
|
||||
return DataType.list(getDataTypeFor(annotation.typeArguments()[0]));
|
||||
|
||||
case SET:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 1);
|
||||
return DataType.set(getDataTypeFor(annotation.typeArguments()[0]));
|
||||
|
||||
default:
|
||||
return CassandraSimpleTypeHolder.getDataTypeFor(type);
|
||||
case LIST:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 1);
|
||||
return DataType.list(getDataTypeFor(annotation.typeArguments()[0]));
|
||||
case SET:
|
||||
ensureTypeArguments(annotation.typeArguments().length, 1);
|
||||
return DataType.set(getDataTypeFor(annotation.typeArguments()[0]));
|
||||
default:
|
||||
return CassandraSimpleTypeHolder.getDataTypeFor(type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,17 +212,17 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
@Override
|
||||
public boolean isPartitionKeyColumn() {
|
||||
|
||||
PrimaryKeyColumn anno = findAnnotation(PrimaryKeyColumn.class);
|
||||
PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);
|
||||
|
||||
return anno != null && anno.type() == PrimaryKeyType.PARTITIONED;
|
||||
return (primaryKeyColumn != null && primaryKeyColumn.type() == PrimaryKeyType.PARTITIONED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClusterKeyColumn() {
|
||||
|
||||
PrimaryKeyColumn anno = findAnnotation(PrimaryKeyColumn.class);
|
||||
PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);
|
||||
|
||||
return anno != null && anno.type() == PrimaryKeyType.CLUSTERED;
|
||||
return (primaryKeyColumn != null && primaryKeyColumn.type() == PrimaryKeyType.CLUSTERED);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -241,39 +232,44 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
|
||||
protected DataType getDataTypeFor(DataType.Name typeName) {
|
||||
DataType dataType = CassandraSimpleTypeHolder.getDataTypeFor(typeName);
|
||||
|
||||
if (dataType == null) {
|
||||
throw new InvalidDataAccessApiUsageException(
|
||||
"only primitive types are allowed inside collections for the property '" + this.getName() + "' type is '"
|
||||
+ this.getType() + "' in the entity " + this.getOwner().getName());
|
||||
throw new InvalidDataAccessApiUsageException(String.format(
|
||||
"only primitive types are allowed inside collections for the property '%1$s' type is '%2$s' in the entity %3$s",
|
||||
getName(), getType(), getOwner().getName()));
|
||||
}
|
||||
|
||||
return dataType;
|
||||
}
|
||||
|
||||
protected DataType getDataTypeFor(Class<?> javaType) {
|
||||
DataType dataType = CassandraSimpleTypeHolder.getDataTypeFor(javaType);
|
||||
|
||||
if (dataType == null) {
|
||||
throw new InvalidDataAccessApiUsageException(
|
||||
"only primitive types are allowed inside collections for the property '" + this.getName() + "' type is '"
|
||||
+ this.getType() + "' in the entity " + this.getOwner().getName());
|
||||
throw new InvalidDataAccessApiUsageException(String.format(
|
||||
"only primitive types are allowed inside collections for the property '%1$s' type is '%2$s' in the entity %3$s",
|
||||
getName(), getType(), getOwner().getName()));
|
||||
}
|
||||
|
||||
return dataType;
|
||||
}
|
||||
|
||||
protected void ensureTypeArguments(int args, int expected) {
|
||||
if (args != expected) {
|
||||
throw new InvalidDataAccessApiUsageException("expected " + expected + " of typed arguments for the property '"
|
||||
+ this.getName() + "' type is '" + this.getType() + "' in the entity " + this.getOwner().getName());
|
||||
throw new InvalidDataAccessApiUsageException(String.format(
|
||||
"expected %1$s of typed arguments for the property '%2$s' type is '%3$s' in the entity %4$s",
|
||||
expected, getName(), getType(), getOwner().getName()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CqlIdentifier> getColumnNames() {
|
||||
|
||||
if (this.columnNames != null) {
|
||||
return columnNames;
|
||||
if (columnNames == null) {
|
||||
columnNames = Collections.unmodifiableList(determineColumnNames());
|
||||
}
|
||||
|
||||
return this.columnNames = Collections.unmodifiableList(determineColumnNames());
|
||||
return columnNames;
|
||||
}
|
||||
|
||||
protected List<CqlIdentifier> determineColumnNames() {
|
||||
@@ -281,38 +277,33 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
List<CqlIdentifier> columnNames = new ArrayList<CqlIdentifier>();
|
||||
|
||||
if (isCompositePrimaryKey()) { // then the id type has @PrimaryKeyClass
|
||||
|
||||
addCompositePrimaryKeyColumnNames(getCompositePrimaryKeyEntity(), columnNames);
|
||||
return columnNames;
|
||||
}
|
||||
else { // else we're dealing with a single-column field
|
||||
String defaultName = getName(); // TODO: replace with naming strategy class
|
||||
String overriddenName;
|
||||
boolean forceQuote;
|
||||
|
||||
// else we're dealing with a single-column field
|
||||
String defaultName = getName(); // TODO: replace with naming strategy class
|
||||
String overriddenName = null;
|
||||
boolean forceQuote = false;
|
||||
if (isIdProperty()) { // then the id is of a simple type (since it's not a composite primary key)
|
||||
PrimaryKey primaryKey = findAnnotation(PrimaryKey.class);
|
||||
overriddenName = primaryKey == null ? null : primaryKey.value();
|
||||
forceQuote = (primaryKey != null && primaryKey.forceQuote());
|
||||
|
||||
if (isIdProperty()) { // then the id is of a simple type (since it's not a composite primary key)
|
||||
} else if (isPrimaryKeyColumn()) { // then it's a simple type
|
||||
PrimaryKeyColumn primaryKeyColumn = findAnnotation(PrimaryKeyColumn.class);
|
||||
overriddenName = primaryKeyColumn == null ? null : primaryKeyColumn.name();
|
||||
forceQuote = (primaryKeyColumn != null && primaryKeyColumn.forceQuote());
|
||||
|
||||
PrimaryKey anno = findAnnotation(PrimaryKey.class);
|
||||
overriddenName = anno == null ? null : anno.value();
|
||||
forceQuote = anno == null ? forceQuote : anno.forceQuote();
|
||||
} else { // then it's a vanilla column with the assumption that it's mapped to a single column
|
||||
Column column = findAnnotation(Column.class);
|
||||
overriddenName = column == null ? null : column.value();
|
||||
forceQuote = (column != null && column.forceQuote());
|
||||
}
|
||||
|
||||
} else if (isPrimaryKeyColumn()) { // then it's a simple type
|
||||
|
||||
PrimaryKeyColumn anno = findAnnotation(PrimaryKeyColumn.class);
|
||||
overriddenName = anno == null ? null : anno.name();
|
||||
forceQuote = anno == null ? forceQuote : anno.forceQuote();
|
||||
|
||||
} else { // then it's a vanilla column with the assumption that it's mapped to a single column
|
||||
|
||||
Column anno = findAnnotation(Column.class);
|
||||
overriddenName = anno == null ? null : anno.value();
|
||||
forceQuote = anno == null ? forceQuote : anno.forceQuote();
|
||||
columnNames.add(createColumnName(defaultName, overriddenName, forceQuote));
|
||||
|
||||
}
|
||||
|
||||
columnNames.add(createColumnName(defaultName, overriddenName, forceQuote));
|
||||
|
||||
return columnNames;
|
||||
}
|
||||
|
||||
@@ -331,7 +322,6 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
final List<CqlIdentifier> columnNames) {
|
||||
|
||||
compositePrimaryKeyEntity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
|
||||
@Override
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty p) {
|
||||
if (p.isCompositePrimaryKey()) {
|
||||
@@ -346,8 +336,8 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
@Override
|
||||
public void setColumnName(CqlIdentifier columnName) {
|
||||
|
||||
Assert.notNull(columnName);
|
||||
setColumnNames(Arrays.asList(new CqlIdentifier[] { columnName }));
|
||||
Assert.notNull(columnName, "columnName must not be null");
|
||||
setColumnNames(Collections.singletonList(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -356,9 +346,8 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
Assert.notNull(columnNames);
|
||||
|
||||
// force calculation of columnNames if not yet known
|
||||
if (this.columnNames == null) {
|
||||
getColumnNames();
|
||||
}
|
||||
getColumnNames();
|
||||
|
||||
if (this.columnNames.size() != columnNames.size()) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"property [%s] on entity [%s] is mapped to [%s] column%s, but given column name list has size [%s]",
|
||||
@@ -366,8 +355,8 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
columnNames.size()));
|
||||
}
|
||||
|
||||
this.columnNames = this.explicitColumnNames = Collections
|
||||
.unmodifiableList(new ArrayList<CqlIdentifier>(columnNames));
|
||||
this.columnNames = this.explicitColumnNames = Collections.unmodifiableList(
|
||||
new ArrayList<CqlIdentifier>(columnNames));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -381,6 +370,7 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
|
||||
List<CqlIdentifier> columnNames = new ArrayList<CqlIdentifier>(this.columnNames == null ? 0
|
||||
: this.columnNames.size());
|
||||
|
||||
for (CqlIdentifier columnName : getColumnNames()) {
|
||||
columnNames.add(cqlId(columnName.getUnquoted(), forceQuote));
|
||||
}
|
||||
@@ -392,8 +382,8 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
public List<CassandraPersistentProperty> getCompositePrimaryKeyProperties() {
|
||||
|
||||
if (!isCompositePrimaryKey()) {
|
||||
throw new IllegalStateException(String.format("[%s] does not represent a composite primary key property",
|
||||
getName()));
|
||||
throw new IllegalStateException(String.format(
|
||||
"[%s] does not represent a composite primary key property", getName()));
|
||||
}
|
||||
|
||||
return getCompositePrimaryKeyEntity().getCompositePrimaryKeyProperties();
|
||||
|
||||
@@ -21,32 +21,30 @@ import java.util.Comparator;
|
||||
* {@link Comparator} implementation that orders {@link CassandraPersistentProperty} instances.
|
||||
* <p/>
|
||||
* Composite primary key properties and primary key properties sort before non-primary key properties.
|
||||
*
|
||||
*
|
||||
* @author Alex Shvid
|
||||
* @author Matthew T. Adams
|
||||
* @author Mark Paluch
|
||||
* @author John Blum
|
||||
* @see java.util.Comparator
|
||||
* @see org.springframework.data.cassandra.mapping.CassandraPersistentProperty
|
||||
*/
|
||||
public enum CassandraPersistentPropertyComparator implements Comparator<CassandraPersistentProperty> {
|
||||
|
||||
/**
|
||||
* The sole instance of this class.
|
||||
*/
|
||||
IT;
|
||||
|
||||
@Override
|
||||
public int compare(CassandraPersistentProperty left, CassandraPersistentProperty right) {
|
||||
|
||||
if (left != null && right == null) {
|
||||
return 1;
|
||||
}
|
||||
if (left == null && right != null) {
|
||||
return -1;
|
||||
}
|
||||
if (left == null && right == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (left.equals(right)) {
|
||||
else if (left != null && right == null) {
|
||||
return 1;
|
||||
}
|
||||
else if (left == null) {
|
||||
return -1;
|
||||
}
|
||||
else if (left.equals(right)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -60,10 +58,9 @@ public enum CassandraPersistentPropertyComparator implements Comparator<Cassandr
|
||||
boolean leftIsPrimaryKey = left.isPrimaryKeyColumn();
|
||||
boolean rightIsPrimaryKey = right.isPrimaryKeyColumn();
|
||||
|
||||
|
||||
if (leftIsPrimaryKey && rightIsPrimaryKey) {
|
||||
return CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(left.findAnnotation(PrimaryKeyColumn.class),
|
||||
right.findAnnotation(PrimaryKeyColumn.class));
|
||||
right.findAnnotation(PrimaryKeyColumn.class));
|
||||
}
|
||||
|
||||
boolean leftIsKey = leftIsCompositePrimaryKey || leftIsPrimaryKey;
|
||||
@@ -72,8 +69,7 @@ public enum CassandraPersistentPropertyComparator implements Comparator<Cassandr
|
||||
if (leftIsKey && !rightIsKey) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!leftIsKey && rightIsKey) {
|
||||
else if (!leftIsKey && rightIsKey) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -82,19 +78,17 @@ public enum CassandraPersistentPropertyComparator implements Comparator<Cassandr
|
||||
Column leftColumn = left.findAnnotation(Column.class);
|
||||
Column rightColumn = right.findAnnotation(Column.class);
|
||||
|
||||
if (leftColumn == null && rightColumn == null) {
|
||||
return left.getName().compareTo(right.getName());
|
||||
}
|
||||
|
||||
if (leftColumn != null && rightColumn != null) {
|
||||
return CassandraColumnAnnotationComparator.IT.compare(leftColumn, rightColumn);
|
||||
}
|
||||
|
||||
if (leftColumn != null && rightColumn == null) {
|
||||
else if (leftColumn != null) {
|
||||
return leftColumn.value().compareTo(left.getName());
|
||||
}
|
||||
|
||||
// else leftColumn == null && rightColumn != null)
|
||||
return left.getName().compareTo(rightColumn.value());
|
||||
else if (rightColumn != null) {
|
||||
return left.getName().compareTo(rightColumn.value());
|
||||
}
|
||||
else {
|
||||
return left.getName().compareTo(right.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.cassandra.core.Ordering;
|
||||
import org.springframework.cassandra.core.PrimaryKeyType;
|
||||
|
||||
/**
|
||||
* {@link Comparator} implementation that uses, in order, the
|
||||
* {@link Comparator} implementation that uses, in order, the...
|
||||
* <ul>
|
||||
* <li>{@link PrimaryKeyColumn#type()}, then, if ordered the same,</li>
|
||||
* <li>{@link PrimaryKeyColumn#ordinal()}, then, if ordered the same</li>
|
||||
@@ -32,32 +32,22 @@ import org.springframework.cassandra.core.PrimaryKeyType;
|
||||
* @see PrimaryKeyType#compareTo(PrimaryKeyType)
|
||||
* @see Ordering#compareTo(Ordering)
|
||||
* @author Matthew T. Adams
|
||||
* @author John Blum
|
||||
* @see java.util.Comparator
|
||||
* @see org.springframework.data.cassandra.mapping.PrimaryKeyColumn
|
||||
*/
|
||||
public enum CassandraPrimaryKeyColumnAnnotationComparator implements Comparator<PrimaryKeyColumn> {
|
||||
|
||||
/**
|
||||
* The sole instance of this class.
|
||||
*/
|
||||
IT;
|
||||
|
||||
@Override
|
||||
public int compare(PrimaryKeyColumn left, PrimaryKeyColumn right) {
|
||||
|
||||
int comparison = left.type().compareTo(right.type());
|
||||
if (comparison != 0) {
|
||||
return comparison;
|
||||
}
|
||||
|
||||
comparison = new Integer(left.ordinal()).compareTo(right.ordinal());
|
||||
if (comparison != 0) {
|
||||
return comparison;
|
||||
}
|
||||
comparison = (comparison != 0 ? comparison : Integer.valueOf(left.ordinal()).compareTo(right.ordinal()));
|
||||
comparison = (comparison != 0 ? comparison : left.name().compareTo(right.name()));
|
||||
comparison = (comparison != 0 ? comparison : left.ordering().compareTo(right.ordering()));
|
||||
|
||||
comparison = left.name().compareTo(right.name());
|
||||
if (comparison != 0) {
|
||||
return comparison;
|
||||
}
|
||||
|
||||
return left.ordering().compareTo(right.ordering());
|
||||
return comparison;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright 2013-2016 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.cassandra.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* The CassandraPersistentPropertyComparatorUnitTests class is a test suite of test cases testing the contract
|
||||
* and functionality of the {@link CassandraPersistentPropertyComparator} class.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.data.cassandra.mapping.CassandraPersistentPropertyComparator
|
||||
* @since 1.5.0
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CassandraPersistentPropertyComparatorUnitTests {
|
||||
|
||||
@Mock
|
||||
private CassandraPersistentProperty left;
|
||||
|
||||
@Mock
|
||||
private CassandraPersistentProperty right;
|
||||
|
||||
@Test
|
||||
public void leftAndRightAreNullReturnsZero() {
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(null, null), is(equalTo(0)));
|
||||
verifyZeroInteractions(left);
|
||||
verifyZeroInteractions(right);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leftIsNotNullAndRightIsNullReturnsOne() {
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, null), is(equalTo(1)));
|
||||
verifyZeroInteractions(left);
|
||||
verifyZeroInteractions(right);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leftIsNullAndRightIsNotNullReturnsMinusOne() {
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(null, right), is(equalTo(-1)));
|
||||
verifyZeroInteractions(left);
|
||||
verifyZeroInteractions(right);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leftAndRightAreEqualReturnsZero() {
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, left), is(equalTo(0)));
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(right, right), is(equalTo(0)));
|
||||
verifyZeroInteractions(left);
|
||||
verifyZeroInteractions(right);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leftAndRightAreCompositePrimaryKeysReturnsZero() {
|
||||
when(left.isCompositePrimaryKey()).thenReturn(true);
|
||||
when(right.isCompositePrimaryKey()).thenReturn(true);
|
||||
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, right), is(equalTo(0)));
|
||||
|
||||
verify(left, times(1)).isCompositePrimaryKey();
|
||||
verify(right, times(1)).isCompositePrimaryKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leftIsCompositePrimaryKeyReturnsMinusOne() {
|
||||
when(left.isCompositePrimaryKey()).thenReturn(true);
|
||||
when(left.isPrimaryKeyColumn()).thenReturn(false);
|
||||
when(right.isCompositePrimaryKey()).thenReturn(false);
|
||||
when(right.isPrimaryKeyColumn()).thenReturn(false);
|
||||
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, right), is(equalTo(-1)));
|
||||
|
||||
verify(left, times(1)).isCompositePrimaryKey();
|
||||
verify(left, times(1)).isPrimaryKeyColumn();
|
||||
verify(right, times(1)).isCompositePrimaryKey();
|
||||
verify(right, times(1)).isPrimaryKeyColumn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void leftIsPrimaryKeyColumnReturnsMinusOne() {
|
||||
when(left.isCompositePrimaryKey()).thenReturn(false);
|
||||
when(left.isPrimaryKeyColumn()).thenReturn(true);
|
||||
when(right.isCompositePrimaryKey()).thenReturn(false);
|
||||
when(right.isPrimaryKeyColumn()).thenReturn(false);
|
||||
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, right), is(equalTo(-1)));
|
||||
|
||||
verify(left, times(1)).isCompositePrimaryKey();
|
||||
verify(left, times(1)).isPrimaryKeyColumn();
|
||||
verify(right, times(1)).isCompositePrimaryKey();
|
||||
verify(right, times(1)).isPrimaryKeyColumn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rightIsCompositePrimaryKeyReturnsOne() {
|
||||
when(left.isCompositePrimaryKey()).thenReturn(false);
|
||||
when(left.isPrimaryKeyColumn()).thenReturn(false);
|
||||
when(right.isCompositePrimaryKey()).thenReturn(true);
|
||||
when(right.isPrimaryKeyColumn()).thenReturn(false);
|
||||
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, right), is(equalTo(1)));
|
||||
|
||||
verify(left, times(1)).isCompositePrimaryKey();
|
||||
verify(left, times(1)).isPrimaryKeyColumn();
|
||||
verify(right, times(1)).isCompositePrimaryKey();
|
||||
verify(right, times(1)).isPrimaryKeyColumn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rightIsPrimaryKeyColumnReturnsOne() {
|
||||
when(left.isCompositePrimaryKey()).thenReturn(false);
|
||||
when(left.isPrimaryKeyColumn()).thenReturn(false);
|
||||
when(right.isCompositePrimaryKey()).thenReturn(false);
|
||||
when(right.isPrimaryKeyColumn()).thenReturn(true);
|
||||
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, right), is(equalTo(1)));
|
||||
|
||||
verify(left, times(1)).isCompositePrimaryKey();
|
||||
verify(left, times(1)).isPrimaryKeyColumn();
|
||||
verify(right, times(1)).isCompositePrimaryKey();
|
||||
verify(right, times(1)).isPrimaryKeyColumn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareLeftAndRightNamesReturnsNegativeValue() {
|
||||
when(left.isCompositePrimaryKey()).thenReturn(false);
|
||||
when(left.isPrimaryKeyColumn()).thenReturn(true);
|
||||
when(right.isCompositePrimaryKey()).thenReturn(true);
|
||||
when(right.isPrimaryKeyColumn()).thenReturn(false);
|
||||
when(left.findAnnotation(eq(Column.class))).thenReturn(null);
|
||||
when(right.findAnnotation(eq(Column.class))).thenReturn(null);
|
||||
when(left.getName()).thenReturn("left");
|
||||
when(right.getName()).thenReturn("right");
|
||||
|
||||
assertThat(CassandraPersistentPropertyComparator.IT.compare(left, right), is(lessThan(0)));
|
||||
|
||||
verify(left, times(1)).isCompositePrimaryKey();
|
||||
verify(left, times(1)).isPrimaryKeyColumn();
|
||||
verify(left, times(1)).findAnnotation(eq(Column.class));
|
||||
verify(left, times(1)).getName();
|
||||
verify(right, times(1)).isCompositePrimaryKey();
|
||||
verify(right, times(1)).isPrimaryKeyColumn();
|
||||
verify(right, times(1)).findAnnotation(eq(Column.class));
|
||||
verify(right, times(1)).getName();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2013-2016 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.cassandra.mapping;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cassandra.core.Ordering;
|
||||
import org.springframework.cassandra.core.PrimaryKeyType;
|
||||
|
||||
/**
|
||||
* The CassandraPrimaryKeyColumnAnnotationComparatorUnitTests class is a test suite of test cases testing the contract
|
||||
* and functionality of the {@link CassandraPrimaryKeyColumnAnnotationComparator} class.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.springframework.data.cassandra.mapping.CassandraPrimaryKeyColumnAnnotationComparator
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class CassandraPrimaryKeyColumnAnnotationComparatorUnitTests {
|
||||
|
||||
private static PrimaryKeyColumn entityOne;
|
||||
private static PrimaryKeyColumn entityTwo;
|
||||
private static PrimaryKeyColumn entityThree;
|
||||
private static PrimaryKeyColumn entityFour;
|
||||
private static PrimaryKeyColumn entityFive;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws Exception {
|
||||
entityOne = EntityOne.class.getDeclaredField("id").getAnnotation(PrimaryKeyColumn.class);
|
||||
entityTwo = EntityTwo.class.getDeclaredField("id").getAnnotation(PrimaryKeyColumn.class);
|
||||
entityThree = EntityThree.class.getDeclaredField("id").getAnnotation(PrimaryKeyColumn.class);
|
||||
entityFour = EntityFour.class.getDeclaredField("id").getAnnotation(PrimaryKeyColumn.class);
|
||||
entityFive = EntityFive.class.getDeclaredField("id").getAnnotation(PrimaryKeyColumn.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTypes() {
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityOne, entityTwo), is(equalTo(1)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityTwo, entityTwo), is(equalTo(0)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityTwo, entityOne), is(equalTo(-1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOrdinals() {
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityOne, entityThree), is(equalTo(-1)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityThree, entityThree), is(equalTo(0)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityThree, entityOne), is(equalTo(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareName() {
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityOne, entityFour), is(equalTo(-1)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityFour, entityFour), is(equalTo(0)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityFour, entityOne), is(equalTo(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOrdering() {
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityOne, entityFive), is(equalTo(-1)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityFive, entityFive), is(equalTo(0)));
|
||||
assertThat(CassandraPrimaryKeyColumnAnnotationComparator.IT.compare(entityFive, entityOne), is(equalTo(1)));
|
||||
}
|
||||
|
||||
static class EntityOne {
|
||||
@PrimaryKeyColumn(type = PrimaryKeyType.CLUSTERED, ordinal = 1, name = "A", ordering = Ordering.ASCENDING)
|
||||
Integer id;
|
||||
}
|
||||
|
||||
static class EntityTwo {
|
||||
@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED, ordinal = 1, name = "A", ordering = Ordering.ASCENDING)
|
||||
Long id;
|
||||
}
|
||||
|
||||
static class EntityThree {
|
||||
@PrimaryKeyColumn(type = PrimaryKeyType.CLUSTERED, ordinal = 2, name = "A", ordering = Ordering.ASCENDING)
|
||||
String id;
|
||||
}
|
||||
|
||||
static class EntityFour {
|
||||
@PrimaryKeyColumn(type = PrimaryKeyType.CLUSTERED, ordinal = 1, name = "B", ordering = Ordering.ASCENDING)
|
||||
Timestamp id;
|
||||
}
|
||||
|
||||
static class EntityFive {
|
||||
@PrimaryKeyColumn(type = PrimaryKeyType.CLUSTERED, ordinal = 1, name = "A", ordering = Ordering.DESCENDING)
|
||||
UUID id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user