From 2502779445bd9084078b66d18d974ccc38a06eef Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Fri, 7 Feb 2014 10:48:51 -0600 Subject: [PATCH 1/2] DATACASS-33 - moved test support classes & improved CompoundPrimaryKeyIntegrationTests --- .../config/CassandraNamespaceTests.java | 2 +- ...andraPersistentEntityIntegrationTests.java | 2 +- .../CompoundPrimaryKeyIntegrationTests.java | 23 +++++++++++-------- ...rRepositoryJavaConfigIntegrationTests.java | 4 ++-- ...erRepositoryXmlConfigIntegrationTests.java | 2 +- ...gDataEmbeddedCassandraIntegrationTest.java | 3 +-- .../{config => support}/TestConfig.java | 3 +-- .../template/CassandraDataOperationsTest.java | 4 ++-- 8 files changed, 23 insertions(+), 20 deletions(-) rename spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/{ => support}/AbstractSpringDataEmbeddedCassandraIntegrationTest.java (93%) rename spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/{config => support}/TestConfig.java (83%) diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java index 66eb3ee1a..33377606e 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/CassandraNamespaceTests.java @@ -4,7 +4,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.Assert; diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/BasicCassandraPersistentEntityIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/BasicCassandraPersistentEntityIntegrationTests.java index 69dda8e6b..306351a00 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/BasicCassandraPersistentEntityIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/BasicCassandraPersistentEntityIntegrationTests.java @@ -28,7 +28,7 @@ import org.mockito.runners.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; import org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntity; import org.springframework.data.cassandra.mapping.Table; -import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; import org.springframework.data.util.ClassTypeInformation; /** diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/CompoundPrimaryKeyIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/CompoundPrimaryKeyIntegrationTests.java index 7b868fb0d..f02017567 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/CompoundPrimaryKeyIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/mapping/CompoundPrimaryKeyIntegrationTests.java @@ -22,6 +22,7 @@ import java.util.Date; import org.junit.Before; import org.junit.Test; +import org.springframework.cassandra.core.PrimaryKeyType; import org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntity; import org.springframework.data.cassandra.mapping.BasicCassandraPersistentProperty; import org.springframework.data.cassandra.mapping.CachingCassandraPersistentProperty; @@ -32,6 +33,7 @@ import org.springframework.data.cassandra.mapping.Column; import org.springframework.data.cassandra.mapping.PrimaryKey; import org.springframework.data.cassandra.mapping.PrimaryKeyClass; import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; +import org.springframework.data.cassandra.mapping.Table; import org.springframework.data.util.ClassTypeInformation; import org.springframework.util.ReflectionUtils; @@ -39,41 +41,44 @@ import org.springframework.util.ReflectionUtils; * Integration test for {@link BasicCassandraPersistentProperty}. * * @author Alex Shvid + * @author Matthew T. Adams */ public class CompoundPrimaryKeyIntegrationTests { @PrimaryKeyClass static class TimelineKey { - @PrimaryKeyColumn(ordinal = 0) - String id; + @PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED) + String string; - @PrimaryKeyColumn(ordinal = 1) - Date dt; + @PrimaryKeyColumn(ordinal = 1, type = PrimaryKeyType.PARTITIONED) + Date datetime; } + @Table static class Timeline { - @PrimaryKey TimelineKey id; @Column("message") String text; - } + CassandraPersistentEntity cpk; CassandraPersistentEntity entity; @Before public void setup() { + cpk = new BasicCassandraPersistentEntity(ClassTypeInformation.from(TimelineKey.class)); entity = new BasicCassandraPersistentEntity(ClassTypeInformation.from(Timeline.class)); } @Test - public void checksIdProperty() { - Field field = ReflectionUtils.findField(Timeline.class, "id"); - CassandraPersistentProperty property = getPropertyFor(field); + public void checkIdProperty() { + Field id = ReflectionUtils.findField(Timeline.class, "id"); + CassandraPersistentProperty property = getPropertyFor(id); assertTrue(property.isIdProperty()); + assertTrue(property.isCompositePrimaryKey()); } private CassandraPersistentProperty getPropertyFor(Field field) { diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigIntegrationTests.java index 7c1236543..9ca1eac3e 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryJavaConfigIntegrationTests.java @@ -30,8 +30,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.config.SchemaAction; import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; -import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.config.TestConfig; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.data.cassandra.test.integration.support.TestConfig; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests.java index 05a9e8431..3fe881fca 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests.java @@ -21,7 +21,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.cassandra.core.CassandraOperations; -import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/AbstractSpringDataEmbeddedCassandraIntegrationTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/AbstractSpringDataEmbeddedCassandraIntegrationTest.java similarity index 93% rename from spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/AbstractSpringDataEmbeddedCassandraIntegrationTest.java rename to spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/AbstractSpringDataEmbeddedCassandraIntegrationTest.java index 6c97490c4..cfbafbfc4 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/AbstractSpringDataEmbeddedCassandraIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/AbstractSpringDataEmbeddedCassandraIntegrationTest.java @@ -1,4 +1,4 @@ -package org.springframework.data.cassandra.test.integration; +package org.springframework.data.cassandra.test.integration.support; import static org.springframework.cassandra.core.keyspace.DropTableSpecification.dropTable; @@ -13,7 +13,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest; import org.springframework.core.io.ClassPathResource; import org.springframework.data.cassandra.core.CassandraOperations; -import org.springframework.data.cassandra.test.integration.support.SpringDataCassandraBuildProperties; import org.springframework.data.cassandra.test.integration.template.CassandraDataOperationsTest.Config; import org.springframework.util.Assert; diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/TestConfig.java similarity index 83% rename from spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java rename to spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/TestConfig.java index 1b66f649b..752b8abbd 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/support/TestConfig.java @@ -1,9 +1,8 @@ -package org.springframework.data.cassandra.test.integration.config; +package org.springframework.data.cassandra.test.integration.support; import org.springframework.cassandra.test.unit.support.Utils; import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.config.java.AbstractSpringDataCassandraConfiguration; -import org.springframework.data.cassandra.test.integration.support.SpringDataCassandraBuildProperties; /** * Setup any spring configuration for unit tests diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java index 173729928..047545bc8 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraDataOperationsTest.java @@ -41,9 +41,9 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.data.cassandra.config.SchemaAction; import org.springframework.data.cassandra.core.CassandraOperations; -import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest; -import org.springframework.data.cassandra.test.integration.config.TestConfig; import org.springframework.data.cassandra.test.integration.simpletons.Book; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.data.cassandra.test.integration.support.TestConfig; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; From 47998cea742a9b582e637424ba20c8dfcfe8cd2b Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Fri, 7 Feb 2014 15:28:34 -0600 Subject: [PATCH 2/2] DATACASS-33 - basic composite primary key support working --- .../CassandraDataSessionFactoryBean.java | 5 +- .../cassandra/convert/CassandraConverter.java | 3 +- .../convert/CassandraRowValueProvider.java | 11 ++ ... => DefaultCassandraRowValueProvider.java} | 12 +- .../convert/MappingCassandraConverter.java | 59 ++++++--- .../cassandra/core/CassandraTemplate.java | 2 +- .../BasicCassandraPersistentEntity.java | 1 + .../mapping/CassandraMappingContext.java | 30 +++++ .../mapping/CassandraPersistentProperty.java | 3 + .../DefaultCassandraMappingContext.java | 35 +++++ .../test/integration/composites/Comment.java | 82 ++++++------ .../integration/composites/CommentKey.java | 99 +++++++++++++++ .../integration/composites/CommentPK.java | 65 ---------- .../composites/CommentRepository.java | 6 + .../CommentRepositoryIntegrationTests.java | 120 ++++++++++++++++++ ...tRepositoryJavaConfigIntegrationTests.java | 110 ++++++++++++++++ ...ntRepositoryXmlConfigIntegrationTests.java | 71 +++++++++++ .../test/integration/composites/PostPK.java | 6 - ...itoryXmlConfigIntegrationTests-context.xml | 33 +++++ ...itoryXmlConfigIntegrationTests-context.xml | 38 ++---- .../repository/cassandra.properties | 3 - .../resources/spring-data-cassandra-basic.xml | 24 ++++ .../spring-data-cassandra-build.properties | 1 + 23 files changed, 647 insertions(+), 172 deletions(-) create mode 100644 spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraRowValueProvider.java rename spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/{CassandraPropertyValueProvider.java => DefaultCassandraRowValueProvider.java} (84%) create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentKey.java delete mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentPK.java create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepository.java create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryIntegrationTests.java create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryJavaConfigIntegrationTests.java create mode 100644 spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests.java create mode 100644 spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests-context.xml delete mode 100644 spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/cassandra.properties create mode 100644 spring-data-cassandra/src/test/resources/spring-data-cassandra-basic.xml create mode 100644 spring-data-cassandra/src/test/resources/spring-data-cassandra-build.properties diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraDataSessionFactoryBean.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraDataSessionFactoryBean.java index 8b30a0a98..436b66b68 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraDataSessionFactoryBean.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CassandraDataSessionFactoryBean.java @@ -112,7 +112,8 @@ public class CassandraDataSessionFactoryBean extends CassandraSessionFactoryBean } } - Collection> entities = converter.getMappingContext().getPersistentEntities(); + Collection> entities = converter.getMappingContext() + .getNonPrimaryKeyEntities(); for (CassandraPersistentEntity entity : entities) { admin.createTable(false, entity.getTableName(), entity.getType(), null); // TODO: allow spec of table options @@ -135,7 +136,7 @@ public class CassandraDataSessionFactoryBean extends CassandraSessionFactoryBean public void setConverter(CassandraConverter converter) { Assert.notNull(converter); this.converter = converter; - this.mappingContext = converter.getCassandraMappingContext(); + this.mappingContext = converter.getMappingContext(); } public Mapping getMapping() { diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraConverter.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraConverter.java index 5d6d6f079..faed60112 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraConverter.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraConverter.java @@ -29,5 +29,6 @@ import org.springframework.data.convert.EntityConverter; public interface CassandraConverter extends EntityConverter, CassandraPersistentProperty, Object, Object> { - CassandraMappingContext getCassandraMappingContext(); + @Override + CassandraMappingContext getMappingContext(); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraRowValueProvider.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraRowValueProvider.java new file mode 100644 index 000000000..cac24915e --- /dev/null +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraRowValueProvider.java @@ -0,0 +1,11 @@ +package org.springframework.data.cassandra.convert; + +import org.springframework.data.cassandra.mapping.CassandraPersistentProperty; +import org.springframework.data.mapping.model.PropertyValueProvider; + +import com.datastax.driver.core.Row; + +public interface CassandraRowValueProvider extends PropertyValueProvider { + + Row getRow(); +} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraPropertyValueProvider.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/DefaultCassandraRowValueProvider.java similarity index 84% rename from spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraPropertyValueProvider.java rename to spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/DefaultCassandraRowValueProvider.java index d308945f9..720195eb7 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/CassandraPropertyValueProvider.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/DefaultCassandraRowValueProvider.java @@ -32,22 +32,23 @@ import com.datastax.driver.core.Row; * {@link PropertyValueProvider} to read property values from a {@link Row}. * * @author Alex Shvid + * @author Matthew T. Adams */ -public class CassandraPropertyValueProvider implements PropertyValueProvider { +public class DefaultCassandraRowValueProvider implements CassandraRowValueProvider { - private static Logger log = LoggerFactory.getLogger(CassandraPropertyValueProvider.class); + private static Logger log = LoggerFactory.getLogger(DefaultCassandraRowValueProvider.class); private final Row source; private final SpELExpressionEvaluator evaluator; /** - * Creates a new {@link CassandraPropertyValueProvider} with the given {@link Row} and + * Creates a new {@link DefaultCassandraRowValueProvider} with the given {@link Row} and * {@link DefaultSpELExpressionEvaluator}. * * @param source must not be {@literal null}. * @param evaluator must not be {@literal null}. */ - public CassandraPropertyValueProvider(Row source, DefaultSpELExpressionEvaluator evaluator) { + public DefaultCassandraRowValueProvider(Row source, DefaultSpELExpressionEvaluator evaluator) { Assert.notNull(source); Assert.notNull(evaluator); @@ -84,4 +85,7 @@ public class CassandraPropertyValueProvider implements PropertyValueProvider, CassandraPersistentProperty> getMappingContext() { - return mappingContext; - } - @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; @@ -112,57 +107,81 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem protected S readEntityFromRow(final CassandraPersistentEntity entity, final Row row) { - final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(row, spELContext); + DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(row, spELContext); - final CassandraPropertyValueProvider propertyProvider = new CassandraPropertyValueProvider(row, evaluator); + DefaultCassandraRowValueProvider rowValueProvider = new DefaultCassandraRowValueProvider(row, evaluator); CassandraPersistentEntityParameterValueProvider parameterProvider = new CassandraPersistentEntityParameterValueProvider( - entity, propertyProvider, null); + entity, rowValueProvider, null); EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity); S instance = instantiator.createInstance(entity, parameterProvider); - final BeanWrapper, S> wrapper = BeanWrapper.create(instance, conversionService); + BeanWrapper, S> wrapper = BeanWrapper.create(instance, conversionService); - readPropertiesFromRow(entity, row, propertyProvider, wrapper); + readPropertiesFromRow(entity, rowValueProvider, wrapper); return wrapper.getBean(); } - protected void readPropertiesFromRow(final CassandraPersistentEntity entity, final Row row, - final CassandraPropertyValueProvider propertyProvider, final BeanWrapper wrapper) { + protected void readPropertiesFromRow(final CassandraPersistentEntity entity, + final DefaultCassandraRowValueProvider row, final BeanWrapper wrapper) { entity.doWithProperties(new PropertyHandler() { @Override public void doWithPersistentProperty(CassandraPersistentProperty prop) { - MappingCassandraConverter.this.readPropertyFromRow(row, entity, prop, propertyProvider, wrapper); + MappingCassandraConverter.this.readPropertyFromRow(entity, prop, row, wrapper); } }); } - protected void readPropertyFromRow(final Row row, final CassandraPersistentEntity entity, - final CassandraPersistentProperty prop, final CassandraPropertyValueProvider propertyProvider, - final BeanWrapper wrapper) { + protected void readPropertyFromRow(final CassandraPersistentEntity entity, final CassandraPersistentProperty prop, + final DefaultCassandraRowValueProvider row, final BeanWrapper wrapper) { if (entity.isConstructorArgument(prop)) { // skip 'cause prop was set in ctor return; } if (prop.isCompositePrimaryKey()) { - readPropertiesFromRow(prop.getCompositePrimaryKeyEntity(), row, propertyProvider, wrapper); + + // get the key + CassandraPersistentProperty keyProperty = entity.getIdProperty(); + Object key = wrapper.getProperty(keyProperty); + if (key == null) { + key = instantiatePrimaryKey(keyProperty.getCompositePrimaryKeyEntity(), keyProperty, row); + } + + // wrap the key + @SuppressWarnings("rawtypes") + BeanWrapper keyWrapper = BeanWrapper.create(key, conversionService); + + // now recurse on using the key this time + readPropertiesFromRow(prop.getCompositePrimaryKeyEntity(), row, keyWrapper); + + // now that the key's properties have been populated, set the key property on the entity + wrapper.setProperty(keyProperty, keyWrapper.getBean(), useFieldAccessOnly); return; } - if (!row.getColumnDefinitions().contains(prop.getColumnName())) { + if (!row.getRow().getColumnDefinitions().contains(prop.getColumnName())) { return; } - Object obj = propertyProvider.getPropertyValue(prop); + Object obj = row.getPropertyValue(prop); wrapper.setProperty(prop, obj, useFieldAccessOnly); } + protected Object instantiatePrimaryKey(CassandraPersistentEntity entity, CassandraPersistentProperty keyProperty, + DefaultCassandraRowValueProvider propertyProvider) { + + EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity); + + return instantiator.createInstance(entity, new CassandraPersistentEntityParameterValueProvider(entity, + propertyProvider, null)); + } + public boolean getUseFieldAccessOnly() { return useFieldAccessOnly; } @@ -311,7 +330,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem } @Override - public CassandraMappingContext getCassandraMappingContext() { + public CassandraMappingContext getMappingContext() { return mappingContext; } } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java index 2cc461678..189719802 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java @@ -84,7 +84,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Assert.notNull(cassandraConverter); this.cassandraConverter = cassandraConverter; - mappingContext = cassandraConverter.getCassandraMappingContext(); + mappingContext = cassandraConverter.getMappingContext(); } @Override diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentEntity.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentEntity.java index 863e8f838..a1b52dd25 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentEntity.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/BasicCassandraPersistentEntity.java @@ -27,6 +27,7 @@ import org.springframework.context.expression.BeanFactoryResolver; import org.springframework.data.cassandra.util.CassandraNamingUtils; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.AssociationHandler; +import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.model.BasicPersistentEntity; import org.springframework.data.util.TypeInformation; diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraMappingContext.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraMappingContext.java index 126605c9a..d468e4b0a 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraMappingContext.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraMappingContext.java @@ -1,5 +1,7 @@ package org.springframework.data.cassandra.mapping; +import java.util.Collection; + import org.springframework.cassandra.core.keyspace.CreateTableSpecification; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.util.TypeInformation; @@ -14,6 +16,34 @@ import com.datastax.driver.core.TableMetadata; public interface CassandraMappingContext extends MappingContext, CassandraPersistentProperty> { + /** + * Returns only those entities that don't represent primary key types. + * + * @see #getPersistentEntities(boolean) + */ + @Override + public Collection> getPersistentEntities(); + + /** + * Returns all persistent entities or only non-primary-key entities. + * + * @param includePrimaryKeyTypes If true, returns all entities, including entities that represent primary + * key types. If false, returns only entities that don't represent primary key types. + */ + public Collection> getPersistentEntities(boolean includePrimaryKeyTypes); + + /** + * Returns only those entities representing primary key types. + */ + Collection> getPrimaryKeyEntities(); + + /** + * Returns only those entities not representing primary key types. + * + * @see #getPersistentEntities(boolean) + */ + Collection> getNonPrimaryKeyEntities(); + /** * Returns a {@link CreateTableSpecification} for the given entity, including all mapping information. * diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentProperty.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentProperty.java index e19addf69..01654d151 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentProperty.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/CassandraPersistentProperty.java @@ -99,4 +99,7 @@ public interface CassandraPersistentProperty extends PersistentProperty getOwner(); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/DefaultCassandraMappingContext.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/DefaultCassandraMappingContext.java index 779637227..e48f7ec7d 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/DefaultCassandraMappingContext.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/DefaultCassandraMappingContext.java @@ -19,6 +19,8 @@ import static org.springframework.cassandra.core.keyspace.CreateTableSpecificati import java.beans.PropertyDescriptor; import java.lang.reflect.Field; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -51,6 +53,8 @@ public class DefaultCassandraMappingContext extends protected ApplicationContext context; protected Map>> entitySetsByTableName = new HashMap>>(); + protected Set> nonPrimaryKeyEntities = new HashSet>(); + protected Set> primaryKeyEntities = new HashSet>(); /** * Creates a new {@link DefaultCassandraMappingContext}. @@ -59,6 +63,29 @@ public class DefaultCassandraMappingContext extends setSimpleTypeHolder(new CassandraSimpleTypeHolder()); } + @Override + public Collection> getPersistentEntities() { + return getPersistentEntities(false); + } + + @Override + public Collection> getPrimaryKeyEntities() { + return Collections.unmodifiableSet(primaryKeyEntities); + } + + @Override + public Collection> getNonPrimaryKeyEntities() { + return Collections.unmodifiableSet(nonPrimaryKeyEntities); + } + + @Override + public Collection> getPersistentEntities(boolean includePrimaryKeyTypes) { + if (includePrimaryKeyTypes) { + return super.getPersistentEntities(); + } + return Collections.unmodifiableSet(nonPrimaryKeyEntities); + } + @Override public CassandraPersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor, CassandraPersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { @@ -81,6 +108,8 @@ public class DefaultCassandraMappingContext extends entity.setApplicationContext(context); } + // now do some caching of the entity + Set> entities = entitySetsByTableName.get(entity.getTableName()); if (entities == null) { entities = new HashSet>(); @@ -88,6 +117,12 @@ public class DefaultCassandraMappingContext extends entities.add(entity); entitySetsByTableName.put(entity.getTableName(), entities); + if (entity.isCompositePrimaryKey()) { + primaryKeyEntities.add(entity); + } else { + nonPrimaryKeyEntities.add(entity); + } + return entity; } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/Comment.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/Comment.java index 30e8b06ca..8f1661462 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/Comment.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/Comment.java @@ -15,46 +15,46 @@ */ package org.springframework.data.cassandra.test.integration.composites; -import java.util.Date; -import java.util.Set; - import org.springframework.data.cassandra.mapping.PrimaryKey; -import org.springframework.data.cassandra.mapping.CassandraType; import org.springframework.data.cassandra.mapping.Table; - -import com.datastax.driver.core.DataType; +import org.springframework.util.Assert; /** * This is an example of dynamic table (wide row). PartitionKey (former RowId) is pk.author. ClusteredColumn (former * Column Id) is pk.time * * @author Alex Shvid + * @author Matthew T. Adams */ @Table("comments") public class Comment { - /* - * Primary Key - */ @PrimaryKey - private CommentPK pk; + private CommentKey pk; private String text; - @CassandraType(type = DataType.Name.SET, typeArguments = { DataType.Name.TEXT }) - private Set likes; - - /* - * Reference to the Post + /** + * @deprecated Only for use by persistence infrastructure */ - private String postAuthor; - private Date postTime; + @Deprecated + protected Comment() { + } - public CommentPK getPk() { + public Comment(String author, String company) { + this(new CommentKey(author, company)); + } + + public Comment(CommentKey pk) { + Assert.notNull(pk); + this.pk = pk; + } + + public CommentKey getId() { return pk; } - public void setPk(CommentPK pk) { + public void setPk(CommentKey pk) { this.pk = pk; } @@ -66,28 +66,30 @@ public class Comment { this.text = text; } - public Set getLikes() { - return likes; + @Override + public boolean equals(Object that) { + + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (!(that instanceof Comment)) { + return false; + } + + Comment other = (Comment) that; + + if (this.pk == null) { + return other.pk == null; + } + + return this.pk.equals(other.pk); } - public void setLikes(Set likes) { - this.likes = likes; + @Override + public int hashCode() { + return pk.hashCode(); } - - public String getPostAuthor() { - return postAuthor; - } - - public void setPostAuthor(String postAuthor) { - this.postAuthor = postAuthor; - } - - public Date getPostTime() { - return postTime; - } - - public void setPostTime(Date postTime) { - this.postTime = postTime; - } - } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentKey.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentKey.java new file mode 100644 index 000000000..bf1797d20 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentKey.java @@ -0,0 +1,99 @@ +/* + * Copyright 2010-2013 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.test.integration.composites; + +import java.io.Serializable; + +import org.springframework.cassandra.core.PrimaryKeyType; +import org.springframework.data.cassandra.mapping.PrimaryKeyClass; +import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; + +/** + * This is an example of dynamic table (wide row) that creates each time new column with timestamp. + * + * @author Alex Shvid + * @author Matthew T. Adams + */ +@PrimaryKeyClass +public class CommentKey implements Serializable { + + private static final long serialVersionUID = -7871651389236401141L; + + @PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED) + private String author; + + @PrimaryKeyColumn(ordinal = 1) + private String company; + + /** + * @deprecated Only for use by persistence infrastructure + */ + @Deprecated + protected CommentKey() { + } + + public CommentKey(String author, String company) { + setAuthor(author); + setCompany(company); + } + + public String getAuthor() { + return author; + } + + protected void setAuthor(String author) { + this.author = author; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((author == null) ? 0 : author.hashCode()); + result = prime * result + ((company == null) ? 0 : company.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CommentKey other = (CommentKey) obj; + if (author == null) { + if (other.author != null) + return false; + } else if (!author.equals(other.author)) + return false; + if (company == null) { + if (other.company != null) + return false; + } else if (!company.equals(other.company)) + return false; + return true; + } +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentPK.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentPK.java deleted file mode 100644 index 8aa49748e..000000000 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentPK.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2010-2013 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.test.integration.composites; - -import java.util.Date; - -import org.springframework.cassandra.core.PrimaryKeyType; -import org.springframework.data.cassandra.mapping.PrimaryKeyClass; -import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; -import org.springframework.data.cassandra.mapping.CassandraType; - -import com.datastax.driver.core.DataType; - -/** - * This is an example of dynamic table (wide row) that creates each time new column with timestamp. - * - * @author Alex Shvid - */ - -@PrimaryKeyClass -public class CommentPK { - - /* - * Row ID - */ - @PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED) - private String author; - - /* - * Clustered Column - */ - @PrimaryKeyColumn(ordinal = 1) - @CassandraType(type = DataType.Name.TIMESTAMP) - private Date time; - - public String getAuthor() { - return author; - } - - public void setAuthor(String author) { - this.author = author; - } - - public Date getTime() { - return time; - } - - public void setTime(Date time) { - this.time = time; - } - -} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepository.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepository.java new file mode 100644 index 000000000..8cebc62e6 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepository.java @@ -0,0 +1,6 @@ +package org.springframework.data.cassandra.test.integration.composites; + +import org.springframework.data.cassandra.repository.CassandraRepository; + +public interface CommentRepository extends CassandraRepository { +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryIntegrationTests.java new file mode 100644 index 000000000..ff60c4793 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryIntegrationTests.java @@ -0,0 +1,120 @@ +/* + * Copyright 2011-2013 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.test.integration.composites; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; + +import org.springframework.data.cassandra.core.CassandraOperations; + +/** + * Tests for {@link CommentRepository}. + * + * @author Matthew T. Adams + */ +public class CommentRepositoryIntegrationTests { + + CommentRepository repository; + CassandraOperations template; + + public CommentRepositoryIntegrationTests() { + } + + public CommentRepositoryIntegrationTests(CommentRepository repository, CassandraOperations template) { + this.repository = repository; + this.template = template; + } + + public void before() { + repository.deleteAll(); + } + + public void after() { + repository.deleteAll(); + } + + public void testInsert() { + + String author = "testAuthorInsert"; + String company = "testCompanyInsert"; + + Comment c = new Comment(author, company); + c.setText("testTextInsert"); + + CommentKey key = c.getId(); + + repository.save(c); + + Comment retrieved = repository.findOne(key); + + assertNotSame(c, retrieved); + assertEquals(c, retrieved); + assertEquals(c.getText(), retrieved.getText()); + } + + public void testUpdateNonKeyField() { + + String author = "testAuthorUpdate"; + String company = "testCompanyUpdate"; + + Comment c = new Comment(author, company); + c.setText("testTextUpdate"); + + CommentKey key = c.getId(); + + repository.save(c); + + Comment retrieved = repository.findOne(key); + + assertNotSame(c, retrieved); + assertEquals(c, retrieved); + assertEquals(c.getText(), retrieved.getText()); + + String newText = "x" + retrieved.getText(); + retrieved.setText(newText); + + repository.save(retrieved); + + Comment updated = repository.findOne(key); + + assertNotSame(retrieved, updated); + assertEquals(newText, updated.getText()); + } + + public void testDelete() { + + String author = "testAuthorDelete"; + String company = "testCompanyDelete"; + + Comment c = new Comment(author, company); + c.setText("testTextDelete"); + + CommentKey key = c.getId(); + + repository.save(c); + + Comment retrieved = repository.findOne(key); + + assertNotSame(c, retrieved); + assertEquals(c, retrieved); + assertEquals(c.getText(), retrieved.getText()); + + repository.delete(retrieved); + + assertNull(repository.findOne(key)); + } +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryJavaConfigIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryJavaConfigIntegrationTests.java new file mode 100644 index 000000000..4515b97af --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryJavaConfigIntegrationTests.java @@ -0,0 +1,110 @@ +/* + * Copyright 2011-2013 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.test.integration.composites; + +import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.cassandra.config.SchemaAction; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; +import org.springframework.data.cassandra.test.integration.repository.UserRepository; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.data.cassandra.test.integration.support.TestConfig; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Base class for Java config tests for {@link UserRepository}. + * + * @author Matthew T. Adams + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class CommentRepositoryJavaConfigIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { + + @Configuration + @EnableCassandraRepositories(basePackageClasses = CommentRepository.class) + public static class Config extends TestConfig { + + @Override + protected String getKeyspaceName() { + return CommentRepositoryJavaConfigIntegrationTests.class.getSimpleName(); + } + + @Override + protected List getKeyspaceCreations() { + List creates = new ArrayList(); + + creates.add(createKeyspace().name(getKeyspaceName()).withSimpleReplication()); + + return creates; + } + + @Override + public SchemaAction getSchemaAction() { + return SchemaAction.RECREATE; + } + + @Override + public String getEntityBasePackage() { + return Comment.class.getPackage().getName(); + } + } + + @Autowired + protected CommentRepository repository; + + @Autowired + protected CassandraOperations template; + + CommentRepositoryIntegrationTests tests; + + @Before + public void before() { + tests = new CommentRepositoryIntegrationTests(repository, template); + tests.before(); + } + + @After + public void after() { + tests.after(); + } + + @Test + public void testInsert() { + tests.testInsert(); + } + + @Test + public void testDelete() { + tests.testDelete(); + } + + @Test + public void testUpdateNonKeyField() { + tests.testUpdateNonKeyField(); + } +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests.java new file mode 100644 index 000000000..a43264305 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2011-2013 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.test.integration.composites; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.test.integration.repository.UserRepository; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Base class for xml config tests for {@link UserRepository}. + * + * @author Matthew T. Adams + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class CommentRepositoryXmlConfigIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { + + @Autowired + protected CommentRepository repository; + + @Autowired + protected CassandraOperations template; + + CommentRepositoryIntegrationTests tests; + + @Before + public void setUp() throws InterruptedException { + tests = new CommentRepositoryIntegrationTests(repository, template); + tests.before(); + } + + @After + public void after() { + tests.after(); + } + + @Test + public void testInsert() { + tests.testInsert(); + } + + @Test + public void testDelete() { + tests.testDelete(); + } + + @Test + public void testUpdateNonKeyField() { + tests.testUpdateNonKeyField(); + } +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/PostPK.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/PostPK.java index aa913449c..b353e0097 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/PostPK.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/composites/PostPK.java @@ -34,15 +34,9 @@ import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; @PrimaryKeyClass public class PostPK { - /* - * Row ID - */ @PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED) private String author; - /* - * Clustered Column - */ @PrimaryKeyColumn(ordinal = 1) private Date time; diff --git a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests-context.xml b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests-context.xml new file mode 100644 index 000000000..a181a4745 --- /dev/null +++ b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/composites/CommentRepositoryXmlConfigIntegrationTests-context.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml index c9b35cf56..a80188b22 100644 --- a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml +++ b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml @@ -1,45 +1,26 @@ + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd + "> - + - - - + port="${cassandra.native_transport_port}"> - - - - - - - - - - diff --git a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/cassandra.properties b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/cassandra.properties deleted file mode 100644 index 2dda9f04e..000000000 --- a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/cassandra.properties +++ /dev/null @@ -1,3 +0,0 @@ -cassandra.contactPoints=localhost -cassandra.native_transport_port=@build.cassandra.native_transport_port@ -cassandra.keyspace=UserRepositoryXmlConfigIntegrationTests diff --git a/spring-data-cassandra/src/test/resources/spring-data-cassandra-basic.xml b/spring-data-cassandra/src/test/resources/spring-data-cassandra-basic.xml new file mode 100644 index 000000000..7c6868246 --- /dev/null +++ b/spring-data-cassandra/src/test/resources/spring-data-cassandra-basic.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + diff --git a/spring-data-cassandra/src/test/resources/spring-data-cassandra-build.properties b/spring-data-cassandra/src/test/resources/spring-data-cassandra-build.properties new file mode 100644 index 000000000..ab9c6a319 --- /dev/null +++ b/spring-data-cassandra/src/test/resources/spring-data-cassandra-build.properties @@ -0,0 +1 @@ +cassandra.native_transport_port=@build.cassandra.native_transport_port@