initialize cassandra-unit only in static methods in integration tests

This commit is contained in:
Albert Shift 2015-04-09 14:45:51 -07:00
parent bde9ffd7a0
commit e2e8bcabf3
2 changed files with 55 additions and 57 deletions

View file

@ -15,13 +15,8 @@
*/
package com.noorq.casser.test.integration.build;
import java.io.IOException;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import com.datastax.driver.core.Cluster;
@ -30,69 +25,53 @@ import com.datastax.driver.core.Session;
public abstract class AbstractEmbeddedCassandraTest {
private Cluster cluster;
private static Cluster cluster;
private String keyspace = BuildProperties.getRandomKeyspace();
private static String keyspace = BuildProperties.getRandomKeyspace();
private Session session;
private static Session session;
private boolean keep;
private static boolean keep;
public AbstractEmbeddedCassandraTest() {
this(false);
}
public AbstractEmbeddedCassandraTest(boolean keep) {
this.keep = keep;
}
@BeforeClass
public static void beforeClass() throws ConfigurationException, TTransportException, IOException,
InterruptedException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(BuildProperties.getCassandraConfig());
}
public boolean isConnected() {
public static boolean isConnected() {
return session != null;
}
public Cluster getCluster() {
public static Cluster getCluster() {
return cluster;
}
public Session getSession() {
public static Session getSession() {
return session;
}
public String getKeyspace() {
public static String getKeyspace() {
return keyspace;
}
@Before
public void before() {
if (!isConnected()) {
cluster = Cluster.builder()
.addContactPoint(BuildProperties.getCassandraHost())
.withPort(BuildProperties.getCassandraNativePort())
.build();
@BeforeClass
public static void before() throws Exception {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(BuildProperties.getCassandraConfig());
cluster = Cluster.builder()
.addContactPoint(BuildProperties.getCassandraHost())
.withPort(BuildProperties.getCassandraNativePort())
.build();
KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace);
if (kmd == null) {
session = cluster.connect();
session.execute("CREATE KEYSPACE " + keyspace
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};");
session.execute("USE " + keyspace + ";");
} else {
session = cluster.connect(keyspace);
}
KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace);
if (kmd == null) {
session = cluster.connect();
session.execute("CREATE KEYSPACE " + keyspace
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};");
session.execute("USE " + keyspace + ";");
} else {
session = cluster.connect(keyspace);
}
}
@After
public void after() {
@AfterClass
public static void after() {
if (!keep && isConnected()) {
session.close();
session = null;

View file

@ -15,10 +15,12 @@
*/
package com.noorq.casser.test.integration.core.usertype;
import static com.noorq.casser.core.Query.eq;
import java.util.UUID;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.datastax.driver.core.DataType;
@ -36,16 +38,15 @@ import com.datastax.driver.core.schemabuilder.CreateType;
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.noorq.casser.core.Casser;
import com.noorq.casser.core.CasserSession;
import com.noorq.casser.core.Query;
import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest;
public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
Account account;
static Account account;
CasserSession csession;
static CasserSession csession;
UserType fullname;
static UserType fullname;
public static class AccountImpl implements Account {
@ -100,8 +101,8 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
}
@Before
public void beforeTest() {
@BeforeClass
public static void beforeTest() {
account = Casser.dsl(Account.class);
@ -166,6 +167,24 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
System.out.println(account);
}
@Test
public void testMapping() {
AddressImpl addr = new AddressImpl();
addr.street = "1 st";
addr.city = "San Jose";
AccountImpl acc = new AccountImpl();
acc.id = 123L;
acc.address = addr;
csession.upsert(acc).sync();
String streetName = csession.select(account.address()::street).where(account::id, eq(123L)).sync().findFirst().get()._1;
Assert.assertEquals("1 st", streetName);
}
@Test
public void testNoMapping() {
@ -182,7 +201,7 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
csession.upsert(acc).sync();
UDTValue found = csession.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst().get()._1;
UDTValue found = csession.select(account::addressNoMapping).where(account::id, eq(777L)).sync().findFirst().get()._1;
Assert.assertEquals(addressNoMapping.getType(), found.getType());
Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1"));