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; 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.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.After; import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster;
@ -30,69 +25,53 @@ import com.datastax.driver.core.Session;
public abstract class AbstractEmbeddedCassandraTest { 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() { public static boolean isConnected() {
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() {
return session != null; return session != null;
} }
public Cluster getCluster() { public static Cluster getCluster() {
return cluster; return cluster;
} }
public Session getSession() { public static Session getSession() {
return session; return session;
} }
public String getKeyspace() { public static String getKeyspace() {
return keyspace; return keyspace;
} }
@Before @BeforeClass
public void before() { public static void before() throws Exception {
if (!isConnected()) { EmbeddedCassandraServerHelper.startEmbeddedCassandra(BuildProperties.getCassandraConfig());
cluster = Cluster.builder() cluster = Cluster.builder()
.addContactPoint(BuildProperties.getCassandraHost()) .addContactPoint(BuildProperties.getCassandraHost())
.withPort(BuildProperties.getCassandraNativePort()) .withPort(BuildProperties.getCassandraNativePort())
.build(); .build();
KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace); KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace);
if (kmd == null) { if (kmd == null) {
session = cluster.connect(); session = cluster.connect();
session.execute("CREATE KEYSPACE " + keyspace session.execute("CREATE KEYSPACE " + keyspace
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};"); + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};");
session.execute("USE " + keyspace + ";"); session.execute("USE " + keyspace + ";");
} else { } else {
session = cluster.connect(keyspace); session = cluster.connect(keyspace);
}
} }
} }
@After @AfterClass
public void after() { public static void after() {
if (!keep && isConnected()) { if (!keep && isConnected()) {
session.close(); session.close();
session = null; session = null;

View file

@ -15,10 +15,12 @@
*/ */
package com.noorq.casser.test.integration.core.usertype; package com.noorq.casser.test.integration.core.usertype;
import static com.noorq.casser.core.Query.eq;
import java.util.UUID; import java.util.UUID;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.datastax.driver.core.DataType; 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.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.noorq.casser.core.Casser; import com.noorq.casser.core.Casser;
import com.noorq.casser.core.CasserSession; import com.noorq.casser.core.CasserSession;
import com.noorq.casser.core.Query;
import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest; import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest;
public class UserDefinedTypeTest extends 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 { public static class AccountImpl implements Account {
@ -100,8 +101,8 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
} }
@Before @BeforeClass
public void beforeTest() { public static void beforeTest() {
account = Casser.dsl(Account.class); account = Casser.dsl(Account.class);
@ -166,6 +167,24 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
System.out.println(account); 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 @Test
public void testNoMapping() { public void testNoMapping() {
@ -182,7 +201,7 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
csession.upsert(acc).sync(); 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.getType(), found.getType());
Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1")); Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1"));