From a962be4151dbdc74f12322e19f641d698c031a10 Mon Sep 17 00:00:00 2001 From: john-mcpeek Date: Tue, 17 Dec 2013 08:25:17 -0500 Subject: [PATCH] Added Keyspace tests. --- .../CreateKeyspaceCqlGeneratorTests.java | 64 +++++++++++++++++++ .../KeyspaceOperationCqlGeneratorTest.java | 39 +++++++++++ 2 files changed, 103 insertions(+) create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateKeyspaceCqlGeneratorTests.java create mode 100644 spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/KeyspaceOperationCqlGeneratorTest.java diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateKeyspaceCqlGeneratorTests.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateKeyspaceCqlGeneratorTests.java new file mode 100644 index 000000000..a596a9b3d --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/CreateKeyspaceCqlGeneratorTests.java @@ -0,0 +1,64 @@ +package org.springframework.cassandra.test.unit.core.cql.generator; + +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.springframework.cassandra.core.cql.generator.CreateKeyspaceCqlGenerator; +import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification; +import org.springframework.cassandra.core.keyspace.DefaultOption; +import org.springframework.cassandra.core.keyspace.KeyspaceOption; +import org.springframework.cassandra.core.keyspace.Option; + +public class CreateKeyspaceCqlGeneratorTests { + + /** + * Asserts that the preamble is first & correctly formatted in the given CQL string. + */ + public static void assertPreamble(String keyspaceName, String cql) { + System.out.println( "'" + cql + "'" ); + assertTrue(cql.startsWith("CREATE KEYSPACE " + keyspaceName + " ")); + } + + /** + * Convenient base class that other test classes can use so as not to repeat the generics declarations or + * {@link #generator()} method. + */ + public static abstract class CreateKeyspaceTest extends + KeyspaceOperationCqlGeneratorTest { + + public CreateKeyspaceCqlGenerator generator() { + return new CreateKeyspaceCqlGenerator(specification); + } + } + + public static class BasicTest extends CreateKeyspaceTest { + + public String name = "mytable"; + public Boolean durableWrites = true; + + public Map replicationMap = new HashMap(); + + @Override + public CreateKeyspaceSpecification specification() { + replicationMap.put( new DefaultOption( "class", String.class, false, false, true ), "SimpleStrategy" ); + replicationMap.put( new DefaultOption( "replication_factor", Long.class, false, false, true ), 1 ); + replicationMap.put( new DefaultOption( "dc1", Long.class, false, false, true ), 2 ); + replicationMap.put( new DefaultOption( "dc2", Long.class, false, false, true ), 3 ); + + return (CreateKeyspaceSpecification) CreateKeyspaceSpecification.createKeyspace() + .name( name ) + .with( KeyspaceOption.REPLICATION, replicationMap ) + .with( KeyspaceOption.DURABLE_WRITES, durableWrites ); + } + + @Test + public void test() { + prepare(); + + assertPreamble(name, cql); + } + } +} diff --git a/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/KeyspaceOperationCqlGeneratorTest.java b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/KeyspaceOperationCqlGeneratorTest.java new file mode 100644 index 000000000..6aebc93f8 --- /dev/null +++ b/spring-cassandra/src/test/java/org/springframework/cassandra/test/unit/core/cql/generator/KeyspaceOperationCqlGeneratorTest.java @@ -0,0 +1,39 @@ +package org.springframework.cassandra.test.unit.core.cql.generator; + +import org.junit.Test; +import org.springframework.cassandra.core.cql.generator.KeyspaceNameCqlGenerator; +import org.springframework.cassandra.core.cql.generator.TableNameCqlGenerator; +import org.springframework.cassandra.core.keyspace.KeyspaceNameSpecification; +import org.springframework.cassandra.core.keyspace.TableNameSpecification; + +/** + * Useful test class that specifies just about as much as you can for a CQL generation test. Intended to be extended by + * classes that contain methods annotated with {@link Test}. Everything is public because this is a test class with no + * need for encapsulation, and it makes for easier reuse in other tests like integration tests (hint hint). + * + * @author Matthew T. Adams + * + * @param The type of the {@link TableNameSpecification} + * @param The type of the {@link TableNameCqlGenerator} + */ +public abstract class KeyspaceOperationCqlGeneratorTest, G extends KeyspaceNameCqlGenerator> { + + public abstract S specification(); + + public abstract G generator(); + + public String tableName; + public S specification; + public G generator; + public String cql; + + public void prepare() { + this.specification = specification(); + this.generator = generator(); + this.cql = generateCql(); + } + + public String generateCql() { + return generator.toCql(); + } +} \ No newline at end of file