Skip to content

Commit

Permalink
Merge branch 'master' into DATACASS-1
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewadams committed Jan 20, 2014
2 parents a3390c3 + 4ac4d1d commit 363feaf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,40 @@
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Session;

public abstract class AbstractEmbeddedCassandraIntegrationTest {

private static Logger log = LoggerFactory.getLogger(AbstractEmbeddedCassandraIntegrationTest.class);

protected final static String CASSANDRA_CONFIG = "spring-cassandra.yaml";
protected final static String CASSANDRA_HOST = "localhost";
protected final static int CASSANDRA_NATIVE_PORT = 9042;

@BeforeClass
public static void startCassandra() throws ConfigurationException, TTransportException, IOException,
InterruptedException {
log.info("Starting Cassandra Embedded Server");
EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG);
}

public AbstractEmbeddedCassandraIntegrationTest() {
try {
startCassandra();
} catch (Exception e) {
throw new RuntimeException(e);
if (session == null) {
connect();
}
}

public AbstractEmbeddedCassandraIntegrationTest(String keyspace) {
this.keyspace = keyspace;
if (session == null) {
connect();
}
connect();
}

/**
Expand All @@ -48,10 +59,11 @@ public AbstractEmbeddedCassandraIntegrationTest() {
* If not <code>null</code>, get a {@link Session} for the from the {@link #cluster}.
*/
protected String keyspace = "ks" + UUID.randomUUID().toString().replace("-", "");

/**
* The {@link Session} for the {@link #keyspace} from the {@link #cluster}.
*/
protected Session session;
protected static Session session;

protected String keyspace() {
return keyspace;
Expand All @@ -69,7 +81,11 @@ public Cluster cluster() {
}

public void connect() {

if (connect && !connected()) {

log.info("Connecting to Cassandra");

cluster = cluster();

if (keyspace() == null) {
Expand All @@ -91,8 +107,11 @@ public void connect() {

@After
public void after() {
log.info("After: clear -> " + clear + ", connected -> " + connected());
if (clear && connected()) {
log.info("Cleaning Cassandra");
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import static org.springframework.cassandra.test.integration.core.cql.generator.CqlTableSpecificationAssertions.assertNoTable;
import static org.springframework.cassandra.test.integration.core.cql.generator.CqlTableSpecificationAssertions.assertTable;

import org.cassandraunit.CassandraCQLUnit;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,6 +42,18 @@ public class TableLifecycleIntegrationTest extends AbstractEmbeddedCassandraInte

CreateTableCqlGeneratorTests.MultipleOptionsTest createTableTest = new CreateTableCqlGeneratorTests.MultipleOptionsTest();

public TableLifecycleIntegrationTest() {
super("tlit");
clear = true;
}

// This only ensures the keyspace exists before each test, while using a static session from the parent object.
// TODO - DW Make this better.
@Rule
public CassandraCQLUnit cassandraCQLUnit = new CassandraCQLUnit(new ClassPathCQLDataSet(
"cassandraOperationsTest-cql-dataload.cql", this.keyspace), CASSANDRA_CONFIG, CASSANDRA_HOST,
CASSANDRA_NATIVE_PORT);

@Test
public void testDrop() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
*/
public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegrationTest {

private CassandraOperations cassandraTemplate;
private static CassandraOperations cassandraTemplate;

private static Logger log = LoggerFactory.getLogger(CassandraOperationsTest.class);

Expand All @@ -88,9 +88,26 @@ public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegratio
"cassandraOperationsTest-cql-dataload.cql", this.keyspace), CASSANDRA_CONFIG, CASSANDRA_HOST,
CASSANDRA_NATIVE_PORT);

public CassandraOperationsTest() {
super("sdctest");
clear = true;
}

@Before
public void setupTemplate() {
cassandraTemplate = new CassandraTemplate(session);

log.info("Running setupTemplate()");

if (cassandraTemplate == null) {

log.info("null Template ... Initialzing DB test CQL");

// CassandraCQLUnit cassandraCQLUnit = new CassandraCQLUnit(new ClassPathCQLDataSet(
// "cassandraOperationsTest-cql-dataload.cql", keyspace), CASSANDRA_CONFIG, CASSANDRA_HOST,
// CASSANDRA_NATIVE_PORT);

cassandraTemplate = new CassandraTemplate(session);
}
}

@Test
Expand Down Expand Up @@ -140,9 +157,11 @@ public Collection<MyHost> mapHosts(Set<Host> host) throws DriverException {
}

@Test
@SuppressWarnings( "unchecked" )
@SuppressWarnings("unchecked")
public void ingestionTestListOfList() {

log.info("Keyspace => " + keyspace);

String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";

List<List<?>> values = new LinkedList<List<?>>();
Expand All @@ -166,6 +185,8 @@ public void ingestionTestListOfList() {
@Test
public void ingestionTestObjectArray() {

log.info("Keyspace => " + keyspace);

String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";

Object[][] values = new Object[3][];
Expand Down Expand Up @@ -508,7 +529,7 @@ public void queryForObjectTestCqlStringRowMapperNotOneRowReturned() {
// Insert our 3 test books.
ingestionTestObjectArray();

@SuppressWarnings( "unused" )
@SuppressWarnings("unused")
Book book = cassandraTemplate.queryForObject("select * from book where isbn in ('1234','2345','3456')",
new RowMapper<Book>() {
@Override
Expand Down Expand Up @@ -563,7 +584,7 @@ public void quertForObjectTestCqlStringRequiredType() {
@Test(expected = ClassCastException.class)
public void queryForObjectTestCqlStringRequiredTypeInvalid() {

@SuppressWarnings( "unused" )
@SuppressWarnings("unused")
Float title = cassandraTemplate.queryForObject("select title from book where isbn in ('" + ISBN_NINES + "')",
Float.class);

Expand Down

0 comments on commit 363feaf

Please sign in to comment.