kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 61311abbf0989bd7cf7fb137001dc0755e2ce82f247de599424f272b689b043e |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-01-18 21:11 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-01-27 18:51 +01:00 |
| Parents | d3d82a63787d |
93 files changed
modified driver-core/src/test/java/com/datastax/driver/core/AbstractPoliciesTest.java +53 −86
| @@ -15,6 +15,10 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 19 | +import org.slf4j.Logger; | |
| 20 | +import org.slf4j.LoggerFactory; | |
| 21 | +import org.testng.annotations.AfterMethod; | |
| 18 | 22 | import org.testng.annotations.BeforeMethod; |
| 19 | 23 | |
| 20 | 24 | import java.net.InetAddress; |
| @@ -23,45 +27,53 @@ | ||
| 23 | 27 | import java.util.Map; |
| 24 | 28 | import java.util.concurrent.Callable; |
| 25 | 29 | |
| 26 | -import static com.datastax.driver.core.TestUtils.*; | |
| 30 | +import static com.datastax.driver.core.ConditionChecker.check; | |
| 31 | +import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_GENERIC_FORMAT; | |
| 32 | +import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT; | |
| 27 | 33 | import static com.datastax.driver.core.querybuilder.QueryBuilder.batch; |
| 28 | 34 | import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; |
| 29 | -import static org.testng.Assert.*; | |
| 35 | +import static java.util.concurrent.TimeUnit.MINUTES; | |
| 36 | +import static org.testng.Assert.assertEquals; | |
| 30 | 37 | |
| 31 | 38 | public abstract class AbstractPoliciesTest extends CCMTestsSupport { |
| 32 | 39 | |
| 33 | - private static final boolean DEBUG = false; | |
| 40 | + private static final Logger logger = LoggerFactory.getLogger(AbstractPoliciesTest.class); | |
| 41 | + private String tableName; | |
| 42 | + | |
| 43 | + private static class SchemaInAgreement implements Callable<Boolean> { | |
| 44 | + | |
| 45 | + private final Cluster cluster; | |
| 46 | + | |
| 47 | + private SchemaInAgreement(Cluster cluster) { | |
| 48 | + this.cluster = cluster; | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public Boolean call() throws Exception { | |
| 53 | + return cluster.getMetadata().checkSchemaAgreement(); | |
| 54 | + } | |
| 55 | + } | |
| 34 | 56 | |
| 35 | 57 | protected Map<InetAddress, Integer> coordinators = new HashMap<InetAddress, Integer>(); |
| 36 | 58 | |
| 37 | 59 | protected PreparedStatement prepared; |
| 38 | 60 | |
| 39 | 61 | protected void createSchema(int replicationFactor) { |
| 40 | - final String ks = TestUtils.getAvailableKeyspaceName(); | |
| 41 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ks, replicationFactor)); | |
| 42 | - // tests fail randomly here with InvalidQueryException: Keyspace 'xxx' does not exist | |
| 43 | - ConditionChecker.awaitUntil(new Callable<Boolean>() { | |
| 44 | - @Override | |
| 45 | - public Boolean call() { | |
| 46 | - return cluster.getMetadata().getKeyspace(ks) != null; | |
| 47 | - } | |
| 48 | - }, 60000); | |
| 49 | - session.execute("USE " + ks); | |
| 50 | - session.execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", SIMPLE_TABLE)); | |
| 62 | + final String ks = TestUtils.generateIdentifier("ks_"); | |
| 63 | + tableName = TestUtils.generateIdentifier("table_"); | |
| 64 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ks, replicationFactor)); | |
| 65 | + useKeyspace(ks); | |
| 66 | + session().execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", tableName)); | |
| 67 | + check().before(5, MINUTES).that(new SchemaInAgreement(cluster())).becomesTrue(); | |
| 51 | 68 | } |
| 52 | 69 | |
| 53 | 70 | protected void createMultiDCSchema(int dc1RF, int dc2RF) { |
| 54 | - final String ks = TestUtils.getAvailableKeyspaceName(); | |
| 55 | - session.execute(String.format(CREATE_KEYSPACE_GENERIC_FORMAT, ks, "NetworkTopologyStrategy", String.format("'dc1' : %d, 'dc2' : %d", dc1RF, dc2RF))); | |
| 56 | - // tests fail randomly here with InvalidQueryException: Keyspace 'xxx' does not exist | |
| 57 | - ConditionChecker.awaitUntil(new Callable<Boolean>() { | |
| 58 | - @Override | |
| 59 | - public Boolean call() { | |
| 60 | - return cluster.getMetadata().getKeyspace(ks) != null; | |
| 61 | - } | |
| 62 | - }, 60000); | |
| 63 | - session.execute("USE " + ks); | |
| 64 | - session.execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", SIMPLE_TABLE)); | |
| 71 | + final String ks = TestUtils.generateIdentifier("ks_"); | |
| 72 | + tableName = TestUtils.generateIdentifier("table_"); | |
| 73 | + session().execute(String.format(CREATE_KEYSPACE_GENERIC_FORMAT, ks, "NetworkTopologyStrategy", String.format("'dc1' : %d, 'dc2' : %d", dc1RF, dc2RF))); | |
| 74 | + useKeyspace(ks); | |
| 75 | + session().execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, i int)", tableName)); | |
| 76 | + check().before(5, MINUTES).that(new SchemaInAgreement(cluster())).becomesTrue(); | |
| 65 | 77 | } |
| 66 | 78 | |
| 67 | 79 | /** |
| @@ -78,6 +90,12 @@ | ||
| 78 | 90 | coordinators = new HashMap<InetAddress, Integer>(); |
| 79 | 91 | } |
| 80 | 92 | |
| 93 | + @AfterMethod(groups = "long") | |
| 94 | + protected void pause() { | |
| 95 | + // pause before engaging in another expensive CCM cluster creation | |
| 96 | + Uninterruptibles.sleepUninterruptibly(1, MINUTES); | |
| 97 | + } | |
| 98 | + | |
| 81 | 99 | private String queriedMapString() { |
| 82 | 100 | StringBuilder sb = new StringBuilder(); |
| 83 | 101 | sb.append("{"); |
| @@ -92,67 +110,15 @@ | ||
| 92 | 110 | protected void assertQueried(String host, int n) { |
| 93 | 111 | try { |
| 94 | 112 | Integer queried = coordinators.get(InetAddress.getByName(host)); |
| 95 | - if (DEBUG) | |
| 96 | - System.out.println(String.format("Expected: %s\tReceived: %s", n, queried)); | |
| 97 | - else { | |
| 98 | - assertEquals(queried == null ? 0 : queried, n, queriedMapString()); | |
| 99 | - } | |
| 100 | - } catch (Exception e) { | |
| 101 | - throw new RuntimeException(e); | |
| 102 | - } | |
| 103 | - } | |
| 104 | - | |
| 105 | - protected void assertQueriedAtLeast(String host, int n) { | |
| 106 | - try { | |
| 107 | - Integer queried = coordinators.get(InetAddress.getByName(host)); | |
| 108 | - queried = queried == null ? 0 : queried; | |
| 109 | - if (DEBUG) | |
| 110 | - System.out.println(String.format("Expected > %s\tReceived: %s", n, queried)); | |
| 113 | + if (logger.isDebugEnabled()) | |
| 114 | + logger.debug(String.format("Expected: %s\tReceived: %s", n, queried)); | |
| 111 | 115 | else |
| 112 | - assertTrue(queried >= n, "For " + host); | |
| 113 | - } catch (Exception e) { | |
| 114 | - throw new RuntimeException(e); | |
| 115 | - } | |
| 116 | - } | |
| 117 | - | |
| 118 | - /** | |
| 119 | - * Assert that one of the nodes in the list was queried with n, no matter which one | |
| 120 | - */ | |
| 121 | - protected void assertOneNodeQueried(int n, String... hosts) { | |
| 122 | - try { | |
| 123 | - boolean found = false; | |
| 124 | - for (String host : hosts) { | |
| 125 | - InetAddress addr = InetAddress.getByName(host); | |
| 126 | - int queried = coordinators.containsKey(addr) ? coordinators.get(addr) : 0; | |
| 127 | - if (DEBUG) | |
| 128 | - System.out.println(String.format("Expected: %s\tReceived: %s", n, queried)); | |
| 129 | - else { | |
| 130 | - | |
| 131 | - if (n == queried) { | |
| 132 | - if (found) | |
| 133 | - throw new AssertionError(String.format("Found 2 nodes with %s queries in %s", n, queriedMapString())); | |
| 134 | - found = true; | |
| 135 | - } else { | |
| 136 | - if (queried != 0) | |
| 137 | - throw new AssertionError(String.format("Host %s should have be queried: %s", addr, queriedMapString())); | |
| 138 | - } | |
| 139 | - } | |
| 140 | - } | |
| 141 | - if (!found) | |
| 142 | - throw new AssertionError("Found no host queried exactly " + n + " times in " + queriedMapString()); | |
| 116 | + assertEquals(queried == null ? 0 : queried, n, queriedMapString()); | |
| 143 | 117 | } catch (Exception e) { |
| 144 | 118 | throw new RuntimeException(e); |
| 145 | 119 | } |
| 146 | 120 | } |
| 147 | 121 | |
| 148 | - protected void failDebug(String message) { | |
| 149 | - if (DEBUG) | |
| 150 | - System.out.println(message); | |
| 151 | - else | |
| 152 | - fail(message); | |
| 153 | - } | |
| 154 | - | |
| 155 | - | |
| 156 | 122 | /** |
| 157 | 123 | * Init methods that handle writes using batch and consistency options. |
| 158 | 124 | */ |
| @@ -170,7 +136,7 @@ | ||
| 170 | 136 | |
| 171 | 137 | protected void init(int n, boolean batch, ConsistencyLevel cl) { |
| 172 | 138 | write(n, batch, cl); |
| 173 | - prepared = session.prepare("SELECT * FROM " + SIMPLE_TABLE + " WHERE k = ?").setConsistencyLevel(cl); | |
| 139 | + prepared = session().prepare("SELECT * FROM " + tableName + " WHERE k = ?").setConsistencyLevel(cl); | |
| 174 | 140 | } |
| 175 | 141 | |
| 176 | 142 | protected void write(int n) { |
| @@ -191,11 +157,11 @@ | ||
| 191 | 157 | for (int i = 0; i < n; ++i) |
| 192 | 158 | if (batch) |
| 193 | 159 | // BUG: WriteType == SIMPLE |
| 194 | - session.execute(batch() | |
| 195 | - .add(insertInto(SIMPLE_TABLE).values(new String[]{"k", "i"}, new Object[]{0, 0})) | |
| 160 | + session().execute(batch() | |
| 161 | + .add(insertInto(tableName).values(new String[]{"k", "i"}, new Object[]{0, 0})) | |
| 196 | 162 | .setConsistencyLevel(cl)); |
| 197 | 163 | else |
| 198 | - session.execute(new SimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", SIMPLE_TABLE)).setConsistencyLevel(cl)); | |
| 164 | + session().execute(new SimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", tableName)).setConsistencyLevel(cl)); | |
| 199 | 165 | } |
| 200 | 166 | |
| 201 | 167 | |
| @@ -218,12 +184,13 @@ | ||
| 218 | 184 | if (usePrepared) { |
| 219 | 185 | BoundStatement bs = prepared.bind(0); |
| 220 | 186 | for (int i = 0; i < n; ++i) |
| 221 | - addCoordinator(session.execute(bs)); | |
| 187 | + addCoordinator(session().execute(bs)); | |
| 222 | 188 | } else { |
| 223 | 189 | ByteBuffer routingKey = ByteBuffer.allocate(4); |
| 224 | 190 | routingKey.putInt(0, 0); |
| 225 | 191 | for (int i = 0; i < n; ++i) |
| 226 | - addCoordinator(session.execute(new SimpleStatement(String.format("SELECT * FROM %s WHERE k = 0", SIMPLE_TABLE)).setRoutingKey(routingKey).setConsistencyLevel(cl))); | |
| 192 | + addCoordinator(session().execute(new SimpleStatement(String.format("SELECT * FROM %s WHERE k = 0", tableName)).setRoutingKey(routingKey).setConsistencyLevel(cl))); | |
| 227 | 193 | } |
| 228 | 194 | } |
| 195 | + | |
| 229 | 196 | } |
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java +13 −11
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | import java.util.concurrent.*; |
| 32 | 32 | import java.util.concurrent.atomic.AtomicReference; |
| 33 | 33 | |
| 34 | +import static com.datastax.driver.core.ConditionChecker.check; | |
| 34 | 35 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | 36 | import static org.mockito.Mockito.*; |
| 36 | 37 | import static org.testng.Assert.fail; |
| @@ -41,8 +42,14 @@ | ||
| 41 | 42 | ScheduledExecutorService executor; |
| 42 | 43 | MockReconnectionSchedule schedule; |
| 43 | 44 | MockReconnectionWork work; |
| 44 | - final AtomicReference<ListenableFuture<?>> future = new AtomicReference<ListenableFuture<?>>(); | |
| 45 | + AtomicReference<ListenableFuture<?>> future = new AtomicReference<ListenableFuture<?>>(); | |
| 45 | 46 | AbstractReconnectionHandler handler; |
| 47 | + Callable<Boolean> nextTryAssigned = new Callable<Boolean>() { | |
| 48 | + @Override | |
| 49 | + public Boolean call() throws Exception { | |
| 50 | + return handler.handlerFuture.nextTry != null; | |
| 51 | + } | |
| 52 | + }; | |
| 46 | 53 | |
| 47 | 54 | @BeforeMethod(groups = {"unit", "short"}) |
| 48 | 55 | public void setup() { |
| @@ -58,7 +65,7 @@ | ||
| 58 | 65 | |
| 59 | 66 | @Override |
| 60 | 67 | protected void onReconnection(Connection connection) { |
| 61 | - work.onReconnection(connection); | |
| 68 | + work.onReconnection(); | |
| 62 | 69 | } |
| 63 | 70 | }; |
| 64 | 71 | } |
| @@ -149,12 +156,7 @@ | ||
| 149 | 156 | verify(executor, timeout(10000)).schedule(handler, schedule.delay, TimeUnit.MILLISECONDS); |
| 150 | 157 | |
| 151 | 158 | // Wait until nextTry is assigned after schedule completes. |
| 152 | - ConditionChecker.awaitUntil(new Callable<Boolean>() { | |
| 153 | - @Override | |
| 154 | - public Boolean call() throws Exception { | |
| 155 | - return handler.handlerFuture.nextTry != null; | |
| 156 | - } | |
| 157 | - }, 10000); | |
| 159 | + check().before(10000).that(nextTryAssigned).becomesTrue(); | |
| 158 | 160 | |
| 159 | 161 | future.get().cancel(false); |
| 160 | 162 | |
| @@ -341,7 +343,7 @@ | ||
| 341 | 343 | tries += 1; |
| 342 | 344 | logger.debug("in reconnection work, wait for tick from main thread"); |
| 343 | 345 | try { |
| 344 | - barrier.await(10, TimeUnit.SECONDS); | |
| 346 | + barrier.await(60, TimeUnit.SECONDS); | |
| 345 | 347 | logger.debug("in reconnection work, got tick from main thread, proceeding"); |
| 346 | 348 | } catch (Exception e) { |
| 347 | 349 | fail("Error while waiting for tick", e); |
| @@ -362,14 +364,14 @@ | ||
| 362 | 364 | public void tick() { |
| 363 | 365 | logger.debug("send tick to reconnection work"); |
| 364 | 366 | try { |
| 365 | - barrier.await(10, TimeUnit.SECONDS); | |
| 367 | + barrier.await(60, TimeUnit.SECONDS); | |
| 366 | 368 | } catch (Exception e) { |
| 367 | 369 | fail("Error while sending tick, no thread was waiting", e); |
| 368 | 370 | } |
| 369 | 371 | barrier.reset(); |
| 370 | 372 | } |
| 371 | 373 | |
| 372 | - protected void onReconnection(Connection connection) { | |
| 374 | + protected void onReconnection() { | |
| 373 | 375 | success = true; |
| 374 | 376 | } |
| 375 | 377 | } |
modified driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java +10 −14
| @@ -23,7 +23,6 @@ | ||
| 23 | 23 | import org.testng.annotations.Test; |
| 24 | 24 | |
| 25 | 25 | import java.util.Collection; |
| 26 | -import java.util.List; | |
| 27 | 26 | import java.util.concurrent.*; |
| 28 | 27 | |
| 29 | 28 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -38,18 +37,15 @@ | ||
| 38 | 37 | } |
| 39 | 38 | |
| 40 | 39 | @Override |
| 41 | - public Collection<String> createTestFixtures() { | |
| 42 | - List<String> definitions = Lists.newArrayList(); | |
| 43 | - | |
| 40 | + public void onTestContextInitialized() { | |
| 44 | 41 | for (Object[] objects : keyspace()) { |
| 45 | 42 | String keyspace = (String) objects[0]; |
| 46 | - | |
| 47 | - definitions.add(String.format("create keyspace %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}", keyspace)); | |
| 48 | - definitions.add(String.format("create table %s.foo(k int primary key, v int)", keyspace)); | |
| 49 | - definitions.add(String.format("insert into %s.foo (k, v) values (1, 1)", keyspace)); | |
| 43 | + execute( | |
| 44 | + String.format("create keyspace %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}", keyspace), | |
| 45 | + String.format("create table %s.foo(k int primary key, v int)", keyspace), | |
| 46 | + String.format("insert into %s.foo (k, v) values (1, 1)", keyspace) | |
| 47 | + ); | |
| 50 | 48 | } |
| 51 | - | |
| 52 | - return definitions; | |
| 53 | 49 | } |
| 54 | 50 | |
| 55 | 51 | /** |
| @@ -57,13 +53,13 @@ | ||
| 57 | 53 | */ |
| 58 | 54 | @Test(groups = "short") |
| 59 | 55 | public void cancelled_query_should_release_the_connection() throws InterruptedException { |
| 60 | - ResultSetFuture future = session.executeAsync("select release_version from system.local"); | |
| 56 | + ResultSetFuture future = session().executeAsync("select release_version from system.local"); | |
| 61 | 57 | future.cancel(true); |
| 62 | 58 | assertTrue(future.isCancelled()); |
| 63 | 59 | |
| 64 | 60 | TimeUnit.MILLISECONDS.sleep(100); |
| 65 | 61 | |
| 66 | - HostConnectionPool pool = getPool(session); | |
| 62 | + HostConnectionPool pool = getPool(session()); | |
| 67 | 63 | for (Connection connection : pool.connections) { |
| 68 | 64 | assertEquals(connection.inFlight.get(), 0); |
| 69 | 65 | } |
| @@ -73,7 +69,7 @@ | ||
| 73 | 69 | public void should_init_cluster_and_session_if_needed() throws Exception { |
| 74 | 70 | // For this test we need an uninitialized cluster, so we can't reuse the one provided by the |
| 75 | 71 | // parent class. Rebuild a new one with the same (unique) host. |
| 76 | - Host host = cluster.getMetadata().allHosts().iterator().next(); | |
| 72 | + Host host = cluster().getMetadata().allHosts().iterator().next(); | |
| 77 | 73 | |
| 78 | 74 | Cluster cluster2 = null; |
| 79 | 75 | try { |
| @@ -130,7 +126,7 @@ | ||
| 130 | 126 | } |
| 131 | 127 | |
| 132 | 128 | private ListenableFuture<Integer> connectAndQuery(String keyspace, Executor executor) { |
| 133 | - ListenableFuture<Session> sessionFuture = cluster.connectAsync(keyspace); | |
| 129 | + ListenableFuture<Session> sessionFuture = cluster().connectAsync(keyspace); | |
| 134 | 130 | ListenableFuture<ResultSet> queryFuture = Futures.transform(sessionFuture, new AsyncFunction<Session, ResultSet>() { |
| 135 | 131 | @Override |
| 136 | 132 | public ListenableFuture<ResultSet> apply(Session session) throws Exception { |
modified driver-core/src/test/java/com/datastax/driver/core/AuthenticationTest.java +7 −4
| @@ -39,7 +39,7 @@ | ||
| 39 | 39 | public void sleepIf12() { |
| 40 | 40 | // For C* 1.2, sleep before attempting to connect as there is a small delay between |
| 41 | 41 | // user being created. |
| 42 | - if (ccm.getVersion().getMajor() < 2) { | |
| 42 | + if (ccm().getVersion().getMajor() < 2) { | |
| 43 | 43 | Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); |
| 44 | 44 | } |
| 45 | 45 | } |
| @@ -47,7 +47,8 @@ | ||
| 47 | 47 | @Test(groups = "short") |
| 48 | 48 | public void should_connect_with_credentials() throws InterruptedException { |
| 49 | 49 | Cluster cluster = register(Cluster.builder() |
| 50 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 50 | + .addContactPoints(getContactPoints()) | |
| 51 | + .withPort(ccm().getBinaryPort()) | |
| 51 | 52 | .withCredentials("cassandra", "cassandra") |
| 52 | 53 | .build()); |
| 53 | 54 | cluster.connect(); |
| @@ -56,7 +57,8 @@ | ||
| 56 | 57 | @Test(groups = "short", expectedExceptions = AuthenticationException.class) |
| 57 | 58 | public void should_fail_to_connect_with_wrong_credentials() throws InterruptedException { |
| 58 | 59 | Cluster cluster = register(Cluster.builder() |
| 59 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 60 | + .addContactPoints(getContactPoints()) | |
| 61 | + .withPort(ccm().getBinaryPort()) | |
| 60 | 62 | .withCredentials("bogus", "bogus") |
| 61 | 63 | .build()); |
| 62 | 64 | cluster.connect(); |
| @@ -65,7 +67,8 @@ | ||
| 65 | 67 | @Test(groups = "short", expectedExceptions = AuthenticationException.class) |
| 66 | 68 | public void should_fail_to_connect_without_credentials() throws InterruptedException { |
| 67 | 69 | Cluster cluster = register(Cluster.builder() |
| 68 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 70 | + .addContactPoints(getContactPoints()) | |
| 71 | + .withPort(ccm().getBinaryPort()) | |
| 69 | 72 | .build()); |
| 70 | 73 | cluster.connect(); |
| 71 | 74 | } |
modified driver-core/src/test/java/com/datastax/driver/core/BatchStatementTest.java +9 −12
| @@ -19,9 +19,6 @@ | ||
| 19 | 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | 20 | import org.testng.annotations.Test; |
| 21 | 21 | |
| 22 | -import java.util.Collection; | |
| 23 | -import java.util.Collections; | |
| 24 | - | |
| 25 | 22 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 26 | 23 | import static org.testng.Assert.assertEquals; |
| 27 | 24 | import static org.testng.Assert.assertTrue; |
| @@ -30,14 +27,14 @@ | ||
| 30 | 27 | public class BatchStatementTest extends CCMTestsSupport { |
| 31 | 28 | |
| 32 | 29 | @Override |
| 33 | - public Collection<String> createTestFixtures() { | |
| 34 | - return Collections.singletonList("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 30 | + public void onTestContextInitialized() { | |
| 31 | + execute("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 35 | 32 | } |
| 36 | 33 | |
| 37 | 34 | @Test(groups = "short") |
| 38 | 35 | public void simpleBatchTest() { |
| 39 | 36 | try { |
| 40 | - PreparedStatement st = session.prepare("INSERT INTO test (k, v) VALUES (?, ?)"); | |
| 37 | + PreparedStatement st = session().prepare("INSERT INTO test (k, v) VALUES (?, ?)"); | |
| 41 | 38 | |
| 42 | 39 | BatchStatement batch = new BatchStatement(); |
| 43 | 40 | |
| @@ -47,9 +44,9 @@ | ||
| 47 | 44 | |
| 48 | 45 | assertEquals(3, batch.size()); |
| 49 | 46 | |
| 50 | - session.execute(batch); | |
| 47 | + session().execute(batch); | |
| 51 | 48 | |
| 52 | - ResultSet rs = session.execute("SELECT * FROM test"); | |
| 49 | + ResultSet rs = session().execute("SELECT * FROM test"); | |
| 53 | 50 | |
| 54 | 51 | Row r; |
| 55 | 52 | |
| @@ -69,14 +66,14 @@ | ||
| 69 | 66 | |
| 70 | 67 | } catch (UnsupportedFeatureException e) { |
| 71 | 68 | // This is expected when testing the protocol v1 |
| 72 | - assertEquals(cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum(), ProtocolVersion.V1); | |
| 69 | + assertEquals(cluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum(), ProtocolVersion.V1); | |
| 73 | 70 | } |
| 74 | 71 | } |
| 75 | 72 | |
| 76 | 73 | @Test(groups = "short") |
| 77 | 74 | @CassandraVersion(major = 2.0, minor = 9, description = "This will only work with C* 2.0.9 (CASSANDRA-7337)") |
| 78 | 75 | public void casBatchTest() { |
| 79 | - PreparedStatement st = session.prepare("INSERT INTO test (k, v) VALUES (?, ?) IF NOT EXISTS"); | |
| 76 | + PreparedStatement st = session().prepare("INSERT INTO test (k, v) VALUES (?, ?) IF NOT EXISTS"); | |
| 80 | 77 | |
| 81 | 78 | BatchStatement batch = new BatchStatement(); |
| 82 | 79 | |
| @@ -86,12 +83,12 @@ | ||
| 86 | 83 | |
| 87 | 84 | assertEquals(3, batch.size()); |
| 88 | 85 | |
| 89 | - ResultSet rs = session.execute(batch); | |
| 86 | + ResultSet rs = session().execute(batch); | |
| 90 | 87 | Row r = rs.one(); |
| 91 | 88 | assertTrue(!r.isNull("[applied]")); |
| 92 | 89 | assertEquals(r.getBool("[applied]"), true); |
| 93 | 90 | |
| 94 | - rs = session.execute(batch); | |
| 91 | + rs = session().execute(batch); | |
| 95 | 92 | r = rs.one(); |
| 96 | 93 | assertTrue(!r.isNull("[applied]")); |
| 97 | 94 | assertEquals(r.getBool("[applied]"), false); |
modified driver-core/src/test/java/com/datastax/driver/core/BoundStatementTest.java +3 −5
| @@ -20,8 +20,6 @@ | ||
| 20 | 20 | import org.testng.annotations.BeforeClass; |
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | 22 | |
| 23 | -import java.util.Collection; | |
| 24 | - | |
| 25 | 23 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | 24 | import static org.assertj.core.api.Assertions.fail; |
| 27 | 25 | |
| @@ -30,13 +28,13 @@ | ||
| 30 | 28 | PreparedStatement prepared; |
| 31 | 29 | |
| 32 | 30 | @Override |
| 33 | - public Collection<String> createTestFixtures() { | |
| 34 | - return Lists.newArrayList("CREATE TABLE foo (k int primary key, v1 text, v2 list<int>)"); | |
| 31 | + public void onTestContextInitialized() { | |
| 32 | + execute("CREATE TABLE foo (k int primary key, v1 text, v2 list<int>)"); | |
| 35 | 33 | } |
| 36 | 34 | |
| 37 | 35 | @BeforeClass(groups = "short") |
| 38 | 36 | public void setup() { |
| 39 | - prepared = session.prepare("INSERT INTO foo (k, v1, v2) VALUES (?, ?, ?)"); | |
| 37 | + prepared = session().prepare("INSERT INTO foo (k, v1, v2) VALUES (?, ?, ?)"); | |
| 40 | 38 | } |
| 41 | 39 | |
| 42 | 40 | @Test(groups = "short") |
modified driver-core/src/test/java/com/datastax/driver/core/CCMAccess.java +7 −1
| @@ -22,6 +22,8 @@ | ||
| 22 | 22 | |
| 23 | 23 | public interface CCMAccess extends Closeable { |
| 24 | 24 | |
| 25 | + enum Workload {cassandra, solr, hadoop, spark, cfs, graph} | |
| 26 | + | |
| 25 | 27 | // Inspection methods |
| 26 | 28 | |
| 27 | 29 | /** |
| @@ -130,6 +132,10 @@ | ||
| 130 | 132 | */ |
| 131 | 133 | void updateDSEConfig(Map<String, Object> configs); |
| 132 | 134 | |
| 135 | + /** | |
| 136 | + * Checks for errors in the logs of all nodes in the cluster. | |
| 137 | + */ | |
| 138 | + String checkForErrors(); | |
| 133 | 139 | |
| 134 | 140 | // Methods altering nodes |
| 135 | 141 | |
| @@ -201,7 +207,7 @@ | ||
| 201 | 207 | * |
| 202 | 208 | * @param n the node number (starting from 1). |
| 203 | 209 | */ |
| 204 | - void setWorkload(int n, String workload); | |
| 210 | + void setWorkload(int n, Workload workload); | |
| 205 | 211 | |
| 206 | 212 | |
| 207 | 213 | // Methods blocking until nodes are up or down |
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +78 −25
| @@ -31,7 +31,6 @@ | ||
| 31 | 31 | import java.net.InetSocketAddress; |
| 32 | 32 | import java.util.*; |
| 33 | 33 | import java.util.concurrent.TimeUnit; |
| 34 | -import java.util.concurrent.atomic.AtomicInteger; | |
| 35 | 34 | |
| 36 | 35 | import static com.datastax.driver.core.TestUtils.executeNoFail; |
| 37 | 36 | import static com.datastax.driver.core.TestUtils.findAvailablePort; |
| @@ -380,7 +379,17 @@ | ||
| 380 | 379 | return; |
| 381 | 380 | if (logger.isDebugEnabled()) |
| 382 | 381 | logger.debug("Starting: {} - free memory: {} MB", this, TestUtils.getFreeMemoryMB()); |
| 383 | - execute(CCM_COMMAND + " start --wait-other-notice --wait-for-binary-proto" + jvmArgs); | |
| 382 | + try { | |
| 383 | + execute(CCM_COMMAND + " start --wait-other-notice --wait-for-binary-proto" + jvmArgs); | |
| 384 | + } catch (CCMException e) { | |
| 385 | + logger.error("Could not start " + this, e); | |
| 386 | + logger.error("CCM output:\n{}", e.getOut()); | |
| 387 | + setKeepLogs(true); | |
| 388 | + String errors = checkForErrors(); | |
| 389 | + if (errors != null) | |
| 390 | + logger.error("CCM check errors:\n{}", errors); | |
| 391 | + throw e; | |
| 392 | + } | |
| 384 | 393 | if (logger.isDebugEnabled()) |
| 385 | 394 | logger.debug("Started: {} - Free memory: {} MB", this, TestUtils.getFreeMemoryMB()); |
| 386 | 395 | started = true; |
| @@ -392,7 +401,6 @@ | ||
| 392 | 401 | return; |
| 393 | 402 | if (logger.isDebugEnabled()) |
| 394 | 403 | logger.debug("Stopping: {} - free memory: {} MB", this, TestUtils.getFreeMemoryMB()); |
| 395 | - execute(CCM_COMMAND + " stop"); | |
| 396 | 404 | if (logger.isDebugEnabled()) |
| 397 | 405 | logger.debug("Stopped: {} - free memory: {} MB", this, TestUtils.getFreeMemoryMB()); |
| 398 | 406 | closed = true; |
| @@ -407,10 +415,38 @@ | ||
| 407 | 415 | closed = true; |
| 408 | 416 | } |
| 409 | 417 | |
| 418 | + @Override | |
| 419 | + public synchronized void remove() { | |
| 420 | + stop(); | |
| 421 | + logger.debug("Removing: {}", this); | |
| 422 | + execute(CCM_COMMAND + " remove"); | |
| 423 | + } | |
| 424 | + | |
| 425 | + @Override | |
| 426 | + public String checkForErrors() { | |
| 427 | + logger.debug("Checking for errors in: {}", this); | |
| 428 | + try { | |
| 429 | + return execute(CCM_COMMAND + " checklogerror"); | |
| 430 | + } catch (CCMException e) { | |
| 431 | + logger.warn("Check for errors failed"); | |
| 432 | + return null; | |
| 433 | + } | |
| 434 | + } | |
| 435 | + | |
| 410 | 436 | @Override |
| 411 | 437 | public void start(int n) { |
| 412 | 438 | logger.debug(String.format("Starting: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this)); |
| 413 | - execute(CCM_COMMAND + " node%d start --wait-other-notice --wait-for-binary-proto" + jvmArgs, n); | |
| 439 | + try { | |
| 440 | + execute(CCM_COMMAND + " node%d start --wait-other-notice --wait-for-binary-proto" + jvmArgs, n); | |
| 441 | + } catch (CCMException e) { | |
| 442 | + logger.error(String.format("Could not start node %s in %s", n, this), e); | |
| 443 | + logger.error("CCM output:\n{}", e.getOut()); | |
| 444 | + setKeepLogs(true); | |
| 445 | + String errors = checkForErrors(); | |
| 446 | + if (errors != null) | |
| 447 | + logger.error("CCM check errors:\n{}", errors); | |
| 448 | + throw e; | |
| 449 | + } | |
| 414 | 450 | } |
| 415 | 451 | |
| 416 | 452 | @Override |
| @@ -425,13 +461,6 @@ | ||
| 425 | 461 | execute(CCM_COMMAND + " node%d stop --not-gently", n); |
| 426 | 462 | } |
| 427 | 463 | |
| 428 | - @Override | |
| 429 | - public synchronized void remove() { | |
| 430 | - stop(); | |
| 431 | - logger.debug("Removing: {}", this); | |
| 432 | - execute(CCM_COMMAND + " remove"); | |
| 433 | - } | |
| 434 | - | |
| 435 | 464 | @Override |
| 436 | 465 | public void remove(int n) { |
| 437 | 466 | logger.debug(String.format("Removing: node %s (%s%s:%s) from %s", n, TestUtils.IP_PREFIX, n, binaryPort, this)); |
| @@ -505,31 +534,36 @@ | ||
| 505 | 534 | } |
| 506 | 535 | |
| 507 | 536 | @Override |
| 508 | - public void setWorkload(int node, String workload) { | |
| 537 | + public void setWorkload(int node, Workload workload) { | |
| 509 | 538 | execute(CCM_COMMAND + " node%d setworkload %s", node, workload); |
| 510 | 539 | } |
| 511 | 540 | |
| 512 | - private void execute(String command, Object... args) { | |
| 513 | - | |
| 541 | + private String execute(String command, Object... args) { | |
| 514 | 542 | String fullCommand = String.format(command, args) + " --config-dir=" + ccmDir; |
| 515 | 543 | Closer closer = Closer.create(); |
| 516 | 544 | // 10 minutes timeout |
| 517 | 545 | ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.MINUTES.toMillis(10)); |
| 546 | + StringWriter sw = new StringWriter(); | |
| 547 | + final PrintWriter pw = new PrintWriter(sw); | |
| 548 | + closer.register(pw); | |
| 518 | 549 | try { |
| 519 | 550 | logger.trace("Executing: " + fullCommand); |
| 520 | 551 | CommandLine cli = CommandLine.parse(fullCommand); |
| 521 | 552 | Executor executor = new DefaultExecutor(); |
| 522 | - | |
| 523 | 553 | LogOutputStream outStream = new LogOutputStream() { |
| 524 | 554 | @Override |
| 525 | 555 | protected void processLine(String line, int logLevel) { |
| 526 | - logger.debug("ccmout> " + line); | |
| 556 | + String out = "ccmout> " + line; | |
| 557 | + logger.debug(out); | |
| 558 | + pw.println(out); | |
| 527 | 559 | } |
| 528 | 560 | }; |
| 529 | 561 | LogOutputStream errStream = new LogOutputStream() { |
| 530 | 562 | @Override |
| 531 | 563 | protected void processLine(String line, int logLevel) { |
| 532 | - logger.error("ccmerr> " + line); | |
| 564 | + String err = "ccmerr> " + line; | |
| 565 | + logger.error(err); | |
| 566 | + pw.println(err); | |
| 533 | 567 | } |
| 534 | 568 | }; |
| 535 | 569 | closer.register(outStream); |
| @@ -537,16 +571,17 @@ | ||
| 537 | 571 | ExecuteStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream); |
| 538 | 572 | executor.setStreamHandler(streamHandler); |
| 539 | 573 | executor.setWatchdog(watchDog); |
| 540 | - | |
| 541 | 574 | int retValue = executor.execute(cli, ENVIRONMENT_MAP); |
| 542 | 575 | if (retValue != 0) { |
| 543 | 576 | logger.error("Non-zero exit code ({}) returned from executing ccm command: {}", retValue, fullCommand); |
| 544 | - throw new RuntimeException(); | |
| 577 | + pw.flush(); | |
| 578 | + throw new CCMException(String.format("Non-zero exit code (%s) returned from executing ccm command: %s", retValue, fullCommand), sw.toString()); | |
| 545 | 579 | } |
| 546 | 580 | } catch (IOException e) { |
| 547 | 581 | if (watchDog.killedProcess()) |
| 548 | - logger.error("The command {} was killed after 5 minutes", fullCommand); | |
| 549 | - throw new RuntimeException(String.format("The command %s failed to execute", fullCommand), e); | |
| 582 | + logger.error("The command {} was killed after 10 minutes", fullCommand); | |
| 583 | + pw.flush(); | |
| 584 | + throw new CCMException(String.format("The command %s failed to execute", fullCommand), sw.toString(), e); | |
| 550 | 585 | } finally { |
| 551 | 586 | try { |
| 552 | 587 | closer.close(); |
| @@ -554,6 +589,7 @@ | ||
| 554 | 589 | Throwables.propagate(e); |
| 555 | 590 | } |
| 556 | 591 | } |
| 592 | + return sw.toString(); | |
| 557 | 593 | } |
| 558 | 594 | |
| 559 | 595 | /** |
| @@ -622,8 +658,7 @@ | ||
| 622 | 658 | * use {@link #builder()} to get an instance |
| 623 | 659 | */ |
| 624 | 660 | public static class Builder { |
| 625 | - private static final AtomicInteger COUNTER = new AtomicInteger(0); | |
| 626 | - private final String clusterName = "test_" + COUNTER.incrementAndGet(); | |
| 661 | + private final String clusterName = TestUtils.generateIdentifier("cluster_"); | |
| 627 | 662 | int[] nodes = {1}; |
| 628 | 663 | private boolean start = true; |
| 629 | 664 | private boolean isDSE = isDSE(); |
| @@ -636,6 +671,7 @@ | ||
| 636 | 671 | private int thriftPort = -1; |
| 637 | 672 | private int binaryPort = -1; |
| 638 | 673 | private int storagePort = -1; |
| 674 | + private Map<Integer, Workload> workloads = new HashMap<Integer, Workload>(); | |
| 639 | 675 | |
| 640 | 676 | private Builder() { |
| 641 | 677 | } |
| @@ -710,7 +746,7 @@ | ||
| 710 | 746 | |
| 711 | 747 | /** |
| 712 | 748 | * Free-form options that will be added at the end of the {@code ccm create} command |
| 713 | - * (defaults to {@link #getInstallArguments()} if this is never called). | |
| 749 | + * (defaults to {@link #getInstallArguments()} if this is never called). | |
| 714 | 750 | */ |
| 715 | 751 | public Builder withCreateOptions(String... createOptions) { |
| 716 | 752 | Collections.addAll(this.createOptions, createOptions); |
| @@ -758,6 +794,18 @@ | ||
| 758 | 794 | return this; |
| 759 | 795 | } |
| 760 | 796 | |
| 797 | + /** | |
| 798 | + * Sets the DSE workload for a given node. | |
| 799 | + * | |
| 800 | + * @param node The node to set the workload for (starting with 1). | |
| 801 | + * @param workload The workload (e.g. solr, spark, hadoop) | |
| 802 | + * @return This builder | |
| 803 | + */ | |
| 804 | + public Builder withWorkload(int node, Workload workload) { | |
| 805 | + this.workloads.put(node, workload); | |
| 806 | + return this; | |
| 807 | + } | |
| 808 | + | |
| 761 | 809 | public CCMBridge build() { |
| 762 | 810 | // be careful NOT to alter internal state (hashCode/equals) during build! |
| 763 | 811 | int storagePort = this.storagePort == -1 ? findAvailablePort() : this.storagePort; |
| @@ -781,6 +829,9 @@ | ||
| 781 | 829 | ccm.updateConfig(config); |
| 782 | 830 | if (!dseConfiguration.isEmpty()) |
| 783 | 831 | ccm.updateDSEConfig(dseConfiguration); |
| 832 | + for (Map.Entry<Integer, Workload> entry : workloads.entrySet()) { | |
| 833 | + ccm.setWorkload(entry.getKey(), entry.getValue()); | |
| 834 | + } | |
| 784 | 835 | if (start) |
| 785 | 836 | ccm.start(); |
| 786 | 837 | return ccm; |
| @@ -886,8 +937,9 @@ | ||
| 886 | 937 | if (!Arrays.equals(nodes, builder.nodes)) return false; |
| 887 | 938 | if (!createOptions.equals(builder.createOptions)) return false; |
| 888 | 939 | if (!jvmArgs.equals(builder.jvmArgs)) return false; |
| 889 | - if (!dseConfiguration.equals(builder.dseConfiguration)) return false; | |
| 890 | 940 | if (!cassandraConfiguration.equals(builder.cassandraConfiguration)) return false; |
| 941 | + if (!dseConfiguration.equals(builder.dseConfiguration)) return false; | |
| 942 | + if (!workloads.equals(builder.workloads)) return false; | |
| 891 | 943 | return version.equals(builder.version); |
| 892 | 944 | } |
| 893 | 945 | |
| @@ -901,6 +953,7 @@ | ||
| 901 | 953 | result = 31 * result + jvmArgs.hashCode(); |
| 902 | 954 | result = 31 * result + cassandraConfiguration.hashCode(); |
| 903 | 955 | result = 31 * result + dseConfiguration.hashCode(); |
| 956 | + result = 31 * result + workloads.hashCode(); | |
| 904 | 957 | result = 31 * result + thriftPort; |
| 905 | 958 | result = 31 * result + binaryPort; |
| 906 | 959 | result = 31 * result + storagePort; |
modified driver-core/src/test/java/com/datastax/driver/core/CCMCache.java +6 −1
| @@ -139,6 +139,11 @@ | ||
| 139 | 139 | ccm.updateDSEConfig(configs); |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | + @Override | |
| 143 | + public String checkForErrors() { | |
| 144 | + return ccm.checkForErrors(); | |
| 145 | + } | |
| 146 | + | |
| 142 | 147 | @Override |
| 143 | 148 | public void start(int n) { |
| 144 | 149 | ccm.start(n); |
| @@ -185,7 +190,7 @@ | ||
| 185 | 190 | } |
| 186 | 191 | |
| 187 | 192 | @Override |
| 188 | - public void setWorkload(int n, String workload) { | |
| 193 | + public void setWorkload(int n, Workload workload) { | |
| 189 | 194 | ccm.setWorkload(n, workload); |
| 190 | 195 | } |
| 191 | 196 |
modified driver-core/src/test/java/com/datastax/driver/core/CCMConfig.java +84 −22
| @@ -34,7 +34,9 @@ | ||
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | 36 | * The number of nodes to create, per data center. |
| 37 | - * If not set, this defaults to {1}, i.e., one data center with one node. | |
| 37 | + * If not set, this defaults to {@code {1}}, i.e., one data center with one node. | |
| 38 | + * <p/> | |
| 39 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 38 | 40 | * |
| 39 | 41 | * @return The number of nodes to create, per data center. |
| 40 | 42 | */ |
| @@ -44,9 +46,11 @@ | ||
| 44 | 46 | * The C* or DSE version to use; defaults to the version defined by |
| 45 | 47 | * the System property {@code cassandra.version}. |
| 46 | 48 | * <p/> |
| 47 | - * Note that setting this property completely | |
| 49 | + * Note that setting this attribute completely | |
| 48 | 50 | * overrides the System properties {@code cassandra.version} |
| 49 | 51 | * and {@code cassandra.directory}. |
| 52 | + * <p/> | |
| 53 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 50 | 54 | * |
| 51 | 55 | * @return The C* or DSE version to use |
| 52 | 56 | * @see CCMBridge#getCassandraVersion() |
| @@ -56,8 +60,10 @@ | ||
| 56 | 60 | /** |
| 57 | 61 | * Whether to launch a DSE instance rather than an OSS C*. |
| 58 | 62 | * <p/> |
| 59 | - * Note that setting this property completely | |
| 63 | + * Note that setting this attribute completely | |
| 60 | 64 | * overrides the System property {@code dse}. |
| 65 | + * <p/> | |
| 66 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 61 | 67 | * |
| 62 | 68 | * @return {@code true} to launch a DSE instance, {@code false} to launch an OSS C* instance (default). |
| 63 | 69 | */ |
| @@ -66,6 +72,8 @@ | ||
| 66 | 72 | /** |
| 67 | 73 | * Configuration items to add to cassandra.yaml configuration file. |
| 68 | 74 | * Each configuration item must be in the form {@code key:value}. |
| 75 | + * <p/> | |
| 76 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 69 | 77 | * |
| 70 | 78 | * @return Configuration items to add to cassandra.yaml configuration file. |
| 71 | 79 | */ |
| @@ -74,6 +82,8 @@ | ||
| 74 | 82 | /** |
| 75 | 83 | * Configuration items to add to dse.yaml configuration file. |
| 76 | 84 | * Each configuration item must be in the form {@code key:value}. |
| 85 | + * <p/> | |
| 86 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 77 | 87 | * |
| 78 | 88 | * @return Configuration items to add to dse.yaml configuration file. |
| 79 | 89 | */ |
| @@ -83,6 +93,8 @@ | ||
| 83 | 93 | * JVM args to use when starting hosts. |
| 84 | 94 | * System properties should be provided one by one, in the form |
| 85 | 95 | * {@code -Dname=value}. |
| 96 | + * <p/> | |
| 97 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 86 | 98 | * |
| 87 | 99 | * @return JVM args to use when starting hosts. |
| 88 | 100 | */ |
| @@ -90,6 +102,8 @@ | ||
| 90 | 102 | |
| 91 | 103 | /** |
| 92 | 104 | * Free-form options that will be added at the end of the {@code ccm create} command. |
| 105 | + * <p/> | |
| 106 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 93 | 107 | * |
| 94 | 108 | * @return Free-form options that will be added at the end of the {@code ccm create} command. |
| 95 | 109 | */ |
| @@ -97,6 +111,8 @@ | ||
| 97 | 111 | |
| 98 | 112 | /** |
| 99 | 113 | * Whether to use SSL encryption. |
| 114 | + * <p/> | |
| 115 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 100 | 116 | * |
| 101 | 117 | * @return {@code true} to use encryption, {@code false} to use unencrypted communication (default). |
| 102 | 118 | */ |
| @@ -104,11 +120,23 @@ | ||
| 104 | 120 | |
| 105 | 121 | /** |
| 106 | 122 | * Whether to use authentication. Implies the use of SSL encryption. |
| 123 | + * <p/> | |
| 124 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 107 | 125 | * |
| 108 | 126 | * @return {@code true} to use authentication, {@code false} to use unauthenticated communication (default). |
| 109 | 127 | */ |
| 110 | 128 | boolean[] auth() default {}; |
| 111 | 129 | |
| 130 | + /** | |
| 131 | + * The workloads to assign to each node. If this attribute is defined, | |
| 132 | + * the number of its elements must be lesser than or equal to the number of nodes in the cluster. | |
| 133 | + * <p/> | |
| 134 | + * This attribute is ignored if {@link #ccmProvider()} is defined. | |
| 135 | + * | |
| 136 | + * @return The workloads to assign to each node. | |
| 137 | + */ | |
| 138 | + CCMAccess.Workload[] workloads() default {}; | |
| 139 | + | |
| 112 | 140 | /** |
| 113 | 141 | * Returns {@code true} if a {@link CCMBridge} instance should be automatically created, {@code false} otherwise. |
| 114 | 142 | * <p/> |
| @@ -160,10 +188,10 @@ | ||
| 160 | 188 | * and this session will be used to create the test keyspace. |
| 161 | 189 | * <p/> |
| 162 | 190 | * The keyspace will be created once for the whole class if CCM test mode is {@link CreateCCM.TestMode#PER_CLASS}, |
| 163 | - * or once per test method, if the CCM test mode is {@link CreateCCM.TestMode#PER_METHOD}, | |
| 191 | + * or once per test method, if the CCM test mode is {@link CreateCCM.TestMode#PER_METHOD}. | |
| 164 | 192 | * <p/> |
| 165 | 193 | * The test keyspace will be automatically populated upon creation with fixtures provided by |
| 166 | - * {@link #fixturesProvider()} and {@link #fixturesProviderClass()}. | |
| 194 | + * {@link #testInitializer()} and {@link #testInitializerClass()}. | |
| 167 | 195 | * |
| 168 | 196 | * @return {@code true} if a test keyspace should be automatically created, {@code false} otherwise. |
| 169 | 197 | */ |
| @@ -183,12 +211,44 @@ | ||
| 183 | 211 | */ |
| 184 | 212 | boolean[] dirtiesContext() default {}; |
| 185 | 213 | |
| 214 | + /** | |
| 215 | + * Returns the name of the method that should be invoked to obtain | |
| 216 | + * a {@link com.datastax.driver.core.CCMBridge.Builder} instance. | |
| 217 | + * <p/> | |
| 218 | + * This method should be declared in {@link #ccmProviderClass()}, | |
| 219 | + * or if that attribute is not set, | |
| 220 | + * it will be looked up on the test class itself. | |
| 221 | + * <p/> | |
| 222 | + * The method should not have parameters. It can be static or not, | |
| 223 | + * and have any visibility. | |
| 224 | + * <p/> | |
| 225 | + * By default, a {@link com.datastax.driver.core.CCMBridge.Builder} instance | |
| 226 | + * is obtained by parsing other attributes of this annotation. | |
| 227 | + * | |
| 228 | + * @return The name of the method that should be invoked to obtain a | |
| 229 | + * {@link com.datastax.driver.core.CCMBridge.Builder} instance. | |
| 230 | + */ | |
| 231 | + String ccmProvider() default ""; | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Returns the name of the class that should be invoked to obtain | |
| 235 | + * a {@link com.datastax.driver.core.CCMBridge.Builder} instance. | |
| 236 | + * <p/> | |
| 237 | + * This class should contain a method named after {@link #ccmProvider()}; | |
| 238 | + * if this attribute is not set, | |
| 239 | + * it will default to the test class itself. | |
| 240 | + * | |
| 241 | + * @return The name of the class that should be invoked to obtain a | |
| 242 | + * {@link com.datastax.driver.core.CCMBridge.Builder} instance. | |
| 243 | + */ | |
| 244 | + Class<?> ccmProviderClass() default Undefined.class; | |
| 245 | + | |
| 186 | 246 | /** |
| 187 | 247 | * Returns the name of the method that should be invoked to obtain |
| 188 | 248 | * a {@link com.datastax.driver.core.Cluster.Builder} instance. |
| 189 | 249 | * <p/> |
| 190 | 250 | * This method should be declared in {@link #clusterProviderClass()}, |
| 191 | - * or if that property is not set, | |
| 251 | + * or if that attribute is not set, | |
| 192 | 252 | * it will be looked up on the test class itself. |
| 193 | 253 | * <p/> |
| 194 | 254 | * The method should not have parameters. It can be static or not, |
| @@ -206,7 +266,7 @@ | ||
| 206 | 266 | * a {@link com.datastax.driver.core.Cluster.Builder} instance. |
| 207 | 267 | * <p/> |
| 208 | 268 | * This class should contain a method named after {@link #clusterProvider()}; |
| 209 | - * if this property is not set, | |
| 269 | + * if this attribute is not set, | |
| 210 | 270 | * it will default to the test class itself. |
| 211 | 271 | * |
| 212 | 272 | * @return The name of the class that should be invoked to obtain a |
| @@ -215,33 +275,35 @@ | ||
| 215 | 275 | Class<?> clusterProviderClass() default Undefined.class; |
| 216 | 276 | |
| 217 | 277 | /** |
| 218 | - * Returns the name of the method that should be invoked to obtain | |
| 219 | - * the test fixtures to use. | |
| 278 | + * Returns the name of the method that should be invoked once the test context | |
| 279 | + * is initialized. | |
| 220 | 280 | * <p/> |
| 221 | - * This method should return {@code Collection<String>}. | |
| 222 | - * <p/> | |
| 223 | - * This method should be declared in {@link #fixturesProviderClass()}, | |
| 224 | - * or if that property is not set, | |
| 281 | + * This method should be declared in {@link #testInitializerClass()}, | |
| 282 | + * or if that attribute is not set, | |
| 225 | 283 | * it will be looked up on the test class itself. |
| 226 | 284 | * <p/> |
| 227 | 285 | * The method should not have parameters. It can be static or not, |
| 228 | 286 | * and have any visibility. |
| 287 | + * <p/> | |
| 288 | + * By default, the framework will look for a method named | |
| 289 | + * {@code onTestContextInitialized}. | |
| 229 | 290 | * |
| 230 | - * @return The name of the method that should be invoked to obtain the test fixtures to use. | |
| 291 | + * @return The name of the method that should be invoked once the test context | |
| 292 | + * is initialized. | |
| 231 | 293 | */ |
| 232 | - String fixturesProvider() default ""; | |
| 294 | + String testInitializer() default ""; | |
| 233 | 295 | |
| 234 | 296 | /** |
| 235 | - * Returns the name of the class that should be invoked to obtain | |
| 236 | - * the test fixtures to use. | |
| 297 | + * Returns the name of the class that should be invoked once the test context | |
| 298 | + * is initialized. | |
| 237 | 299 | * <p/> |
| 238 | - * This class should contain a method named after {@link #fixturesProvider()}; | |
| 239 | - * if this property is not set, | |
| 300 | + * This class should contain a method named after {@link #testInitializer()}; | |
| 301 | + * if this attribute is not set, | |
| 240 | 302 | * it will default to the test class itself. |
| 241 | 303 | * |
| 242 | - * @return The name of the class that should be invoked to obtain a | |
| 243 | - * {@link com.datastax.driver.core.Cluster.Builder} instance. | |
| 304 | + * @return The name of the class that should be invoked once the test context | |
| 305 | + * is initialized. | |
| 244 | 306 | */ |
| 245 | - Class<?> fixturesProviderClass() default Undefined.class; | |
| 307 | + Class<?> testInitializerClass() default Undefined.class; | |
| 246 | 308 | |
| 247 | 309 | } |
added driver-core/src/test/java/com/datastax/driver/core/CCMException.java +36 −0
| @@ -0,0 +1,36 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +public class CCMException extends RuntimeException { | |
| 19 | + | |
| 20 | + private final String out; | |
| 21 | + | |
| 22 | + public CCMException(String message, String out) { | |
| 23 | + super(message); | |
| 24 | + this.out = out; | |
| 25 | + } | |
| 26 | + | |
| 27 | + public CCMException(String message, String out, Throwable cause) { | |
| 28 | + super(message, cause); | |
| 29 | + this.out = out; | |
| 30 | + } | |
| 31 | + | |
| 32 | + public String getOut() { | |
| 33 | + return out; | |
| 34 | + } | |
| 35 | + | |
| 36 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/CCMTestsSupport.java +275 −95
| @@ -15,11 +15,14 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.CCMAccess.Workload; | |
| 18 | 19 | import com.datastax.driver.core.CreateCCM.TestMode; |
| 20 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 19 | 21 | import com.google.common.base.Throwables; |
| 20 | 22 | import com.google.common.collect.ImmutableMap; |
| 21 | 23 | import com.google.common.collect.Lists; |
| 22 | 24 | import com.google.common.io.Closer; |
| 25 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 23 | 26 | import org.slf4j.Logger; |
| 24 | 27 | import org.slf4j.LoggerFactory; |
| 25 | 28 | import org.testng.ITestResult; |
| @@ -32,10 +35,12 @@ | ||
| 32 | 35 | import java.lang.reflect.Constructor; |
| 33 | 36 | import java.lang.reflect.Method; |
| 34 | 37 | import java.lang.reflect.Modifier; |
| 35 | -import java.lang.reflect.ParameterizedType; | |
| 38 | +import java.net.InetAddress; | |
| 36 | 39 | import java.net.InetSocketAddress; |
| 40 | +import java.net.UnknownHostException; | |
| 37 | 41 | import java.util.*; |
| 38 | 42 | import java.util.concurrent.Callable; |
| 43 | +import java.util.concurrent.TimeUnit; | |
| 39 | 44 | import java.util.concurrent.atomic.AtomicInteger; |
| 40 | 45 | |
| 41 | 46 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_CLASS; |
| @@ -59,12 +64,11 @@ | ||
| 59 | 64 | .put("enable_user_defined_functions", VersionNumber.parse("2.2.0")) |
| 60 | 65 | .build(); |
| 61 | 66 | |
| 62 | - | |
| 63 | - private static class ReadOnlyCCMBridge implements CCMAccess { | |
| 67 | + private static class ReadOnlyCCMAccess implements CCMAccess { | |
| 64 | 68 | |
| 65 | 69 | private final CCMAccess delegate; |
| 66 | 70 | |
| 67 | - private ReadOnlyCCMBridge(CCMAccess delegate) { | |
| 71 | + private ReadOnlyCCMAccess(CCMAccess delegate) { | |
| 68 | 72 | this.delegate = delegate; |
| 69 | 73 | } |
| 70 | 74 | |
| @@ -123,6 +127,11 @@ | ||
| 123 | 127 | delegate.setKeepLogs(keepLogs); |
| 124 | 128 | } |
| 125 | 129 | |
| 130 | + @Override | |
| 131 | + public String checkForErrors() { | |
| 132 | + return delegate.checkForErrors(); | |
| 133 | + } | |
| 134 | + | |
| 126 | 135 | @Override |
| 127 | 136 | public void close() { |
| 128 | 137 | throw new UnsupportedOperationException("This CCM cluster is read-only"); |
| @@ -204,7 +213,7 @@ | ||
| 204 | 213 | } |
| 205 | 214 | |
| 206 | 215 | @Override |
| 207 | - public void setWorkload(int node, String workload) { | |
| 216 | + public void setWorkload(int node, Workload workload) { | |
| 208 | 217 | throw new UnsupportedOperationException("This CCM cluster is read-only"); |
| 209 | 218 | } |
| 210 | 219 | |
| @@ -281,7 +290,7 @@ | ||
| 281 | 290 | @SuppressWarnings("SimplifiableIfStatement") |
| 282 | 291 | private Map<String, Object> config() { |
| 283 | 292 | Map<String, Object> config = new HashMap<String, Object>(); |
| 284 | - for (int i = annotations.size() - 1; i == 0; i--) { | |
| 293 | + for (int i = annotations.size() - 1; i >= 0; i--) { | |
| 285 | 294 | CCMConfig ann = annotations.get(i); |
| 286 | 295 | addConfigOptions(ann.config(), config); |
| 287 | 296 | } |
| @@ -342,6 +351,22 @@ | ||
| 342 | 351 | return args; |
| 343 | 352 | } |
| 344 | 353 | |
| 354 | + private List<Workload> workloads() { | |
| 355 | + int total = 0; | |
| 356 | + for (int perDc : numberOfNodes()) | |
| 357 | + total += perDc; | |
| 358 | + List<Workload> workloads = new ArrayList<Workload>(Collections.<Workload>nCopies(total, null)); | |
| 359 | + for (int i = annotations.size() - 1; i >= 0; i--) { | |
| 360 | + CCMConfig ann = annotations.get(i); | |
| 361 | + Workload[] annWorkloads = ann.workloads(); | |
| 362 | + for (int j = 0; j < annWorkloads.length; j++) { | |
| 363 | + Workload workload = annWorkloads[j]; | |
| 364 | + workloads.set(j, workload); | |
| 365 | + } | |
| 366 | + } | |
| 367 | + return workloads; | |
| 368 | + } | |
| 369 | + | |
| 345 | 370 | @SuppressWarnings("SimplifiableIfStatement") |
| 346 | 371 | private boolean createCcm() { |
| 347 | 372 | for (CCMConfig ann : annotations) { |
| @@ -387,34 +412,69 @@ | ||
| 387 | 412 | return false; |
| 388 | 413 | } |
| 389 | 414 | |
| 390 | - private CCMBridge.Builder ccmBuilder() { | |
| 415 | + private CCMBridge.Builder ccmBuilder(Object testInstance) throws Exception { | |
| 391 | 416 | if (ccmBuilder == null) { |
| 392 | - ccmBuilder = CCMBridge.builder().withNodes(numberOfNodes()).notStarted(); | |
| 393 | - if (version() != null) | |
| 394 | - ccmBuilder.withVersion(version()); | |
| 395 | - if (dse()) | |
| 396 | - ccmBuilder.withDSE(); | |
| 397 | - if (ssl()) | |
| 398 | - ccmBuilder.withSSL(); | |
| 399 | - if (auth()) | |
| 400 | - ccmBuilder.withAuth(); | |
| 401 | - for (Map.Entry<String, Object> entry : config().entrySet()) { | |
| 402 | - ccmBuilder.withCassandraConfiguration(entry.getKey(), entry.getValue()); | |
| 403 | - } | |
| 404 | - for (Map.Entry<String, Object> entry : dseConfig().entrySet()) { | |
| 405 | - ccmBuilder.withDSEConfiguration(entry.getKey(), entry.getValue()); | |
| 417 | + ccmBuilder = ccmProvider(testInstance); | |
| 418 | + if (ccmBuilder == null) { | |
| 419 | + ccmBuilder = CCMBridge.builder().withNodes(numberOfNodes()).notStarted(); | |
| 420 | + if (version() != null) | |
| 421 | + ccmBuilder.withVersion(version()); | |
| 422 | + if (dse()) | |
| 423 | + ccmBuilder.withDSE(); | |
| 424 | + if (ssl()) | |
| 425 | + ccmBuilder.withSSL(); | |
| 426 | + if (auth()) | |
| 427 | + ccmBuilder.withAuth(); | |
| 428 | + for (Map.Entry<String, Object> entry : config().entrySet()) { | |
| 429 | + ccmBuilder.withCassandraConfiguration(entry.getKey(), entry.getValue()); | |
| 430 | + } | |
| 431 | + for (Map.Entry<String, Object> entry : dseConfig().entrySet()) { | |
| 432 | + ccmBuilder.withDSEConfiguration(entry.getKey(), entry.getValue()); | |
| 433 | + } | |
| 434 | + for (String option : startOptions()) { | |
| 435 | + ccmBuilder.withCreateOptions(option); | |
| 436 | + } | |
| 437 | + for (String arg : jvmArgs()) { | |
| 438 | + ccmBuilder.withJvmArgs(arg); | |
| 439 | + } | |
| 440 | + List<Workload> workloads = workloads(); | |
| 441 | + for (int i = 0; i < workloads.size(); i++) { | |
| 442 | + Workload workload = workloads.get(i); | |
| 443 | + if (workload != null) | |
| 444 | + ccmBuilder.withWorkload(i + 1, workload); | |
| 445 | + } | |
| 406 | 446 | } |
| 407 | - for (String option : startOptions()) { | |
| 408 | - ccmBuilder.withCreateOptions(option); | |
| 447 | + } | |
| 448 | + return ccmBuilder; | |
| 449 | + } | |
| 450 | + | |
| 451 | + private CCMBridge.Builder ccmProvider(Object testInstance) throws Exception { | |
| 452 | + String methodName = null; | |
| 453 | + Class<?> clazz = null; | |
| 454 | + for (int i = annotations.size() - 1; i >= 0; i--) { | |
| 455 | + CCMConfig ann = annotations.get(i); | |
| 456 | + if (!ann.ccmProvider().isEmpty()) { | |
| 457 | + methodName = ann.ccmProvider(); | |
| 409 | 458 | } |
| 410 | - for (String arg : jvmArgs()) { | |
| 411 | - ccmBuilder.withJvmArgs(arg); | |
| 459 | + if (!ann.ccmProviderClass().equals(CCMConfig.Undefined.class)) { | |
| 460 | + clazz = ann.ccmProviderClass(); | |
| 412 | 461 | } |
| 413 | 462 | } |
| 414 | - return ccmBuilder; | |
| 463 | + if (methodName == null) | |
| 464 | + return null; | |
| 465 | + if (clazz == null) | |
| 466 | + clazz = testInstance.getClass(); | |
| 467 | + Method method = locateMethod(methodName, clazz); | |
| 468 | + assert CCMBridge.Builder.class.isAssignableFrom(method.getReturnType()); | |
| 469 | + if (Modifier.isStatic(method.getModifiers())) { | |
| 470 | + return (CCMBridge.Builder) method.invoke(null); | |
| 471 | + } else { | |
| 472 | + Object receiver = testInstance.getClass().equals(clazz) ? testInstance : instantiate(clazz); | |
| 473 | + return (CCMBridge.Builder) method.invoke(receiver); | |
| 474 | + } | |
| 415 | 475 | } |
| 416 | 476 | |
| 417 | - private Cluster.Builder clusterBuilder(Object testInstance) throws Exception { | |
| 477 | + private Cluster.Builder clusterProvider(Object testInstance) throws Exception { | |
| 418 | 478 | String methodName = null; |
| 419 | 479 | Class<?> clazz = null; |
| 420 | 480 | for (int i = annotations.size() - 1; i >= 0; i--) { |
| @@ -441,31 +501,28 @@ | ||
| 441 | 501 | } |
| 442 | 502 | |
| 443 | 503 | @SuppressWarnings("unchecked") |
| 444 | - private Collection<String> fixtures(Object testInstance) throws Exception { | |
| 504 | + private void invokeInitTest(Object testInstance) throws Exception { | |
| 445 | 505 | String methodName = null; |
| 446 | 506 | Class<?> clazz = null; |
| 447 | 507 | for (int i = annotations.size() - 1; i >= 0; i--) { |
| 448 | 508 | CCMConfig ann = annotations.get(i); |
| 449 | - if (!ann.fixturesProvider().isEmpty()) { | |
| 450 | - methodName = ann.fixturesProvider(); | |
| 509 | + if (!ann.testInitializer().isEmpty()) { | |
| 510 | + methodName = ann.testInitializer(); | |
| 451 | 511 | } |
| 452 | - if (!ann.fixturesProviderClass().equals(CCMConfig.Undefined.class)) { | |
| 453 | - clazz = ann.fixturesProviderClass(); | |
| 512 | + if (!ann.testInitializerClass().equals(CCMConfig.Undefined.class)) { | |
| 513 | + clazz = ann.testInitializerClass(); | |
| 454 | 514 | } |
| 455 | 515 | } |
| 456 | 516 | if (methodName == null) |
| 457 | - methodName = "createTestFixtures"; | |
| 517 | + methodName = "onTestContextInitialized"; | |
| 458 | 518 | if (clazz == null) |
| 459 | 519 | clazz = testInstance.getClass(); |
| 460 | 520 | Method method = locateMethod(methodName, clazz); |
| 461 | - assert Collection.class.isAssignableFrom(method.getReturnType()); | |
| 462 | - assert method.getGenericReturnType() instanceof ParameterizedType; | |
| 463 | - assert String.class.equals(((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0]); | |
| 464 | 521 | if (Modifier.isStatic(method.getModifiers())) { |
| 465 | - return (Collection<String>) method.invoke(null); | |
| 522 | + method.invoke(null); | |
| 466 | 523 | } else { |
| 467 | 524 | Object receiver = testInstance.getClass().equals(clazz) ? testInstance : instantiate(clazz); |
| 468 | - return (Collection<String>) method.invoke(receiver); | |
| 525 | + method.invoke(receiver); | |
| 469 | 526 | } |
| 470 | 527 | } |
| 471 | 528 | |
| @@ -473,13 +530,15 @@ | ||
| 473 | 530 | |
| 474 | 531 | private TestMode testMode; |
| 475 | 532 | |
| 476 | - private CCMTestConfig ccmTestConfig; | |
| 533 | + protected CCMTestConfig ccmTestConfig; | |
| 534 | + | |
| 535 | + private CCMAccess ccm; | |
| 477 | 536 | |
| 478 | - protected CCMAccess ccm; | |
| 537 | + private CCMBridge.Builder ccmBuilder; | |
| 479 | 538 | |
| 480 | - protected Cluster cluster; | |
| 539 | + private Cluster cluster; | |
| 481 | 540 | |
| 482 | - protected Session session; | |
| 541 | + private Session session; | |
| 483 | 542 | |
| 484 | 543 | protected String keyspace; |
| 485 | 544 | |
| @@ -508,11 +567,18 @@ | ||
| 508 | 567 | testMode = determineTestMode(testInstance.getClass()); |
| 509 | 568 | if (testMode == PER_CLASS) { |
| 510 | 569 | closer = Closer.create(); |
| 511 | - initTestContext(testInstance, null); | |
| 512 | - initTestCluster(testInstance); | |
| 513 | - initTestSession(); | |
| 514 | - initTestKeyspace(); | |
| 515 | - initTestFixtures(testInstance); | |
| 570 | + try { | |
| 571 | + initTestContext(testInstance, null); | |
| 572 | + initTestCluster(testInstance); | |
| 573 | + initTestSession(); | |
| 574 | + initTestKeyspace(); | |
| 575 | + initTest(testInstance); | |
| 576 | + } catch (Exception e) { | |
| 577 | + LOGGER.error(e.getMessage(), e); | |
| 578 | + errorOut(); | |
| 579 | + fail(e.getMessage()); | |
| 580 | + | |
| 581 | + } | |
| 516 | 582 | } |
| 517 | 583 | } |
| 518 | 584 | |
| @@ -538,11 +604,17 @@ | ||
| 538 | 604 | if (closer == null) |
| 539 | 605 | closer = Closer.create(); |
| 540 | 606 | if (testMode == PER_METHOD || erroredOut) { |
| 541 | - initTestContext(testInstance, testMethod); | |
| 542 | - initTestCluster(testInstance); | |
| 543 | - initTestSession(); | |
| 544 | - initTestKeyspace(); | |
| 545 | - initTestFixtures(testInstance); | |
| 607 | + try { | |
| 608 | + initTestContext(testInstance, testMethod); | |
| 609 | + initTestCluster(testInstance); | |
| 610 | + initTestSession(); | |
| 611 | + initTestKeyspace(); | |
| 612 | + initTest(testInstance); | |
| 613 | + } catch (Exception e) { | |
| 614 | + LOGGER.error(e.getMessage(), e); | |
| 615 | + errorOut(); | |
| 616 | + fail(e.getMessage()); | |
| 617 | + } | |
| 546 | 618 | } |
| 547 | 619 | assert ccmTestConfig != null; |
| 548 | 620 | assert !ccmTestConfig.createCcm() || ccm != null; |
| @@ -553,7 +625,7 @@ | ||
| 553 | 625 | * Hook executed after each test method. |
| 554 | 626 | */ |
| 555 | 627 | @AfterMethod(groups = {"isolated", "short", "long", "stress", "duration"}, alwaysRun = true) |
| 556 | - public void afterTestMethod(ITestResult tr) { | |
| 628 | + public void afterTestMethod(ITestResult tr) throws Exception { | |
| 557 | 629 | if (isCcmEnabled(tr.getMethod().getConstructorOrMethod().getMethod())) { |
| 558 | 630 | if (tr.getStatus() == ITestResult.FAILURE) { |
| 559 | 631 | errorOut(); |
| @@ -571,7 +643,7 @@ | ||
| 571 | 643 | * Hook executed after each test class. |
| 572 | 644 | */ |
| 573 | 645 | @AfterClass(groups = {"isolated", "short", "long", "stress", "duration"}, alwaysRun = true) |
| 574 | - public void afterTestClass() { | |
| 646 | + public void afterTestClass() throws Exception { | |
| 575 | 647 | if (testMode == PER_CLASS) { |
| 576 | 648 | closeCloseables(); |
| 577 | 649 | closeTestCluster(); |
| @@ -607,20 +679,85 @@ | ||
| 607 | 679 | } |
| 608 | 680 | |
| 609 | 681 | /** |
| 610 | - * The CQL statements to execute before the tests in this class. | |
| 611 | - * Useful to create tables or other objects inside the test keyspace, | |
| 612 | - * or to insert test data. | |
| 682 | + * Hook invoked when the test context is ready, but before the test itself. | |
| 683 | + * Useful to create fixtures or to insert test data. | |
| 684 | + * <p/> | |
| 685 | + * This method is invoked once per class is the {@link TestMode} is {@link TestMode#PER_CLASS} | |
| 686 | + * or before each test method, if the {@link TestMode} is {@link TestMode#PER_METHOD}. | |
| 687 | + * <p/> | |
| 688 | + * When this method is called, the cluster and the session are ready | |
| 689 | + * to be used (unless the configuration specifies that such objects | |
| 690 | + * should not be created). | |
| 691 | + * <p/> | |
| 692 | + * Statements executed inside this method do not need to be qualified with a keyspace name, | |
| 693 | + * in which case they are executed with the default keyspace for the test | |
| 694 | + * (unless the configuration specifies that no keyspace should be creaed for the test). | |
| 695 | + * <p/> | |
| 696 | + * The default implementation does nothing (no fixtures required). | |
| 697 | + */ | |
| 698 | + public void onTestContextInitialized() { | |
| 699 | + // nothing to do by default | |
| 700 | + } | |
| 701 | + | |
| 702 | + /** | |
| 703 | + * @return The {@link CCMAccess} instance to use with this test. | |
| 704 | + */ | |
| 705 | + public CCMAccess ccm() { | |
| 706 | + return ccm; | |
| 707 | + } | |
| 708 | + | |
| 709 | + /** | |
| 710 | + * @return The {@link Cluster} instance to use with this test. | |
| 711 | + * Can be null if CCM configuration specifies {@code createCluster = false}. | |
| 712 | + */ | |
| 713 | + public Cluster cluster() { | |
| 714 | + return cluster; | |
| 715 | + } | |
| 716 | + | |
| 717 | + /** | |
| 718 | + * @return The {@link Session} instance to use with this test. | |
| 719 | + * Can be null if CCM configuration specifies {@code createSession = false}. | |
| 720 | + */ | |
| 721 | + public Session session() { | |
| 722 | + return session; | |
| 723 | + } | |
| 724 | + | |
| 725 | + /** | |
| 726 | + * Executes the given statements with the test's session object. | |
| 613 | 727 | * <p/> |
| 614 | - * Statements do not need to be qualified with keyspace name. | |
| 728 | + * Useful to create test fixtures and/or load data before tests. | |
| 615 | 729 | * <p/> |
| 616 | - * The default implementation returns an empty list (no fixtures required). | |
| 730 | + * This method should not be called if a session object hasn't been created | |
| 731 | + * (if CCM configuration specifies {@code createSession = false}) | |
| 617 | 732 | * |
| 618 | - * @return The DDL statements to execute before the tests. | |
| 733 | + * @param statements The statements to execute. | |
| 619 | 734 | */ |
| 620 | - public Collection<String> createTestFixtures() { | |
| 621 | - return Collections.emptyList(); | |
| 735 | + public void execute(String... statements) { | |
| 736 | + execute(Arrays.asList(statements)); | |
| 622 | 737 | } |
| 623 | 738 | |
| 739 | + /** | |
| 740 | + * Executes the given statements with the test's session object. | |
| 741 | + * <p/> | |
| 742 | + * Useful to create test fixtures and/or load data before tests. | |
| 743 | + * <p/> | |
| 744 | + * This method should not be called if a session object hasn't been created | |
| 745 | + * (if CCM configuration specifies {@code createSession = false}) | |
| 746 | + * | |
| 747 | + * @param statements The statements to execute. | |
| 748 | + */ | |
| 749 | + public void execute(Collection<String> statements) { | |
| 750 | + assert session != null; | |
| 751 | + for (String stmt : statements) { | |
| 752 | + try { | |
| 753 | + session.execute(stmt); | |
| 754 | + } catch (Exception e) { | |
| 755 | + errorOut(); | |
| 756 | + LOGGER.error("Could not execute statement: " + stmt, e); | |
| 757 | + Throwables.propagate(e); | |
| 758 | + } | |
| 759 | + } | |
| 760 | + } | |
| 624 | 761 | /** |
| 625 | 762 | * Signals that the test has encountered an unexpected error. |
| 626 | 763 | * <p/> |
| @@ -635,8 +772,40 @@ | ||
| 635 | 772 | */ |
| 636 | 773 | public void errorOut() { |
| 637 | 774 | erroredOut = true; |
| 638 | - if (ccm != null) | |
| 775 | + if (ccm != null) { | |
| 639 | 776 | ccm.setKeepLogs(true); |
| 777 | + } | |
| 778 | + } | |
| 779 | + | |
| 780 | + /** | |
| 781 | + * Returns the contact points to use to contact the CCM cluster. | |
| 782 | + * <p/> | |
| 783 | + * This method returns as many contact points as the number of nodes initially present in the CCM cluster, | |
| 784 | + * according to {@link CCMConfig} annotations. | |
| 785 | + * <p/> | |
| 786 | + * On a multi-DC setup, this will include nodes in all data centers. | |
| 787 | + * <p/> | |
| 788 | + * This method should not be called before the test has started, nor after the test is finished. | |
| 789 | + * | |
| 790 | + * @return the contact points to use to contact the CCM cluster. | |
| 791 | + */ | |
| 792 | + public List<InetAddress> getContactPoints() { | |
| 793 | + assert ccmTestConfig != null; | |
| 794 | + List<InetAddress> contactPoints = new ArrayList<InetAddress>(); | |
| 795 | + int n = 1; | |
| 796 | + int[] numberOfNodes = ccmTestConfig.numberOfNodes(); | |
| 797 | + for (int dc = 1; dc <= numberOfNodes.length; dc++) { | |
| 798 | + int nodesInDc = numberOfNodes[dc - 1]; | |
| 799 | + for (int i = 0; i < nodesInDc; i++) { | |
| 800 | + try { | |
| 801 | + contactPoints.add(InetAddress.getByName(ipOfNode(n))); | |
| 802 | + } catch (UnknownHostException e) { | |
| 803 | + Throwables.propagate(e); | |
| 804 | + } | |
| 805 | + n++; | |
| 806 | + } | |
| 807 | + } | |
| 808 | + return contactPoints; | |
| 640 | 809 | } |
| 641 | 810 | |
| 642 | 811 | /** |
| @@ -651,7 +820,7 @@ | ||
| 651 | 820 | * |
| 652 | 821 | * @return the contact points to use to contact the CCM cluster. |
| 653 | 822 | */ |
| 654 | - public List<InetSocketAddress> getInitialContactPoints() { | |
| 823 | + public List<InetSocketAddress> getContactPointsWithPorts() { | |
| 655 | 824 | assert ccmTestConfig != null; |
| 656 | 825 | List<InetSocketAddress> contactPoints = new ArrayList<InetSocketAddress>(); |
| 657 | 826 | int n = 1; |
| @@ -679,52 +848,73 @@ | ||
| 679 | 848 | return closeable; |
| 680 | 849 | } |
| 681 | 850 | |
| 682 | - private void initTestContext(Object testInstance, Method testMethod) throws Exception { | |
| 851 | + /** | |
| 852 | + * Tests fail randomly with InvalidQueryException: Keyspace 'xxx' does not exist; | |
| 853 | + * this method tries at most 3 times to issue a successful USE statement. | |
| 854 | + * | |
| 855 | + * @param ks The keyspace to use | |
| 856 | + */ | |
| 857 | + public void useKeyspace(String ks) { | |
| 858 | + final int maxTries = 3; | |
| 859 | + for (int i = 1; i <= maxTries; i++) { | |
| 860 | + try { | |
| 861 | + session.execute("USE " + ks); | |
| 862 | + } catch (InvalidQueryException e) { | |
| 863 | + if (i == maxTries) | |
| 864 | + throw e; | |
| 865 | + LOGGER.error("Could not USE keyspace, retrying"); | |
| 866 | + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MINUTES); | |
| 867 | + } | |
| 868 | + } | |
| 869 | + } | |
| 870 | + | |
| 871 | + protected void initTestContext(Object testInstance, Method testMethod) throws Exception { | |
| 683 | 872 | erroredOut = false; |
| 684 | 873 | ccmTestConfig = createCCMTestConfig(testInstance, testMethod); |
| 685 | 874 | assert ccmTestConfig != null; |
| 686 | 875 | if (ccmTestConfig.createCcm()) { |
| 687 | - CCMAccess ccm = CCMCache.get(ccmTestConfig.ccmBuilder()); | |
| 876 | + ccmBuilder = ccmTestConfig.ccmBuilder(testInstance); | |
| 877 | + CCMAccess ccm = CCMCache.get(ccmBuilder); | |
| 688 | 878 | assert ccm != null; |
| 689 | 879 | if (ccmTestConfig.dirtiesContext()) { |
| 690 | 880 | this.ccm = ccm; |
| 691 | 881 | } else { |
| 692 | - this.ccm = new ReadOnlyCCMBridge(ccm); | |
| 882 | + this.ccm = new ReadOnlyCCMAccess(ccm); | |
| 693 | 883 | } |
| 694 | 884 | try { |
| 695 | 885 | ccm.start(); |
| 696 | - } catch (RuntimeException e) { | |
| 697 | - LOGGER.error("Could not start " + ccm, e); | |
| 886 | + } catch (CCMException e) { | |
| 887 | + errorOut(); | |
| 698 | 888 | fail(e.getMessage()); |
| 699 | 889 | } |
| 700 | 890 | LOGGER.debug("Using {}", ccm); |
| 701 | 891 | } |
| 702 | 892 | } |
| 703 | 893 | |
| 704 | - private void initTestCluster(Object testInstance) throws Exception { | |
| 894 | + protected void initTestCluster(Object testInstance) throws Exception { | |
| 705 | 895 | if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster()) { |
| 706 | - Cluster.Builder builder = ccmTestConfig.clusterBuilder(testInstance); | |
| 896 | + Cluster.Builder builder = ccmTestConfig.clusterProvider(testInstance); | |
| 707 | 897 | // add contact points only if the provided builder didn't do so |
| 708 | 898 | if (builder.getContactPoints().isEmpty()) |
| 709 | - builder.addContactPointsWithPorts(getInitialContactPoints()); | |
| 899 | + builder.addContactPoints(getContactPoints()); | |
| 710 | 900 | builder.withPort(ccm.getBinaryPort()); |
| 711 | 901 | cluster = register(builder.build()); |
| 712 | 902 | cluster.init(); |
| 713 | 903 | } |
| 714 | 904 | } |
| 715 | 905 | |
| 716 | - private void initTestSession() throws Exception { | |
| 906 | + protected void initTestSession() throws Exception { | |
| 717 | 907 | if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster() && ccmTestConfig.createSession()) |
| 718 | 908 | session = register(cluster.connect()); |
| 719 | 909 | } |
| 720 | 910 | |
| 721 | - private void initTestKeyspace() { | |
| 911 | + protected void initTestKeyspace() { | |
| 722 | 912 | if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster() && ccmTestConfig.createSession() && ccmTestConfig.createKeyspace()) { |
| 723 | 913 | try { |
| 724 | - keyspace = TestUtils.getAvailableKeyspaceName(); | |
| 914 | + keyspace = TestUtils.generateIdentifier("ks_"); | |
| 725 | 915 | LOGGER.debug("Using keyspace " + keyspace); |
| 726 | 916 | session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); |
| 727 | - session.execute("USE " + keyspace); | |
| 917 | + useKeyspace(keyspace); | |
| 728 | 918 | } catch (Exception e) { |
| 729 | 919 | errorOut(); |
| 730 | 920 | LOGGER.error("Could not create test keyspace", e); |
| @@ -733,35 +923,25 @@ | ||
| 733 | 923 | } |
| 734 | 924 | } |
| 735 | 925 | |
| 736 | - private void initTestFixtures(Object testInstance) throws Exception { | |
| 737 | - if (ccmTestConfig.createCcm() && ccmTestConfig.createCluster() && ccmTestConfig.createSession()) { | |
| 738 | - for (String stmt : ccmTestConfig.fixtures(testInstance)) { | |
| 739 | - try { | |
| 740 | - session.execute(stmt); | |
| 741 | - } catch (Exception e) { | |
| 742 | - errorOut(); | |
| 743 | - LOGGER.error("Could not execute statement: " + stmt, e); | |
| 744 | - Throwables.propagate(e); | |
| 745 | - } | |
| 746 | - } | |
| 747 | - } | |
| 926 | + protected void initTest(Object testInstance) throws Exception { | |
| 927 | + ccmTestConfig.invokeInitTest(testInstance); | |
| 748 | 928 | } |
| 749 | 929 | |
| 750 | - private void closeTestContext() { | |
| 751 | - if (ccmTestConfig != null && ccm != null) { | |
| 752 | - CCMBridge.Builder key = ccmTestConfig.ccmBuilder(); | |
| 930 | + protected void closeTestContext() throws Exception { | |
| 931 | + if (ccmTestConfig != null && ccmBuilder != null && ccm != null) { | |
| 753 | 932 | if (ccmTestConfig.dirtiesContext()) { |
| 754 | - CCMCache.remove(key); | |
| 933 | + CCMCache.remove(ccmBuilder); | |
| 755 | 934 | ccm.close(); |
| 756 | 935 | } else { |
| 757 | - ((ReadOnlyCCMBridge) ccm).delegate.close(); | |
| 936 | + ((ReadOnlyCCMAccess) ccm).delegate.close(); | |
| 758 | 937 | } |
| 759 | 938 | } |
| 760 | 939 | ccmTestConfig = null; |
| 940 | + ccmBuilder = null; | |
| 761 | 941 | ccm = null; |
| 762 | 942 | } |
| 763 | 943 | |
| 764 | - private void closeTestCluster() { | |
| 944 | + protected void closeTestCluster() { | |
| 765 | 945 | if (cluster != null && !cluster.isClosed()) |
| 766 | 946 | executeNoFail(new Runnable() { |
| 767 | 947 | @Override |
| @@ -774,7 +954,7 @@ | ||
| 774 | 954 | keyspace = null; |
| 775 | 955 | } |
| 776 | 956 | |
| 777 | - private void closeCloseables() { | |
| 957 | + protected void closeCloseables() { | |
| 778 | 958 | if (closer != null) |
| 779 | 959 | executeNoFail(new Callable<Void>() { |
| 780 | 960 | @Override |
modified driver-core/src/test/java/com/datastax/driver/core/CaseSensitivityTest.java +4 −4
| @@ -26,7 +26,7 @@ | ||
| 26 | 26 | @Test(groups = "short") |
| 27 | 27 | public void testCaseInsensitiveKeyspace() throws Throwable { |
| 28 | 28 | String ksName = "MyKeyspace1"; |
| 29 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ksName, 1)); | |
| 29 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ksName, 1)); | |
| 30 | 30 | assertExists(ksName, "mykeyspace1"); |
| 31 | 31 | assertExists("mykeyspace1", "mykeyspace1"); |
| 32 | 32 | assertExists("MYKEYSPACE1", "mykeyspace1"); |
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | @Test(groups = "short") |
| 36 | 36 | public void testCaseSensitiveKeyspace() throws Throwable { |
| 37 | 37 | String ksName = "\"MyKeyspace2\""; |
| 38 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ksName, 1)); | |
| 38 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ksName, 1)); | |
| 39 | 39 | assertExists(ksName, "MyKeyspace2"); |
| 40 | 40 | assertExists(Metadata.quote("MyKeyspace2"), "MyKeyspace2"); |
| 41 | 41 | assertNotExists("mykeyspace2"); |
| @@ -44,13 +44,13 @@ | ||
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | private void assertExists(String fetchName, String realName) { |
| 47 | - KeyspaceMetadata km = cluster.getMetadata().getKeyspace(fetchName); | |
| 47 | + KeyspaceMetadata km = cluster().getMetadata().getKeyspace(fetchName); | |
| 48 | 48 | assertNotNull(km); |
| 49 | 49 | assertEquals(realName, km.getName()); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | private void assertNotExists(String name) { |
| 53 | - assertNull(cluster.getMetadata().getKeyspace(name)); | |
| 53 | + assertNull(cluster().getMetadata().getKeyspace(name)); | |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterDelegateTest.java +1 −1
| @@ -23,7 +23,7 @@ | ||
| 23 | 23 | |
| 24 | 24 | @Test(groups = "short") |
| 25 | 25 | public void should_allow_subclass_to_delegate_to_other_instance() { |
| 26 | - SimpleDelegatingCluster delegatingCluster = new SimpleDelegatingCluster(cluster); | |
| 26 | + SimpleDelegatingCluster delegatingCluster = new SimpleDelegatingCluster(cluster()); | |
| 27 | 27 | |
| 28 | 28 | ResultSet rs = delegatingCluster.connect().execute("select * from system.local"); |
| 29 | 29 |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterStressTest.java +7 −5
| @@ -172,7 +172,9 @@ | ||
| 172 | 172 | |
| 173 | 173 | CreateClusterAndCheckConnections(CountDownLatch startSignal) { |
| 174 | 174 | this.startSignal = startSignal; |
| 175 | - this.cluster = Cluster.builder().addContactPointsWithPorts(getInitialContactPoints()) | |
| 175 | + this.cluster = Cluster.builder() | |
| 176 | + .addContactPoints(getContactPoints()) | |
| 177 | + .withPort(ccm().getBinaryPort()) | |
| 176 | 178 | .withPoolingOptions(new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1)) |
| 177 | 179 | .withNettyOptions(channelMonitor.nettyOptions()).build(); |
| 178 | 180 | } |
| @@ -186,19 +188,19 @@ | ||
| 186 | 188 | cluster.init(); |
| 187 | 189 | assertEquals(cluster.manager.sessions.size(), 0); |
| 188 | 190 | assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); |
| 189 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 1); | |
| 191 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 1); | |
| 190 | 192 | |
| 191 | 193 | // The first session initializes the cluster and its control connection |
| 192 | 194 | Session session = cluster.connect(); |
| 193 | 195 | assertEquals(cluster.manager.sessions.size(), 1); |
| 194 | 196 | assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + TestUtils.numberOfLocalCoreConnections(cluster)); |
| 195 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 1 + TestUtils.numberOfLocalCoreConnections(cluster)); | |
| 197 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 1 + TestUtils.numberOfLocalCoreConnections(cluster)); | |
| 196 | 198 | |
| 197 | 199 | // Closing the session keeps the control connection opened |
| 198 | 200 | session.close(); |
| 199 | 201 | assertEquals(cluster.manager.sessions.size(), 0); |
| 200 | 202 | assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); |
| 201 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 1); | |
| 203 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 1); | |
| 202 | 204 | |
| 203 | 205 | return this; |
| 204 | 206 | } catch (AssertionError e) { |
| @@ -230,7 +232,7 @@ | ||
| 230 | 232 | try { |
| 231 | 233 | cluster.close(); |
| 232 | 234 | assertEquals(cluster.manager.sessions.size(), 0); |
| 233 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 0); | |
| 235 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 0); | |
| 234 | 236 | } finally { |
| 235 | 237 | channelMonitor.stop(); |
| 236 | 238 | cluster = null; |
modified driver-core/src/test/java/com/datastax/driver/core/ConditionChecker.java +78 −57
| @@ -23,83 +23,106 @@ | ||
| 23 | 23 | import java.util.Timer; |
| 24 | 24 | import java.util.TimerTask; |
| 25 | 25 | import java.util.concurrent.Callable; |
| 26 | +import java.util.concurrent.TimeUnit; | |
| 26 | 27 | import java.util.concurrent.locks.Condition; |
| 27 | 28 | import java.util.concurrent.locks.Lock; |
| 28 | 29 | import java.util.concurrent.locks.ReentrantLock; |
| 29 | 30 | |
| 30 | -import static com.google.common.base.Preconditions.checkState; | |
| 31 | -import static java.util.concurrent.TimeUnit.MILLISECONDS; | |
| 32 | 31 | import static org.assertj.core.api.Fail.fail; |
| 33 | 32 | |
| 34 | 33 | public class ConditionChecker { |
| 35 | 34 | |
| 36 | - private static final Logger logger = LoggerFactory.getLogger(ConditionChecker.class); | |
| 37 | - | |
| 38 | 35 | private static final int DEFAULT_PERIOD_MILLIS = 500; |
| 39 | 36 | |
| 40 | - public static <T> void awaitUntil(T input, Predicate<? super T> predicate, long timeoutMillis) { | |
| 41 | - new ConditionChecker(input, predicate).await(timeoutMillis); | |
| 42 | - } | |
| 37 | + private static final int DEFAULT_TIMEOUT_MILLIS = 60000; | |
| 43 | 38 | |
| 44 | - public static void awaitUntil(final Callable<Boolean> condition, long timeoutMillis) { | |
| 45 | - new ConditionChecker(condition).await(timeoutMillis); | |
| 46 | - } | |
| 39 | + public static class ConditionCheckerBuilder { | |
| 47 | 40 | |
| 48 | - public static <T> void awaitWhile(T input, Predicate<? super T> predicate, long timeoutMillis) { | |
| 49 | - new ConditionChecker(input, Predicates.not(predicate)).await(timeoutMillis); | |
| 50 | - } | |
| 41 | + private long timeout = DEFAULT_TIMEOUT_MILLIS; | |
| 51 | 42 | |
| 52 | - public static void awaitWhile(final Callable<Boolean> condition, long timeoutMillis) { | |
| 53 | - new ConditionChecker(new Callable<Boolean>() { | |
| 54 | - @Override | |
| 55 | - public Boolean call() throws Exception { | |
| 56 | - return !condition.call(); | |
| 57 | - } | |
| 58 | - }).await(timeoutMillis); | |
| 59 | - } | |
| 43 | + private TimeUnit timeoutUnit = TimeUnit.MILLISECONDS; | |
| 60 | 44 | |
| 61 | - private final Object input; | |
| 45 | + private long period = DEFAULT_PERIOD_MILLIS; | |
| 62 | 46 | |
| 63 | - private final Predicate<Object> predicate; | |
| 47 | + private TimeUnit periodUnit = TimeUnit.MILLISECONDS; | |
| 64 | 48 | |
| 65 | - private final Lock lock; | |
| 49 | + private Object input; | |
| 66 | 50 | |
| 67 | - private final Condition condition; | |
| 51 | + private Predicate<?> predicate; | |
| 68 | 52 | |
| 69 | - private final Timer timer; | |
| 53 | + public ConditionCheckerBuilder every(long period, TimeUnit unit) { | |
| 54 | + this.period = period; | |
| 55 | + periodUnit = unit; | |
| 56 | + return this; | |
| 57 | + } | |
| 70 | 58 | |
| 71 | - /** | |
| 72 | - * When using this constructor, one must override {@link #evalCondition()}. | |
| 73 | - */ | |
| 74 | - public ConditionChecker() { | |
| 75 | - this(null, null); | |
| 76 | - checkState(!this.getClass().equals(ConditionChecker.class), "This constructor requires subclassing ConditionChecker"); | |
| 77 | - } | |
| 59 | + public ConditionCheckerBuilder every(long periodMillis) { | |
| 60 | + period = periodMillis; | |
| 61 | + periodUnit = TimeUnit.MILLISECONDS; | |
| 62 | + return this; | |
| 63 | + } | |
| 78 | 64 | |
| 79 | - public ConditionChecker(Callable<Boolean> condition) { | |
| 80 | - this(condition, DEFAULT_PERIOD_MILLIS); | |
| 81 | - } | |
| 65 | + public ConditionCheckerBuilder before(long timeout, TimeUnit unit) { | |
| 66 | + this.timeout = timeout; | |
| 67 | + timeoutUnit = unit; | |
| 68 | + return this; | |
| 69 | + } | |
| 82 | 70 | |
| 83 | - public ConditionChecker(final Callable<Boolean> condition, int periodMillis) { | |
| 84 | - this(null, new Predicate<Void>() { | |
| 85 | - @Override | |
| 86 | - public boolean apply(Void input) { | |
| 87 | - try { | |
| 88 | - return condition.call(); | |
| 89 | - } catch (Exception e) { | |
| 90 | - logger.error("Evaluation of condition threw exception", e); | |
| 91 | - return false; | |
| 71 | + public ConditionCheckerBuilder before(long timeoutMillis) { | |
| 72 | + timeout = timeoutMillis; | |
| 73 | + timeoutUnit = TimeUnit.MILLISECONDS; | |
| 74 | + return this; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public <T> ConditionCheckerBuilder that(T input, Predicate<? super T> predicate) { | |
| 78 | + this.input = input; | |
| 79 | + this.predicate = predicate; | |
| 80 | + return this; | |
| 81 | + } | |
| 82 | + | |
| 83 | + public ConditionCheckerBuilder that(final Callable<Boolean> condition) { | |
| 84 | + this.input = null; | |
| 85 | + this.predicate = new Predicate<Void>() { | |
| 86 | + @Override | |
| 87 | + public boolean apply(Void input) { | |
| 88 | + try { | |
| 89 | + return condition.call(); | |
| 90 | + } catch (Exception e) { | |
| 91 | + logger.error("Evaluation of condition threw exception", e); | |
| 92 | + return false; | |
| 93 | + } | |
| 92 | 94 | } |
| 93 | - } | |
| 94 | - }, periodMillis); | |
| 95 | + }; | |
| 96 | + return this; | |
| 97 | + } | |
| 98 | + | |
| 99 | + @SuppressWarnings("unchecked") | |
| 100 | + public void becomesTrue() { | |
| 101 | + new ConditionChecker(input, (Predicate<Object>) predicate, period, periodUnit).await(timeout, timeoutUnit); | |
| 102 | + } | |
| 103 | + | |
| 104 | + @SuppressWarnings("unchecked") | |
| 105 | + public void becomesFalse() { | |
| 106 | + this.predicate = Predicates.not(predicate); | |
| 107 | + new ConditionChecker(input, (Predicate<Object>) predicate, period, periodUnit).await(timeout, timeoutUnit); | |
| 108 | + } | |
| 109 | + | |
| 95 | 110 | } |
| 96 | 111 | |
| 97 | - public <T> ConditionChecker(T input, Predicate<? super T> predicate) { | |
| 98 | - this(input, predicate, DEFAULT_PERIOD_MILLIS); | |
| 112 | + private static final Logger logger = LoggerFactory.getLogger(ConditionChecker.class); | |
| 113 | + | |
| 114 | + public static ConditionCheckerBuilder check() { | |
| 115 | + return new ConditionCheckerBuilder(); | |
| 99 | 116 | } |
| 100 | 117 | |
| 118 | + private final Object input; | |
| 119 | + private final Predicate<Object> predicate; | |
| 120 | + private final Lock lock; | |
| 121 | + private final Condition condition; | |
| 122 | + private final Timer timer; | |
| 123 | + | |
| 101 | 124 | @SuppressWarnings("unchecked") |
| 102 | - public <T> ConditionChecker(T input, Predicate<? super T> predicate, int periodMillis) { | |
| 125 | + public <T> ConditionChecker(T input, Predicate<? super T> predicate, long period, TimeUnit periodUnit) { | |
| 103 | 126 | this.input = input; |
| 104 | 127 | this.predicate = (Predicate<Object>) predicate; |
| 105 | 128 | lock = new ReentrantLock(); |
| @@ -110,23 +133,21 @@ | ||
| 110 | 133 | public void run() { |
| 111 | 134 | checkCondition(); |
| 112 | 135 | } |
| 113 | - }, 0, periodMillis); | |
| 136 | + }, 0, periodUnit.toMillis(period)); | |
| 114 | 137 | } |
| 115 | 138 | |
| 116 | 139 | /** |
| 117 | 140 | * Waits until the predicate becomes true, |
| 118 | 141 | * or a timeout occurs, whichever happens first. |
| 119 | - * | |
| 120 | - * @param timeoutMillis timeout in milliseconds | |
| 121 | 142 | */ |
| 122 | - public void await(long timeoutMillis) { | |
| 143 | + public void await(long timeout, TimeUnit unit) { | |
| 123 | 144 | boolean interrupted = false; |
| 124 | - long nanos = MILLISECONDS.toNanos(timeoutMillis); | |
| 145 | + long nanos = unit.toNanos(timeout); | |
| 125 | 146 | lock.lock(); |
| 126 | 147 | try { |
| 127 | 148 | while (!evalCondition()) { |
| 128 | 149 | if (nanos <= 0L) |
| 129 | - fail(String.format("Timeout after %s milliseconds while waiting for condition", timeoutMillis)); | |
| 150 | + fail(String.format("Timeout after %s %s while waiting for condition", timeout, unit.toString().toLowerCase())); | |
| 130 | 151 | try { |
| 131 | 152 | nanos = condition.awaitNanos(nanos); |
| 132 | 153 | } catch (InterruptedException e) { |
| @@ -151,7 +172,7 @@ | ||
| 151 | 172 | } |
| 152 | 173 | } |
| 153 | 174 | |
| 154 | - protected boolean evalCondition() { | |
| 175 | + private boolean evalCondition() { | |
| 155 | 176 | return predicate.apply(input); |
| 156 | 177 | } |
| 157 | 178 |
modified driver-core/src/test/java/com/datastax/driver/core/ConditionalUpdateTest.java +24 −27
| @@ -18,9 +18,6 @@ | ||
| 18 | 18 | import com.datastax.driver.core.utils.CassandraVersion; |
| 19 | 19 | import org.testng.annotations.Test; |
| 20 | 20 | |
| 21 | -import java.util.Collection; | |
| 22 | -import java.util.Collections; | |
| 23 | - | |
| 24 | 21 | import static org.testng.Assert.assertFalse; |
| 25 | 22 | import static org.testng.Assert.assertTrue; |
| 26 | 23 | |
| @@ -31,55 +28,55 @@ | ||
| 31 | 28 | public class ConditionalUpdateTest extends CCMTestsSupport { |
| 32 | 29 | |
| 33 | 30 | @Override |
| 34 | - public Collection<String> createTestFixtures() { | |
| 35 | - return Collections.singletonList("CREATE TABLE test(k1 int, k2 int, v int, PRIMARY KEY (k1, k2))"); | |
| 31 | + public void onTestContextInitialized() { | |
| 32 | + execute("CREATE TABLE test(k1 int, k2 int, v int, PRIMARY KEY (k1, k2))"); | |
| 36 | 33 | } |
| 37 | 34 | |
| 38 | 35 | @Test(groups = "short") |
| 39 | 36 | public void singleUpdateTest() { |
| 40 | - session.execute("TRUNCATE test"); | |
| 41 | - session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); | |
| 37 | + session().execute("TRUNCATE test"); | |
| 38 | + session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); | |
| 42 | 39 | |
| 43 | - ResultSet rs = session.execute("UPDATE test SET v = 3 WHERE k1 = 1 AND k2 = 1 IF v = 2"); | |
| 40 | + ResultSet rs = session().execute("UPDATE test SET v = 3 WHERE k1 = 1 AND k2 = 1 IF v = 2"); | |
| 44 | 41 | assertFalse(rs.wasApplied()); |
| 45 | 42 | // Ensure that reading the status does not consume a row: |
| 46 | 43 | assertFalse(rs.isExhausted()); |
| 47 | 44 | |
| 48 | - rs = session.execute("UPDATE test SET v = 3 WHERE k1 = 1 AND k2 = 1 IF v = 1"); | |
| 45 | + rs = session().execute("UPDATE test SET v = 3 WHERE k1 = 1 AND k2 = 1 IF v = 1"); | |
| 49 | 46 | assertTrue(rs.wasApplied()); |
| 50 | 47 | assertFalse(rs.isExhausted()); |
| 51 | 48 | |
| 52 | 49 | // Non-conditional statement |
| 53 | - rs = session.execute("UPDATE test SET v = 4 WHERE k1 = 1 AND k2 = 1"); | |
| 50 | + rs = session().execute("UPDATE test SET v = 4 WHERE k1 = 1 AND k2 = 1"); | |
| 54 | 51 | assertTrue(rs.wasApplied()); |
| 55 | 52 | } |
| 56 | 53 | |
| 57 | 54 | @Test(groups = "short") |
| 58 | 55 | public void batchUpdateTest() { |
| 59 | - session.execute("TRUNCATE test"); | |
| 60 | - session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); | |
| 61 | - session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)"); | |
| 62 | - | |
| 63 | - PreparedStatement ps = session.prepare("UPDATE test SET v = :new WHERE k1 = :k1 AND k2 = :k2 IF v = :old"); | |
| 56 | + session().execute("TRUNCATE test"); | |
| 57 | + session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); | |
| 58 | + session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)"); | |
| 59 | + | |
| 60 | + PreparedStatement ps = session().prepare("UPDATE test SET v = :new WHERE k1 = :k1 AND k2 = :k2 IF v = :old"); | |
| 64 | 61 | BatchStatement batch = new BatchStatement(); |
| 65 | 62 | batch.add(ps.bind().setInt("k1", 1).setInt("k2", 1).setInt("old", 2).setInt("new", 3)); // will fail |
| 66 | 63 | batch.add(ps.bind().setInt("k1", 1).setInt("k2", 2).setInt("old", 1).setInt("new", 3)); |
| 67 | 64 | |
| 68 | 65 | |
| 69 | - ResultSet rs = session.execute(batch); | |
| 66 | + ResultSet rs = session().execute(batch); | |
| 70 | 67 | assertFalse(rs.wasApplied()); |
| 71 | 68 | } |
| 72 | 69 | |
| 73 | 70 | |
| 74 | 71 | @Test(groups = "short") |
| 75 | 72 | public void multipageResultSetTest() { |
| 76 | - session.execute("TRUNCATE test"); | |
| 77 | - session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); | |
| 78 | - session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)"); | |
| 73 | + session().execute("TRUNCATE test"); | |
| 74 | + session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); | |
| 75 | + session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)"); | |
| 79 | 76 | |
| 80 | 77 | // This is really contrived, we just want to cover the code path in ArrayBackedResultSet#MultiPage. |
| 81 | 78 | // Currently CAS update results are never multipage, so it's hard to come up with a meaningful example. |
| 82 | - ResultSet rs = session.execute(new SimpleStatement("SELECT * FROM test WHERE k1 = 1").setFetchSize(1)); | |
| 79 | + ResultSet rs = session().execute(new SimpleStatement("SELECT * FROM test WHERE k1 = 1").setFetchSize(1)); | |
| 83 | 80 | |
| 84 | 81 | assertTrue(rs.wasApplied()); |
| 85 | 82 | } |
| @@ -95,13 +92,13 @@ | ||
| 95 | 92 | @Test(groups = "short") |
| 96 | 93 | public void insert_if_not_exist_should_support_wasApplied_boolean() { |
| 97 | 94 | // First, make sure the test table and the row exist |
| 98 | - session.execute("CREATE TABLE IF NOT EXISTS Java358 (key int primary key, value int)"); | |
| 95 | + session().execute("CREATE TABLE IF NOT EXISTS Java358 (key int primary key, value int)"); | |
| 99 | 96 | ResultSet rs; |
| 100 | - rs = session.execute("INSERT INTO Java358(key, value) VALUES (42, 42) IF NOT EXISTS"); | |
| 97 | + rs = session().execute("INSERT INTO Java358(key, value) VALUES (42, 42) IF NOT EXISTS"); | |
| 101 | 98 | assertTrue(rs.wasApplied()); |
| 102 | 99 | |
| 103 | 100 | // Then, make sure the flag reports correctly that we did not create a new row |
| 104 | - rs = session.execute("INSERT INTO Java358(key, value) VALUES (42, 42) IF NOT EXISTS"); | |
| 101 | + rs = session().execute("INSERT INTO Java358(key, value) VALUES (42, 42) IF NOT EXISTS"); | |
| 105 | 102 | assertFalse(rs.wasApplied()); |
| 106 | 103 | } |
| 107 | 104 | |
| @@ -116,16 +113,16 @@ | ||
| 116 | 113 | @Test(groups = "short") |
| 117 | 114 | public void delete_if_not_exist_should_support_wasApplied_boolean() { |
| 118 | 115 | // First, make sure the test table and the row exist |
| 119 | - session.execute("CREATE TABLE IF NOT EXISTS Java358 (key int primary key, value int)"); | |
| 120 | - session.execute("INSERT INTO Java358(key, value) VALUES (42, 42)"); | |
| 116 | + session().execute("CREATE TABLE IF NOT EXISTS Java358 (key int primary key, value int)"); | |
| 117 | + session().execute("INSERT INTO Java358(key, value) VALUES (42, 42)"); | |
| 121 | 118 | |
| 122 | 119 | // Then, make sure the flag reports correctly that we did delete the row |
| 123 | 120 | ResultSet rs; |
| 124 | - rs = session.execute("DELETE FROM Java358 WHERE KEY=42 IF EXISTS"); | |
| 121 | + rs = session().execute("DELETE FROM Java358 WHERE KEY=42 IF EXISTS"); | |
| 125 | 122 | assertTrue(rs.wasApplied()); |
| 126 | 123 | |
| 127 | 124 | // Finally, make sure the flag reports correctly that we did did not delete an non-existing row |
| 128 | - rs = session.execute("DELETE FROM Java358 WHERE KEY=42 IF EXISTS"); | |
| 125 | + rs = session().execute("DELETE FROM Java358 WHERE KEY=42 IF EXISTS"); | |
| 129 | 126 | assertFalse(rs.wasApplied()); |
| 130 | 127 | } |
| 131 | 128 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ConsistencyTest.java +7 −5
| @@ -48,10 +48,12 @@ | ||
| 48 | 48 | dirtiesContext = true, |
| 49 | 49 | createKeyspace = false, |
| 50 | 50 | config = { |
| 51 | + // tests fail often with write or read timeouts | |
| 51 | 52 | "hinted_handoff_enabled:true", |
| 52 | - "phi_convict_threshold:1", | |
| 53 | + "phi_convict_threshold:5", | |
| 53 | 54 | "read_request_timeout_in_ms:100000", |
| 54 | - "write_request_timeout_in_ms:100000"} | |
| 55 | + "write_request_timeout_in_ms:100000" | |
| 56 | + } | |
| 55 | 57 | ) |
| 56 | 58 | public class ConsistencyTest extends AbstractPoliciesTest { |
| 57 | 59 | |
| @@ -719,9 +721,9 @@ | ||
| 719 | 721 | |
| 720 | 722 | private void stopAndWait(int node) { |
| 721 | 723 | logger.debug("Stopping node " + node); |
| 722 | - ccm.stop(node); | |
| 723 | - ccm.waitForDown(node);// this uses port ping | |
| 724 | - TestUtils.waitForDown(ipOfNode(node), cluster, 10); // this uses UP/DOWN events | |
| 724 | + ccm().stop(node); | |
| 725 | + ccm().waitForDown(node);// this uses port ping | |
| 726 | + TestUtils.waitForDown(ipOfNode(node), cluster()); // this uses UP/DOWN events | |
| 725 | 727 | logger.debug("Node " + node + " stopped, sleeping one extra minute to allow nodes to gossip"); |
| 726 | 728 | Uninterruptibles.sleepUninterruptibly(1, TimeUnit.MINUTES); |
| 727 | 729 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +9 −9
| @@ -74,19 +74,19 @@ | ||
| 74 | 74 | |
| 75 | 75 | // We pass only the first host as contact point, so we know the control connection will be on this host |
| 76 | 76 | cluster = register(Cluster.builder() |
| 77 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 78 | - .withPort(ccm.getBinaryPort()) | |
| 77 | + .addContactPoints(getContactPoints().get(0)) | |
| 78 | + .withPort(ccm().getBinaryPort()) | |
| 79 | 79 | .withReconnectionPolicy(reconnectionPolicy) |
| 80 | 80 | .withLoadBalancingPolicy(loadBalancingPolicy) |
| 81 | 81 | .build()); |
| 82 | 82 | cluster.init(); |
| 83 | 83 | |
| 84 | 84 | // Kill the control connection host, there should be exactly one reconnection attempt |
| 85 | - ccm.stop(1); | |
| 85 | + ccm().stop(1); | |
| 86 | 86 | TimeUnit.SECONDS.sleep(1); // Sleep for a while to make sure our final count is not the result of lucky timing |
| 87 | 87 | assertThat(reconnectionAttempts.get()).isEqualTo(1); |
| 88 | 88 | |
| 89 | - ccm.stop(2); | |
| 89 | + ccm().stop(2); | |
| 90 | 90 | TimeUnit.SECONDS.sleep(1); |
| 91 | 91 | assertThat(reconnectionAttempts.get()).isEqualTo(2); |
| 92 | 92 | } |
| @@ -101,11 +101,11 @@ | ||
| 101 | 101 | @CassandraVersion(major = 2.1) |
| 102 | 102 | public void should_parse_UDT_definitions_when_using_default_protocol_version() { |
| 103 | 103 | Cluster cluster; |
| 104 | - InetSocketAddress firstHost = ccm.addressOfNode(1); | |
| 104 | + InetSocketAddress firstHost = ccm().addressOfNode(1); | |
| 105 | 105 | // First driver instance: create UDT |
| 106 | 106 | cluster = register(Cluster.builder() |
| 107 | 107 | .addContactPointsWithPorts(firstHost) |
| 108 | - .withPort(ccm.getBinaryPort()) | |
| 108 | + .withPort(ccm().getBinaryPort()) | |
| 109 | 109 | .build()); |
| 110 | 110 | Session session = cluster.connect(); |
| 111 | 111 | session.execute("create keyspace ks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}"); |
| @@ -131,10 +131,10 @@ | ||
| 131 | 131 | @Test(groups = "long") |
| 132 | 132 | @CCMConfig(numberOfNodes = 3) |
| 133 | 133 | public void should_reestablish_if_control_node_decommissioned() throws InterruptedException { |
| 134 | - InetSocketAddress firstHost = ccm.addressOfNode(1); | |
| 134 | + InetSocketAddress firstHost = ccm().addressOfNode(1); | |
| 135 | 135 | Cluster cluster = register(Cluster.builder() |
| 136 | 136 | .addContactPointsWithPorts(firstHost) |
| 137 | - .withPort(ccm.getBinaryPort()) | |
| 137 | + .withPort(ccm().getBinaryPort()) | |
| 138 | 138 | .withQueryOptions(nonDebouncingQueryOptions()) |
| 139 | 139 | .build()); |
| 140 | 140 | cluster.init(); |
| @@ -144,7 +144,7 @@ | ||
| 144 | 144 | assertThat(controlHost).isEqualTo(firstHost.getAddress()); |
| 145 | 145 | |
| 146 | 146 | // Decommission the node. |
| 147 | - ccm.decommission(1); | |
| 147 | + ccm().decommission(1); | |
| 148 | 148 | |
| 149 | 149 | // Ensure that the new control connection is not null and it's host is not equal to the decommissioned node. |
| 150 | 150 | Host newHost = cluster.manager.controlConnection.connectedHost(); |
modified driver-core/src/test/java/com/datastax/driver/core/CustomTypeTest.java +6 −8
| @@ -19,8 +19,6 @@ | ||
| 19 | 19 | |
| 20 | 20 | import java.nio.ByteBuffer; |
| 21 | 21 | import java.util.ArrayList; |
| 22 | -import java.util.Collection; | |
| 23 | -import java.util.Collections; | |
| 24 | 22 | import java.util.List; |
| 25 | 23 | |
| 26 | 24 | import static org.testng.Assert.assertEquals; |
| @@ -31,8 +29,8 @@ | ||
| 31 | 29 | public class CustomTypeTest extends CCMTestsSupport { |
| 32 | 30 | |
| 33 | 31 | @Override |
| 34 | - public Collection<String> createTestFixtures() { | |
| 35 | - return Collections.singleton( | |
| 32 | + public void onTestContextInitialized() { | |
| 33 | + execute( | |
| 36 | 34 | "CREATE TABLE test (" |
| 37 | 35 | + " k int," |
| 38 | 36 | + " c 'DynamicCompositeType(s => UTF8Type, i => Int32Type)'," |
| @@ -79,11 +77,11 @@ | ||
| 79 | 77 | @Test(groups = "short") |
| 80 | 78 | public void DynamicCompositeTypeTest() { |
| 81 | 79 | |
| 82 | - session.execute("INSERT INTO test(k, c, v) VALUES (0, 's@foo:i@32', 1)"); | |
| 83 | - session.execute("INSERT INTO test(k, c, v) VALUES (0, 'i@42', 2)"); | |
| 84 | - session.execute("INSERT INTO test(k, c, v) VALUES (0, 'i@12:i@3', 3)"); | |
| 80 | + session().execute("INSERT INTO test(k, c, v) VALUES (0, 's@foo:i@32', 1)"); | |
| 81 | + session().execute("INSERT INTO test(k, c, v) VALUES (0, 'i@42', 2)"); | |
| 82 | + session().execute("INSERT INTO test(k, c, v) VALUES (0, 'i@12:i@3', 3)"); | |
| 85 | 83 | |
| 86 | - ResultSet rs = session.execute("SELECT * FROM test"); | |
| 84 | + ResultSet rs = session().execute("SELECT * FROM test"); | |
| 87 | 85 | |
| 88 | 86 | Row r = rs.one(); |
| 89 | 87 | assertEquals(r.getInt("k"), 0); |
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java +10 −12
| @@ -45,10 +45,9 @@ | ||
| 45 | 45 | enum StatementType {RAW_STRING, SIMPLE_WITH_PARAM, PREPARED} |
| 46 | 46 | |
| 47 | 47 | @Override |
| 48 | - public Collection<String> createTestFixtures() { | |
| 49 | - Host host = cluster.getMetadata().getAllHosts().iterator().next(); | |
| 48 | + public void onTestContextInitialized() { | |
| 49 | + Host host = cluster().getMetadata().getAllHosts().iterator().next(); | |
| 50 | 50 | cassandraVersion = host.getCassandraVersion().nextStable(); |
| 51 | - | |
| 52 | 51 | List<String> statements = Lists.newArrayList(); |
| 53 | 52 | for (TestTable table : tables) { |
| 54 | 53 | if (cassandraVersion.compareTo(table.minCassandraVersion) < 0) |
| @@ -57,8 +56,7 @@ | ||
| 57 | 56 | else |
| 58 | 57 | statements.add(table.createStatement); |
| 59 | 58 | } |
| 60 | - | |
| 61 | - return statements; | |
| 59 | + execute(statements); | |
| 62 | 60 | } |
| 63 | 61 | |
| 64 | 62 | @Test(groups = "long") |
| @@ -78,7 +76,7 @@ | ||
| 78 | 76 | } |
| 79 | 77 | |
| 80 | 78 | protected void should_insert_and_retrieve_data(StatementType statementType) { |
| 81 | - ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum(); | |
| 79 | + ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum(); | |
| 82 | 80 | |
| 83 | 81 | for (TestTable table : tables) { |
| 84 | 82 | if (cassandraVersion.compareTo(table.minCassandraVersion) < 0) |
| @@ -86,20 +84,20 @@ | ||
| 86 | 84 | |
| 87 | 85 | switch (statementType) { |
| 88 | 86 | case RAW_STRING: |
| 89 | - session.execute(table.insertStatement.replace("?", table.testColumnType.format(table.sampleValue))); | |
| 87 | + session().execute(table.insertStatement.replace("?", table.testColumnType.format(table.sampleValue))); | |
| 90 | 88 | break; |
| 91 | 89 | case SIMPLE_WITH_PARAM: |
| 92 | - session.execute(table.insertStatement, table.sampleValue); | |
| 90 | + session().execute(table.insertStatement, table.sampleValue); | |
| 93 | 91 | break; |
| 94 | 92 | case PREPARED: |
| 95 | - PreparedStatement ps = session.prepare(table.insertStatement); | |
| 93 | + PreparedStatement ps = session().prepare(table.insertStatement); | |
| 96 | 94 | BoundStatement bs = ps.bind(table.sampleValue); |
| 97 | 95 | checkGetterReturnsBoundValue(bs, table); |
| 98 | - session.execute(bs); | |
| 96 | + session().execute(bs); | |
| 99 | 97 | break; |
| 100 | 98 | } |
| 101 | 99 | |
| 102 | - Row row = session.execute(table.selectStatement).one(); | |
| 100 | + Row row = session().execute(table.selectStatement).one(); | |
| 103 | 101 | Object queriedValue = table.testColumnType.deserialize(row.getBytesUnsafe("v"), protocolVersion); |
| 104 | 102 | |
| 105 | 103 | assertThat(queriedValue) |
| @@ -110,7 +108,7 @@ | ||
| 110 | 108 | table.insertStatement) |
| 111 | 109 | .isEqualTo(table.sampleValue); |
| 112 | 110 | |
| 113 | - session.execute(table.truncateStatement); | |
| 111 | + session().execute(table.truncateStatement); | |
| 114 | 112 | } |
| 115 | 113 | } |
| 116 | 114 |
modified driver-core/src/test/java/com/datastax/driver/core/DseCCMClusterTest.java +7 −0
| @@ -59,6 +59,13 @@ | ||
| 59 | 59 | * dse_username = myusername |
| 60 | 60 | * dse_password = mypassword |
| 61 | 61 | * </pre> |
| 62 | + * <p/> | |
| 63 | + * <h3>Other requirements</h3> | |
| 64 | + * <p/> | |
| 65 | + * DSE requires your {@code PATH} variable to provide access | |
| 66 | + * to super-user executables in {@code /usr/sbin}. | |
| 67 | + * <p/> | |
| 68 | + * A correct example is as follows: {@code /usr/bin:/usr/local/bin:/bin:/usr/sbin:$JAVA_HOME/bin:$PATH}. | |
| 62 | 69 | */ |
| 63 | 70 | @Test(enabled = false) |
| 64 | 71 | @CCMConfig(dse = true, version = "4.8.3") |
modified driver-core/src/test/java/com/datastax/driver/core/EventDebouncerIntegrationTest.java +4 −4
| @@ -44,8 +44,8 @@ | ||
| 44 | 44 | public void should_wait_until_load_balancing_policy_is_fully_initialized() throws InterruptedException { |
| 45 | 45 | TestLoadBalancingPolicy policy = new TestLoadBalancingPolicy(); |
| 46 | 46 | final Cluster cluster = register(createClusterBuilderNoDebouncing() |
| 47 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 48 | - .withPort(ccm.getBinaryPort()) | |
| 47 | + .addContactPoints(getContactPoints().get(0)) | |
| 48 | + .withPort(ccm().getBinaryPort()) | |
| 49 | 49 | .withLoadBalancingPolicy(policy).build()); |
| 50 | 50 | new Thread() { |
| 51 | 51 | @Override |
| @@ -59,8 +59,8 @@ | ||
| 59 | 59 | // because the debouncers are not started |
| 60 | 60 | // note: a graceful stop notify other nodes which send a topology change event to the driver right away |
| 61 | 61 | // while forceStop kills the node so other nodes take much more time to detect node failure |
| 62 | - ccm.stop(3); | |
| 63 | - ccm.waitForDown(3); | |
| 62 | + ccm().stop(3); | |
| 63 | + ccm().waitForDown(3); | |
| 64 | 64 | // finish cluster initialization and deliver the DOWN event |
| 65 | 65 | policy.proceed(); |
| 66 | 66 | assertThat(policy.onDownCalledBeforeInit).isFalse(); |
modified driver-core/src/test/java/com/datastax/driver/core/FetchingTest.java +5 −8
| @@ -18,9 +18,6 @@ | ||
| 18 | 18 | import com.datastax.driver.core.exceptions.UnsupportedFeatureException; |
| 19 | 19 | import org.testng.annotations.Test; |
| 20 | 20 | |
| 21 | -import java.util.Collection; | |
| 22 | -import java.util.Collections; | |
| 23 | - | |
| 24 | 21 | import static org.testng.Assert.*; |
| 25 | 22 | |
| 26 | 23 | /** |
| @@ -29,8 +26,8 @@ | ||
| 29 | 26 | public class FetchingTest extends CCMTestsSupport { |
| 30 | 27 | |
| 31 | 28 | @Override |
| 32 | - public Collection<String> createTestFixtures() { | |
| 33 | - return Collections.singletonList("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 29 | + public void onTestContextInitialized() { | |
| 30 | + execute("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 34 | 31 | } |
| 35 | 32 | |
| 36 | 33 | @Test(groups = "short") |
| @@ -39,11 +36,11 @@ | ||
| 39 | 36 | // Insert data |
| 40 | 37 | String key = "paging_test"; |
| 41 | 38 | for (int i = 0; i < 100; i++) |
| 42 | - session.execute(String.format("INSERT INTO test (k, v) VALUES ('%s', %d)", key, i)); | |
| 39 | + session().execute(String.format("INSERT INTO test (k, v) VALUES ('%s', %d)", key, i)); | |
| 43 | 40 | |
| 44 | 41 | SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", key)); |
| 45 | 42 | st.setFetchSize(5); // Ridiculously small fetch size for testing purpose. Don't do at home. |
| 46 | - ResultSet rs = session.execute(st); | |
| 43 | + ResultSet rs = session().execute(st); | |
| 47 | 44 | |
| 48 | 45 | assertFalse(rs.isFullyFetched()); |
| 49 | 46 | |
| @@ -59,7 +56,7 @@ | ||
| 59 | 56 | |
| 60 | 57 | } catch (UnsupportedFeatureException e) { |
| 61 | 58 | // This is expected when testing the protocol v1 |
| 62 | - assertEquals(cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum(), ProtocolVersion.V1); | |
| 59 | + assertEquals(cluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum(), ProtocolVersion.V1); | |
| 63 | 60 | } |
| 64 | 61 | } |
| 65 | 62 | } |
modified driver-core/src/test/java/com/datastax/driver/core/HeartbeatTest.java +4 −4
| @@ -57,8 +57,8 @@ | ||
| 57 | 57 | @Test(groups = "long") |
| 58 | 58 | public void should_send_heartbeat_when_connection_is_inactive() throws InterruptedException { |
| 59 | 59 | Cluster cluster = register(Cluster.builder() |
| 60 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 61 | - .withPort(ccm.getBinaryPort()) | |
| 60 | + .addContactPoints(getContactPoints().get(0)) | |
| 61 | + .withPort(ccm().getBinaryPort()) | |
| 62 | 62 | .withPoolingOptions(new PoolingOptions() |
| 63 | 63 | .setHeartbeatIntervalSeconds(3)) |
| 64 | 64 | .build()); |
| @@ -113,8 +113,8 @@ | ||
| 113 | 113 | @Test(groups = "long") |
| 114 | 114 | public void should_not_send_heartbeat_when_disabled() throws InterruptedException { |
| 115 | 115 | Cluster cluster = register(Cluster.builder() |
| 116 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 117 | - .withPort(ccm.getBinaryPort()) | |
| 116 | + .addContactPoints(getContactPoints().get(0)) | |
| 117 | + .withPort(ccm().getBinaryPort()) | |
| 118 | 118 | .withPoolingOptions(new PoolingOptions() |
| 119 | 119 | .setHeartbeatIntervalSeconds(0)) |
| 120 | 120 | .build()); |
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +0 −1
| @@ -432,7 +432,6 @@ | ||
| 432 | 432 | // On returning of the connection, should detect that there are no available streams and trash it. |
| 433 | 433 | assertThat(pool.trash).hasSize(0); |
| 434 | 434 | pool.returnConnection(extra1); |
| 435 | - assertThat(pool.connections).hasSize(1); | |
| 436 | 435 | assertThat(pool.trash).hasSize(1); |
| 437 | 436 | } finally { |
| 438 | 437 | completeRequests(requests); |
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +11 −12
| @@ -23,7 +23,6 @@ | ||
| 23 | 23 | import org.testng.annotations.Test; |
| 24 | 24 | |
| 25 | 25 | import java.nio.ByteBuffer; |
| 26 | -import java.util.Collection; | |
| 27 | 26 | import java.util.List; |
| 28 | 27 | |
| 29 | 28 | import static com.datastax.driver.core.Assertions.assertThat; |
| @@ -49,7 +48,7 @@ | ||
| 49 | 48 | }); |
| 50 | 49 | |
| 51 | 50 | @Override |
| 52 | - public Collection<String> createTestFixtures() { | |
| 51 | + public void onTestContextInitialized() { | |
| 53 | 52 | String createTable = "CREATE TABLE indexing (" |
| 54 | 53 | + "id int primary key," |
| 55 | 54 | + "map_values map<text, int>," |
| @@ -58,21 +57,21 @@ | ||
| 58 | 57 | + "text_column text" |
| 59 | 58 | + |
| 60 | 59 | // Frozen collections was introduced only in C* 2.1.3 |
| 61 | - (ccm.getVersion().compareTo(VersionNumber.parse("2.1.3")) >= 0 | |
| 60 | + (ccm().getVersion().compareTo(VersionNumber.parse("2.1.3")) >= 0 | |
| 62 | 61 | ? |
| 63 | 62 | ", map_full frozen<map<text, int>>," |
| 64 | 63 | + "set_full frozen<set<text>>," |
| 65 | 64 | + "list_full frozen<list<text>>);" |
| 66 | 65 | : |
| 67 | 66 | ")"); |
| 68 | - return ImmutableList.of(createTable); | |
| 67 | + execute(createTable); | |
| 69 | 68 | } |
| 70 | 69 | |
| 71 | 70 | @Test( |
| 72 | 71 | groups = "short") |
| 73 | 72 | public void should_not_flag_text_column_index_type() { |
| 74 | 73 | String createValuesIndex = String.format("CREATE INDEX text_column_index ON %s.indexing (text_column);", keyspace); |
| 75 | - session.execute(createValuesIndex); | |
| 74 | + session().execute(createValuesIndex); | |
| 76 | 75 | IndexMetadata index = getIndexForColumn("text_column"); |
| 77 | 76 | assertThat(index).hasName("text_column_index") |
| 78 | 77 | .isNotKeys() |
| @@ -88,7 +87,7 @@ | ||
| 88 | 87 | major = 2.1) |
| 89 | 88 | public void should_not_flag_map_index_type() { |
| 90 | 89 | String createValuesIndex = String.format("CREATE INDEX map_values_index ON %s.indexing (map_values);", keyspace); |
| 91 | - session.execute(createValuesIndex); | |
| 90 | + session().execute(createValuesIndex); | |
| 92 | 91 | IndexMetadata index = getIndexForColumn("map_values"); |
| 93 | 92 | assertThat(index).hasName("map_values_index") |
| 94 | 93 | .isNotKeys() |
| @@ -104,7 +103,7 @@ | ||
| 104 | 103 | major = 2.1) |
| 105 | 104 | public void should_flag_map_index_type_as_keys() { |
| 106 | 105 | String createKeysIndex = String.format("CREATE INDEX map_keys_index ON %s.indexing (KEYS(map_keys));", keyspace); |
| 107 | - session.execute(createKeysIndex); | |
| 106 | + session().execute(createKeysIndex); | |
| 108 | 107 | IndexMetadata index = getIndexForColumn("map_keys"); |
| 109 | 108 | assertThat(index).hasName("map_keys_index") |
| 110 | 109 | .isKeys() |
| @@ -121,7 +120,7 @@ | ||
| 121 | 120 | minor = 3) |
| 122 | 121 | public void should_flag_map_index_type_as_full() { |
| 123 | 122 | String createFullIndex = String.format("CREATE INDEX map_full_index ON %s.indexing (FULL(map_full));", keyspace); |
| 124 | - session.execute(createFullIndex); | |
| 123 | + session().execute(createFullIndex); | |
| 125 | 124 | IndexMetadata index = getIndexForColumn("map_full"); |
| 126 | 125 | assertThat(index).hasName("map_full_index") |
| 127 | 126 | .isNotKeys() |
| @@ -138,7 +137,7 @@ | ||
| 138 | 137 | minor = 3) |
| 139 | 138 | public void should_flag_set_index_type_as_full() { |
| 140 | 139 | String createFullIndex = String.format("CREATE INDEX set_full_index ON %s.indexing (FULL(set_full));", keyspace); |
| 141 | - session.execute(createFullIndex); | |
| 140 | + session().execute(createFullIndex); | |
| 142 | 141 | IndexMetadata index = getIndexForColumn("set_full"); |
| 143 | 142 | assertThat(index).hasName("set_full_index") |
| 144 | 143 | .isNotKeys() |
| @@ -155,7 +154,7 @@ | ||
| 155 | 154 | minor = 3) |
| 156 | 155 | public void should_flag_list_index_type_as_full() { |
| 157 | 156 | String createFullIndex = String.format("CREATE INDEX list_full_index ON %s.indexing (FULL(list_full));", keyspace); |
| 158 | - session.execute(createFullIndex); | |
| 157 | + session().execute(createFullIndex); | |
| 159 | 158 | IndexMetadata index = getIndexForColumn("list_full"); |
| 160 | 159 | assertThat(index).hasName("list_full_index") |
| 161 | 160 | .isNotKeys() |
| @@ -171,7 +170,7 @@ | ||
| 171 | 170 | major = 3.0) |
| 172 | 171 | public void should_flag_map_index_type_as_entries() { |
| 173 | 172 | String createEntriesIndex = String.format("CREATE INDEX map_entries_index ON %s.indexing (ENTRIES(map_entries));", keyspace); |
| 174 | - session.execute(createEntriesIndex); | |
| 173 | + session().execute(createEntriesIndex); | |
| 175 | 174 | IndexMetadata index = getIndexForColumn("map_entries"); |
| 176 | 175 | assertThat(index).hasName("map_entries_index") |
| 177 | 176 | .isNotKeys() |
| @@ -265,7 +264,7 @@ | ||
| 265 | 264 | } |
| 266 | 265 | |
| 267 | 266 | private TableMetadata getTable(String name) { |
| 268 | - return cluster.getMetadata().getKeyspace(keyspace).getTable(name); | |
| 267 | + return cluster().getMetadata().getKeyspace(keyspace).getTable(name); | |
| 269 | 268 | } |
| 270 | 269 | |
| 271 | 270 | } |
modified driver-core/src/test/java/com/datastax/driver/core/LargeDataTest.java +32 −32
| @@ -43,11 +43,11 @@ | ||
| 43 | 43 | private void testWideRows(int key) throws Throwable { |
| 44 | 44 | // Write data |
| 45 | 45 | for (int i = 0; i < 1000000; ++i) { |
| 46 | - session.execute(insertInto("wide_rows").value("k", key).value("i", i).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 46 | + session().execute(insertInto("wide_rows").value("k", key).value("i", i).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // Read data |
| 50 | - ResultSet rs = session.execute(select("i").from("wide_rows").where(eq("k", key))); | |
| 50 | + ResultSet rs = session().execute(select("i").from("wide_rows").where(eq("k", key))); | |
| 51 | 51 | |
| 52 | 52 | // Verify data |
| 53 | 53 | int i = 0; |
| @@ -68,10 +68,10 @@ | ||
| 68 | 68 | for (int i = 0; i < 10000; ++i) { |
| 69 | 69 | q = q.add(insertInto("wide_batch_rows").value("k", key).value("i", i)); |
| 70 | 70 | } |
| 71 | - session.execute(q.setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 71 | + session().execute(q.setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 72 | 72 | |
| 73 | 73 | // Read data |
| 74 | - ResultSet rs = session.execute(select("i").from("wide_batch_rows").where(eq("k", key))); | |
| 74 | + ResultSet rs = session().execute(select("i").from("wide_batch_rows").where(eq("k", key))); | |
| 75 | 75 | |
| 76 | 76 | // Verify data |
| 77 | 77 | int i = 0; |
| @@ -94,11 +94,11 @@ | ||
| 94 | 94 | |
| 95 | 95 | // Write data |
| 96 | 96 | for (int i = 0; i < 1000000; ++i) { |
| 97 | - session.execute(insertInto("wide_byte_rows").value("k", key).value("i", bb).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 97 | + session().execute(insertInto("wide_byte_rows").value("k", key).value("i", bb).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Read data |
| 101 | - ResultSet rs = session.execute(select("i").from("wide_byte_rows").where(eq("k", key))); | |
| 101 | + ResultSet rs = session().execute(select("i").from("wide_byte_rows").where(eq("k", key))); | |
| 102 | 102 | |
| 103 | 103 | // Verify data |
| 104 | 104 | for (Row row : rs) { |
| @@ -119,10 +119,10 @@ | ||
| 119 | 119 | // Create ultra-long text |
| 120 | 120 | b.append(i); |
| 121 | 121 | } |
| 122 | - session.execute(insertInto("large_text").value("k", key).value("txt", b.toString()).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 122 | + session().execute(insertInto("large_text").value("k", key).value("txt", b.toString()).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 123 | 123 | |
| 124 | 124 | // Read data |
| 125 | - Row row = session.execute(select().all().from("large_text").where(eq("k", key))).one(); | |
| 125 | + Row row = session().execute(select().all().from("large_text").where(eq("k", key))).one(); | |
| 126 | 126 | |
| 127 | 127 | // Verify data |
| 128 | 128 | assertTrue(b.toString().equals(row.getString("txt"))); |
| @@ -166,10 +166,10 @@ | ||
| 166 | 166 | for (int i = 0; i < 330; ++i) { |
| 167 | 167 | insertStatement = insertStatement.value(createColumnName(i), i); |
| 168 | 168 | } |
| 169 | - session.execute(insertStatement.setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 169 | + session().execute(insertStatement.setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 170 | 170 | |
| 171 | 171 | // Read data |
| 172 | - Row row = session.execute(select().all().from("wide_table").where(eq("k", key))).one(); | |
| 172 | + Row row = session().execute(select().all().from("wide_table").where(eq("k", key))).one(); | |
| 173 | 173 | |
| 174 | 174 | // Verify data |
| 175 | 175 | for (int i = 0; i < 330; ++i) { |
| @@ -186,9 +186,9 @@ | ||
| 186 | 186 | @Test(groups = "stress") |
| 187 | 187 | @CassandraVersion(major = 2.0, minor = 0, description = "< 2.0 is skipped as 1.2 does not handle reading wide rows well.") |
| 188 | 188 | public void wideRows() throws Throwable { |
| 189 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 190 | - session.execute("USE large_data"); | |
| 191 | - session.execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_rows")); | |
| 189 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 190 | + session().execute("USE large_data"); | |
| 191 | + session().execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_rows")); | |
| 192 | 192 | testWideRows(0); |
| 193 | 193 | } |
| 194 | 194 | |
| @@ -200,9 +200,9 @@ | ||
| 200 | 200 | @Test(groups = "stress") |
| 201 | 201 | @CassandraVersion(major = 2.0, minor = 0, description = "< 2.0 is skipped as 1.2 does not handle large batches well.") |
| 202 | 202 | public void wideBatchRows() throws Throwable { |
| 203 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 204 | - session.execute("USE large_data"); | |
| 205 | - session.execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_batch_rows")); | |
| 203 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 204 | + session().execute("USE large_data"); | |
| 205 | + session().execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_batch_rows")); | |
| 206 | 206 | testWideBatchRows(0); |
| 207 | 207 | } |
| 208 | 208 | |
| @@ -213,9 +213,9 @@ | ||
| 213 | 213 | */ |
| 214 | 214 | @Test(groups = "stress") |
| 215 | 215 | public void wideByteRows() throws Throwable { |
| 216 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 217 | - session.execute("USE large_data"); | |
| 218 | - session.execute(String.format("CREATE TABLE %s (k INT, i BLOB, PRIMARY KEY(k, i))", "wide_byte_rows")); | |
| 216 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 217 | + session().execute("USE large_data"); | |
| 218 | + session().execute(String.format("CREATE TABLE %s (k INT, i BLOB, PRIMARY KEY(k, i))", "wide_byte_rows")); | |
| 219 | 219 | testByteRows(0); |
| 220 | 220 | } |
| 221 | 221 | |
| @@ -226,9 +226,9 @@ | ||
| 226 | 226 | */ |
| 227 | 227 | @Test(groups = "stress") |
| 228 | 228 | public void largeText() throws Throwable { |
| 229 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 230 | - session.execute("USE large_data"); | |
| 231 | - session.execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, txt text)", "large_text")); | |
| 229 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 230 | + session().execute("USE large_data"); | |
| 231 | + session().execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, txt text)", "large_text")); | |
| 232 | 232 | testLargeText(0); |
| 233 | 233 | } |
| 234 | 234 | |
| @@ -239,8 +239,8 @@ | ||
| 239 | 239 | */ |
| 240 | 240 | @Test(groups = "stress") |
| 241 | 241 | public void wideTable() throws Throwable { |
| 242 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 243 | - session.execute("USE large_data"); | |
| 242 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 1)); | |
| 243 | + session().execute("USE large_data"); | |
| 244 | 244 | // Create the extra wide table definition |
| 245 | 245 | StringBuilder tableDeclaration = new StringBuilder(); |
| 246 | 246 | tableDeclaration.append("CREATE TABLE wide_table ("); |
| @@ -249,7 +249,7 @@ | ||
| 249 | 249 | tableDeclaration.append(String.format(", %s INT", createColumnName(i))); |
| 250 | 250 | } |
| 251 | 251 | tableDeclaration.append(')'); |
| 252 | - session.execute(tableDeclaration.toString()); | |
| 252 | + session().execute(tableDeclaration.toString()); | |
| 253 | 253 | testWideTable(0); |
| 254 | 254 | } |
| 255 | 255 | |
| @@ -261,12 +261,12 @@ | ||
| 261 | 261 | @Test(groups = "duration") |
| 262 | 262 | @CCMConfig(numberOfNodes = 3) |
| 263 | 263 | public void mixedDurationTest() throws Throwable { |
| 264 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 3)); | |
| 265 | - session.execute("USE large_data"); | |
| 266 | - session.execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_rows")); | |
| 267 | - session.execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_batch_rows")); | |
| 268 | - session.execute(String.format("CREATE TABLE %s (k INT, i BLOB, PRIMARY KEY(k, i))", "wide_byte_rows")); | |
| 269 | - session.execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, txt text)", "large_text")); | |
| 264 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, "large_data", 3)); | |
| 265 | + session().execute("USE large_data"); | |
| 266 | + session().execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_rows")); | |
| 267 | + session().execute(String.format("CREATE TABLE %s (k INT, i INT, PRIMARY KEY(k, i))", "wide_batch_rows")); | |
| 268 | + session().execute(String.format("CREATE TABLE %s (k INT, i BLOB, PRIMARY KEY(k, i))", "wide_byte_rows")); | |
| 269 | + session().execute(String.format("CREATE TABLE %s (k int PRIMARY KEY, txt text)", "large_text")); | |
| 270 | 270 | // Create the extra wide table definition |
| 271 | 271 | StringBuilder tableDeclaration = new StringBuilder(); |
| 272 | 272 | tableDeclaration.append("CREATE TABLE wide_table ("); |
| @@ -275,7 +275,7 @@ | ||
| 275 | 275 | tableDeclaration.append(String.format(", %s INT", createColumnName(i))); |
| 276 | 276 | } |
| 277 | 277 | tableDeclaration.append(')'); |
| 278 | - session.execute(tableDeclaration.toString()); | |
| 278 | + session().execute(tableDeclaration.toString()); | |
| 279 | 279 | for (int i = 0; i < 10; ++i) { |
| 280 | 280 | switch ((int) (Math.random() * 5)) { |
| 281 | 281 | case 0: |
modified driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyBootstrapTest.java +8 −8
| @@ -48,8 +48,8 @@ | ||
| 48 | 48 | HistoryPolicy policy = new HistoryPolicy(new RoundRobinPolicy()); |
| 49 | 49 | |
| 50 | 50 | Cluster cluster = register(Cluster.builder() |
| 51 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 52 | - .withPort(ccm.getBinaryPort()) | |
| 51 | + .addContactPoints(getContactPoints()) | |
| 52 | + .withPort(ccm().getBinaryPort()) | |
| 53 | 53 | .withLoadBalancingPolicy(policy) |
| 54 | 54 | .build()); |
| 55 | 55 | |
| @@ -85,13 +85,13 @@ | ||
| 85 | 85 | nodeToStop = (nodeToStop == 1) ? 2 : 1; // switch nodes at each try |
| 86 | 86 | int activeNode = nodeToStop == 2 ? 1 : 2; |
| 87 | 87 | |
| 88 | - ccm.stop(nodeToStop); | |
| 89 | - ccm.waitForDown(nodeToStop); | |
| 88 | + ccm().stop(nodeToStop); | |
| 89 | + ccm().waitForDown(nodeToStop); | |
| 90 | 90 | |
| 91 | 91 | HistoryPolicy policy = new HistoryPolicy(new RoundRobinPolicy()); |
| 92 | 92 | Cluster cluster = register(Cluster.builder() |
| 93 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 94 | - .withPort(ccm.getBinaryPort()) | |
| 93 | + .addContactPoints(getContactPoints()) | |
| 94 | + .withPort(ccm().getBinaryPort()) | |
| 95 | 95 | .withLoadBalancingPolicy(policy) |
| 96 | 96 | .build()); |
| 97 | 97 | |
| @@ -114,8 +114,8 @@ | ||
| 114 | 114 | |
| 115 | 115 | cluster.close(); |
| 116 | 116 | |
| 117 | - ccm.start(nodeToStop); | |
| 118 | - ccm.waitForUp(nodeToStop); | |
| 117 | + ccm().start(nodeToStop); | |
| 118 | + ccm().waitForUp(nodeToStop); | |
| 119 | 119 | } |
| 120 | 120 | } |
| 121 | 121 |
modified driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyRefreshTest.java +5 −5
| @@ -82,20 +82,20 @@ | ||
| 82 | 82 | public void refreshTest() throws Throwable { |
| 83 | 83 | // Ugly |
| 84 | 84 | Host[] hosts = new Host[2]; |
| 85 | - for (Host h : cluster.getMetadata().getAllHosts()) { | |
| 85 | + for (Host h : cluster().getMetadata().getAllHosts()) { | |
| 86 | 86 | if (h.getAddress().equals(InetAddress.getByName(TestUtils.IP_PREFIX + '1'))) |
| 87 | 87 | hosts[0] = h; |
| 88 | 88 | else |
| 89 | 89 | hosts[1] = h; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - assertTrue(session.getState().getConnectedHosts().contains(hosts[0]), "Connected hosts: " + session.getState().getConnectedHosts()); | |
| 93 | - assertTrue(!session.getState().getConnectedHosts().contains(hosts[1]), "Connected hosts: " + session.getState().getConnectedHosts()); | |
| 92 | + assertTrue(session().getState().getConnectedHosts().contains(hosts[0]), "Connected hosts: " + session().getState().getConnectedHosts()); | |
| 93 | + assertTrue(!session().getState().getConnectedHosts().contains(hosts[1]), "Connected hosts: " + session().getState().getConnectedHosts()); | |
| 94 | 94 | |
| 95 | 95 | policy.changeTheHost(hosts[1]); |
| 96 | 96 | |
| 97 | - assertTrue(!session.getState().getConnectedHosts().contains(hosts[0]), "Connected hosts: " + session.getState().getConnectedHosts()); | |
| 98 | - assertTrue(session.getState().getConnectedHosts().contains(hosts[1]), "Connected hosts: " + session.getState().getConnectedHosts()); | |
| 97 | + assertTrue(!session().getState().getConnectedHosts().contains(hosts[0]), "Connected hosts: " + session().getState().getConnectedHosts()); | |
| 98 | + assertTrue(session().getState().getConnectedHosts().contains(hosts[1]), "Connected hosts: " + session().getState().getConnectedHosts()); | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | @SuppressWarnings("unused") |
modified driver-core/src/test/java/com/datastax/driver/core/MetadataTest.java +8 −8
| @@ -23,7 +23,7 @@ | ||
| 23 | 23 | import static com.datastax.driver.core.Assertions.assertThat; |
| 24 | 24 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 25 | 25 | import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; |
| 26 | -import static com.datastax.driver.core.TestUtils.waitFor; | |
| 26 | +import static com.datastax.driver.core.TestUtils.waitForUp; | |
| 27 | 27 | |
| 28 | 28 | @CreateCCM(PER_METHOD) |
| 29 | 29 | public class MetadataTest extends CCMTestsSupport { |
| @@ -55,8 +55,8 @@ | ||
| 55 | 55 | @CCMConfig(numberOfNodes = 3, dirtiesContext = true, createCluster = false) |
| 56 | 56 | public void should_update_metadata_on_topology_change() { |
| 57 | 57 | Cluster cluster = register(Cluster.builder() |
| 58 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 59 | - .withPort(ccm.getBinaryPort()) | |
| 58 | + .addContactPoints(getContactPoints().get(0)) | |
| 59 | + .withPort(ccm().getBinaryPort()) | |
| 60 | 60 | .withQueryOptions(nonDebouncingQueryOptions()) |
| 61 | 61 | .build()); |
| 62 | 62 | Session session = cluster.connect(); |
| @@ -73,8 +73,8 @@ | ||
| 73 | 73 | Host host3 = TestUtils.findHost(cluster, 3); |
| 74 | 74 | Token host3Token = tokensForHost.get(host3); |
| 75 | 75 | |
| 76 | - ccm.decommission(3); | |
| 77 | - ccm.remove(3); | |
| 76 | + ccm().decommission(3); | |
| 77 | + ccm().remove(3); | |
| 78 | 78 | |
| 79 | 79 | // Ensure that the token ranges were updated, there should only be 2 ranges now. |
| 80 | 80 | assertThat(metadata.getTokenRanges()).hasSize(2); |
| @@ -87,9 +87,9 @@ | ||
| 87 | 87 | assertThat(cluster).hasValidTokenRanges(); |
| 88 | 88 | |
| 89 | 89 | // Add an additional node. |
| 90 | - ccm.add(4); | |
| 91 | - ccm.start(4); | |
| 92 | - waitFor(TestUtils.IP_PREFIX + '4', cluster); | |
| 90 | + ccm().add(4); | |
| 91 | + ccm().start(4); | |
| 92 | + waitForUp(TestUtils.IP_PREFIX + '4', cluster); | |
| 93 | 93 | |
| 94 | 94 | // Ensure that the token ranges were updated, there should only be 3 ranges now. |
| 95 | 95 | assertThat(metadata.getTokenRanges()).hasSize(3); |
modified driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +5 −8
| @@ -18,11 +18,8 @@ | ||
| 18 | 18 | import com.datastax.driver.core.Metrics.Errors; |
| 19 | 19 | import com.datastax.driver.core.policies.RetryPolicy; |
| 20 | 20 | import com.datastax.driver.core.policies.RetryPolicy.RetryDecision; |
| 21 | -import com.google.common.collect.Lists; | |
| 22 | 21 | import org.testng.annotations.Test; |
| 23 | 22 | |
| 24 | -import java.util.Collection; | |
| 25 | - | |
| 26 | 23 | import static org.testng.Assert.assertEquals; |
| 27 | 24 | |
| 28 | 25 | public class MetricsTest extends CCMTestsSupport { |
| @@ -49,8 +46,8 @@ | ||
| 49 | 46 | } |
| 50 | 47 | |
| 51 | 48 | @Override |
| 52 | - public Collection<String> createTestFixtures() { | |
| 53 | - return Lists.newArrayList("CREATE TABLE test (k int primary key, v int)", | |
| 49 | + public void onTestContextInitialized() { | |
| 50 | + execute("CREATE TABLE test (k int primary key, v int)", | |
| 54 | 51 | "INSERT INTO test (k, v) VALUES (1, 1)"); |
| 55 | 52 | } |
| 56 | 53 | |
| @@ -60,15 +57,15 @@ | ||
| 60 | 57 | |
| 61 | 58 | // We only have one node, this will throw an unavailable exception |
| 62 | 59 | Statement statement = new SimpleStatement("SELECT v FROM test WHERE k = 1").setConsistencyLevel(ConsistencyLevel.TWO); |
| 63 | - session.execute(statement); | |
| 60 | + session().execute(statement); | |
| 64 | 61 | |
| 65 | - Errors errors = cluster.getMetrics().getErrorMetrics(); | |
| 62 | + Errors errors = cluster().getMetrics().getErrorMetrics(); | |
| 66 | 63 | assertEquals(errors.getUnavailables().getCount(), 1); |
| 67 | 64 | assertEquals(errors.getRetries().getCount(), 1); |
| 68 | 65 | assertEquals(errors.getRetriesOnUnavailable().getCount(), 1); |
| 69 | 66 | |
| 70 | 67 | retryDecision = RetryDecision.ignore(); |
| 71 | - session.execute(statement); | |
| 68 | + session().execute(statement); | |
| 72 | 69 | |
| 73 | 70 | assertEquals(errors.getUnavailables().getCount(), 2); |
| 74 | 71 | assertEquals(errors.getIgnores().getCount(), 1); |
modified driver-core/src/test/java/com/datastax/driver/core/MissingRpcAddressTest.java +5 −5
| @@ -42,22 +42,22 @@ | ||
| 42 | 42 | deleteNode2RpcAddressFromNode1(); |
| 43 | 43 | // Use only one contact point to make sure that the control connection is on node1 |
| 44 | 44 | Cluster cluster = register(Cluster.builder() |
| 45 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 46 | - .withPort(ccm.getBinaryPort()) | |
| 45 | + .addContactPoints(getContactPoints().get(0)) | |
| 46 | + .withPort(ccm().getBinaryPort()) | |
| 47 | 47 | .build()); |
| 48 | 48 | cluster.connect(); |
| 49 | 49 | |
| 50 | 50 | // Since node2's RPC address is unknown on our control host, it should have been ignored |
| 51 | 51 | assertEquals(cluster.getMetrics().getConnectedToHosts().getValue().intValue(), 1); |
| 52 | - assertNull(cluster.getMetadata().getHost(getInitialContactPoints().get(1))); | |
| 52 | + assertNull(cluster.getMetadata().getHost(getContactPointsWithPorts().get(1))); | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | // Artificially modify the system tables to simulate the missing rpc_address. |
| 56 | 56 | private void deleteNode2RpcAddressFromNode1() throws Exception { |
| 57 | - InetSocketAddress firstHost = ccm.addressOfNode(1); | |
| 57 | + InetSocketAddress firstHost = ccm().addressOfNode(1); | |
| 58 | 58 | Cluster cluster = register(Cluster.builder() |
| 59 | 59 | .addContactPointsWithPorts(singleton(firstHost)) |
| 60 | - .withPort(ccm.getBinaryPort()) | |
| 60 | + .withPort(ccm().getBinaryPort()) | |
| 61 | 61 | // ensure we will only connect to node1 |
| 62 | 62 | .withLoadBalancingPolicy(new WhiteListPolicy(Policies.defaultLoadBalancingPolicy(), |
| 63 | 63 | Lists.newArrayList(firstHost))) |
modified driver-core/src/test/java/com/datastax/driver/core/NettyOptionsTest.java +2 −2
| @@ -65,8 +65,8 @@ | ||
| 65 | 65 | } |
| 66 | 66 | }).when(nettyOptions).afterChannelInitialized(any(SocketChannel.class)); |
| 67 | 67 | Cluster cluster = register(Cluster.builder() |
| 68 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 69 | - .withPort(ccm.getBinaryPort()) | |
| 68 | + .addContactPoints(getContactPoints().get(0)) | |
| 69 | + .withPort(ccm().getBinaryPort()) | |
| 70 | 70 | .withPoolingOptions(new PoolingOptions() |
| 71 | 71 | .setConnectionsPerHost(HostDistance.LOCAL, coreConnections, coreConnections) |
| 72 | 72 | ) |
modified driver-core/src/test/java/com/datastax/driver/core/NodeListRefreshDebouncerTest.java +6 −11
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import org.testng.annotations.AfterMethod; | |
| 19 | 18 | import org.testng.annotations.BeforeMethod; |
| 20 | 19 | import org.testng.annotations.Test; |
| 21 | 20 | |
| @@ -51,10 +50,11 @@ | ||
| 51 | 50 | queryOptions.setMaxPendingRefreshNodeListRequests(5); |
| 52 | 51 | queryOptions.setRefreshSchemaIntervalMillis(0); |
| 53 | 52 | // Create a separate cluster that will receive the schema events on its control connection. |
| 54 | - cluster2 = Cluster.builder() | |
| 55 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 53 | + cluster2 = register(Cluster.builder() | |
| 54 | + .addContactPoints(getContactPoints()) | |
| 55 | + .withPort(ccm().getBinaryPort()) | |
| 56 | 56 | .withQueryOptions(queryOptions) |
| 57 | - .build(); | |
| 57 | + .build()); | |
| 58 | 58 | session2 = cluster2.connect(); |
| 59 | 59 | |
| 60 | 60 | // Create a spy of the Cluster's control connection and replace it with the spy. |
| @@ -63,11 +63,6 @@ | ||
| 63 | 63 | reset(controlConnection); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - @AfterMethod(groups = "short") | |
| 67 | - public void teardown() { | |
| 68 | - cluster2.close(); | |
| 69 | - } | |
| 70 | - | |
| 71 | 66 | /** |
| 72 | 67 | * Ensures that when a keyspace is created that refresh node list request |
| 73 | 68 | * is debounced and processed within {@link QueryOptions#getRefreshSchemaIntervalMillis()} |
| @@ -79,7 +74,7 @@ | ||
| 79 | 74 | @Test(groups = "short") |
| 80 | 75 | public void should_debounce_refresh_when_keyspace_created() { |
| 81 | 76 | String keyspace = "sdrwkc"; |
| 82 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 77 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 83 | 78 | keyspaces.add(keyspace); |
| 84 | 79 | |
| 85 | 80 | verify(controlConnection, timeout(DEBOUNCE_TIME + queryOptions.getRefreshSchemaIntervalMillis())).refreshNodeListAndTokenMap(); |
| @@ -100,7 +95,7 @@ | ||
| 100 | 95 | String prefix = "srwmprr"; |
| 101 | 96 | for (int i = 0; i < 5; i++) { |
| 102 | 97 | String keyspace = prefix + i; |
| 103 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 98 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 104 | 99 | keyspaces.add(keyspace); |
| 105 | 100 | } |
| 106 | 101 |
modified driver-core/src/test/java/com/datastax/driver/core/NodeRefreshDebouncerTest.java +6 −6
| @@ -43,17 +43,17 @@ | ||
| 43 | 43 | int refreshNodeInterval = 30000; |
| 44 | 44 | QueryOptions queryOptions = new QueryOptions().setRefreshNodeIntervalMillis(refreshNodeInterval); |
| 45 | 45 | Cluster cluster = register(Cluster.builder() |
| 46 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 47 | - .withPort(ccm.getBinaryPort()) | |
| 46 | + .addContactPoints(getContactPoints().get(0)) | |
| 47 | + .withPort(ccm().getBinaryPort()) | |
| 48 | 48 | .withQueryOptions(queryOptions) |
| 49 | 49 | .build()); |
| 50 | 50 | cluster.connect(); |
| 51 | 51 | Host.StateListener listener = mock(Host.StateListener.class); |
| 52 | 52 | cluster.register(listener); |
| 53 | - ccm.add(2); | |
| 54 | - ccm.start(2); | |
| 55 | - ccm.stop(2); | |
| 56 | - ccm.start(2); | |
| 53 | + ccm().add(2); | |
| 54 | + ccm().start(2); | |
| 55 | + ccm().stop(2); | |
| 56 | + ccm().start(2); | |
| 57 | 57 | |
| 58 | 58 | ArgumentCaptor<Host> captor = forClass(Host.class); |
| 59 | 59 |
modified driver-core/src/test/java/com/datastax/driver/core/PagingStateTest.java +16 −23
| @@ -19,11 +19,8 @@ | ||
| 19 | 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | 20 | import org.slf4j.Logger; |
| 21 | 21 | import org.slf4j.LoggerFactory; |
| 22 | -import org.testng.annotations.BeforeClass; | |
| 23 | 22 | import org.testng.annotations.Test; |
| 24 | 23 | |
| 25 | -import java.util.Collection; | |
| 26 | -import java.util.Collections; | |
| 27 | 24 | import java.util.Iterator; |
| 28 | 25 | |
| 29 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -36,14 +33,10 @@ | ||
| 36 | 33 | public static final String KEY = "paging_test"; |
| 37 | 34 | |
| 38 | 35 | @Override |
| 39 | - public Collection<String> createTestFixtures() { | |
| 40 | - return Collections.singletonList("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 41 | - } | |
| 42 | - | |
| 43 | - @BeforeClass(groups = "short") | |
| 44 | - public void initData() { | |
| 36 | + public void onTestContextInitialized() { | |
| 37 | + execute("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 45 | 38 | for (int i = 0; i < 100; i++) { |
| 46 | - session.execute(String.format("INSERT INTO test (k, v) VALUES ('%s', %d)", KEY, i)); | |
| 39 | + execute(String.format("INSERT INTO test (k, v) VALUES ('%s', %d)", KEY, i)); | |
| 47 | 40 | } |
| 48 | 41 | } |
| 49 | 42 | |
| @@ -57,12 +50,12 @@ | ||
| 57 | 50 | @Test(groups = "short") |
| 58 | 51 | public void should_complete_when_using_paging_state() { |
| 59 | 52 | SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 60 | - ResultSet result = session.execute(st.setFetchSize(20)); | |
| 53 | + ResultSet result = session().execute(st.setFetchSize(20)); | |
| 61 | 54 | int pageSize = result.getAvailableWithoutFetching(); |
| 62 | 55 | String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); |
| 63 | 56 | |
| 64 | 57 | st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 65 | - result = session.execute(st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); | |
| 58 | + result = session().execute(st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); | |
| 66 | 59 | |
| 67 | 60 | //We have the result starting from the next page we stopped |
| 68 | 61 | assertThat(result.one().getInt("v")).isEqualTo(pageSize); |
| @@ -87,7 +80,7 @@ | ||
| 87 | 80 | boolean setWithWrongStatement = false; |
| 88 | 81 | |
| 89 | 82 | SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 90 | - ResultSet result = session.execute(st.setFetchSize(20)); | |
| 83 | + ResultSet result = session().execute(st.setFetchSize(20)); | |
| 91 | 84 | |
| 92 | 85 | PagingState savedPagingState = result.getExecutionInfo().getPagingState(); |
| 93 | 86 | byte[] savedPagingStateBuffer = savedPagingState.toBytes(); |
| @@ -129,14 +122,14 @@ | ||
| 129 | 122 | @Test(groups = "short") |
| 130 | 123 | @CassandraVersion(major = 2.0) |
| 131 | 124 | public void should_be_able_to_use_state_with_bound_statement() { |
| 132 | - PreparedStatement prepared = session.prepare("SELECT v from test where k=?"); | |
| 125 | + PreparedStatement prepared = session().prepare("SELECT v from test where k=?"); | |
| 133 | 126 | BoundStatement bs = prepared.bind(KEY); |
| 134 | 127 | |
| 135 | - ResultSet result = session.execute(bs.setFetchSize(20)); | |
| 128 | + ResultSet result = session().execute(bs.setFetchSize(20)); | |
| 136 | 129 | int pageSize = result.getAvailableWithoutFetching(); |
| 137 | 130 | PagingState pagingState = result.getExecutionInfo().getPagingState(); |
| 138 | 131 | |
| 139 | - result = session.execute(bs.setFetchSize(20).setPagingState(pagingState)); | |
| 132 | + result = session().execute(bs.setFetchSize(20).setPagingState(pagingState)); | |
| 140 | 133 | |
| 141 | 134 | //We have the result starting from the next page we stopped |
| 142 | 135 | assertThat(result.one().getInt("v")).isEqualTo(pageSize); |
| @@ -152,14 +145,14 @@ | ||
| 152 | 145 | @Test(groups = "short", expectedExceptions = {PagingStateException.class}) |
| 153 | 146 | @CassandraVersion(major = 2.0) |
| 154 | 147 | public void should_not_be_able_to_use_state_with_different_bound_statement() { |
| 155 | - PreparedStatement prepared = session.prepare("SELECT v from test where k=?"); | |
| 148 | + PreparedStatement prepared = session().prepare("SELECT v from test where k=?"); | |
| 156 | 149 | BoundStatement bs0 = prepared.bind(KEY); |
| 157 | 150 | |
| 158 | - ResultSet result = session.execute(bs0.setFetchSize(20)); | |
| 151 | + ResultSet result = session().execute(bs0.setFetchSize(20)); | |
| 159 | 152 | PagingState pagingState = result.getExecutionInfo().getPagingState(); |
| 160 | 153 | |
| 161 | 154 | BoundStatement bs1 = prepared.bind("different_key"); |
| 162 | - session.execute(bs1.setFetchSize(20).setPagingState(pagingState)); | |
| 155 | + session().execute(bs1.setFetchSize(20).setPagingState(pagingState)); | |
| 163 | 156 | } |
| 164 | 157 | |
| 165 | 158 | /** |
| @@ -172,7 +165,7 @@ | ||
| 172 | 165 | @Test(groups = "short") |
| 173 | 166 | public void should_return_no_rows_when_paged_to_end() { |
| 174 | 167 | SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 175 | - ResultSet result = session.execute(st.setFetchSize(20)); | |
| 168 | + ResultSet result = session().execute(st.setFetchSize(20)); | |
| 176 | 169 | |
| 177 | 170 | // Consume enough of the iterator to cause all the results to be paged in. |
| 178 | 171 | Iterator<Row> rowIt = result.iterator(); |
| @@ -183,7 +176,7 @@ | ||
| 183 | 176 | String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); |
| 184 | 177 | |
| 185 | 178 | st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 186 | - result = session.execute(st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); | |
| 179 | + result = session().execute(st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); | |
| 187 | 180 | |
| 188 | 181 | assertThat(result.one()).isNull(); |
| 189 | 182 | } |
| @@ -220,12 +213,12 @@ | ||
| 220 | 213 | @Test(groups = "short") |
| 221 | 214 | public void should_complete_when_using_unsafe_paging_state() { |
| 222 | 215 | SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 223 | - ResultSet result = session.execute(st.setFetchSize(20)); | |
| 216 | + ResultSet result = session().execute(st.setFetchSize(20)); | |
| 224 | 217 | int pageSize = result.getAvailableWithoutFetching(); |
| 225 | 218 | byte[] savedPagingState = result.getExecutionInfo().getPagingStateUnsafe(); |
| 226 | 219 | |
| 227 | 220 | st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 228 | - result = session.execute(st.setFetchSize(20).setPagingStateUnsafe(savedPagingState)); | |
| 221 | + result = session().execute(st.setFetchSize(20).setPagingStateUnsafe(savedPagingState)); | |
| 229 | 222 | |
| 230 | 223 | //We have the result starting from the next page we stopped |
| 231 | 224 | assertThat(result.one().getInt("v")).isEqualTo(pageSize); |
modified driver-core/src/test/java/com/datastax/driver/core/PoolingOptionsIntegrationTest.java +5 −5
| @@ -58,21 +58,21 @@ | ||
| 58 | 58 | */ |
| 59 | 59 | @Test(groups = "short") |
| 60 | 60 | public void should_be_able_to_use_custom_initialization_executor() { |
| 61 | - cluster.init(); | |
| 61 | + cluster().init(); | |
| 62 | 62 | // Ensure executor used. |
| 63 | 63 | verify(executor, atLeastOnce()).execute(any(Runnable.class)); |
| 64 | 64 | |
| 65 | 65 | // Reset invocation count. |
| 66 | 66 | reset(); |
| 67 | 67 | |
| 68 | - Session session = cluster.connect(); | |
| 68 | + Session session = cluster().connect(); | |
| 69 | 69 | |
| 70 | 70 | // Ensure executor used again to establish core connections. |
| 71 | 71 | verify(executor, atLeastOnce()).execute(any(Runnable.class)); |
| 72 | 72 | |
| 73 | 73 | // Expect core connections + control connection. |
| 74 | - assertThat(cluster.getMetrics().getOpenConnections().getValue()).isEqualTo( | |
| 75 | - TestUtils.numberOfLocalCoreConnections(cluster) + 1); | |
| 74 | + assertThat(cluster().getMetrics().getOpenConnections().getValue()).isEqualTo( | |
| 75 | + TestUtils.numberOfLocalCoreConnections(cluster()) + 1); | |
| 76 | 76 | |
| 77 | 77 | reset(); |
| 78 | 78 | |
| @@ -82,6 +82,6 @@ | ||
| 82 | 82 | verify(executor, atLeastOnce()).execute(any(Runnable.class)); |
| 83 | 83 | |
| 84 | 84 | // Only the control connection should remain. |
| 85 | - assertThat(cluster.getMetrics().getOpenConnections().getValue()).isEqualTo(1); | |
| 85 | + assertThat(cluster().getMetrics().getOpenConnections().getValue()).isEqualTo(1); | |
| 86 | 86 | } |
| 87 | 87 | } |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +64 −58
| @@ -21,7 +21,10 @@ | ||
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | 22 | |
| 23 | 23 | import java.net.InetAddress; |
| 24 | -import java.util.*; | |
| 24 | +import java.util.ArrayList; | |
| 25 | +import java.util.List; | |
| 26 | +import java.util.Map; | |
| 27 | +import java.util.Set; | |
| 25 | 28 | |
| 26 | 29 | import static com.datastax.driver.core.TestUtils.*; |
| 27 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -47,8 +50,11 @@ | ||
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | @Override |
| 50 | - public Collection<String> createTestFixtures() { | |
| 53 | + public void onTestContextInitialized() { | |
| 54 | + execute(createTestFixtures()); | |
| 55 | + } | |
| 51 | 56 | |
| 57 | + private List<String> createTestFixtures() { | |
| 52 | 58 | List<String> defs = new ArrayList<String>(4); |
| 53 | 59 | |
| 54 | 60 | StringBuilder sb = new StringBuilder(); |
| @@ -101,7 +107,6 @@ | ||
| 101 | 107 | |
| 102 | 108 | defs.add(String.format("CREATE TABLE %s (k text PRIMARY KEY, i int)", SIMPLE_TABLE)); |
| 103 | 109 | defs.add(String.format("CREATE TABLE %s (k text PRIMARY KEY, v text)", SIMPLE_TABLE2)); |
| 104 | - | |
| 105 | 110 | return defs; |
| 106 | 111 | } |
| 107 | 112 | |
| @@ -114,11 +119,11 @@ | ||
| 114 | 119 | continue; |
| 115 | 120 | |
| 116 | 121 | String name = "c_" + type; |
| 117 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name)); | |
| 122 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name)); | |
| 118 | 123 | BoundStatement bs = ps.bind(); |
| 119 | - session.execute(setBoundValue(bs, name, type, getFixedValue(type))); | |
| 124 | + session().execute(setBoundValue(bs, name, type, getFixedValue(type))); | |
| 120 | 125 | |
| 121 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one(); | |
| 126 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one(); | |
| 122 | 127 | assertEquals(getValue(row, name, type), getFixedValue(type), "For type " + type); |
| 123 | 128 | } |
| 124 | 129 | } |
| @@ -135,11 +140,11 @@ | ||
| 135 | 140 | continue; |
| 136 | 141 | |
| 137 | 142 | String name = "c_" + type; |
| 138 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name)); | |
| 143 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name)); | |
| 139 | 144 | BoundStatement bs = ps.bind(); |
| 140 | - session.execute(setBoundValue(bs, name, type, getFixedValue2(type))); | |
| 145 | + session().execute(setBoundValue(bs, name, type, getFixedValue2(type))); | |
| 141 | 146 | |
| 142 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one(); | |
| 147 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one(); | |
| 143 | 148 | assertEquals(getValue(row, name, type), getFixedValue2(type), "For type " + type); |
| 144 | 149 | } |
| 145 | 150 | } |
| @@ -156,12 +161,12 @@ | ||
| 156 | 161 | String name = "c_list_" + rawType; |
| 157 | 162 | DataType type = DataType.list(rawType); |
| 158 | 163 | List<Object> value = (List<Object>) getFixedValue(type); |
| 159 | - ; | |
| 160 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); | |
| 164 | + | |
| 165 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); | |
| 161 | 166 | BoundStatement bs = ps.bind(); |
| 162 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 167 | + session().execute(setBoundValue(bs, name, type, value)); | |
| 163 | 168 | |
| 164 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one(); | |
| 169 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one(); | |
| 165 | 170 | assertEquals(getValue(row, name, type), value, "For type " + type); |
| 166 | 171 | } |
| 167 | 172 | } |
| @@ -181,12 +186,12 @@ | ||
| 181 | 186 | String name = "c_list_" + rawType; |
| 182 | 187 | DataType type = DataType.list(rawType); |
| 183 | 188 | List<Object> value = (List<Object>) getFixedValue2(type); |
| 184 | - ; | |
| 185 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); | |
| 189 | + | |
| 190 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); | |
| 186 | 191 | BoundStatement bs = ps.bind(); |
| 187 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 192 | + session().execute(setBoundValue(bs, name, type, value)); | |
| 188 | 193 | |
| 189 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one(); | |
| 194 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one(); | |
| 190 | 195 | assertEquals(getValue(row, name, type), value, "For type " + type); |
| 191 | 196 | } |
| 192 | 197 | } |
| @@ -203,12 +208,12 @@ | ||
| 203 | 208 | String name = "c_set_" + rawType; |
| 204 | 209 | DataType type = DataType.set(rawType); |
| 205 | 210 | Set<Object> value = (Set<Object>) getFixedValue(type); |
| 206 | - ; | |
| 207 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); | |
| 211 | + | |
| 212 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); | |
| 208 | 213 | BoundStatement bs = ps.bind(); |
| 209 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 214 | + session().execute(setBoundValue(bs, name, type, value)); | |
| 210 | 215 | |
| 211 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one(); | |
| 216 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one(); | |
| 212 | 217 | assertEquals(getValue(row, name, type), value, "For type " + type); |
| 213 | 218 | } |
| 214 | 219 | } |
| @@ -228,12 +233,12 @@ | ||
| 228 | 233 | String name = "c_set_" + rawType; |
| 229 | 234 | DataType type = DataType.set(rawType); |
| 230 | 235 | Set<Object> value = (Set<Object>) getFixedValue2(type); |
| 231 | - ; | |
| 232 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); | |
| 236 | + | |
| 237 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); | |
| 233 | 238 | BoundStatement bs = ps.bind(); |
| 234 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 239 | + session().execute(setBoundValue(bs, name, type, value)); | |
| 235 | 240 | |
| 236 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one(); | |
| 241 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one(); | |
| 237 | 242 | assertEquals(getValue(row, name, type), value, "For type " + type); |
| 238 | 243 | } |
| 239 | 244 | } |
| @@ -255,12 +260,12 @@ | ||
| 255 | 260 | String name = "c_map_" + rawKeyType + '_' + rawValueType; |
| 256 | 261 | DataType type = DataType.map(rawKeyType, rawValueType); |
| 257 | 262 | Map<Object, Object> value = (Map<Object, Object>) getFixedValue(type); |
| 258 | - ; | |
| 259 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); | |
| 263 | + | |
| 264 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); | |
| 260 | 265 | BoundStatement bs = ps.bind(); |
| 261 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 266 | + session().execute(setBoundValue(bs, name, type, value)); | |
| 262 | 267 | |
| 263 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one(); | |
| 268 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one(); | |
| 264 | 269 | assertEquals(getValue(row, name, type), value, "For type " + type); |
| 265 | 270 | } |
| 266 | 271 | } |
| @@ -286,12 +291,12 @@ | ||
| 286 | 291 | String name = "c_map_" + rawKeyType + '_' + rawValueType; |
| 287 | 292 | DataType type = DataType.map(rawKeyType, rawValueType); |
| 288 | 293 | Map<Object, Object> value = (Map<Object, Object>) getFixedValue2(type); |
| 289 | - ; | |
| 290 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); | |
| 294 | + | |
| 295 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); | |
| 291 | 296 | BoundStatement bs = ps.bind(); |
| 292 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 297 | + session().execute(setBoundValue(bs, name, type, value)); | |
| 293 | 298 | |
| 294 | - Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one(); | |
| 299 | + Row row = session().execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one(); | |
| 295 | 300 | assertEquals(getValue(row, name, type), value, "For type " + type); |
| 296 | 301 | } |
| 297 | 302 | } |
| @@ -300,16 +305,16 @@ | ||
| 300 | 305 | @Test(groups = "short") |
| 301 | 306 | public void prepareWithNullValuesTest() throws Exception { |
| 302 | 307 | |
| 303 | - PreparedStatement ps = session.prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, ?)"); | |
| 308 | + PreparedStatement ps = session().prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, ?)"); | |
| 304 | 309 | |
| 305 | - session.execute(ps.bind("prepWithNull1", null)); | |
| 310 | + session().execute(ps.bind("prepWithNull1", null)); | |
| 306 | 311 | |
| 307 | 312 | BoundStatement bs = ps.bind(); |
| 308 | 313 | bs.setString("k", "prepWithNull2"); |
| 309 | 314 | bs.setString("v", null); |
| 310 | - session.execute(bs); | |
| 315 | + session().execute(bs); | |
| 311 | 316 | |
| 312 | - ResultSet rs = session.execute("SELECT * FROM " + SIMPLE_TABLE2 + " WHERE k IN ('prepWithNull1', 'prepWithNull2')"); | |
| 317 | + ResultSet rs = session().execute("SELECT * FROM " + SIMPLE_TABLE2 + " WHERE k IN ('prepWithNull1', 'prepWithNull2')"); | |
| 313 | 318 | Row r1 = rs.one(); |
| 314 | 319 | Row r2 = rs.one(); |
| 315 | 320 | assertTrue(rs.isExhausted()); |
| @@ -328,7 +333,7 @@ | ||
| 328 | 333 | toPrepare.setConsistencyLevel(ConsistencyLevel.QUORUM); |
| 329 | 334 | toPrepare.enableTracing(); |
| 330 | 335 | |
| 331 | - PreparedStatement prepared = session.prepare(toPrepare); | |
| 336 | + PreparedStatement prepared = session().prepare(toPrepare); | |
| 332 | 337 | BoundStatement bs = prepared.bind("someValue"); |
| 333 | 338 | |
| 334 | 339 | assertEquals(ConsistencyLevel.QUORUM, bs.getConsistencyLevel()); |
| @@ -350,17 +355,17 @@ | ||
| 350 | 355 | public void batchTest() throws Exception { |
| 351 | 356 | |
| 352 | 357 | try { |
| 353 | - PreparedStatement ps1 = session.prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, ?)"); | |
| 354 | - PreparedStatement ps2 = session.prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, 'bar')"); | |
| 358 | + PreparedStatement ps1 = session().prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, ?)"); | |
| 359 | + PreparedStatement ps2 = session().prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, 'bar')"); | |
| 355 | 360 | |
| 356 | 361 | BatchStatement bs = new BatchStatement(); |
| 357 | 362 | bs.add(ps1.bind("one", "foo")); |
| 358 | 363 | bs.add(ps2.bind("two")); |
| 359 | 364 | bs.add(new SimpleStatement("INSERT INTO " + SIMPLE_TABLE2 + " (k, v) VALUES ('three', 'foobar')")); |
| 360 | 365 | |
| 361 | - session.execute(bs); | |
| 366 | + session().execute(bs); | |
| 362 | 367 | |
| 363 | - List<Row> all = session.execute("SELECT * FROM " + SIMPLE_TABLE2).all(); | |
| 368 | + List<Row> all = session().execute("SELECT * FROM " + SIMPLE_TABLE2).all(); | |
| 364 | 369 | |
| 365 | 370 | assertEquals("three", all.get(0).getString("k")); |
| 366 | 371 | assertEquals("foobar", all.get(0).getString("v")); |
| @@ -372,46 +377,46 @@ | ||
| 372 | 377 | assertEquals("bar", all.get(2).getString("v")); |
| 373 | 378 | } catch (UnsupportedFeatureException e) { |
| 374 | 379 | // This is expected when testing the protocol v1 |
| 375 | - if (cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum() != ProtocolVersion.V1) | |
| 380 | + if (cluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum() != ProtocolVersion.V1) | |
| 376 | 381 | throw e; |
| 377 | 382 | } |
| 378 | 383 | } |
| 379 | 384 | |
| 380 | 385 | @Test(groups = "short", expectedExceptions = {IllegalStateException.class}) |
| 381 | 386 | public void unboundVariableInBoundStatementTest() { |
| 382 | - PreparedStatement ps = session.prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (?, ?)"); | |
| 387 | + PreparedStatement ps = session().prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (?, ?)"); | |
| 383 | 388 | BoundStatement bs = ps.bind("k"); |
| 384 | 389 | assertFalse(bs.isSet("i")); |
| 385 | - session.execute(bs); | |
| 390 | + session().execute(bs); | |
| 386 | 391 | } |
| 387 | 392 | |
| 388 | 393 | @Test(groups = "short", expectedExceptions = {IllegalStateException.class}) |
| 389 | 394 | @CassandraVersion(major = 2.0) |
| 390 | 395 | public void unboundVariableInBatchStatementTest() { |
| 391 | - PreparedStatement ps = session.prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (?, ?)"); | |
| 396 | + PreparedStatement ps = session().prepare("INSERT INTO " + SIMPLE_TABLE + " (k, i) VALUES (?, ?)"); | |
| 392 | 397 | BatchStatement batch = new BatchStatement(); |
| 393 | 398 | batch.add(ps.bind("k")); |
| 394 | - session.execute(batch); | |
| 399 | + session().execute(batch); | |
| 395 | 400 | } |
| 396 | 401 | |
| 397 | 402 | @Test(groups = "short") |
| 398 | 403 | public void should_set_routing_key_on_case_insensitive_keyspace_and_table() { |
| 399 | - session.execute(String.format("CREATE TABLE %s.foo (i int PRIMARY KEY)", keyspace)); | |
| 404 | + session().execute(String.format("CREATE TABLE %s.foo (i int PRIMARY KEY)", keyspace)); | |
| 400 | 405 | |
| 401 | - PreparedStatement ps = session.prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)", keyspace)); | |
| 406 | + PreparedStatement ps = session().prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)", keyspace)); | |
| 402 | 407 | BoundStatement bs = ps.bind(1); |
| 403 | 408 | assertThat(bs.getRoutingKey()).isNotNull(); |
| 404 | 409 | } |
| 405 | 410 | |
| 406 | 411 | @Test(groups = "short") |
| 407 | 412 | public void should_set_routing_key_on_case_sensitive_keyspace_and_table() { |
| 408 | - session.execute("CREATE KEYSPACE \"Test\" WITH replication = { " | |
| 413 | + session().execute("CREATE KEYSPACE \"Test\" WITH replication = { " | |
| 409 | 414 | + " 'class': 'SimpleStrategy'," |
| 410 | 415 | + " 'replication_factor': '1'" |
| 411 | 416 | + "}"); |
| 412 | - session.execute("CREATE TABLE \"Test\".\"Foo\" (i int PRIMARY KEY)"); | |
| 417 | + session().execute("CREATE TABLE \"Test\".\"Foo\" (i int PRIMARY KEY)"); | |
| 413 | 418 | |
| 414 | - PreparedStatement ps = session.prepare("INSERT INTO \"Test\".\"Foo\" (i) VALUES (?)"); | |
| 419 | + PreparedStatement ps = session().prepare("INSERT INTO \"Test\".\"Foo\" (i) VALUES (?)"); | |
| 415 | 420 | BoundStatement bs = ps.bind(1); |
| 416 | 421 | assertThat(bs.getRoutingKey()).isNotNull(); |
| 417 | 422 | } |
| @@ -419,14 +424,15 @@ | ||
| 419 | 424 | @Test(groups = "short", expectedExceptions = InvalidQueryException.class) |
| 420 | 425 | public void should_fail_when_prepared_on_another_cluster() throws Exception { |
| 421 | 426 | Cluster otherCluster = Cluster.builder() |
| 422 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 427 | + .addContactPoints(getContactPoints()) | |
| 428 | + .withPort(ccm().getBinaryPort()) | |
| 423 | 429 | .build(); |
| 424 | 430 | try { |
| 425 | 431 | PreparedStatement pst = otherCluster.connect().prepare("select * from system.peers where inet = ?"); |
| 426 | 432 | BoundStatement bs = pst.bind().setInet(0, InetAddress.getByName("localhost")); |
| 427 | 433 | |
| 428 | 434 | // We expect that the error gets detected without a roundtrip to the server, so use executeAsync |
| 429 | - session.executeAsync(bs); | |
| 435 | + session().executeAsync(bs); | |
| 430 | 436 | } finally { |
| 431 | 437 | otherCluster.close(); |
| 432 | 438 | } |
| @@ -434,7 +440,7 @@ | ||
| 434 | 440 | |
| 435 | 441 | @Test(groups = "short") |
| 436 | 442 | public void should_propagate_idempotence_in_statements() { |
| 437 | - session.execute(String.format("CREATE TABLE %s.idempotencetest (i int PRIMARY KEY)", keyspace)); | |
| 443 | + session().execute(String.format("CREATE TABLE %s.idempotencetest (i int PRIMARY KEY)", keyspace)); | |
| 438 | 444 | |
| 439 | 445 | SimpleStatement statement; |
| 440 | 446 | IdempotenceAwarePreparedStatement prepared; |
| @@ -442,21 +448,21 @@ | ||
| 442 | 448 | |
| 443 | 449 | statement = new SimpleStatement(String.format("SELECT * FROM %s.idempotencetest WHERE i = ?", keyspace)); |
| 444 | 450 | |
| 445 | - prepared = (IdempotenceAwarePreparedStatement) session.prepare(statement); | |
| 451 | + prepared = (IdempotenceAwarePreparedStatement) session().prepare(statement); | |
| 446 | 452 | bound = prepared.bind(1); |
| 447 | 453 | |
| 448 | 454 | assertThat(prepared.isIdempotent()).isNull(); |
| 449 | 455 | assertThat(bound.isIdempotent()).isNull(); |
| 450 | 456 | |
| 451 | 457 | statement.setIdempotent(true); |
| 452 | - prepared = (IdempotenceAwarePreparedStatement) session.prepare(statement); | |
| 458 | + prepared = (IdempotenceAwarePreparedStatement) session().prepare(statement); | |
| 453 | 459 | bound = prepared.bind(1); |
| 454 | 460 | |
| 455 | 461 | assertThat(prepared.isIdempotent()).isTrue(); |
| 456 | 462 | assertThat(bound.isIdempotent()).isTrue(); |
| 457 | 463 | |
| 458 | 464 | statement.setIdempotent(false); |
| 459 | - prepared = (IdempotenceAwarePreparedStatement) session.prepare(statement); | |
| 465 | + prepared = (IdempotenceAwarePreparedStatement) session().prepare(statement); | |
| 460 | 466 | bound = prepared.bind(1); |
| 461 | 467 | |
| 462 | 468 | assertThat(prepared.isIdempotent()).isFalse(); |
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java +88 −88
| @@ -122,8 +122,8 @@ | ||
| 122 | 122 | |
| 123 | 123 | @AfterMethod(groups = {"short", "unit"}, alwaysRun = true) |
| 124 | 124 | public void unregisterQueryLogger() { |
| 125 | - if (cluster != null && queryLogger != null) { | |
| 126 | - cluster.unregister(queryLogger); | |
| 125 | + if (cluster() != null && queryLogger != null) { | |
| 126 | + cluster().unregister(queryLogger); | |
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | |
| @@ -133,12 +133,12 @@ | ||
| 133 | 133 | public void should_log_regular_statements() throws Exception { |
| 134 | 134 | // given |
| 135 | 135 | normal.setLevel(DEBUG); |
| 136 | - queryLogger = QueryLogger.builder(cluster) | |
| 136 | + queryLogger = QueryLogger.builder(cluster()) | |
| 137 | 137 | .withConstantThreshold(Long.MAX_VALUE) |
| 138 | 138 | .build(); |
| 139 | - cluster.register(queryLogger); | |
| 139 | + cluster().register(queryLogger); | |
| 140 | 140 | String query = "SELECT c_text FROM test WHERE pk = 42"; |
| 141 | - session.execute(query); | |
| 141 | + session().execute(query); | |
| 142 | 142 | // then |
| 143 | 143 | String line = normalAppender.waitAndGet(10000); |
| 144 | 144 | assertThat(line) |
| @@ -152,14 +152,14 @@ | ||
| 152 | 152 | public void should_log_bound_statements() throws Exception { |
| 153 | 153 | // given |
| 154 | 154 | normal.setLevel(DEBUG); |
| 155 | - queryLogger = QueryLogger.builder(cluster) | |
| 155 | + queryLogger = QueryLogger.builder(cluster()) | |
| 156 | 156 | .withConstantThreshold(Long.MAX_VALUE) |
| 157 | 157 | .build(); |
| 158 | - cluster.register(queryLogger); | |
| 158 | + cluster().register(queryLogger); | |
| 159 | 159 | String query = "SELECT * FROM test where pk = ?"; |
| 160 | - PreparedStatement ps = session.prepare(query); | |
| 160 | + PreparedStatement ps = session().prepare(query); | |
| 161 | 161 | BoundStatement bs = ps.bind(42); |
| 162 | - session.execute(bs); | |
| 162 | + session().execute(bs); | |
| 163 | 163 | // then |
| 164 | 164 | String line = normalAppender.waitAndGet(10000); |
| 165 | 165 | assertThat(line) |
| @@ -174,20 +174,20 @@ | ||
| 174 | 174 | public void should_log_batch_statements() throws Exception { |
| 175 | 175 | // given |
| 176 | 176 | normal.setLevel(DEBUG); |
| 177 | - queryLogger = QueryLogger.builder(cluster) | |
| 177 | + queryLogger = QueryLogger.builder(cluster()) | |
| 178 | 178 | .withConstantThreshold(Long.MAX_VALUE) |
| 179 | 179 | .withMaxQueryStringLength(Integer.MAX_VALUE) |
| 180 | 180 | .build(); |
| 181 | - cluster.register(queryLogger); | |
| 181 | + cluster().register(queryLogger); | |
| 182 | 182 | // when |
| 183 | 183 | String query1 = "INSERT INTO test (pk) VALUES (?)"; |
| 184 | 184 | String query2 = "UPDATE test SET c_int = ? WHERE pk = 42"; |
| 185 | - PreparedStatement ps1 = session.prepare(query1); | |
| 186 | - PreparedStatement ps2 = session.prepare(query2); | |
| 185 | + PreparedStatement ps1 = session().prepare(query1); | |
| 186 | + PreparedStatement ps2 = session().prepare(query2); | |
| 187 | 187 | BatchStatement batch = new BatchStatement(); |
| 188 | 188 | batch.add(ps1.bind(42)); |
| 189 | 189 | batch.add(ps2.bind(1234)); |
| 190 | - session.execute(batch); | |
| 190 | + session().execute(batch); | |
| 191 | 191 | // then |
| 192 | 192 | String line = normalAppender.waitAndGet(10000); |
| 193 | 193 | assertThat(line) |
| @@ -205,20 +205,20 @@ | ||
| 205 | 205 | public void should_log_unlogged_batch_statements() throws Exception { |
| 206 | 206 | // given |
| 207 | 207 | normal.setLevel(DEBUG); |
| 208 | - queryLogger = QueryLogger.builder(cluster) | |
| 208 | + queryLogger = QueryLogger.builder(cluster()) | |
| 209 | 209 | .withConstantThreshold(Long.MAX_VALUE) |
| 210 | 210 | .withMaxQueryStringLength(Integer.MAX_VALUE) |
| 211 | 211 | .build(); |
| 212 | - cluster.register(queryLogger); | |
| 212 | + cluster().register(queryLogger); | |
| 213 | 213 | // when |
| 214 | 214 | String query1 = "INSERT INTO test (pk) VALUES (?)"; |
| 215 | 215 | String query2 = "UPDATE test SET c_int = ? WHERE pk = 42"; |
| 216 | - PreparedStatement ps1 = session.prepare(query1); | |
| 217 | - PreparedStatement ps2 = session.prepare(query2); | |
| 216 | + PreparedStatement ps1 = session().prepare(query1); | |
| 217 | + PreparedStatement ps2 = session().prepare(query2); | |
| 218 | 218 | BatchStatement batch = new BatchStatement(UNLOGGED); |
| 219 | 219 | batch.add(ps1.bind(42)); |
| 220 | 220 | batch.add(ps2.bind(1234)); |
| 221 | - session.execute(batch); | |
| 221 | + session().execute(batch); | |
| 222 | 222 | // then |
| 223 | 223 | String line = normalAppender.waitAndGet(10000); |
| 224 | 224 | assertThat(line) |
| @@ -235,25 +235,25 @@ | ||
| 235 | 235 | @CassandraVersion(major = 2.0) |
| 236 | 236 | public void should_log_counter_batch_statements() throws Exception { |
| 237 | 237 | // Create a special table for testing with counters. |
| 238 | - session.execute( | |
| 238 | + session().execute( | |
| 239 | 239 | "CREATE TABLE IF NOT EXISTS counter_test (pk int PRIMARY KEY, c_count COUNTER, c_count2 COUNTER)"); |
| 240 | 240 | |
| 241 | 241 | // given |
| 242 | 242 | normal.setLevel(DEBUG); |
| 243 | - queryLogger = QueryLogger.builder(cluster) | |
| 243 | + queryLogger = QueryLogger.builder(cluster()) | |
| 244 | 244 | .withConstantThreshold(Long.MAX_VALUE) |
| 245 | 245 | .withMaxQueryStringLength(Integer.MAX_VALUE) |
| 246 | 246 | .build(); |
| 247 | - cluster.register(queryLogger); | |
| 247 | + cluster().register(queryLogger); | |
| 248 | 248 | // when |
| 249 | 249 | String query1 = "UPDATE counter_test SET c_count = c_count + ? WHERE pk = 42"; |
| 250 | 250 | String query2 = "UPDATE counter_test SET c_count2 = c_count2 + ? WHERE pk = 42"; |
| 251 | - PreparedStatement ps1 = session.prepare(query1); | |
| 252 | - PreparedStatement ps2 = session.prepare(query2); | |
| 251 | + PreparedStatement ps1 = session().prepare(query1); | |
| 252 | + PreparedStatement ps2 = session().prepare(query2); | |
| 253 | 253 | BatchStatement batch = new BatchStatement(COUNTER); |
| 254 | 254 | batch.add(ps1.bind(1234L)); |
| 255 | 255 | batch.add(ps2.bind(5678L)); |
| 256 | - session.execute(batch); | |
| 256 | + session().execute(batch); | |
| 257 | 257 | // then |
| 258 | 258 | String line = normalAppender.waitAndGet(10000); |
| 259 | 259 | assertThat(line) |
| @@ -347,16 +347,16 @@ | ||
| 347 | 347 | public void should_log_normal_queries() throws Exception { |
| 348 | 348 | // given |
| 349 | 349 | normal.setLevel(DEBUG); |
| 350 | - queryLogger = QueryLogger.builder(cluster) | |
| 350 | + queryLogger = QueryLogger.builder(cluster()) | |
| 351 | 351 | .withConstantThreshold(Long.MAX_VALUE) |
| 352 | 352 | .withMaxQueryStringLength(Integer.MAX_VALUE) |
| 353 | 353 | .build(); |
| 354 | - cluster.register(queryLogger); | |
| 354 | + cluster().register(queryLogger); | |
| 355 | 355 | // when |
| 356 | 356 | String query = "SELECT * FROM test where pk = ?"; |
| 357 | - PreparedStatement ps = session.prepare(query); | |
| 357 | + PreparedStatement ps = session().prepare(query); | |
| 358 | 358 | BoundStatement bs = ps.bind(42); |
| 359 | - session.execute(bs); | |
| 359 | + session().execute(bs); | |
| 360 | 360 | // then |
| 361 | 361 | String line = normalAppender.waitAndGet(10000); |
| 362 | 362 | assertThat(line) |
| @@ -375,18 +375,18 @@ | ||
| 375 | 375 | public void should_log_non_null_named_parameter() throws Exception { |
| 376 | 376 | // given |
| 377 | 377 | normal.setLevel(TRACE); |
| 378 | - queryLogger = QueryLogger.builder(cluster) | |
| 378 | + queryLogger = QueryLogger.builder(cluster()) | |
| 379 | 379 | .withConstantThreshold(Long.MAX_VALUE) |
| 380 | 380 | .withMaxQueryStringLength(Integer.MAX_VALUE) |
| 381 | 381 | .build(); |
| 382 | - cluster.register(queryLogger); | |
| 382 | + cluster().register(queryLogger); | |
| 383 | 383 | // when |
| 384 | 384 | String query = "UPDATE test SET c_text = :param1 WHERE pk = :param2"; |
| 385 | - PreparedStatement ps = session.prepare(query); | |
| 385 | + PreparedStatement ps = session().prepare(query); | |
| 386 | 386 | BoundStatement bs = ps.bind(); |
| 387 | 387 | bs.setString("param1", "foo"); |
| 388 | 388 | bs.setInt("param2", 42); |
| 389 | - session.execute(bs); | |
| 389 | + session().execute(bs); | |
| 390 | 390 | // then |
| 391 | 391 | String line = normalAppender.waitAndGet(10000); |
| 392 | 392 | assertThat(line) |
| @@ -401,15 +401,15 @@ | ||
| 401 | 401 | public void should_log_non_null_positional_parameter() throws Exception { |
| 402 | 402 | // given |
| 403 | 403 | normal.setLevel(TRACE); |
| 404 | - queryLogger = QueryLogger.builder(cluster).build(); | |
| 405 | - cluster.register(queryLogger); | |
| 404 | + queryLogger = QueryLogger.builder(cluster()).build(); | |
| 405 | + cluster().register(queryLogger); | |
| 406 | 406 | // when |
| 407 | 407 | String query = "UPDATE test SET c_text = ? WHERE pk = ?"; |
| 408 | - PreparedStatement ps = session.prepare(query); | |
| 408 | + PreparedStatement ps = session().prepare(query); | |
| 409 | 409 | BoundStatement bs = ps.bind(); |
| 410 | 410 | bs.setString("c_text", "foo"); |
| 411 | 411 | bs.setInt("pk", 42); |
| 412 | - session.execute(bs); | |
| 412 | + session().execute(bs); | |
| 413 | 413 | // then |
| 414 | 414 | String line = normalAppender.waitAndGet(10000); |
| 415 | 415 | assertThat(line) |
| @@ -424,15 +424,15 @@ | ||
| 424 | 424 | public void should_log_null_parameter() throws Exception { |
| 425 | 425 | // given |
| 426 | 426 | normal.setLevel(TRACE); |
| 427 | - queryLogger = QueryLogger.builder(cluster).build(); | |
| 428 | - cluster.register(queryLogger); | |
| 427 | + queryLogger = QueryLogger.builder(cluster()).build(); | |
| 428 | + cluster().register(queryLogger); | |
| 429 | 429 | // when |
| 430 | 430 | String query = "UPDATE test SET c_text = ? WHERE pk = ?"; |
| 431 | - PreparedStatement ps = session.prepare(query); | |
| 431 | + PreparedStatement ps = session().prepare(query); | |
| 432 | 432 | BoundStatement bs = ps.bind(); |
| 433 | 433 | bs.setString("c_text", null); |
| 434 | 434 | bs.setInt("pk", 42); |
| 435 | - session.execute(bs); | |
| 435 | + session().execute(bs); | |
| 436 | 436 | // then |
| 437 | 437 | String line = normalAppender.waitAndGet(10000); |
| 438 | 438 | assertThat(line) |
| @@ -448,15 +448,15 @@ | ||
| 448 | 448 | public void should_log_bound_statement_parameters_inside_batch_statement() throws Exception { |
| 449 | 449 | // given |
| 450 | 450 | normal.setLevel(TRACE); |
| 451 | - queryLogger = QueryLogger.builder(cluster).build(); | |
| 452 | - cluster.register(queryLogger); | |
| 451 | + queryLogger = QueryLogger.builder(cluster()).build(); | |
| 452 | + cluster().register(queryLogger); | |
| 453 | 453 | // when |
| 454 | 454 | String query1 = "UPDATE test SET c_text = ? WHERE pk = ?"; |
| 455 | 455 | String query2 = "UPDATE test SET c_int = ? WHERE pk = ?"; |
| 456 | 456 | BatchStatement batch = new BatchStatement(); |
| 457 | - batch.add(session.prepare(query1).bind("foo", 42)); | |
| 458 | - batch.add(session.prepare(query2).bind(12345, 43)); | |
| 459 | - session.execute(batch); | |
| 457 | + batch.add(session().prepare(query1).bind("foo", 42)); | |
| 458 | + batch.add(session().prepare(query2).bind(12345, 43)); | |
| 459 | + session().execute(batch); | |
| 460 | 460 | // then |
| 461 | 461 | String line = normalAppender.waitAndGet(10000); |
| 462 | 462 | assertThat(line) |
| @@ -475,15 +475,15 @@ | ||
| 475 | 475 | public void should_log_all_parameter_types() throws Exception { |
| 476 | 476 | // given |
| 477 | 477 | normal.setLevel(TRACE); |
| 478 | - queryLogger = QueryLogger.builder(cluster) | |
| 478 | + queryLogger = QueryLogger.builder(cluster()) | |
| 479 | 479 | .withMaxParameterValueLength(Integer.MAX_VALUE) |
| 480 | 480 | .build(); |
| 481 | - cluster.register(queryLogger); | |
| 481 | + cluster().register(queryLogger); | |
| 482 | 482 | // when |
| 483 | 483 | String query = "UPDATE test SET " + assignments + " WHERE pk = 42"; |
| 484 | - PreparedStatement ps = session.prepare(query); | |
| 484 | + PreparedStatement ps = session().prepare(query); | |
| 485 | 485 | BoundStatement bs = ps.bind(values.toArray()); |
| 486 | - session.execute(bs); | |
| 486 | + session().execute(bs); | |
| 487 | 487 | // then |
| 488 | 488 | String line = normalAppender.waitAndGet(10000); |
| 489 | 489 | assertThat(line) |
| @@ -501,13 +501,13 @@ | ||
| 501 | 501 | public void should_truncate_query_when_max_length_exceeded() throws Exception { |
| 502 | 502 | // given |
| 503 | 503 | normal.setLevel(DEBUG); |
| 504 | - queryLogger = QueryLogger.builder(cluster) | |
| 504 | + queryLogger = QueryLogger.builder(cluster()) | |
| 505 | 505 | .withMaxQueryStringLength(5) |
| 506 | 506 | .build(); |
| 507 | - cluster.register(queryLogger); | |
| 507 | + cluster().register(queryLogger); | |
| 508 | 508 | // when |
| 509 | 509 | String query = "SELECT * FROM test WHERE pk = 42"; |
| 510 | - session.execute(query); | |
| 510 | + session().execute(query); | |
| 511 | 511 | // then |
| 512 | 512 | String line = normalAppender.waitAndGet(10000); |
| 513 | 513 | assertThat(line) |
| @@ -522,17 +522,17 @@ | ||
| 522 | 522 | public void should_show_total_statements_for_batches_even_if_query_truncated() throws Exception { |
| 523 | 523 | // given |
| 524 | 524 | normal.setLevel(DEBUG); |
| 525 | - queryLogger = QueryLogger.builder(cluster) | |
| 525 | + queryLogger = QueryLogger.builder(cluster()) | |
| 526 | 526 | .withMaxQueryStringLength(5) |
| 527 | 527 | .build(); |
| 528 | - cluster.register(queryLogger); | |
| 528 | + cluster().register(queryLogger); | |
| 529 | 529 | // when |
| 530 | 530 | String query1 = "UPDATE test SET c_text = ? WHERE pk = ?"; |
| 531 | 531 | String query2 = "UPDATE test SET c_int = ? WHERE pk = ?"; |
| 532 | 532 | BatchStatement batch = new BatchStatement(); |
| 533 | - batch.add(session.prepare(query1).bind("foo", 42)); | |
| 534 | - batch.add(session.prepare(query2).bind(12345, 43)); | |
| 535 | - session.execute(batch); | |
| 533 | + batch.add(session().prepare(query1).bind("foo", 42)); | |
| 534 | + batch.add(session().prepare(query2).bind(12345, 43)); | |
| 535 | + session().execute(batch); | |
| 536 | 536 | // then |
| 537 | 537 | String line = normalAppender.waitAndGet(10000); |
| 538 | 538 | assertThat(line) |
| @@ -548,13 +548,13 @@ | ||
| 548 | 548 | public void should_not_truncate_query_when_max_length_unlimited() throws Exception { |
| 549 | 549 | // given |
| 550 | 550 | normal.setLevel(DEBUG); |
| 551 | - queryLogger = QueryLogger.builder(cluster) | |
| 551 | + queryLogger = QueryLogger.builder(cluster()) | |
| 552 | 552 | .withMaxQueryStringLength(-1) |
| 553 | 553 | .build(); |
| 554 | - cluster.register(queryLogger); | |
| 554 | + cluster().register(queryLogger); | |
| 555 | 555 | // when |
| 556 | 556 | String query = "SELECT * FROM test WHERE pk = 42"; |
| 557 | - session.execute(query); | |
| 557 | + session().execute(query); | |
| 558 | 558 | // then |
| 559 | 559 | String line = normalAppender.waitAndGet(10000); |
| 560 | 560 | assertThat(line) |
| @@ -569,17 +569,17 @@ | ||
| 569 | 569 | public void should_truncate_parameter_when_max_length_exceeded() throws Exception { |
| 570 | 570 | // given |
| 571 | 571 | normal.setLevel(TRACE); |
| 572 | - queryLogger = QueryLogger.builder(cluster) | |
| 572 | + queryLogger = QueryLogger.builder(cluster()) | |
| 573 | 573 | .withMaxParameterValueLength(5) |
| 574 | 574 | .build(); |
| 575 | - cluster.register(queryLogger); | |
| 575 | + cluster().register(queryLogger); | |
| 576 | 576 | // when |
| 577 | 577 | String query = "UPDATE test SET c_int = ? WHERE pk = ?"; |
| 578 | - PreparedStatement ps = session.prepare(query); | |
| 578 | + PreparedStatement ps = session().prepare(query); | |
| 579 | 579 | BoundStatement bs = ps.bind(); |
| 580 | 580 | bs.setInt("c_int", 123456); |
| 581 | 581 | bs.setInt("pk", 42); |
| 582 | - session.execute(bs); | |
| 582 | + session().execute(bs); | |
| 583 | 583 | // then |
| 584 | 584 | String line = normalAppender.waitAndGet(10000); |
| 585 | 585 | assertThat(line) |
| @@ -593,17 +593,17 @@ | ||
| 593 | 593 | public void should_truncate_blob_parameter_when_max_length_exceeded() throws Exception { |
| 594 | 594 | // given |
| 595 | 595 | normal.setLevel(TRACE); |
| 596 | - queryLogger = QueryLogger.builder(cluster) | |
| 596 | + queryLogger = QueryLogger.builder(cluster()) | |
| 597 | 597 | .withMaxParameterValueLength(6) |
| 598 | 598 | .build(); |
| 599 | - cluster.register(queryLogger); | |
| 599 | + cluster().register(queryLogger); | |
| 600 | 600 | // when |
| 601 | 601 | String query = "UPDATE test SET c_blob = ? WHERE pk = ?"; |
| 602 | - PreparedStatement ps = session.prepare(query); | |
| 602 | + PreparedStatement ps = session().prepare(query); | |
| 603 | 603 | BoundStatement bs = ps.bind(); |
| 604 | 604 | bs.setBytes("c_blob", ByteBuffer.wrap(Bytes.toArray(Lists.newArrayList(1, 2, 3)))); |
| 605 | 605 | bs.setInt("pk", 42); |
| 606 | - session.execute(bs); | |
| 606 | + session().execute(bs); | |
| 607 | 607 | // then |
| 608 | 608 | String line = normalAppender.waitAndGet(10000); |
| 609 | 609 | assertThat(line) |
| @@ -617,17 +617,17 @@ | ||
| 617 | 617 | public void should_not_truncate_parameter_when_max_length_unlimited() throws Exception { |
| 618 | 618 | // given |
| 619 | 619 | normal.setLevel(TRACE); |
| 620 | - queryLogger = QueryLogger.builder(cluster) | |
| 620 | + queryLogger = QueryLogger.builder(cluster()) | |
| 621 | 621 | .withMaxParameterValueLength(-1) |
| 622 | 622 | .build(); |
| 623 | - cluster.register(queryLogger); | |
| 623 | + cluster().register(queryLogger); | |
| 624 | 624 | // when |
| 625 | 625 | String query = "UPDATE test SET c_int = ? WHERE pk = ?"; |
| 626 | - PreparedStatement ps = session.prepare(query); | |
| 626 | + PreparedStatement ps = session().prepare(query); | |
| 627 | 627 | BoundStatement bs = ps.bind(); |
| 628 | 628 | bs.setInt("c_int", 123456); |
| 629 | 629 | bs.setInt("pk", 42); |
| 630 | - session.execute(bs); | |
| 630 | + session().execute(bs); | |
| 631 | 631 | // then |
| 632 | 632 | String line = normalAppender.waitAndGet(10000); |
| 633 | 633 | assertThat(line) |
| @@ -641,17 +641,17 @@ | ||
| 641 | 641 | public void should_not_log_exceeding_number_of_parameters() throws Exception { |
| 642 | 642 | // given |
| 643 | 643 | normal.setLevel(TRACE); |
| 644 | - queryLogger = QueryLogger.builder(cluster) | |
| 644 | + queryLogger = QueryLogger.builder(cluster()) | |
| 645 | 645 | .withMaxLoggedParameters(1) |
| 646 | 646 | .build(); |
| 647 | - cluster.register(queryLogger); | |
| 647 | + cluster().register(queryLogger); | |
| 648 | 648 | // when |
| 649 | 649 | String query = "UPDATE test SET c_int = ? WHERE pk = ?"; |
| 650 | - PreparedStatement ps = session.prepare(query); | |
| 650 | + PreparedStatement ps = session().prepare(query); | |
| 651 | 651 | BoundStatement bs = ps.bind(); |
| 652 | 652 | bs.setInt("c_int", 123456); |
| 653 | 653 | bs.setInt("pk", 42); |
| 654 | - session.execute(bs); | |
| 654 | + session().execute(bs); | |
| 655 | 655 | // then |
| 656 | 656 | String line = normalAppender.waitAndGet(10000); |
| 657 | 657 | assertThat(line) |
| @@ -667,17 +667,17 @@ | ||
| 667 | 667 | public void should_not_log_exceeding_number_of_parameters_in_batch_statement() throws Exception { |
| 668 | 668 | // given |
| 669 | 669 | normal.setLevel(TRACE); |
| 670 | - queryLogger = QueryLogger.builder(cluster) | |
| 670 | + queryLogger = QueryLogger.builder(cluster()) | |
| 671 | 671 | .withMaxLoggedParameters(1) |
| 672 | 672 | .build(); |
| 673 | - cluster.register(queryLogger); | |
| 673 | + cluster().register(queryLogger); | |
| 674 | 674 | // when |
| 675 | 675 | String query1 = "UPDATE test SET c_text = ? WHERE pk = ?"; |
| 676 | 676 | String query2 = "UPDATE test SET c_int = ? WHERE pk = ?"; |
| 677 | 677 | BatchStatement batch = new BatchStatement(); |
| 678 | - batch.add(session.prepare(query1).bind("foo", 42)); | |
| 679 | - batch.add(session.prepare(query2).bind(12345, 43)); | |
| 680 | - session.execute(batch); | |
| 678 | + batch.add(session().prepare(query1).bind("foo", 42)); | |
| 679 | + batch.add(session().prepare(query2).bind(12345, 43)); | |
| 680 | + session().execute(batch); | |
| 681 | 681 | // then |
| 682 | 682 | String line = normalAppender.waitAndGet(10000); |
| 683 | 683 | assertThat(line) |
| @@ -696,17 +696,17 @@ | ||
| 696 | 696 | public void should_log_all_parameters_when_max_unlimited() throws Exception { |
| 697 | 697 | // given |
| 698 | 698 | normal.setLevel(TRACE); |
| 699 | - queryLogger = QueryLogger.builder(cluster) | |
| 699 | + queryLogger = QueryLogger.builder(cluster()) | |
| 700 | 700 | .withMaxLoggedParameters(-1) |
| 701 | 701 | .build(); |
| 702 | - cluster.register(queryLogger); | |
| 702 | + cluster().register(queryLogger); | |
| 703 | 703 | // when |
| 704 | 704 | String query = "UPDATE test SET c_int = ? WHERE pk = ?"; |
| 705 | - PreparedStatement ps = session.prepare(query); | |
| 705 | + PreparedStatement ps = session().prepare(query); | |
| 706 | 706 | BoundStatement bs = ps.bind(); |
| 707 | 707 | bs.setInt("c_int", 123456); |
| 708 | 708 | bs.setInt("pk", 42); |
| 709 | - session.execute(bs); | |
| 709 | + session().execute(bs); | |
| 710 | 710 | // then |
| 711 | 711 | String line = normalAppender.waitAndGet(10000); |
| 712 | 712 | assertThat(line) |
| @@ -717,8 +717,8 @@ | ||
| 717 | 717 | } |
| 718 | 718 | |
| 719 | 719 | @Override |
| 720 | - public List<String> createTestFixtures() { | |
| 721 | - return Lists.newArrayList("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")"); | |
| 720 | + public void onTestContextInitialized() { | |
| 721 | + execute("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")"); | |
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | } |
modified driver-core/src/test/java/com/datastax/driver/core/QueryTimestampTest.java +25 −28
| @@ -18,12 +18,9 @@ | ||
| 18 | 18 | import com.datastax.driver.core.Metrics.Errors; |
| 19 | 19 | import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy; |
| 20 | 20 | import com.datastax.driver.core.utils.CassandraVersion; |
| 21 | -import com.google.common.collect.Lists; | |
| 22 | 21 | import org.testng.annotations.BeforeMethod; |
| 23 | 22 | import org.testng.annotations.Test; |
| 24 | 23 | |
| 25 | -import java.util.Collection; | |
| 26 | - | |
| 27 | 24 | import static org.testng.Assert.assertEquals; |
| 28 | 25 | import static org.testng.Assert.assertTrue; |
| 29 | 26 | |
| @@ -33,13 +30,13 @@ | ||
| 33 | 30 | @CassandraVersion(major = 2.1) |
| 34 | 31 | public class QueryTimestampTest extends CCMTestsSupport { |
| 35 | 32 | |
| 33 | + private volatile long timestampFromGenerator; | |
| 34 | + | |
| 36 | 35 | @Override |
| 37 | - public Collection<String> createTestFixtures() { | |
| 38 | - return Lists.newArrayList("CREATE TABLE foo (k int PRIMARY KEY, v int)"); | |
| 36 | + public void onTestContextInitialized() { | |
| 37 | + execute("CREATE TABLE foo (k int PRIMARY KEY, v int)"); | |
| 39 | 38 | } |
| 40 | 39 | |
| 41 | - private volatile long timestampFromGenerator; | |
| 42 | - | |
| 43 | 40 | @Override |
| 44 | 41 | public Cluster.Builder createClusterBuilder() { |
| 45 | 42 | return Cluster.builder() |
| @@ -54,16 +51,16 @@ | ||
| 54 | 51 | |
| 55 | 52 | @BeforeMethod(groups = "short") |
| 56 | 53 | public void cleanData() { |
| 57 | - session.execute("TRUNCATE foo"); | |
| 54 | + session().execute("TRUNCATE foo"); | |
| 58 | 55 | } |
| 59 | 56 | |
| 60 | 57 | @Test(groups = "short") |
| 61 | 58 | public void should_use_CQL_timestamp_over_anything_else() { |
| 62 | 59 | timestampFromGenerator = 10; |
| 63 | 60 | String query = "INSERT INTO foo (k, v) VALUES (1, 1) USING TIMESTAMP 20"; |
| 64 | - session.execute(new SimpleStatement(query).setDefaultTimestamp(30)); | |
| 61 | + session().execute(new SimpleStatement(query).setDefaultTimestamp(30)); | |
| 65 | 62 | |
| 66 | - long writeTime = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 63 | + long writeTime = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 67 | 64 | assertEquals(writeTime, 20); |
| 68 | 65 | } |
| 69 | 66 | |
| @@ -71,9 +68,9 @@ | ||
| 71 | 68 | public void should_use_statement_timestamp_over_generator() { |
| 72 | 69 | timestampFromGenerator = 10; |
| 73 | 70 | String query = "INSERT INTO foo (k, v) VALUES (1, 1)"; |
| 74 | - session.execute(new SimpleStatement(query).setDefaultTimestamp(30)); | |
| 71 | + session().execute(new SimpleStatement(query).setDefaultTimestamp(30)); | |
| 75 | 72 | |
| 76 | - long writeTime = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 73 | + long writeTime = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 77 | 74 | assertEquals(writeTime, 30); |
| 78 | 75 | } |
| 79 | 76 | |
| @@ -81,9 +78,9 @@ | ||
| 81 | 78 | public void should_use_generator_timestamp_if_none_other_specified() { |
| 82 | 79 | timestampFromGenerator = 10; |
| 83 | 80 | String query = "INSERT INTO foo (k, v) VALUES (1, 1)"; |
| 84 | - session.execute(query); | |
| 81 | + session().execute(query); | |
| 85 | 82 | |
| 86 | - long writeTime = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 83 | + long writeTime = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 87 | 84 | assertEquals(writeTime, 10); |
| 88 | 85 | } |
| 89 | 86 | |
| @@ -92,9 +89,9 @@ | ||
| 92 | 89 | timestampFromGenerator = Long.MIN_VALUE; |
| 93 | 90 | long clientTime = System.currentTimeMillis() * 1000; |
| 94 | 91 | String query = "INSERT INTO foo (k, v) VALUES (1, 1)"; |
| 95 | - session.execute(query); | |
| 92 | + session().execute(query); | |
| 96 | 93 | |
| 97 | - long writeTime = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 94 | + long writeTime = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 98 | 95 | assertTrue(writeTime >= clientTime); |
| 99 | 96 | } |
| 100 | 97 | |
| @@ -104,10 +101,10 @@ | ||
| 104 | 101 | batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); |
| 105 | 102 | batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); |
| 106 | 103 | batch.setDefaultTimestamp(10); |
| 107 | - session.execute(batch); | |
| 104 | + session().execute(batch); | |
| 108 | 105 | |
| 109 | - long writeTime1 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 110 | - long writeTime2 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 2").one().getLong(0); | |
| 106 | + long writeTime1 = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 107 | + long writeTime2 = session().execute("SELECT writeTime(v) FROM foo WHERE k = 2").one().getLong(0); | |
| 111 | 108 | assertEquals(writeTime1, 10); |
| 112 | 109 | assertEquals(writeTime2, 20); |
| 113 | 110 | } |
| @@ -118,10 +115,10 @@ | ||
| 118 | 115 | BatchStatement batch = new BatchStatement(); |
| 119 | 116 | batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); |
| 120 | 117 | batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); |
| 121 | - session.execute(batch); | |
| 118 | + session().execute(batch); | |
| 122 | 119 | |
| 123 | - long writeTime1 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 124 | - long writeTime2 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 2").one().getLong(0); | |
| 120 | + long writeTime1 = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 121 | + long writeTime2 = session().execute("SELECT writeTime(v) FROM foo WHERE k = 2").one().getLong(0); | |
| 125 | 122 | assertEquals(writeTime1, 10); |
| 126 | 123 | assertEquals(writeTime2, 20); |
| 127 | 124 | } |
| @@ -133,10 +130,10 @@ | ||
| 133 | 130 | BatchStatement batch = new BatchStatement(); |
| 134 | 131 | batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); |
| 135 | 132 | batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); |
| 136 | - session.execute(batch); | |
| 133 | + session().execute(batch); | |
| 137 | 134 | |
| 138 | - long writeTime1 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 139 | - long writeTime2 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 2").one().getLong(0); | |
| 135 | + long writeTime1 = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 136 | + long writeTime2 = session().execute("SELECT writeTime(v) FROM foo WHERE k = 2").one().getLong(0); | |
| 140 | 137 | assertTrue(writeTime1 >= clientTime); |
| 141 | 138 | assertEquals(writeTime2, 20); |
| 142 | 139 | } |
| @@ -149,12 +146,12 @@ | ||
| 149 | 146 | // will retry it at ONE. |
| 150 | 147 | statement.setConsistencyLevel(ConsistencyLevel.TWO); |
| 151 | 148 | |
| 152 | - session.execute(statement); | |
| 149 | + session().execute(statement); | |
| 153 | 150 | |
| 154 | - Errors metrics = session.getCluster().getMetrics().getErrorMetrics(); | |
| 151 | + Errors metrics = session().getCluster().getMetrics().getErrorMetrics(); | |
| 155 | 152 | assertEquals(metrics.getRetriesOnUnavailable().getCount(), 1); |
| 156 | 153 | |
| 157 | - long writeTime = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 154 | + long writeTime = session().execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); | |
| 158 | 155 | assertEquals(writeTime, 10); |
| 159 | 156 | } |
| 160 | 157 | } |
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionedNodeTest.java +8 −16
| @@ -45,9 +45,8 @@ | ||
| 45 | 45 | |
| 46 | 46 | @Test(groups = "long") |
| 47 | 47 | public void should_ignore_recommissioned_node_on_reconnection_attempt() throws Exception { |
| 48 | - mainCcmBuilder = CCMBridge.builder().withNodes(3).notStarted(); | |
| 48 | + mainCcmBuilder = CCMBridge.builder().withNodes(3); | |
| 49 | 49 | mainCcm = CCMCache.get(mainCcmBuilder); |
| 50 | - mainCcm.start(); | |
| 51 | 50 | |
| 52 | 51 | // node1 will be our "recommissioned" node, for now we just stop it so that it stays in the peers table. |
| 53 | 52 | mainCcm.stop(1); |
| @@ -67,9 +66,8 @@ | ||
| 67 | 66 | .withStoragePort(mainCcm.getStoragePort()) |
| 68 | 67 | .withThriftPort(mainCcm.getThriftPort()) |
| 69 | 68 | .withBinaryPort(mainCcm.getBinaryPort()) |
| 70 | - .withNodes(1).notStarted(); | |
| 69 | + .withNodes(1); | |
| 71 | 70 | otherCcm = CCMCache.get(otherCcmBuilder); |
| 72 | - otherCcm.start(); | |
| 73 | 71 | otherCcm.waitForUp(1); |
| 74 | 72 | |
| 75 | 73 | // Give the driver the time to notice the node is back up and try to connect to it. |
| @@ -80,9 +78,8 @@ | ||
| 80 | 78 | |
| 81 | 79 | @Test(groups = "long") |
| 82 | 80 | public void should_ignore_recommissioned_node_on_control_connection_reconnect() throws Exception { |
| 83 | - mainCcmBuilder = CCMBridge.builder().withNodes(2).notStarted(); | |
| 81 | + mainCcmBuilder = CCMBridge.builder().withNodes(2); | |
| 84 | 82 | mainCcm = CCMCache.get(mainCcmBuilder); |
| 85 | - mainCcm.start(); | |
| 86 | 83 | mainCcm.stop(1); |
| 87 | 84 | mainCcm.waitForDown(1); |
| 88 | 85 | |
| @@ -99,9 +96,8 @@ | ||
| 99 | 96 | .withStoragePort(mainCcm.getStoragePort()) |
| 100 | 97 | .withThriftPort(mainCcm.getThriftPort()) |
| 101 | 98 | .withBinaryPort(mainCcm.getBinaryPort()) |
| 102 | - .withNodes(1).notStarted(); | |
| 99 | + .withNodes(1); | |
| 103 | 100 | otherCcm = CCMCache.get(otherCcmBuilder); |
| 104 | - otherCcm.start(); | |
| 105 | 101 | otherCcm.waitForUp(1); |
| 106 | 102 | |
| 107 | 103 | // Stop node2, the control connection gets defunct |
| @@ -115,9 +111,8 @@ | ||
| 115 | 111 | @Test(groups = "long") |
| 116 | 112 | public void should_ignore_recommissioned_node_on_session_init() throws Exception { |
| 117 | 113 | // Simulate the bug before starting the cluster |
| 118 | - mainCcmBuilder = CCMBridge.builder().withNodes(2).notStarted(); | |
| 114 | + mainCcmBuilder = CCMBridge.builder().withNodes(2); | |
| 119 | 115 | mainCcm = CCMCache.get(mainCcmBuilder); |
| 120 | - mainCcm.start(); | |
| 121 | 116 | mainCcm.stop(1); |
| 122 | 117 | mainCcm.waitForDown(1); |
| 123 | 118 | |
| @@ -125,9 +120,8 @@ | ||
| 125 | 120 | .withStoragePort(mainCcm.getStoragePort()) |
| 126 | 121 | .withThriftPort(mainCcm.getThriftPort()) |
| 127 | 122 | .withBinaryPort(mainCcm.getBinaryPort()) |
| 128 | - .withNodes(1).notStarted(); | |
| 123 | + .withNodes(1); | |
| 129 | 124 | otherCcm = CCMCache.get(otherCcmBuilder); |
| 130 | - otherCcm.start(); | |
| 131 | 125 | otherCcm.waitForUp(1); |
| 132 | 126 | |
| 133 | 127 | // Start the driver, it should only connect to node 2 |
| @@ -154,9 +148,8 @@ | ||
| 154 | 148 | @CassandraVersion(major = 2.0) |
| 155 | 149 | public void should_ignore_node_that_does_not_support_protocol_version_on_session_init() throws Exception { |
| 156 | 150 | // Simulate the bug before starting the cluster |
| 157 | - mainCcmBuilder = CCMBridge.builder().withNodes(2).notStarted(); | |
| 151 | + mainCcmBuilder = CCMBridge.builder().withNodes(2); | |
| 158 | 152 | mainCcm = CCMCache.get(mainCcmBuilder); |
| 159 | - mainCcm.start(); | |
| 160 | 153 | mainCcm.stop(1); |
| 161 | 154 | mainCcm.waitForDown(1); |
| 162 | 155 | |
| @@ -164,9 +157,8 @@ | ||
| 164 | 157 | .withStoragePort(mainCcm.getStoragePort()) |
| 165 | 158 | .withThriftPort(mainCcm.getThriftPort()) |
| 166 | 159 | .withBinaryPort(mainCcm.getBinaryPort()) |
| 167 | - .withVersion("1.2.19").notStarted(); | |
| 160 | + .withVersion("1.2.19"); | |
| 168 | 161 | otherCcm = CCMCache.get(otherCcmBuilder); |
| 169 | - otherCcm.start(); | |
| 170 | 162 | otherCcm.waitForUp(1); |
| 171 | 163 | |
| 172 | 164 | // Start the driver, it should only connect to node 2 |
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionPolicyTest.java +8 −8
| @@ -46,12 +46,12 @@ | ||
| 46 | 46 | public void exponentialReconnectionPolicyTest() throws Throwable { |
| 47 | 47 | |
| 48 | 48 | // Ensure that ExponentialReconnectionPolicy is what we should be testing |
| 49 | - if (!(cluster.getConfiguration().getPolicies().getReconnectionPolicy() instanceof ExponentialReconnectionPolicy)) { | |
| 49 | + if (!(cluster().getConfiguration().getPolicies().getReconnectionPolicy() instanceof ExponentialReconnectionPolicy)) { | |
| 50 | 50 | fail("Set policy does not match retrieved policy."); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Test basic getters |
| 54 | - ExponentialReconnectionPolicy reconnectionPolicy = (ExponentialReconnectionPolicy) cluster.getConfiguration().getPolicies().getReconnectionPolicy(); | |
| 54 | + ExponentialReconnectionPolicy reconnectionPolicy = (ExponentialReconnectionPolicy) cluster().getConfiguration().getPolicies().getReconnectionPolicy(); | |
| 55 | 55 | assertTrue(reconnectionPolicy.getBaseDelayMs() == 2 * 1000); |
| 56 | 56 | assertTrue(reconnectionPolicy.getMaxDelayMs() == 5 * 60 * 1000); |
| 57 | 57 | |
| @@ -112,12 +112,12 @@ | ||
| 112 | 112 | public void constantReconnectionPolicyTest() throws Throwable { |
| 113 | 113 | |
| 114 | 114 | // Ensure that ConstantReconnectionPolicy is what we should be testing |
| 115 | - if (!(cluster.getConfiguration().getPolicies().getReconnectionPolicy() instanceof ConstantReconnectionPolicy)) { | |
| 115 | + if (!(cluster().getConfiguration().getPolicies().getReconnectionPolicy() instanceof ConstantReconnectionPolicy)) { | |
| 116 | 116 | fail("Set policy does not match retrieved policy."); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // Test basic getters |
| 120 | - ConstantReconnectionPolicy reconnectionPolicy = (ConstantReconnectionPolicy) cluster.getConfiguration().getPolicies().getReconnectionPolicy(); | |
| 120 | + ConstantReconnectionPolicy reconnectionPolicy = (ConstantReconnectionPolicy) cluster().getConfiguration().getPolicies().getReconnectionPolicy(); | |
| 121 | 121 | assertTrue(reconnectionPolicy.getConstantDelayMs() == 10 * 1000); |
| 122 | 122 | |
| 123 | 123 | // Test erroneous instantiations |
| @@ -153,7 +153,7 @@ | ||
| 153 | 153 | // Ensure a basic test works |
| 154 | 154 | assertQueried(TestUtils.IP_PREFIX + '1', 12); |
| 155 | 155 | resetCoordinators(); |
| 156 | - ccm.forceStop(1); | |
| 156 | + ccm().forceStop(1); | |
| 157 | 157 | |
| 158 | 158 | // Start timing and ensure that the node is down |
| 159 | 159 | long startTime = 0; |
| @@ -172,7 +172,7 @@ | ||
| 172 | 172 | |
| 173 | 173 | // Restart node at restartTime |
| 174 | 174 | if (!restarted && thisTime - startTime > restartTime) { |
| 175 | - ccm.start(1); | |
| 175 | + ccm().start(1); | |
| 176 | 176 | restarted = true; |
| 177 | 177 | } |
| 178 | 178 | |
| @@ -197,7 +197,7 @@ | ||
| 197 | 197 | resetCoordinators(); |
| 198 | 198 | |
| 199 | 199 | // Ensure the reconnection times reset |
| 200 | - ccm.forceStop(1); | |
| 200 | + ccm().forceStop(1); | |
| 201 | 201 | |
| 202 | 202 | // Start timing and ensure that the node is down |
| 203 | 203 | startTime = 0; |
| @@ -215,7 +215,7 @@ | ||
| 215 | 215 | |
| 216 | 216 | // Restart node at restartTime |
| 217 | 217 | if (!restarted && thisTime - startTime > restartTime) { |
| 218 | - ccm.start(1); | |
| 218 | + ccm().start(1); | |
| 219 | 219 | restarted = true; |
| 220 | 220 | } |
| 221 | 221 |
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +31 −31
| @@ -50,8 +50,8 @@ | ||
| 50 | 50 | @Test(groups = "long") |
| 51 | 51 | public void should_reconnect_after_full_connectivity_loss() throws InterruptedException { |
| 52 | 52 | Cluster cluster = register(Cluster.builder() |
| 53 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 54 | - .withPort(ccm.getBinaryPort()) | |
| 53 | + .addContactPoints(getContactPoints().get(0)) | |
| 54 | + .withPort(ccm().getBinaryPort()) | |
| 55 | 55 | .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis)) |
| 56 | 56 | .build()); |
| 57 | 57 | cluster.connect(); |
| @@ -60,12 +60,12 @@ | ||
| 60 | 60 | |
| 61 | 61 | // Stop all nodes. We won't get notifications anymore, so the only mechanism to |
| 62 | 62 | // reconnect is the background reconnection attempts. |
| 63 | - ccm.stop(2); | |
| 64 | - ccm.stop(1); | |
| 63 | + ccm().stop(2); | |
| 64 | + ccm().stop(1); | |
| 65 | 65 | |
| 66 | - ccm.waitForDown(2); | |
| 67 | - ccm.start(2); | |
| 68 | - ccm.waitForUp(2); | |
| 66 | + ccm().waitForDown(2); | |
| 67 | + ccm().start(2); | |
| 68 | + ccm().waitForUp(2); | |
| 69 | 69 | |
| 70 | 70 | assertThat(cluster).host(2).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS * 2, SECONDS); |
| 71 | 71 | |
| @@ -83,14 +83,14 @@ | ||
| 83 | 83 | public void should_keep_reconnecting_on_authentication_error() throws InterruptedException { |
| 84 | 84 | // For C* 1.2, sleep before attempting to connect as there is a small delay between |
| 85 | 85 | // user being created. |
| 86 | - if (ccm.getVersion().getMajor() < 2) { | |
| 86 | + if (ccm().getVersion().getMajor() < 2) { | |
| 87 | 87 | Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); |
| 88 | 88 | } |
| 89 | 89 | CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis)); |
| 90 | 90 | CountingAuthProvider authProvider = new CountingAuthProvider("cassandra", "cassandra"); |
| 91 | 91 | Cluster cluster = register(Cluster.builder() |
| 92 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 93 | - .withPort(ccm.getBinaryPort()) | |
| 92 | + .addContactPointsWithPorts(ccm().addressOfNode(1)) | |
| 93 | + .withPort(ccm().getBinaryPort()) | |
| 94 | 94 | // Start with the correct auth so that we can initialize the server |
| 95 | 95 | .withAuthProvider(authProvider) |
| 96 | 96 | .withReconnectionPolicy(reconnectionPolicy) |
| @@ -100,11 +100,11 @@ | ||
| 100 | 100 | assertThat(cluster).usesControlHost(1); |
| 101 | 101 | |
| 102 | 102 | // Stop the server, set wrong credentials and restart |
| 103 | - ccm.stop(1); | |
| 104 | - ccm.waitForDown(1); | |
| 103 | + ccm().stop(1); | |
| 104 | + ccm().waitForDown(1); | |
| 105 | 105 | authProvider.setPassword("wrongPassword"); |
| 106 | - ccm.start(1); | |
| 107 | - ccm.waitForUp(1); | |
| 106 | + ccm().start(1); | |
| 107 | + ccm().waitForUp(1); | |
| 108 | 108 | |
| 109 | 109 | // Wait a few iterations to ensure that our authProvider has returned the wrong credentials at least once |
| 110 | 110 | // NB: authentication errors show up in the logs |
| @@ -130,13 +130,13 @@ | ||
| 130 | 130 | CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis)); |
| 131 | 131 | |
| 132 | 132 | Cluster cluster = register(Cluster.builder() |
| 133 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 134 | - .withPort(ccm.getBinaryPort()) | |
| 133 | + .addContactPointsWithPorts(ccm().addressOfNode(1)) | |
| 134 | + .withPort(ccm().getBinaryPort()) | |
| 135 | 135 | .withReconnectionPolicy(reconnectionPolicy).build()); |
| 136 | 136 | cluster.connect(); |
| 137 | 137 | |
| 138 | 138 | // Stop a node and cancel the reconnection attempts to it |
| 139 | - ccm.stop(2); | |
| 139 | + ccm().stop(2); | |
| 140 | 140 | Host host2 = TestUtils.findHost(cluster, 2); |
| 141 | 141 | host2.getReconnectionAttemptFuture().cancel(false); |
| 142 | 142 | |
| @@ -146,8 +146,8 @@ | ||
| 146 | 146 | assertThat(reconnectionPolicy.count.get()).isEqualTo(initialCount); |
| 147 | 147 | |
| 148 | 148 | // Restart the node, which will trigger an UP notification |
| 149 | - ccm.start(2); | |
| 150 | - ccm.waitForUp(2); | |
| 149 | + ccm().start(2); | |
| 150 | + ccm().waitForUp(2); | |
| 151 | 151 | |
| 152 | 152 | // The driver should now see the node as UP again |
| 153 | 153 | assertThat(cluster).host(2).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS * 2, SECONDS); |
| @@ -158,8 +158,8 @@ | ||
| 158 | 158 | public void should_trigger_one_time_reconnect() throws InterruptedException, IOException { |
| 159 | 159 | TogglabePolicy loadBalancingPolicy = new TogglabePolicy(new RoundRobinPolicy()); |
| 160 | 160 | Cluster cluster = register(Cluster.builder() |
| 161 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 162 | - .withPort(ccm.getBinaryPort()) | |
| 161 | + .addContactPointsWithPorts(ccm().addressOfNode(1)) | |
| 162 | + .withPort(ccm().getBinaryPort()) | |
| 163 | 163 | .withLoadBalancingPolicy(loadBalancingPolicy) |
| 164 | 164 | .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis)) |
| 165 | 165 | .build()); |
| @@ -170,8 +170,8 @@ | ||
| 170 | 170 | loadBalancingPolicy.returnEmptyQueryPlan = true; |
| 171 | 171 | |
| 172 | 172 | // Stop the node, ignore it and cancel reconnection attempts to it |
| 173 | - ccm.stop(1); | |
| 174 | - ccm.waitForDown(1); | |
| 173 | + ccm().stop(1); | |
| 174 | + ccm().waitForDown(1); | |
| 175 | 175 | assertThat(cluster).host(1).goesDownWithin(20, SECONDS); |
| 176 | 176 | Host host1 = TestUtils.findHost(cluster, 1); |
| 177 | 177 | loadBalancingPolicy.setDistance(TestUtils.findHost(cluster, 1), HostDistance.IGNORED); |
| @@ -189,8 +189,8 @@ | ||
| 189 | 189 | // Restart the node (this will not trigger an UP notification thanks to our |
| 190 | 190 | // hack to disable the control connection reconnects). The host should stay |
| 191 | 191 | // down for the driver. |
| 192 | - ccm.start(1); | |
| 193 | - ccm.waitForUp(1); | |
| 192 | + ccm().start(1); | |
| 193 | + ccm().waitForUp(1); | |
| 194 | 194 | assertThat(cluster).host(1).hasState(State.DOWN); |
| 195 | 195 | |
| 196 | 196 | TimeUnit.SECONDS.sleep(Cluster.NEW_NODE_DELAY_SECONDS); |
| @@ -214,8 +214,8 @@ | ||
| 214 | 214 | // Spy SocketOptions.getKeepAlive to count how many connections were instantiated. |
| 215 | 215 | SocketOptions socketOptions = spy(new SocketOptions()); |
| 216 | 216 | Cluster cluster = register(Cluster.builder() |
| 217 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 218 | - .withPort(ccm.getBinaryPort()) | |
| 217 | + .addContactPointsWithPorts(ccm().addressOfNode(1)) | |
| 218 | + .withPort(ccm().getBinaryPort()) | |
| 219 | 219 | .withReconnectionPolicy(new ConstantReconnectionPolicy(5000)) |
| 220 | 220 | .withLoadBalancingPolicy(loadBalancingPolicy) |
| 221 | 221 | .withSocketOptions(socketOptions) |
| @@ -235,14 +235,14 @@ | ||
| 235 | 235 | loadBalancingPolicy.returnEmptyQueryPlan = true; |
| 236 | 236 | |
| 237 | 237 | // Stop the node and cancel the reconnection attempts to it |
| 238 | - ccm.stop(1); | |
| 239 | - ccm.waitForDown(1); | |
| 238 | + ccm().stop(1); | |
| 239 | + ccm().waitForDown(1); | |
| 240 | 240 | assertThat(cluster).host(1).goesDownWithin(20, SECONDS); |
| 241 | 241 | Host host1 = TestUtils.findHost(cluster, 1); |
| 242 | 242 | host1.getReconnectionAttemptFuture().cancel(false); |
| 243 | 243 | |
| 244 | - ccm.start(1); | |
| 245 | - ccm.waitForUp(1); | |
| 244 | + ccm().start(1); | |
| 245 | + ccm().waitForUp(1); | |
| 246 | 246 | |
| 247 | 247 | // Reset the spy and count the number of connections attempts for 1 reconnect |
| 248 | 248 | reset(socketOptions); |
modified driver-core/src/test/java/com/datastax/driver/core/RefreshConnectedHostTest.java +8 −8
| @@ -41,9 +41,9 @@ | ||
| 41 | 41 | LimitingLoadBalancingPolicy loadBalancingPolicy = new LimitingLoadBalancingPolicy(new RoundRobinPolicy(), 2, 1); |
| 42 | 42 | |
| 43 | 43 | PoolingOptions poolingOptions = Mockito.spy(new PoolingOptions()); |
| 44 | - cluster = register(Cluster.builder() | |
| 45 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 46 | - .withPort(ccm.getBinaryPort()) | |
| 44 | + Cluster cluster = register(Cluster.builder() | |
| 45 | + .addContactPoints(getContactPoints().get(0)) | |
| 46 | + .withPort(ccm().getBinaryPort()) | |
| 47 | 47 | .withPoolingOptions(poolingOptions) |
| 48 | 48 | .withLoadBalancingPolicy(loadBalancingPolicy) |
| 49 | 49 | .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)) |
| @@ -63,9 +63,9 @@ | ||
| 63 | 63 | .isAtDistance(HostDistance.LOCAL); |
| 64 | 64 | |
| 65 | 65 | // Add and bring host 3 up, its presence should be acknowledged but it should be ignored |
| 66 | - ccm.add(3); | |
| 67 | - ccm.start(3); | |
| 68 | - ccm.waitForUp(3); | |
| 66 | + ccm().add(3); | |
| 67 | + ccm().start(3); | |
| 68 | + ccm().waitForUp(3); | |
| 69 | 69 | |
| 70 | 70 | assertThat(cluster).host(1) |
| 71 | 71 | .hasState(State.UP) |
| @@ -81,8 +81,8 @@ | ||
| 81 | 81 | assertThat(session).hasNoPoolFor(3); |
| 82 | 82 | |
| 83 | 83 | // Kill host 2, host 3 should take its place |
| 84 | - ccm.stop(2); | |
| 85 | - TestUtils.waitFor(TestUtils.ipOfNode(3), cluster); | |
| 84 | + ccm().stop(2); | |
| 85 | + TestUtils.waitForUp(TestUtils.ipOfNode(3), cluster); | |
| 86 | 86 | |
| 87 | 87 | assertThat(cluster).host(1) |
| 88 | 88 | .hasState(State.UP) |
modified driver-core/src/test/java/com/datastax/driver/core/SSLEncryptionTest.java +6 −6
| @@ -74,8 +74,8 @@ | ||
| 74 | 74 | @Test(groups = "short", expectedExceptions = {NoHostAvailableException.class}) |
| 75 | 75 | public void should_not_connect_without_ssl_but_node_uses_ssl() throws Exception { |
| 76 | 76 | Cluster cluster = register(Cluster.builder() |
| 77 | - .addContactPointsWithPorts(this.getInitialContactPoints()) | |
| 78 | - .withPort(ccm.getBinaryPort()) | |
| 77 | + .addContactPoints(getContactPoints()) | |
| 78 | + .withPort(ccm().getBinaryPort()) | |
| 79 | 79 | .build()); |
| 80 | 80 | cluster.connect(); |
| 81 | 81 | } |
| @@ -93,15 +93,15 @@ | ||
| 93 | 93 | @Test(groups = "long") |
| 94 | 94 | public void should_reconnect_with_ssl_on_node_up() throws Exception { |
| 95 | 95 | Cluster cluster = register(Cluster.builder() |
| 96 | - .addContactPointsWithPorts(this.getInitialContactPoints()) | |
| 97 | - .withPort(ccm.getBinaryPort()) | |
| 96 | + .addContactPoints(getContactPoints()) | |
| 97 | + .withPort(ccm().getBinaryPort()) | |
| 98 | 98 | .withSSL(getSSLOptions(Optional.of(DEFAULT_CLIENT_KEYSTORE_PATH), Optional.of(DEFAULT_CLIENT_TRUSTSTORE_PATH))) |
| 99 | 99 | .build()); |
| 100 | 100 | |
| 101 | 101 | cluster.connect(); |
| 102 | 102 | |
| 103 | - ccm.stop(1); | |
| 104 | - ccm.start(1); | |
| 103 | + ccm().stop(1); | |
| 104 | + ccm().start(1); | |
| 105 | 105 | |
| 106 | 106 | assertThat(cluster).host(1).comesUpWithin(TestUtils.TEST_BASE_NODE_WAIT, TimeUnit.SECONDS); |
| 107 | 107 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SSLTestBase.java +4 −4
| @@ -41,8 +41,8 @@ | ||
| 41 | 41 | */ |
| 42 | 42 | protected void connectWithSSLOptions(SSLOptions sslOptions) throws Exception { |
| 43 | 43 | Cluster cluster = register(Cluster.builder() |
| 44 | - .addContactPointsWithPorts(this.getInitialContactPoints()) | |
| 45 | - .withPort(ccm.getBinaryPort()) | |
| 44 | + .addContactPoints(getContactPoints()) | |
| 45 | + .withPort(ccm().getBinaryPort()) | |
| 46 | 46 | .withSSL(sslOptions) |
| 47 | 47 | .build()); |
| 48 | 48 | cluster.connect(); |
| @@ -59,8 +59,8 @@ | ||
| 59 | 59 | */ |
| 60 | 60 | protected void connectWithSSL() throws Exception { |
| 61 | 61 | Cluster cluster = register(Cluster.builder() |
| 62 | - .addContactPointsWithPorts(this.getInitialContactPoints()) | |
| 63 | - .withPort(ccm.getBinaryPort()) | |
| 62 | + .addContactPoints(getContactPoints()) | |
| 63 | + .withPort(ccm().getBinaryPort()) | |
| 64 | 64 | .withSSL() |
| 65 | 65 | .build()); |
| 66 | 66 | cluster.connect(); |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaAgreementTest.java +9 −9
| @@ -34,32 +34,32 @@ | ||
| 34 | 34 | |
| 35 | 35 | @Test(groups = "short") |
| 36 | 36 | public void should_set_flag_on_successful_agreement() { |
| 37 | - ProtocolOptions protocolOptions = cluster.getConfiguration().getProtocolOptions(); | |
| 37 | + ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); | |
| 38 | 38 | protocolOptions.maxSchemaAgreementWaitSeconds = 10; |
| 39 | - ResultSet rs = session.execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); | |
| 39 | + ResultSet rs = session().execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); | |
| 40 | 40 | assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | @Test(groups = "short") |
| 44 | 44 | public void should_set_flag_on_non_schema_altering_statement() { |
| 45 | - ProtocolOptions protocolOptions = cluster.getConfiguration().getProtocolOptions(); | |
| 45 | + ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); | |
| 46 | 46 | protocolOptions.maxSchemaAgreementWaitSeconds = 10; |
| 47 | - ResultSet rs = session.execute("select release_version from system.local"); | |
| 47 | + ResultSet rs = session().execute("select release_version from system.local"); | |
| 48 | 48 | assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | @Test(groups = "short") |
| 52 | 52 | public void should_unset_flag_on_failed_agreement() { |
| 53 | 53 | // Setting to 0 results in no query being set, so agreement fails |
| 54 | - ProtocolOptions protocolOptions = cluster.getConfiguration().getProtocolOptions(); | |
| 54 | + ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); | |
| 55 | 55 | protocolOptions.maxSchemaAgreementWaitSeconds = 0; |
| 56 | - ResultSet rs = session.execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); | |
| 56 | + ResultSet rs = session().execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); | |
| 57 | 57 | assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isFalse(); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | @Test(groups = "short") |
| 61 | 61 | public void should_check_agreement_through_cluster_metadata() { |
| 62 | - Cluster controlCluster = register(TestUtils.buildControlCluster(cluster, ccm)); | |
| 62 | + Cluster controlCluster = register(TestUtils.buildControlCluster(cluster(), ccm())); | |
| 63 | 63 | Session controlSession = controlCluster.connect(); |
| 64 | 64 | |
| 65 | 65 | Row localRow = controlSession.execute("SELECT schema_version FROM system.local").one(); |
| @@ -71,11 +71,11 @@ | ||
| 71 | 71 | assertThat(localVersion).isEqualTo(peerVersion); |
| 72 | 72 | |
| 73 | 73 | // Now check the method under test: |
| 74 | - assertThat(cluster.getMetadata().checkSchemaAgreement()).isTrue(); | |
| 74 | + assertThat(cluster().getMetadata().checkSchemaAgreement()).isTrue(); | |
| 75 | 75 | |
| 76 | 76 | // Insert a fake version to simulate a disagreement: |
| 77 | 77 | forceSchemaVersion(controlSession, peerAddress, UUIDs.random()); |
| 78 | - assertThat(cluster.getMetadata().checkSchemaAgreement()).isFalse(); | |
| 78 | + assertThat(cluster().getMetadata().checkSchemaAgreement()).isFalse(); | |
| 79 | 79 | |
| 80 | 80 | forceSchemaVersion(controlSession, peerAddress, peerVersion); |
| 81 | 81 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaChangesCCTest.java +5 −5
| @@ -56,15 +56,15 @@ | ||
| 56 | 56 | Cluster cluster = register( |
| 57 | 57 | Cluster.builder() |
| 58 | 58 | .withLoadBalancingPolicy(lbPolicy) |
| 59 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 60 | - .withPort(ccm.getBinaryPort()) | |
| 59 | + .addContactPointsWithPorts(ccm().addressOfNode(1)) | |
| 60 | + .withPort(ccm().getBinaryPort()) | |
| 61 | 61 | .build()); |
| 62 | 62 | // Put cluster2 control connection on node 2 so it doesn't go down (to prevent noise for debugging). |
| 63 | 63 | Cluster cluster2 = register( |
| 64 | 64 | Cluster.builder() |
| 65 | 65 | .withLoadBalancingPolicy(lbPolicy) |
| 66 | - .addContactPointsWithPorts(ccm.addressOfNode(2)) | |
| 67 | - .withPort(ccm.getBinaryPort()) | |
| 66 | + .addContactPointsWithPorts(ccm().addressOfNode(2)) | |
| 67 | + .withPort(ccm().getBinaryPort()) | |
| 68 | 68 | .build()); |
| 69 | 69 | SchemaChangeListener listener = mock(SchemaChangeListener.class); |
| 70 | 70 | |
| @@ -93,7 +93,7 @@ | ||
| 93 | 93 | lbPolicy.returnEmptyQueryPlan = true; |
| 94 | 94 | |
| 95 | 95 | // Stop node 1, which hosts the control connection. |
| 96 | - ccm.stop(1); | |
| 96 | + ccm().stop(1); | |
| 97 | 97 | assertThat(cluster).host(1).goesDownWithin(20, TimeUnit.SECONDS); |
| 98 | 98 | |
| 99 | 99 | // Ensure control connection is down. |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaChangesTest.java +10 −5
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | import org.testng.annotations.*; |
| 25 | 25 | |
| 26 | 26 | import java.util.List; |
| 27 | +import java.util.concurrent.TimeUnit; | |
| 27 | 28 | |
| 28 | 29 | import static com.datastax.driver.core.Assertions.assertThat; |
| 29 | 30 | import static com.datastax.driver.core.Metadata.handleId; |
| @@ -45,7 +46,7 @@ | ||
| 45 | 46 | /** |
| 46 | 47 | * The maximum time that the test will wait to check that listeners have been notified |
| 47 | 48 | */ |
| 48 | - private static final int NOTIF_TIMEOUT_MS = 120000; | |
| 49 | + private static final long NOTIF_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(1); | |
| 49 | 50 | |
| 50 | 51 | Cluster cluster1; |
| 51 | 52 | Cluster cluster2; // a second cluster to check that other clients also get notified |
| @@ -66,12 +67,14 @@ | ||
| 66 | 67 | @BeforeClass(groups = "short") |
| 67 | 68 | public void setup() throws InterruptedException { |
| 68 | 69 | Cluster.Builder builder = Cluster.builder() |
| 69 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 70 | + .addContactPoints(getContactPoints()) | |
| 71 | + .withPort(ccm().getBinaryPort()) | |
| 70 | 72 | .withQueryOptions(nonDebouncingQueryOptions()); |
| 71 | 73 | cluster1 = builder.build(); |
| 72 | 74 | cluster2 = builder.build(); |
| 73 | 75 | schemaDisabledCluster = spy(Cluster.builder() |
| 74 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 76 | + .addContactPoints(getContactPoints()) | |
| 77 | + .withPort(ccm().getBinaryPort()) | |
| 75 | 78 | .withClusterName("schema-disabled") |
| 76 | 79 | .withQueryOptions(nonDebouncingQueryOptions() |
| 77 | 80 | .setMetadataEnabled(false) |
| @@ -343,7 +346,8 @@ | ||
| 343 | 346 | @Test(groups = "short", expectedExceptions = IllegalStateException.class) |
| 344 | 347 | public void should_throw_illegal_state_exception_on_newToken_with_metadata_disabled() { |
| 345 | 348 | Cluster cluster = Cluster.builder() |
| 346 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 349 | + .addContactPoints(getContactPoints()) | |
| 350 | + .withPort(ccm().getBinaryPort()) | |
| 347 | 351 | .withQueryOptions(nonDebouncingQueryOptions() |
| 348 | 352 | .setMetadataEnabled(false) |
| 349 | 353 | ).build(); |
| @@ -367,7 +371,8 @@ | ||
| 367 | 371 | @Test(groups = "short", expectedExceptions = IllegalStateException.class) |
| 368 | 372 | public void should_throw_illegal_state_exception_on_newTokenRange_with_metadata_disabled() { |
| 369 | 373 | Cluster cluster = Cluster.builder() |
| 370 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 374 | + .addContactPoints(getContactPoints()) | |
| 375 | + .withPort(ccm().getBinaryPort()) | |
| 371 | 376 | .withQueryOptions(nonDebouncingQueryOptions() |
| 372 | 377 | .setMetadataEnabled(false) |
| 373 | 378 | ).build(); |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaRefreshDebouncerTest.java +20 −25
| @@ -16,7 +16,6 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import org.mockito.ArgumentCaptor; |
| 19 | -import org.testng.annotations.AfterMethod; | |
| 20 | 19 | import org.testng.annotations.BeforeMethod; |
| 21 | 20 | import org.testng.annotations.Test; |
| 22 | 21 | |
| @@ -52,10 +51,11 @@ | ||
| 52 | 51 | queryOptions.setRefreshSchemaIntervalMillis(DEBOUNCE_TIME); |
| 53 | 52 | queryOptions.setMaxPendingRefreshSchemaRequests(5); |
| 54 | 53 | // Create a separate cluster that will receive the schema events on its control connection. |
| 55 | - cluster2 = Cluster.builder() | |
| 56 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 54 | + cluster2 = register(Cluster.builder() | |
| 55 | + .addContactPoints(getContactPoints()) | |
| 56 | + .withPort(ccm().getBinaryPort()) | |
| 57 | 57 | .withQueryOptions(queryOptions) |
| 58 | - .build(); | |
| 58 | + .build()); | |
| 59 | 59 | session2 = cluster2.connect(); |
| 60 | 60 | |
| 61 | 61 | // Create a spy of the Cluster's control connection and replace it with the spy. |
| @@ -70,11 +70,6 @@ | ||
| 70 | 70 | reset(controlConnection); |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - @AfterMethod(groups = "short") | |
| 74 | - public void beforeMethod() { | |
| 75 | - cluster2.close(); | |
| 76 | - } | |
| 77 | - | |
| 78 | 73 | /** |
| 79 | 74 | * Ensures that when a CREATED and UPDATED schema_change events are received on a control |
| 80 | 75 | * connection for the same keyspace within {@link QueryOptions#getRefreshSchemaIntervalMillis()} |
| @@ -86,9 +81,9 @@ | ||
| 86 | 81 | */ |
| 87 | 82 | @Test(groups = "short") |
| 88 | 83 | public void should_debounce_and_coalesce_create_and_alter_keyspace_into_refresh_keyspace() throws Exception { |
| 89 | - String keyspace = "sdaccaak"; | |
| 90 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 91 | - session.execute(String.format("ALTER KEYSPACE %s WITH DURABLE_WRITES=false", keyspace)); | |
| 84 | + String keyspace = TestUtils.generateIdentifier("ks_"); | |
| 85 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 86 | + session().execute(String.format("ALTER KEYSPACE %s WITH DURABLE_WRITES=false", keyspace)); | |
| 92 | 87 | |
| 93 | 88 | ArgumentCaptor<KeyspaceMetadata> captor = forClass(KeyspaceMetadata.class); |
| 94 | 89 | verify(listener, timeout(DEBOUNCE_TIME * 2).only()).onKeyspaceAdded(captor.capture()); |
| @@ -117,17 +112,17 @@ | ||
| 117 | 112 | */ |
| 118 | 113 | @Test(groups = "short") |
| 119 | 114 | public void should_debounce_and_coalesce_create_keyspace_and_table_into_refresh_keyspace() throws Exception { |
| 120 | - String keyspace = "sdacckt"; | |
| 115 | + String keyspace = TestUtils.generateIdentifier("ks_"); | |
| 121 | 116 | String table = "tbl1"; |
| 122 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 123 | - session.execute(String.format(CREATE_TABLE_SIMPLE_FORMAT, keyspace + "." + table)); | |
| 117 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); | |
| 118 | + session().execute(String.format(CREATE_TABLE_SIMPLE_FORMAT, keyspace + "." + table)); | |
| 124 | 119 | |
| 125 | 120 | ArgumentCaptor<KeyspaceMetadata> keyspaceCaptor = forClass(KeyspaceMetadata.class); |
| 126 | 121 | verify(listener, timeout(DEBOUNCE_TIME * 2).times(1)).onKeyspaceAdded(keyspaceCaptor.capture()); |
| 127 | 122 | assertThat(keyspaceCaptor.getValue()).hasName(keyspace); |
| 128 | 123 | |
| 129 | 124 | ArgumentCaptor<TableMetadata> tableCaptor = forClass(TableMetadata.class); |
| 130 | - verify(listener, times(1)).onTableAdded(tableCaptor.capture()); | |
| 125 | + verify(listener, timeout(DEBOUNCE_TIME * 2).times(1)).onTableAdded(tableCaptor.capture()); | |
| 131 | 126 | assertThat(tableCaptor.getValue()).hasName(table); |
| 132 | 127 | |
| 133 | 128 | // Verify the schema refresh was debounced and coalesced when a keyspace event and table event |
| @@ -152,7 +147,7 @@ | ||
| 152 | 147 | */ |
| 153 | 148 | @Test(groups = "short") |
| 154 | 149 | public void should_debounce_and_coalesce_tables_in_same_keyspace_into_refresh_keyspace() throws Exception { |
| 155 | - String keyspace = "sdactisk"; | |
| 150 | + String keyspace = TestUtils.generateIdentifier("ks_"); | |
| 156 | 151 | session2.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1)); |
| 157 | 152 | // Reset invocations as creating keyspace causes a keyspace refresh. |
| 158 | 153 | reset(controlConnection); |
| @@ -160,7 +155,7 @@ | ||
| 160 | 155 | |
| 161 | 156 | int tableCount = 3; |
| 162 | 157 | for (int i = 0; i < tableCount; i++) { |
| 163 | - session.execute(String.format(CREATE_TABLE_SIMPLE_FORMAT, keyspace + "." + "tbl" + i)); | |
| 158 | + session().execute(String.format(CREATE_TABLE_SIMPLE_FORMAT, keyspace + "." + "tbl" + i)); | |
| 164 | 159 | } |
| 165 | 160 | |
| 166 | 161 | verify(listener, timeout(DEBOUNCE_TIME * 3).times(3)).onTableAdded(any(TableMetadata.class)); |
| @@ -192,7 +187,7 @@ | ||
| 192 | 187 | */ |
| 193 | 188 | @Test(groups = "short") |
| 194 | 189 | public void should_debounce_and_coalesce_multiple_alter_events_on_same_table_into_refresh_table() throws Exception { |
| 195 | - String keyspace = "sdacmaeost"; | |
| 190 | + String keyspace = TestUtils.generateIdentifier("ks_"); | |
| 196 | 191 | String table = "tbl1"; |
| 197 | 192 | String comment = "I am changing this table."; |
| 198 | 193 | String columnName = "added_column"; |
| @@ -202,8 +197,8 @@ | ||
| 202 | 197 | reset(controlConnection); |
| 203 | 198 | reset(listener); |
| 204 | 199 | |
| 205 | - session.execute(String.format("ALTER TABLE %s.%s WITH comment = '%s'", keyspace, table, comment)); | |
| 206 | - session.execute(String.format("ALTER TABLE %s.%s ADD %s int", keyspace, table, columnName)); | |
| 200 | + session().execute(String.format("ALTER TABLE %s.%s WITH comment = '%s'", keyspace, table, comment)); | |
| 201 | + session().execute(String.format("ALTER TABLE %s.%s ADD %s int", keyspace, table, columnName)); | |
| 207 | 202 | |
| 208 | 203 | ArgumentCaptor<TableMetadata> original = forClass(TableMetadata.class); |
| 209 | 204 | ArgumentCaptor<TableMetadata> captor = forClass(TableMetadata.class); |
| @@ -245,9 +240,9 @@ | ||
| 245 | 240 | */ |
| 246 | 241 | @Test(groups = "short") |
| 247 | 242 | public void should_debounce_and_coalesce_multiple_keyspace_creates_into_refresh_entire_schema() throws Exception { |
| 248 | - String prefix = "sdacmkc"; | |
| 243 | + String prefix = TestUtils.generateIdentifier("ks_"); | |
| 249 | 244 | for (int i = 0; i < 3; i++) { |
| 250 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, prefix + i, 1)); | |
| 245 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, prefix + i, 1)); | |
| 251 | 246 | } |
| 252 | 247 | |
| 253 | 248 | verify(listener, timeout(DEBOUNCE_TIME * 3).times(3)).onKeyspaceAdded(any(KeyspaceMetadata.class)); |
| @@ -271,9 +266,9 @@ | ||
| 271 | 266 | */ |
| 272 | 267 | @Test(groups = "short") |
| 273 | 268 | public void should_refresh_when_max_pending_requests_reached() throws Exception { |
| 274 | - String prefix = "srwmprr"; | |
| 269 | + String prefix = TestUtils.generateIdentifier("ks_"); | |
| 275 | 270 | for (int i = 0; i < 5; i++) { |
| 276 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, prefix + i, 1)); | |
| 271 | + session().execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, prefix + i, 1)); | |
| 277 | 272 | } |
| 278 | 273 | |
| 279 | 274 | // Event should be processed immediately as we hit our threshold. |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaTest.java +5 −5
| @@ -36,7 +36,7 @@ | ||
| 36 | 36 | private static String withOptions; |
| 37 | 37 | |
| 38 | 38 | @Override |
| 39 | - public Collection<String> createTestFixtures() { | |
| 39 | + public void onTestContextInitialized() { | |
| 40 | 40 | |
| 41 | 41 | String sparse = String.format("CREATE TABLE %s.sparse (\n" |
| 42 | 42 | + " k text,\n" |
| @@ -131,7 +131,7 @@ | ||
| 131 | 131 | allDefs.addAll(cql3.values()); |
| 132 | 132 | allDefs.addAll(compact.values()); |
| 133 | 133 | allDefs.add(withOptions); |
| 134 | - return allDefs; | |
| 134 | + execute(allDefs); | |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | private static String stripOptions(String def, boolean keepFirst) { |
| @@ -149,7 +149,7 @@ | ||
| 149 | 149 | @Test(groups = "short") |
| 150 | 150 | public void schemaExportTest() { |
| 151 | 151 | |
| 152 | - KeyspaceMetadata metadata = cluster.getMetadata().getKeyspace(keyspace); | |
| 152 | + KeyspaceMetadata metadata = cluster().getMetadata().getKeyspace(keyspace); | |
| 153 | 153 | |
| 154 | 154 | for (Map.Entry<String, String> tableEntry : cql3.entrySet()) { |
| 155 | 155 | String table = tableEntry.getKey(); |
| @@ -167,10 +167,10 @@ | ||
| 167 | 167 | // Same remark as the preceding test |
| 168 | 168 | @Test(groups = "short") |
| 169 | 169 | public void schemaExportOptionsTest() { |
| 170 | - TableMetadata metadata = cluster.getMetadata().getKeyspace(keyspace).getTable("with_options"); | |
| 170 | + TableMetadata metadata = cluster().getMetadata().getKeyspace(keyspace).getTable("with_options"); | |
| 171 | 171 | |
| 172 | 172 | String withOpts = withOptions; |
| 173 | - VersionNumber version = TestUtils.findHost(cluster, 1).getCassandraVersion(); | |
| 173 | + VersionNumber version = TestUtils.findHost(cluster(), 1).getCassandraVersion(); | |
| 174 | 174 | |
| 175 | 175 | if (version.getMajor() == 2) { |
| 176 | 176 | // Strip the last ';' |
modified driver-core/src/test/java/com/datastax/driver/core/SessionLeakTest.java +20 −20
| @@ -39,9 +39,9 @@ | ||
| 39 | 39 | // Checking for JAVA-342 |
| 40 | 40 | channelMonitor = new SocketChannelMonitor(); |
| 41 | 41 | channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); |
| 42 | - cluster = register(Cluster.builder() | |
| 43 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 44 | - .withPort(ccm.getBinaryPort()) | |
| 42 | + Cluster cluster = register(Cluster.builder() | |
| 43 | + .addContactPoints(getContactPoints().get(0)) | |
| 44 | + .withPort(ccm().getBinaryPort()) | |
| 45 | 45 | .withNettyOptions(channelMonitor.nettyOptions()) |
| 46 | 46 | .withQueryOptions(nonDebouncingQueryOptions()) |
| 47 | 47 | .build()); |
| @@ -50,44 +50,44 @@ | ||
| 50 | 50 | |
| 51 | 51 | assertThat(cluster.manager.sessions.size()).isEqualTo(0); |
| 52 | 52 | // Should be 1 control connection after initialization. |
| 53 | - assertOpenConnections(1); | |
| 53 | + assertOpenConnections(1, cluster); | |
| 54 | 54 | |
| 55 | 55 | // ensure sessions.size() returns with 1 control connection + core pool size. |
| 56 | 56 | int corePoolSize = TestUtils.numberOfLocalCoreConnections(cluster); |
| 57 | 57 | Session session = cluster.connect(); |
| 58 | 58 | |
| 59 | 59 | assertThat(cluster.manager.sessions.size()).isEqualTo(1); |
| 60 | - assertOpenConnections(1 + corePoolSize); | |
| 60 | + assertOpenConnections(1 + corePoolSize, cluster); | |
| 61 | 61 | |
| 62 | 62 | // ensure sessions.size() returns to 0 with only 1 active connection (the control connection) |
| 63 | 63 | session.close(); |
| 64 | 64 | assertThat(cluster.manager.sessions.size()).isEqualTo(0); |
| 65 | - assertOpenConnections(1); | |
| 65 | + assertOpenConnections(1, cluster); | |
| 66 | 66 | |
| 67 | 67 | // ensure bootstrapping a node does not create additional connections |
| 68 | - ccm.add(2); | |
| 69 | - ccm.start(2); | |
| 68 | + ccm().add(2); | |
| 69 | + ccm().start(2); | |
| 70 | 70 | assertThat(cluster).host(2).comesUpWithin(2, MINUTES); |
| 71 | 71 | |
| 72 | 72 | assertThat(cluster.manager.sessions.size()).isEqualTo(0); |
| 73 | - assertOpenConnections(1); | |
| 73 | + assertOpenConnections(1, cluster); | |
| 74 | 74 | |
| 75 | 75 | // ensure a new session gets registered and core connections are established |
| 76 | 76 | // there should be corePoolSize more connections to accommodate for the new host. |
| 77 | 77 | Session thisSession = cluster.connect(); |
| 78 | 78 | assertThat(cluster.manager.sessions.size()).isEqualTo(1); |
| 79 | - assertOpenConnections(1 + (corePoolSize * 2)); | |
| 79 | + assertOpenConnections(1 + (corePoolSize * 2), cluster); | |
| 80 | 80 | |
| 81 | 81 | // ensure bootstrapping a node does not create additional connections that won't get cleaned up |
| 82 | 82 | thisSession.close(); |
| 83 | 83 | |
| 84 | 84 | assertThat(cluster.manager.sessions.size()).isEqualTo(0); |
| 85 | - assertOpenConnections(1); | |
| 85 | + assertOpenConnections(1, cluster); | |
| 86 | 86 | cluster.close(); |
| 87 | 87 | // Ensure no channels remain open. |
| 88 | 88 | channelMonitor.stop(); |
| 89 | 89 | channelMonitor.report(); |
| 90 | - assertThat(channelMonitor.openChannels(newArrayList(ccm.addressOfNode(1), ccm.addressOfNode(2))).size()).isEqualTo(0); | |
| 90 | + assertThat(channelMonitor.openChannels(newArrayList(ccm().addressOfNode(1), ccm().addressOfNode(2))).size()).isEqualTo(0); | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | @Test(groups = "short") |
| @@ -95,16 +95,16 @@ | ||
| 95 | 95 | // Checking for JAVA-806 |
| 96 | 96 | channelMonitor = new SocketChannelMonitor(); |
| 97 | 97 | channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); |
| 98 | - cluster = register(Cluster.builder() | |
| 99 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 100 | - .withPort(ccm.getBinaryPort()) | |
| 98 | + Cluster cluster = register(Cluster.builder() | |
| 99 | + .addContactPoints(getContactPoints().get(0)) | |
| 100 | + .withPort(ccm().getBinaryPort()) | |
| 101 | 101 | .withNettyOptions(channelMonitor.nettyOptions()) |
| 102 | 102 | .build()); |
| 103 | 103 | cluster.init(); |
| 104 | 104 | assertThat(cluster.manager.sessions.size()).isEqualTo(0); |
| 105 | 105 | try { |
| 106 | 106 | // Should be 1 control connection after initialization. |
| 107 | - assertOpenConnections(1); | |
| 107 | + assertOpenConnections(1, cluster); | |
| 108 | 108 | cluster.connect("wrong_keyspace"); |
| 109 | 109 | fail("Should not have connected to a wrong keyspace"); |
| 110 | 110 | } catch (InvalidQueryException e) { |
| @@ -115,11 +115,11 @@ | ||
| 115 | 115 | // Ensure no channels remain open. |
| 116 | 116 | channelMonitor.stop(); |
| 117 | 117 | channelMonitor.report(); |
| 118 | - assertThat(channelMonitor.openChannels(ccm.addressOfNode(1), ccm.addressOfNode(2)).size()).isEqualTo(0); | |
| 118 | + assertThat(channelMonitor.openChannels(ccm().addressOfNode(1), ccm().addressOfNode(2)).size()).isEqualTo(0); | |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - private void assertOpenConnections(int expected) { | |
| 122 | - assertThat((cluster.getMetrics().getOpenConnections().getValue())).isEqualTo(expected); | |
| 123 | - assertThat(channelMonitor.openChannels(ccm.addressOfNode(1), ccm.addressOfNode(2)).size()).isEqualTo(expected); | |
| 121 | + private void assertOpenConnections(int expected, Cluster cluster) { | |
| 122 | + assertThat(cluster.getMetrics().getOpenConnections().getValue()).isEqualTo(expected); | |
| 123 | + assertThat(channelMonitor.openChannels(ccm().addressOfNode(1), ccm().addressOfNode(2)).size()).isEqualTo(expected); | |
| 124 | 124 | } |
| 125 | 125 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SessionStressTest.java +10 −8
| @@ -92,7 +92,9 @@ | ||
| 92 | 92 | public void sessions_should_not_leak_connections() { |
| 93 | 93 | // override inherited field with a new cluster object and ensure 0 sessions and connections |
| 94 | 94 | channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); |
| 95 | - stressCluster = Cluster.builder().addContactPointsWithPorts(getInitialContactPoints()) | |
| 95 | + stressCluster = Cluster.builder() | |
| 96 | + .addContactPoints(getContactPoints()) | |
| 97 | + .withPort(ccm().getBinaryPort()) | |
| 96 | 98 | .withPoolingOptions(new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1)) |
| 97 | 99 | .withNettyOptions(channelMonitor.nettyOptions()).build(); |
| 98 | 100 | |
| @@ -109,13 +111,13 @@ | ||
| 109 | 111 | assertEquals(stressCluster.manager.sessions.size(), 1); |
| 110 | 112 | int coreConnections = TestUtils.numberOfLocalCoreConnections(stressCluster); |
| 111 | 113 | assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1 + coreConnections); |
| 112 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 1 + coreConnections); | |
| 114 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 1 + coreConnections); | |
| 113 | 115 | |
| 114 | 116 | // Closing the session keeps the control connection opened |
| 115 | 117 | session.close(); |
| 116 | 118 | assertEquals(stressCluster.manager.sessions.size(), 0); |
| 117 | 119 | assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1); |
| 118 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 1); | |
| 120 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 1); | |
| 119 | 121 | |
| 120 | 122 | int nbOfSessions = 2000; |
| 121 | 123 | int halfOfTheSessions = nbOfSessions / 2; |
| @@ -132,7 +134,7 @@ | ||
| 132 | 134 | assertEquals(stressCluster.manager.sessions.size(), nbOfSessions); |
| 133 | 135 | assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), |
| 134 | 136 | coreConnections * nbOfSessions + 1); |
| 135 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), coreConnections * nbOfSessions + 1); | |
| 137 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), coreConnections * nbOfSessions + 1); | |
| 136 | 138 | |
| 137 | 139 | // Close half of the sessions asynchronously |
| 138 | 140 | logger.info("Closing {}/{} sessions.", halfOfTheSessions, nbOfSessions); |
| @@ -142,7 +144,7 @@ | ||
| 142 | 144 | assertEquals(stressCluster.manager.sessions.size(), halfOfTheSessions); |
| 143 | 145 | assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), |
| 144 | 146 | coreConnections * (nbOfSessions / 2) + 1); |
| 145 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), | |
| 147 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), | |
| 146 | 148 | coreConnections * (nbOfSessions / 2) + 1); |
| 147 | 149 | |
| 148 | 150 | // Close and open the same number of sessions concurrently |
| @@ -160,7 +162,7 @@ | ||
| 160 | 162 | assertEquals(stressCluster.manager.sessions.size(), halfOfTheSessions); |
| 161 | 163 | assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), |
| 162 | 164 | coreConnections * (nbOfSessions / 2) + 1); |
| 163 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), | |
| 165 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), | |
| 164 | 166 | coreConnections * (nbOfSessions / 2) + 1); |
| 165 | 167 | |
| 166 | 168 | // Close the remaining sessions |
| @@ -170,7 +172,7 @@ | ||
| 170 | 172 | // Check that we have a clean state |
| 171 | 173 | assertEquals(stressCluster.manager.sessions.size(), 0); |
| 172 | 174 | assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1); |
| 173 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 1); | |
| 175 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 1); | |
| 174 | 176 | |
| 175 | 177 | // On OSX, the TCP connections are released after 15s by default (sysctl -a net.inet.tcp.msl) |
| 176 | 178 | logger.info("Sleeping {} seconds so that TCP connections are released by the OS", sleepTime); |
| @@ -181,7 +183,7 @@ | ||
| 181 | 183 | stressCluster = null; |
| 182 | 184 | |
| 183 | 185 | // Ensure no channels remain open. |
| 184 | - assertEquals(channelMonitor.openChannels(getInitialContactPoints()).size(), 0); | |
| 186 | + assertEquals(channelMonitor.openChannels(getContactPointsWithPorts()).size(), 0); | |
| 185 | 187 | |
| 186 | 188 | channelMonitor.stop(); |
| 187 | 189 | channelMonitor.report(); |
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +24 −25
| @@ -18,8 +18,6 @@ | ||
| 18 | 18 | import com.datastax.driver.core.utils.CassandraVersion; |
| 19 | 19 | import org.testng.annotations.Test; |
| 20 | 20 | |
| 21 | -import java.util.Arrays; | |
| 22 | -import java.util.Collection; | |
| 23 | 21 | import java.util.List; |
| 24 | 22 | import java.util.Locale; |
| 25 | 23 | import java.util.concurrent.CountDownLatch; |
| @@ -41,8 +39,8 @@ | ||
| 41 | 39 | private static final String COUNTER_TABLE = "counters"; |
| 42 | 40 | |
| 43 | 41 | @Override |
| 44 | - public Collection<String> createTestFixtures() { | |
| 45 | - return Arrays.asList(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1), | |
| 42 | + public void onTestContextInitialized() { | |
| 43 | + execute(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1), | |
| 46 | 44 | String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE2), |
| 47 | 45 | String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE3), |
| 48 | 46 | String.format("CREATE TABLE %s (k text PRIMARY KEY, c counter)", COUNTER_TABLE)); |
| @@ -52,16 +50,16 @@ | ||
| 52 | 50 | public void executeTest() throws Exception { |
| 53 | 51 | // Simple calls to all versions of the execute/executeAsync methods |
| 54 | 52 | String key = "execute_test"; |
| 55 | - ResultSet rs = session.execute(String.format(Locale.US, TestUtils.INSERT_FORMAT, TABLE1, key, "foo", 42, 24.03f)); | |
| 53 | + ResultSet rs = session().execute(String.format(Locale.US, TestUtils.INSERT_FORMAT, TABLE1, key, "foo", 42, 24.03f)); | |
| 56 | 54 | assertTrue(rs.isExhausted()); |
| 57 | 55 | |
| 58 | 56 | // execute |
| 59 | - checkExecuteResultSet(session.execute(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)), key); | |
| 60 | - checkExecuteResultSet(session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 57 | + checkExecuteResultSet(session().execute(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)), key); | |
| 58 | + checkExecuteResultSet(session().execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 61 | 59 | |
| 62 | 60 | // executeAsync |
| 63 | - checkExecuteResultSet(session.executeAsync(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).getUninterruptibly(), key); | |
| 64 | - checkExecuteResultSet(session.executeAsync(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 61 | + checkExecuteResultSet(session().executeAsync(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).getUninterruptibly(), key); | |
| 62 | + checkExecuteResultSet(session().executeAsync(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 65 | 63 | } |
| 66 | 64 | |
| 67 | 65 | @Test(groups = "short") |
| @@ -69,19 +67,19 @@ | ||
| 69 | 67 | // Simple calls to all versions of the execute/executeAsync methods for prepared statements |
| 70 | 68 | // Note: the goal is only to exercice the Session methods, PreparedStatementTest have better prepared statement tests. |
| 71 | 69 | String key = "execute_prepared_test"; |
| 72 | - ResultSet rs = session.execute(String.format(Locale.US, TestUtils.INSERT_FORMAT, TABLE2, key, "foo", 42, 24.03f)); | |
| 70 | + ResultSet rs = session().execute(String.format(Locale.US, TestUtils.INSERT_FORMAT, TABLE2, key, "foo", 42, 24.03f)); | |
| 73 | 71 | assertTrue(rs.isExhausted()); |
| 74 | 72 | |
| 75 | - PreparedStatement p = session.prepare(String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = ?", TABLE2)); | |
| 73 | + PreparedStatement p = session().prepare(String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = ?", TABLE2)); | |
| 76 | 74 | BoundStatement bs = p.bind(key); |
| 77 | 75 | |
| 78 | 76 | // executePrepared |
| 79 | - checkExecuteResultSet(session.execute(bs), key); | |
| 80 | - checkExecuteResultSet(session.execute(bs.setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 77 | + checkExecuteResultSet(session().execute(bs), key); | |
| 78 | + checkExecuteResultSet(session().execute(bs.setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 81 | 79 | |
| 82 | 80 | // executePreparedAsync |
| 83 | - checkExecuteResultSet(session.executeAsync(bs).getUninterruptibly(), key); | |
| 84 | - checkExecuteResultSet(session.executeAsync(bs.setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 81 | + checkExecuteResultSet(session().executeAsync(bs).getUninterruptibly(), key); | |
| 82 | + checkExecuteResultSet(session().executeAsync(bs.setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 85 | 83 | } |
| 86 | 84 | |
| 87 | 85 | private static void checkExecuteResultSet(ResultSet rs, String key) { |
| @@ -96,12 +94,12 @@ | ||
| 96 | 94 | |
| 97 | 95 | @Test(groups = "short") |
| 98 | 96 | public void executePreparedCounterTest() throws Exception { |
| 99 | - PreparedStatement p = session.prepare("UPDATE " + COUNTER_TABLE + " SET c = c + ? WHERE k = ?"); | |
| 97 | + PreparedStatement p = session().prepare("UPDATE " + COUNTER_TABLE + " SET c = c + ? WHERE k = ?"); | |
| 100 | 98 | |
| 101 | - session.execute(p.bind(1L, "row")); | |
| 102 | - session.execute(p.bind(1L, "row")); | |
| 99 | + session().execute(p.bind(1L, "row")); | |
| 100 | + session().execute(p.bind(1L, "row")); | |
| 103 | 101 | |
| 104 | - ResultSet rs = session.execute("SELECT * FROM " + COUNTER_TABLE); | |
| 102 | + ResultSet rs = session().execute("SELECT * FROM " + COUNTER_TABLE); | |
| 105 | 103 | List<Row> rows = rs.all(); |
| 106 | 104 | assertEquals(rows.size(), 1); |
| 107 | 105 | assertEquals(rows.get(0).getLong("c"), 2L); |
| @@ -109,7 +107,7 @@ | ||
| 109 | 107 | |
| 110 | 108 | /** |
| 111 | 109 | * Validates that a session can be established using snappy compression and executes some queries that inserts and |
| 112 | - * retrieves data using that session. | |
| 110 | + * retrieves data using that session(). | |
| 113 | 111 | * |
| 114 | 112 | * @test_category connection:compression |
| 115 | 113 | * @expected_result session established and queries made successfully using it. |
| @@ -121,7 +119,7 @@ | ||
| 121 | 119 | |
| 122 | 120 | /** |
| 123 | 121 | * Validates that a session can be established using lz4 compression and executes some queries that inserts and |
| 124 | - * retrieves data using that session. | |
| 122 | + * retrieves data using that session(). | |
| 125 | 123 | * |
| 126 | 124 | * @test_category connection:compression |
| 127 | 125 | * @expected_result session established and queries made successfully using it. |
| @@ -133,9 +131,9 @@ | ||
| 133 | 131 | } |
| 134 | 132 | |
| 135 | 133 | public void compressionTest(ProtocolOptions.Compression compression) { |
| 136 | - cluster.getConfiguration().getProtocolOptions().setCompression(compression); | |
| 134 | + cluster().getConfiguration().getProtocolOptions().setCompression(compression); | |
| 137 | 135 | try { |
| 138 | - Session compressedSession = cluster.connect(keyspace); | |
| 136 | + Session compressedSession = cluster().connect(keyspace); | |
| 139 | 137 | |
| 140 | 138 | // Simple calls to all versions of the execute/executeAsync methods |
| 141 | 139 | String key = "execute_compressed_test_" + compression; |
| @@ -153,7 +151,7 @@ | ||
| 153 | 151 | checkExecuteResultSet(compressedSession.executeAsync(new SimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); |
| 154 | 152 | |
| 155 | 153 | } finally { |
| 156 | - cluster.getConfiguration().getProtocolOptions().setCompression(ProtocolOptions.Compression.NONE); | |
| 154 | + cluster().getConfiguration().getProtocolOptions().setCompression(ProtocolOptions.Compression.NONE); | |
| 157 | 155 | } |
| 158 | 156 | } |
| 159 | 157 | |
| @@ -167,7 +165,8 @@ | ||
| 167 | 165 | // Use our own cluster and session (not the ones provided by the parent class) because we want an uninitialized cluster |
| 168 | 166 | // (note the use of newSession below) |
| 169 | 167 | final Cluster cluster = Cluster.builder() |
| 170 | - .addContactPointsWithPorts(getInitialContactPoints()) | |
| 168 | + .addContactPoints(getContactPoints()) | |
| 169 | + .withPort(ccm().getBinaryPort()) | |
| 171 | 170 | .withNettyOptions(nonQuietClusterCloseOptions) |
| 172 | 171 | .build(); |
| 173 | 172 | try { |
modified driver-core/src/test/java/com/datastax/driver/core/SingleConnectionPoolTest.java +4 −4
| @@ -32,17 +32,17 @@ | ||
| 32 | 32 | public void should_throttle_requests() { |
| 33 | 33 | // Throttle to a very low value. Even a single thread can generate a higher throughput. |
| 34 | 34 | final int maxRequests = 10; |
| 35 | - cluster.getConfiguration().getPoolingOptions() | |
| 35 | + cluster().getConfiguration().getPoolingOptions() | |
| 36 | 36 | .setMaxRequestsPerConnection(HostDistance.LOCAL, maxRequests); |
| 37 | 37 | |
| 38 | 38 | // Track in flight requests in a dedicated thread every second |
| 39 | 39 | final AtomicBoolean excessInflightQueriesSpotted = new AtomicBoolean(false); |
| 40 | - final Host host = cluster.getMetadata().getHost(ccm.addressOfNode(1)); | |
| 40 | + final Host host = cluster().getMetadata().getHost(ccm().addressOfNode(1)); | |
| 41 | 41 | ScheduledExecutorService openConnectionsWatcherExecutor = Executors.newScheduledThreadPool(1); |
| 42 | 42 | final Runnable openConnectionsWatcher = new Runnable() { |
| 43 | 43 | @Override |
| 44 | 44 | public void run() { |
| 45 | - int inFlight = session.getState().getInFlightQueries(host); | |
| 45 | + int inFlight = session().getState().getInFlightQueries(host); | |
| 46 | 46 | if (inFlight > maxRequests) |
| 47 | 47 | excessInflightQueriesSpotted.set(true); |
| 48 | 48 | } |
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | |
| 52 | 52 | // Generate the load |
| 53 | 53 | for (int i = 0; i < 10000; i++) |
| 54 | - session.executeAsync("SELECT release_version FROM system.local"); | |
| 54 | + session().executeAsync("SELECT release_version FROM system.local"); | |
| 55 | 55 | |
| 56 | 56 | openConnectionsWatcherExecutor.shutdownNow(); |
| 57 | 57 | if (excessInflightQueriesSpotted.get()) { |
modified driver-core/src/test/java/com/datastax/driver/core/SingleTokenIntegrationTest.java +3 −2
| @@ -35,7 +35,8 @@ | ||
| 35 | 35 | */ |
| 36 | 36 | @Test(groups = "short") |
| 37 | 37 | public void should_return_single_non_empty_range_when_cluster_has_one_single_token() { |
| 38 | - Metadata metadata = cluster.getMetadata(); | |
| 38 | + cluster().manager.controlConnection.refreshNodeListAndTokenMap(); | |
| 39 | + Metadata metadata = cluster().getMetadata(); | |
| 39 | 40 | Set<TokenRange> tokenRanges = metadata.getTokenRanges(); |
| 40 | 41 | assertThat(tokenRanges).hasSize(1); |
| 41 | 42 | TokenRange tokenRange = tokenRanges.iterator().next(); |
| @@ -46,7 +47,7 @@ | ||
| 46 | 47 | .isNotWrappedAround(); |
| 47 | 48 | |
| 48 | 49 | Set<Host> hostsForRange = metadata.getReplicas(keyspace, tokenRange); |
| 49 | - Host host1 = TestUtils.findHost(cluster, 1); | |
| 50 | + Host host1 = TestUtils.findHost(cluster(), 1); | |
| 50 | 51 | assertThat(hostsForRange).containsOnly(host1); |
| 51 | 52 | |
| 52 | 53 | ByteBuffer randomPartitionKey = Bytes.fromHexString("0xCAFEBABE"); |
modified driver-core/src/test/java/com/datastax/driver/core/SpeculativeExecutionIntegrationTest.java +4 −7
| @@ -17,12 +17,9 @@ | ||
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy; |
| 19 | 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | -import com.google.common.collect.Lists; | |
| 21 | 20 | import org.mockito.Mockito; |
| 22 | 21 | import org.testng.annotations.Test; |
| 23 | 22 | |
| 24 | -import java.util.Collection; | |
| 25 | - | |
| 26 | 23 | import static org.assertj.core.api.Assertions.assertThat; |
| 27 | 24 | import static org.mockito.Mockito.times; |
| 28 | 25 | |
| @@ -35,8 +32,8 @@ | ||
| 35 | 32 | TimestampGenerator timestampGenerator; |
| 36 | 33 | |
| 37 | 34 | @Override |
| 38 | - public Collection<String> createTestFixtures() { | |
| 39 | - return Lists.newArrayList("create table foo(k int primary key, v int)"); | |
| 35 | + public void onTestContextInitialized() { | |
| 36 | + execute("create table foo(k int primary key, v int)"); | |
| 40 | 37 | } |
| 41 | 38 | |
| 42 | 39 | @Override |
| @@ -59,7 +56,7 @@ | ||
| 59 | 56 | @Test(groups = "short") |
| 60 | 57 | @CassandraVersion(major = 2.1) |
| 61 | 58 | public void should_use_same_default_timestamp_for_all_executions() { |
| 62 | - Metrics.Errors errors = cluster.getMetrics().getErrorMetrics(); | |
| 59 | + Metrics.Errors errors = cluster().getMetrics().getErrorMetrics(); | |
| 63 | 60 | |
| 64 | 61 | Mockito.reset(timestampGenerator); |
| 65 | 62 | long execStartCount = errors.getSpeculativeExecutions().getCount(); |
| @@ -68,7 +65,7 @@ | ||
| 68 | 65 | for (int k = 0; k < 1000; k++) { |
| 69 | 66 | batch.add(new SimpleStatement("insert into foo(k,v) values (?,1)", k)); |
| 70 | 67 | } |
| 71 | - session.execute(batch); | |
| 68 | + session().execute(batch); | |
| 72 | 69 | |
| 73 | 70 | assertThat(errors.getSpeculativeExecutions().getCount()).isEqualTo(execStartCount + 1); |
| 74 | 71 | Mockito.verify(timestampGenerator, times(1)).next(); |
modified driver-core/src/test/java/com/datastax/driver/core/StateListenerTest.java +6 −6
| @@ -35,23 +35,23 @@ | ||
| 35 | 35 | @Test(groups = "long") |
| 36 | 36 | public void should_receive_events_when_node_states_change() throws InterruptedException { |
| 37 | 37 | TestListener listener = new TestListener(); |
| 38 | - cluster.register(listener); | |
| 38 | + cluster().register(listener); | |
| 39 | 39 | |
| 40 | 40 | listener.setExpectedEvent(ADD); |
| 41 | - ccm.add(2); | |
| 42 | - ccm.start(2); | |
| 41 | + ccm().add(2); | |
| 42 | + ccm().start(2); | |
| 43 | 43 | listener.waitForEvent(); |
| 44 | 44 | |
| 45 | 45 | listener.setExpectedEvent(DOWN); |
| 46 | - ccm.forceStop(1); | |
| 46 | + ccm().forceStop(1); | |
| 47 | 47 | listener.waitForEvent(); |
| 48 | 48 | |
| 49 | 49 | listener.setExpectedEvent(UP); |
| 50 | - ccm.start(1); | |
| 50 | + ccm().start(1); | |
| 51 | 51 | listener.waitForEvent(); |
| 52 | 52 | |
| 53 | 53 | listener.setExpectedEvent(REMOVE); |
| 54 | - ccm.decommission(2); | |
| 54 | + ccm().decommission(2); | |
| 55 | 55 | listener.waitForEvent(); |
| 56 | 56 | } |
| 57 | 57 |
modified driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java +6 −6
| @@ -42,10 +42,10 @@ | ||
| 42 | 42 | loadBalancingPolicy.customStatementsHandled.set(0); |
| 43 | 43 | |
| 44 | 44 | SimpleStatement s = new SimpleStatement("select * from system.local"); |
| 45 | - session.execute(s); | |
| 45 | + session().execute(s); | |
| 46 | 46 | assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(0); |
| 47 | 47 | |
| 48 | - session.execute(new CustomStatement(s)); | |
| 48 | + session().execute(new CustomStatement(s)); | |
| 49 | 49 | assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(1); |
| 50 | 50 | } |
| 51 | 51 | |
| @@ -54,10 +54,10 @@ | ||
| 54 | 54 | speculativeExecutionPolicy.customStatementsHandled.set(0); |
| 55 | 55 | |
| 56 | 56 | SimpleStatement s = new SimpleStatement("select * from system.local"); |
| 57 | - session.execute(s); | |
| 57 | + session().execute(s); | |
| 58 | 58 | assertThat(speculativeExecutionPolicy.customStatementsHandled.get()).isEqualTo(0); |
| 59 | 59 | |
| 60 | - session.execute(new CustomStatement(s)); | |
| 60 | + session().execute(new CustomStatement(s)); | |
| 61 | 61 | assertThat(speculativeExecutionPolicy.customStatementsHandled.get()).isEqualTo(1); |
| 62 | 62 | } |
| 63 | 63 | |
| @@ -70,10 +70,10 @@ | ||
| 70 | 70 | Statement s = new SimpleStatement("select * from system.local") |
| 71 | 71 | .setConsistencyLevel(ConsistencyLevel.TWO); |
| 72 | 72 | |
| 73 | - session.execute(s); | |
| 73 | + session().execute(s); | |
| 74 | 74 | assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(0); |
| 75 | 75 | |
| 76 | - session.execute(new CustomStatement(s)); | |
| 76 | + session().execute(new CustomStatement(s)); | |
| 77 | 77 | assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(1); |
| 78 | 78 | } |
| 79 | 79 |
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +3 −6
| @@ -15,18 +15,15 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.google.common.collect.Lists; | |
| 19 | 18 | import org.testng.annotations.Test; |
| 20 | 19 | |
| 21 | -import java.util.Collection; | |
| 22 | - | |
| 23 | 20 | import static org.assertj.core.api.Assertions.assertThat; |
| 24 | 21 | |
| 25 | 22 | @CCMConfig(clusterProvider = "createClusterBuilderNoDebouncing") |
| 26 | 23 | public class TableMetadataTest extends CCMTestsSupport { |
| 27 | 24 | @Override |
| 28 | - public Collection<String> createTestFixtures() { | |
| 29 | - return Lists.newArrayList( | |
| 25 | + public void onTestContextInitialized() { | |
| 26 | + execute( | |
| 30 | 27 | "CREATE TABLE single_quote (\n" |
| 31 | 28 | + " c1 int PRIMARY KEY\n" |
| 32 | 29 | + ") WITH comment = 'comment with single quote '' should work'" |
| @@ -35,7 +32,7 @@ | ||
| 35 | 32 | |
| 36 | 33 | @Test(groups = "short") |
| 37 | 34 | public void should_escape_single_quote_table_comment() { |
| 38 | - TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("single_quote"); | |
| 35 | + TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable("single_quote"); | |
| 39 | 36 | assertThat(table.asCQLQuery()).contains("comment with single quote '' should work"); |
| 40 | 37 | } |
| 41 | 38 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +60 −34
| @@ -37,12 +37,14 @@ | ||
| 37 | 37 | import java.net.Socket; |
| 38 | 38 | import java.nio.ByteBuffer; |
| 39 | 39 | import java.util.*; |
| 40 | -import java.util.concurrent.Callable; | |
| 41 | -import java.util.concurrent.CountDownLatch; | |
| 42 | -import java.util.concurrent.TimeUnit; | |
| 40 | +import java.util.concurrent.*; | |
| 43 | 41 | import java.util.concurrent.atomic.AtomicInteger; |
| 44 | 42 | import java.util.concurrent.locks.ReentrantLock; |
| 45 | 43 | |
| 44 | +import static com.datastax.driver.core.ConditionChecker.check; | |
| 45 | +import static java.util.concurrent.TimeUnit.MINUTES; | |
| 46 | +import static java.util.concurrent.TimeUnit.SECONDS; | |
| 47 | + | |
| 46 | 48 | /** |
| 47 | 49 | * A number of static fields/methods handy for tests. |
| 48 | 50 | */ |
| @@ -63,18 +65,14 @@ | ||
| 63 | 65 | public static final String CREATE_KEYSPACE_SIMPLE_FORMAT = "CREATE KEYSPACE %s WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : %d }"; |
| 64 | 66 | public static final String CREATE_KEYSPACE_GENERIC_FORMAT = "CREATE KEYSPACE %s WITH replication = { 'class' : '%s', %s }"; |
| 65 | 67 | |
| 66 | - public static final String SIMPLE_KEYSPACE = "ks"; | |
| 67 | - public static final String SIMPLE_TABLE = "test"; | |
| 68 | - | |
| 69 | 68 | public static final String CREATE_TABLE_SIMPLE_FORMAT = "CREATE TABLE %s (k text PRIMARY KEY, t text, i int, f float)"; |
| 70 | 69 | |
| 71 | 70 | public static final String INSERT_FORMAT = "INSERT INTO %s (k, t, i, f) VALUES ('%s', '%s', %d, %f)"; |
| 71 | + | |
| 72 | 72 | public static final String SELECT_ALL_FORMAT = "SELECT * FROM %s"; |
| 73 | 73 | |
| 74 | 74 | public static final int TEST_BASE_NODE_WAIT = SystemProperties.getInt("com.datastax.driver.TEST_BASE_NODE_WAIT", 60); |
| 75 | 75 | |
| 76 | - private static final AtomicInteger KS_COUNTER = new AtomicInteger(1); | |
| 77 | - | |
| 78 | 76 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 79 | 77 | public static BoundStatement setBoundValue(BoundStatement bs, String name, DataType type, Object value) { |
| 80 | 78 | switch (type.getName()) { |
| @@ -304,28 +302,20 @@ | ||
| 304 | 302 | // Wait for a node to be up and running |
| 305 | 303 | // This is used because there is some delay between when a node has been |
| 306 | 304 | // added through ccm and when it's actually available for querying |
| 307 | - public static void waitFor(String node, Cluster cluster) { | |
| 305 | + public static void waitForUp(String node, Cluster cluster) { | |
| 308 | 306 | waitFor(node, cluster, TEST_BASE_NODE_WAIT, false); |
| 309 | 307 | } |
| 310 | 308 | |
| 311 | - public static void waitFor(String node, Cluster cluster, int maxTry) { | |
| 312 | - waitFor(node, cluster, maxTry, false); | |
| 309 | + public static void waitForUp(String node, Cluster cluster, int timeoutSeconds) { | |
| 310 | + waitFor(node, cluster, timeoutSeconds, false); | |
| 313 | 311 | } |
| 314 | 312 | |
| 315 | 313 | public static void waitForDown(String node, Cluster cluster) { |
| 316 | 314 | waitFor(node, cluster, TEST_BASE_NODE_WAIT * 3, true); |
| 317 | 315 | } |
| 318 | 316 | |
| 319 | - public static void waitForDown(String node, Cluster cluster, int maxTry) { | |
| 320 | - waitFor(node, cluster, maxTry, true); | |
| 321 | - } | |
| 322 | - | |
| 323 | - public static void waitForDecommission(String node, Cluster cluster) { | |
| 324 | - waitFor(node, cluster, TEST_BASE_NODE_WAIT / 2, true); | |
| 325 | - } | |
| 326 | - | |
| 327 | - public static void waitForDecommission(String node, Cluster cluster, int maxTry) { | |
| 328 | - waitFor(node, cluster, maxTry, true); | |
| 317 | + public static void waitForDown(String node, Cluster cluster, int timeoutSeconds) { | |
| 318 | + waitFor(node, cluster, timeoutSeconds, true); | |
| 329 | 319 | } |
| 330 | 320 | |
| 331 | 321 | private static void waitFor(String node, Cluster cluster, int timeoutSeconds, boolean waitForDown) { |
| @@ -337,23 +327,30 @@ | ||
| 337 | 327 | // tried doing an actual query, the driver won't realize that last node is dead until |
| 338 | 328 | // keep alive kicks in, but that's a fairly long time. So we cheat and trigger a force |
| 339 | 329 | // the detection by forcing a request. |
| 340 | - if (waitForDown) { | |
| 330 | + if (waitForDown) | |
| 341 | 331 | Futures.getUnchecked(cluster.manager.submitSchemaRefresh(null, null, null)); |
| 342 | - } | |
| 343 | 332 | if (waitForDown) { |
| 344 | - ConditionChecker.awaitUntil(new HostIsDownCondition(cluster, node), timeoutSeconds * 1000); | |
| 333 | + check() | |
| 334 | + .every(1, SECONDS) | |
| 335 | + .before(timeoutSeconds, SECONDS) | |
| 336 | + .that(new HostIsDown(cluster, node)) | |
| 337 | + .becomesTrue(); | |
| 345 | 338 | } else { |
| 346 | - ConditionChecker.awaitUntil(new HostIsUpCondition(cluster, node), timeoutSeconds * 1000); | |
| 339 | + check() | |
| 340 | + .every(1, SECONDS) | |
| 341 | + .before(timeoutSeconds, SECONDS) | |
| 342 | + .that(new HostIsUp(cluster, node)) | |
| 343 | + .becomesTrue(); | |
| 347 | 344 | } |
| 348 | 345 | } |
| 349 | 346 | |
| 350 | - private static class HostIsDownCondition implements Callable<Boolean> { | |
| 347 | + private static class HostIsDown implements Callable<Boolean> { | |
| 351 | 348 | |
| 352 | 349 | private final Cluster cluster; |
| 353 | 350 | |
| 354 | 351 | private final String ip; |
| 355 | 352 | |
| 356 | - public HostIsDownCondition(Cluster cluster, String ip) { | |
| 353 | + public HostIsDown(Cluster cluster, String ip) { | |
| 357 | 354 | this.cluster = cluster; |
| 358 | 355 | this.ip = ip; |
| 359 | 356 | } |
| @@ -365,13 +362,13 @@ | ||
| 365 | 362 | } |
| 366 | 363 | } |
| 367 | 364 | |
| 368 | - private static class HostIsUpCondition implements Callable<Boolean> { | |
| 365 | + private static class HostIsUp implements Callable<Boolean> { | |
| 369 | 366 | |
| 370 | 367 | private final Cluster cluster; |
| 371 | 368 | |
| 372 | 369 | private final String ip; |
| 373 | 370 | |
| 374 | - public HostIsUpCondition(Cluster cluster, String ip) { | |
| 371 | + public HostIsUp(Cluster cluster, String ip) { | |
| 375 | 372 | this.cluster = cluster; |
| 376 | 373 | this.ip = ip; |
| 377 | 374 | } |
| @@ -476,8 +473,20 @@ | ||
| 476 | 473 | return ScassandraFactory.createServer(ipOfNode(1), binaryPort, ipOfNode(1), adminPort); |
| 477 | 474 | } |
| 478 | 475 | |
| 479 | - public static String getAvailableKeyspaceName() { | |
| 480 | - return SIMPLE_KEYSPACE + "_" + KS_COUNTER.getAndIncrement(); | |
| 476 | + private static final ConcurrentMap<String, AtomicInteger> IDENTIFIERS = new ConcurrentHashMap<String, AtomicInteger>(); | |
| 477 | + | |
| 478 | + /** | |
| 479 | + * Generates a unique CQL identifier with the given prefix. | |
| 480 | + * | |
| 481 | + * @param prefix The prefix for the identifier | |
| 482 | + * @return a unique CQL identifier. | |
| 483 | + */ | |
| 484 | + public static String generateIdentifier(String prefix) { | |
| 485 | + AtomicInteger seq = new AtomicInteger(0); | |
| 486 | + AtomicInteger previous = IDENTIFIERS.putIfAbsent(prefix, seq); | |
| 487 | + if (previous != null) | |
| 488 | + seq = previous; | |
| 489 | + return prefix + seq.incrementAndGet(); | |
| 481 | 490 | } |
| 482 | 491 | |
| 483 | 492 | // use ports in the ephemeral range |
| @@ -534,11 +543,11 @@ | ||
| 534 | 543 | }; |
| 535 | 544 | |
| 536 | 545 | public static void waitUntilPortIsUp(InetSocketAddress address) { |
| 537 | - ConditionChecker.awaitUntil(address, PORT_IS_UP, TimeUnit.SECONDS.toMillis(10)); | |
| 546 | + check().before(5, MINUTES).that(address, PORT_IS_UP).becomesTrue(); | |
| 538 | 547 | } |
| 539 | 548 | |
| 540 | 549 | public static void waitUntilPortIsDown(InetSocketAddress address) { |
| 541 | - ConditionChecker.awaitWhile(address, PORT_IS_UP, TimeUnit.SECONDS.toMillis(10)); | |
| 550 | + check().before(5, MINUTES).that(address, PORT_IS_UP).becomesFalse(); | |
| 542 | 551 | } |
| 543 | 552 | |
| 544 | 553 | public static boolean pingPort(InetAddress address, int port) { |
| @@ -608,10 +617,16 @@ | ||
| 608 | 617 | public static NettyOptions nonQuietClusterCloseOptions = new NettyOptions() { |
| 609 | 618 | @Override |
| 610 | 619 | public void onClusterClose(EventLoopGroup eventLoopGroup) { |
| 611 | - eventLoopGroup.shutdownGracefully(0, 15, TimeUnit.SECONDS).syncUninterruptibly(); | |
| 620 | + eventLoopGroup.shutdownGracefully(0, 15, SECONDS).syncUninterruptibly(); | |
| 612 | 621 | } |
| 613 | 622 | }; |
| 614 | 623 | |
| 624 | + /** | |
| 625 | + * Executes a task and catches any exception. | |
| 626 | + * | |
| 627 | + * @param task The task to execute. | |
| 628 | + * @param logException Whether to log the exception, if any. | |
| 629 | + */ | |
| 615 | 630 | public static void executeNoFail(Runnable task, boolean logException) { |
| 616 | 631 | try { |
| 617 | 632 | task.run(); |
| @@ -621,6 +636,12 @@ | ||
| 621 | 636 | } |
| 622 | 637 | } |
| 623 | 638 | |
| 639 | + /** | |
| 640 | + * Executes a task and catches any exception. | |
| 641 | + * | |
| 642 | + * @param task The task to execute. | |
| 643 | + * @param logException Whether to log the exception, if any. | |
| 644 | + */ | |
| 624 | 645 | public static void executeNoFail(Callable<?> task, boolean logException) { |
| 625 | 646 | try { |
| 626 | 647 | task.call(); |
| @@ -630,6 +651,11 @@ | ||
| 630 | 651 | } |
| 631 | 652 | } |
| 632 | 653 | |
| 654 | + /** | |
| 655 | + * Returns the system's free memory in megabytes. | |
| 656 | + * <p/> | |
| 657 | + * This includes the free physical memory + the free swap memory. | |
| 658 | + */ | |
| 633 | 659 | public static long getFreeMemoryMB() { |
| 634 | 660 | OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); |
| 635 | 661 | return (bean.getFreePhysicalMemorySize() + bean.getFreeSwapSpaceSize()) / 1024 / 1024; |
modified driver-core/src/test/java/com/datastax/driver/core/TimeoutStressTest.java +20 −21
| @@ -18,7 +18,6 @@ | ||
| 18 | 18 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 19 | 19 | import com.datastax.driver.core.policies.ConstantReconnectionPolicy; |
| 20 | 20 | import com.datastax.driver.core.utils.SocketChannelMonitor; |
| 21 | -import com.google.common.collect.Lists; | |
| 22 | 21 | import com.google.common.util.concurrent.FutureCallback; |
| 23 | 22 | import com.google.common.util.concurrent.Futures; |
| 24 | 23 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| @@ -71,8 +70,8 @@ | ||
| 71 | 70 | } |
| 72 | 71 | |
| 73 | 72 | @Override |
| 74 | - public Collection<String> createTestFixtures() { | |
| 75 | - return Lists.newArrayList( | |
| 73 | + public void onTestContextInitialized() { | |
| 74 | + execute( | |
| 76 | 75 | "create table record (\n" |
| 77 | 76 | + " name text,\n" |
| 78 | 77 | + " phone text,\n" |
| @@ -109,13 +108,13 @@ | ||
| 109 | 108 | @Test(groups = "stress") |
| 110 | 109 | public void host_state_should_be_maintained_with_timeouts() throws Exception { |
| 111 | 110 | insertRecords(); |
| 112 | - session.close(); | |
| 111 | + session().close(); | |
| 113 | 112 | |
| 114 | 113 | // Set very low timeouts. |
| 115 | - cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(CONNECTION_TIMEOUT_IN_MS); | |
| 116 | - cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(READ_TIMEOUT_IN_MS); | |
| 117 | - session = cluster.connect(keyspace); | |
| 118 | - PreparedStatement statement = session.prepare("select * from record where name=? limit 1000;"); | |
| 114 | + cluster().getConfiguration().getSocketOptions().setConnectTimeoutMillis(CONNECTION_TIMEOUT_IN_MS); | |
| 115 | + cluster().getConfiguration().getSocketOptions().setReadTimeoutMillis(READ_TIMEOUT_IN_MS); | |
| 116 | + Session newSession = cluster().connect(keyspace); | |
| 117 | + PreparedStatement statement = newSession.prepare("select * from record where name=? limit 1000;"); | |
| 119 | 118 | |
| 120 | 119 | int workers = Runtime.getRuntime().availableProcessors(); |
| 121 | 120 | ExecutorService workerPool = Executors.newFixedThreadPool(workers, |
| @@ -124,12 +123,12 @@ | ||
| 124 | 123 | AtomicBoolean stopped = new AtomicBoolean(false); |
| 125 | 124 | |
| 126 | 125 | // Ensure that we never exceed MaxConnectionsPerHost * nodes + 1 control connection. |
| 127 | - int maxConnections = TestUtils.numberOfLocalCoreConnections(cluster) * getInitialContactPoints().size() + 1; | |
| 126 | + int maxConnections = TestUtils.numberOfLocalCoreConnections(cluster()) * getContactPoints().size() + 1; | |
| 128 | 127 | |
| 129 | 128 | try { |
| 130 | 129 | Semaphore concurrentQueries = new Semaphore(CONCURRENT_QUERIES); |
| 131 | 130 | for (int i = 0; i < workers; i++) { |
| 132 | - workerPool.submit(new TimeoutStressWorker(session, statement, concurrentQueries, stopped)); | |
| 131 | + workerPool.submit(new TimeoutStressWorker(newSession, statement, concurrentQueries, stopped)); | |
| 133 | 132 | } |
| 134 | 133 | |
| 135 | 134 | long startTime = System.currentTimeMillis(); |
| @@ -138,7 +137,7 @@ | ||
| 138 | 137 | channelMonitor.report(); |
| 139 | 138 | // Some connections that are being closed may have had active requests which are delegated to the |
| 140 | 139 | // reaper for cleanup later. |
| 141 | - Collection<SocketChannel> openChannels = channelMonitor.openChannels(getInitialContactPoints()); | |
| 140 | + Collection<SocketChannel> openChannels = channelMonitor.openChannels(getContactPointsWithPorts()); | |
| 142 | 141 | |
| 143 | 142 | // Ensure that we don't exceed maximum connections. Log as warning as there will be a bit of a timing |
| 144 | 143 | // factor between retrieving open connections and checking the reaper. |
| @@ -152,28 +151,28 @@ | ||
| 152 | 151 | stopped.set(true); |
| 153 | 152 | |
| 154 | 153 | // Reset socket timeouts to allow pool to recover. |
| 155 | - cluster.getConfiguration().getSocketOptions() | |
| 154 | + cluster().getConfiguration().getSocketOptions() | |
| 156 | 155 | .setConnectTimeoutMillis(SocketOptions.DEFAULT_CONNECT_TIMEOUT_MILLIS); |
| 157 | - cluster.getConfiguration().getSocketOptions() | |
| 156 | + cluster().getConfiguration().getSocketOptions() | |
| 158 | 157 | .setReadTimeoutMillis(SocketOptions.DEFAULT_READ_TIMEOUT_MILLIS); |
| 159 | 158 | |
| 160 | 159 | logger.debug("Sleeping 20 seconds to allow connection reaper to clean up connections " + |
| 161 | 160 | "and for the pools to recover."); |
| 162 | 161 | Uninterruptibles.sleepUninterruptibly(20, TimeUnit.SECONDS); |
| 163 | 162 | |
| 164 | - Collection<SocketChannel> openChannels = channelMonitor.openChannels(getInitialContactPoints()); | |
| 163 | + Collection<SocketChannel> openChannels = channelMonitor.openChannels(getContactPointsWithPorts()); | |
| 165 | 164 | assertThat(openChannels.size()) |
| 166 | 165 | .as("Number of open connections does not meet expected: %s", openChannels) |
| 167 | 166 | .isLessThanOrEqualTo(maxConnections); |
| 168 | 167 | |
| 169 | 168 | // Each host should be in an up state. |
| 170 | - assertThat(cluster).host(1).comesUpWithin(0, TimeUnit.SECONDS); | |
| 171 | - assertThat(cluster).host(2).comesUpWithin(0, TimeUnit.SECONDS); | |
| 172 | - assertThat(cluster).host(3).comesUpWithin(0, TimeUnit.SECONDS); | |
| 169 | + assertThat(cluster()).host(1).comesUpWithin(0, TimeUnit.SECONDS); | |
| 170 | + assertThat(cluster()).host(2).comesUpWithin(0, TimeUnit.SECONDS); | |
| 171 | + assertThat(cluster()).host(3).comesUpWithin(0, TimeUnit.SECONDS); | |
| 173 | 172 | |
| 174 | - session.close(); | |
| 173 | + newSession.close(); | |
| 175 | 174 | |
| 176 | - openChannels = channelMonitor.openChannels(getInitialContactPoints()); | |
| 175 | + openChannels = channelMonitor.openChannels(getContactPointsWithPorts()); | |
| 177 | 176 | assertThat(openChannels.size()) |
| 178 | 177 | .as("Number of open connections does not meet expected: %s", openChannels) |
| 179 | 178 | .isEqualTo(1); |
| @@ -184,12 +183,12 @@ | ||
| 184 | 183 | |
| 185 | 184 | private void insertRecords() { |
| 186 | 185 | int records = 30000; |
| 187 | - PreparedStatement insertStmt = session.prepare("insert into record (name, phone, value) values (?, ?, ?)"); | |
| 186 | + PreparedStatement insertStmt = session().prepare("insert into record (name, phone, value) values (?, ?, ?)"); | |
| 188 | 187 | |
| 189 | 188 | for (int i = 0; i < records; i++) { |
| 190 | 189 | if (i % 1000 == 0) |
| 191 | 190 | logger.debug("Inserting record {}.", i); |
| 192 | - session.execute(insertStmt.bind("0", Integer.toString(i), "test")); | |
| 191 | + session().execute(insertStmt.bind("0", Integer.toString(i), "test")); | |
| 193 | 192 | } |
| 194 | 193 | logger.debug("Inserts complete."); |
| 195 | 194 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +33 −30
| @@ -25,7 +25,10 @@ | ||
| 25 | 25 | import com.google.common.collect.Lists; |
| 26 | 26 | import org.testng.annotations.Test; |
| 27 | 27 | |
| 28 | -import java.util.*; | |
| 28 | +import java.util.Collections; | |
| 29 | +import java.util.HashSet; | |
| 30 | +import java.util.List; | |
| 31 | +import java.util.Set; | |
| 29 | 32 | |
| 30 | 33 | import static com.datastax.driver.core.Assertions.assertThat; |
| 31 | 34 | |
| @@ -54,18 +57,18 @@ | ||
| 54 | 57 | public Cluster.Builder createClusterBuilder() { |
| 55 | 58 | // Only connect to node 1, which makes it easier to query system tables in should_expose_tokens_per_host() |
| 56 | 59 | LoadBalancingPolicy lbp = new WhiteListPolicy(new RoundRobinPolicy(), |
| 57 | - Collections.singleton(ccm.addressOfNode(1))); | |
| 60 | + Collections.singleton(ccm().addressOfNode(1))); | |
| 58 | 61 | return Cluster.builder() |
| 59 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 62 | + .addContactPointsWithPorts(ccm().addressOfNode(1)) | |
| 60 | 63 | .withLoadBalancingPolicy(lbp) |
| 61 | 64 | .withQueryOptions(TestUtils.nonDebouncingQueryOptions()); |
| 62 | 65 | } |
| 63 | 66 | |
| 64 | 67 | @Override |
| 65 | - public Collection<String> createTestFixtures() { | |
| 66 | - ks1 = TestUtils.getAvailableKeyspaceName(); | |
| 67 | - ks2 = TestUtils.getAvailableKeyspaceName(); | |
| 68 | - return Lists.newArrayList( | |
| 68 | + public void onTestContextInitialized() { | |
| 69 | + ks1 = TestUtils.generateIdentifier("ks_"); | |
| 70 | + ks2 = TestUtils.generateIdentifier("ks_"); | |
| 71 | + execute( | |
| 69 | 72 | String.format("CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}", ks1), |
| 70 | 73 | String.format("CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2}", ks2), |
| 71 | 74 | String.format("USE %s", ks1), |
| @@ -89,18 +92,18 @@ | ||
| 89 | 92 | */ |
| 90 | 93 | @Test(groups = "short") |
| 91 | 94 | public void should_expose_token_ranges() throws Exception { |
| 92 | - Metadata metadata = cluster.getMetadata(); | |
| 95 | + Metadata metadata = cluster().getMetadata(); | |
| 93 | 96 | |
| 94 | 97 | // Find the replica for a given partition key |
| 95 | 98 | int testKey = 1; |
| 96 | - Set<Host> replicas = metadata.getReplicas(ks1, DataType.cint().serialize(testKey, cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum())); | |
| 99 | + Set<Host> replicas = metadata.getReplicas(ks1, DataType.cint().serialize(testKey, cluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum())); | |
| 97 | 100 | assertThat(replicas).hasSize(1); |
| 98 | 101 | Host replica = replicas.iterator().next(); |
| 99 | 102 | |
| 100 | 103 | // Iterate the cluster's token ranges. For each one, use a range query to ask Cassandra which partition keys |
| 101 | 104 | // are in this range. |
| 102 | 105 | |
| 103 | - PreparedStatement rangeStmt = session.prepare("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?"); | |
| 106 | + PreparedStatement rangeStmt = session().prepare("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?"); | |
| 104 | 107 | |
| 105 | 108 | TokenRange foundRange = null; |
| 106 | 109 | for (TokenRange range : metadata.getTokenRanges()) { |
| @@ -124,7 +127,7 @@ | ||
| 124 | 127 | List<Row> rows = Lists.newArrayList(); |
| 125 | 128 | for (TokenRange subRange : range.unwrap()) { |
| 126 | 129 | Statement statement = rangeStmt.bind(subRange.getStart(), subRange.getEnd()); |
| 127 | - rows.addAll(session.execute(statement).all()); | |
| 130 | + rows.addAll(session().execute(statement).all()); | |
| 128 | 131 | } |
| 129 | 132 | return rows; |
| 130 | 133 | } |
| @@ -154,7 +157,7 @@ | ||
| 154 | 157 | @Test(groups = "short") |
| 155 | 158 | public void should_get_token_from_row_and_set_token_in_query() { |
| 156 | 159 | // get by index: |
| 157 | - Row row = session.execute("SELECT token(i) FROM foo WHERE i = 1").one(); | |
| 160 | + Row row = session().execute("SELECT token(i) FROM foo WHERE i = 1").one(); | |
| 158 | 161 | Token token = row.getToken(0); |
| 159 | 162 | assertThat(token.getType()).isEqualTo(expectedTokenType); |
| 160 | 163 | |
| @@ -162,14 +165,14 @@ | ||
| 162 | 165 | row.getPartitionKeyToken() |
| 163 | 166 | ).isEqualTo(token); |
| 164 | 167 | |
| 165 | - PreparedStatement pst = session.prepare("SELECT * FROM foo WHERE token(i) = ?"); | |
| 166 | - row = session.execute(pst.bind(token)).one(); | |
| 168 | + PreparedStatement pst = session().prepare("SELECT * FROM foo WHERE token(i) = ?"); | |
| 169 | + row = session().execute(pst.bind(token)).one(); | |
| 167 | 170 | assertThat(row.getInt(0)).isEqualTo(1); |
| 168 | 171 | |
| 169 | - row = session.execute(pst.bind().setToken(0, token)).one(); | |
| 172 | + row = session().execute(pst.bind().setToken(0, token)).one(); | |
| 170 | 173 | assertThat(row.getInt(0)).isEqualTo(1); |
| 171 | 174 | |
| 172 | - row = session.execute(pst.bind().setPartitionKeyToken(token)).one(); | |
| 175 | + row = session().execute(pst.bind().setPartitionKeyToken(token)).one(); | |
| 173 | 176 | assertThat(row.getInt(0)).isEqualTo(1); |
| 174 | 177 | } |
| 175 | 178 | |
| @@ -190,15 +193,15 @@ | ||
| 190 | 193 | @Test(groups = "short") |
| 191 | 194 | @CassandraVersion(major = 2) |
| 192 | 195 | public void should_get_token_from_row_and_set_token_in_query_with_binding_and_aliasing() { |
| 193 | - Row row = session.execute("SELECT token(i) AS t FROM foo WHERE i = 1").one(); | |
| 196 | + Row row = session().execute("SELECT token(i) AS t FROM foo WHERE i = 1").one(); | |
| 194 | 197 | Token token = row.getToken("t"); |
| 195 | 198 | assertThat(token.getType()).isEqualTo(expectedTokenType); |
| 196 | 199 | |
| 197 | - PreparedStatement pst = session.prepare("SELECT * FROM foo WHERE token(i) = :myToken"); | |
| 198 | - row = session.execute(pst.bind().setToken("myToken", token)).one(); | |
| 200 | + PreparedStatement pst = session().prepare("SELECT * FROM foo WHERE token(i) = :myToken"); | |
| 201 | + row = session().execute(pst.bind().setToken("myToken", token)).one(); | |
| 199 | 202 | assertThat(row.getInt(0)).isEqualTo(1); |
| 200 | 203 | |
| 201 | - row = session.execute("SELECT * FROM foo WHERE token(i) = ?", token).one(); | |
| 204 | + row = session().execute("SELECT * FROM foo WHERE token(i) = ?", token).one(); | |
| 202 | 205 | assertThat(row.getInt(0)).isEqualTo(1); |
| 203 | 206 | } |
| 204 | 207 | |
| @@ -214,7 +217,7 @@ | ||
| 214 | 217 | */ |
| 215 | 218 | @Test(groups = "short", expectedExceptions = InvalidTypeException.class) |
| 216 | 219 | public void should_raise_exception_when_get_token_on_non_token() { |
| 217 | - Row row = session.execute("SELECT i FROM foo WHERE i = 1").one(); | |
| 220 | + Row row = session().execute("SELECT i FROM foo WHERE i = 1").one(); | |
| 218 | 221 | row.getToken(0); |
| 219 | 222 | } |
| 220 | 223 | |
| @@ -238,7 +241,7 @@ | ||
| 238 | 241 | public void should_expose_token_ranges_per_host() { |
| 239 | 242 | checkRangesPerHost(ks1, 1); |
| 240 | 243 | checkRangesPerHost(ks2, 2); |
| 241 | - assertThat(cluster).hasValidTokenRanges(); | |
| 244 | + assertThat(cluster()).hasValidTokenRanges(); | |
| 242 | 245 | } |
| 243 | 246 | |
| 244 | 247 | private void checkRangesPerHost(String keyspace, int replicationFactor) { |
| @@ -246,8 +249,8 @@ | ||
| 246 | 249 | |
| 247 | 250 | // Get each host's ranges, the count should match the replication factor |
| 248 | 251 | for (int i = 1; i <= 3; i++) { |
| 249 | - Host host = TestUtils.findHost(cluster, i); | |
| 250 | - Set<TokenRange> hostRanges = cluster.getMetadata().getTokenRanges(keyspace, host); | |
| 252 | + Host host = TestUtils.findHost(cluster(), i); | |
| 253 | + Set<TokenRange> hostRanges = cluster().getMetadata().getTokenRanges(keyspace, host); | |
| 251 | 254 | // Special case: When using vnodes the tokens are not evenly assigned to each replica. |
| 252 | 255 | if (!useVnodes) { |
| 253 | 256 | assertThat(hostRanges).hasSize(replicationFactor * numTokens); |
| @@ -263,7 +266,7 @@ | ||
| 263 | 266 | assertThat(allRanges).hasSize(3 * numTokens); |
| 264 | 267 | |
| 265 | 268 | // And the ranges should cover the whole ring and no ranges intersect. |
| 266 | - assertThat(cluster).hasValidTokenRanges(keyspace); | |
| 269 | + assertThat(cluster()).hasValidTokenRanges(keyspace); | |
| 267 | 270 | } |
| 268 | 271 | |
| 269 | 272 | /** |
| @@ -282,15 +285,15 @@ | ||
| 282 | 285 | */ |
| 283 | 286 | @Test(groups = "short") |
| 284 | 287 | public void should_expose_tokens_per_host() { |
| 285 | - for (Host host : cluster.getMetadata().allHosts()) { | |
| 288 | + for (Host host : cluster().getMetadata().allHosts()) { | |
| 286 | 289 | assertThat(host.getTokens()).hasSize(numTokens); |
| 287 | 290 | |
| 288 | 291 | // Check against the info in the system tables, which is a bit weak since it's exactly how the metadata is |
| 289 | 292 | // constructed in the first place, but there's not much else we can do. |
| 290 | 293 | // Note that this relies on all queries going to node 1, which is why we use a WhiteList LBP in setup(). |
| 291 | 294 | Row row = (host.listenAddress == null) |
| 292 | - ? session.execute("select tokens from system.local").one() | |
| 293 | - : session.execute("select tokens from system.peers where peer = '" + host.listenAddress.getHostAddress() + "'").one(); | |
| 295 | + ? session().execute("select tokens from system.local").one() | |
| 296 | + : session().execute("select tokens from system.peers where peer = '" + host.listenAddress.getHostAddress() + "'").one(); | |
| 294 | 297 | Set<String> tokenStrings = row.getSet("tokens", String.class); |
| 295 | 298 | assertThat(tokenStrings).hasSize(numTokens); |
| 296 | 299 | Iterable<Token> tokensFromSystemTable = Iterables.transform(tokenStrings, new Function<String, Token>() { |
| @@ -318,7 +321,7 @@ | ||
| 318 | 321 | */ |
| 319 | 322 | @Test(groups = "short") |
| 320 | 323 | public void should_only_unwrap_one_range_for_all_ranges() { |
| 321 | - Set<TokenRange> ranges = cluster.getMetadata().getTokenRanges(); | |
| 324 | + Set<TokenRange> ranges = cluster().getMetadata().getTokenRanges(); | |
| 322 | 325 | |
| 323 | 326 | assertOnlyOneWrapped(ranges); |
| 324 | 327 | |
| @@ -358,7 +361,7 @@ | ||
| 358 | 361 | |
| 359 | 362 | @Test(groups = "short") |
| 360 | 363 | public void should_expose_token_and_range_creation_methods() { |
| 361 | - Metadata metadata = cluster.getMetadata(); | |
| 364 | + Metadata metadata = cluster().getMetadata(); | |
| 362 | 365 | |
| 363 | 366 | // Pick a random range |
| 364 | 367 | TokenRange range = metadata.getTokenRanges().iterator().next(); |
modified driver-core/src/test/java/com/datastax/driver/core/TracingTest.java +5 −12
| @@ -18,11 +18,8 @@ | ||
| 18 | 18 | import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy; |
| 19 | 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | 20 | import com.google.common.util.concurrent.Uninterruptibles; |
| 21 | -import org.testng.annotations.BeforeClass; | |
| 22 | 21 | import org.testng.annotations.Test; |
| 23 | 22 | |
| 24 | -import java.util.Collection; | |
| 25 | -import java.util.Collections; | |
| 26 | 23 | import java.util.List; |
| 27 | 24 | import java.util.UUID; |
| 28 | 25 | import java.util.concurrent.TimeUnit; |
| @@ -40,14 +37,10 @@ | ||
| 40 | 37 | } |
| 41 | 38 | |
| 42 | 39 | @Override |
| 43 | - public Collection<String> createTestFixtures() { | |
| 44 | - return Collections.singletonList("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 45 | - } | |
| 46 | - | |
| 47 | - @BeforeClass(groups = "short") | |
| 48 | - public void initData() { | |
| 40 | + public void onTestContextInitialized() { | |
| 41 | + execute("CREATE TABLE test (k text, v int, PRIMARY KEY (k, v))"); | |
| 49 | 42 | for (int i = 0; i < 100; i++) { |
| 50 | - session.execute(String.format("INSERT INTO test (k, v) VALUES ('%s', %d)", KEY, i)); | |
| 43 | + execute(String.format("INSERT INTO test (k, v) VALUES ('%s', %d)", KEY, i)); | |
| 51 | 44 | } |
| 52 | 45 | } |
| 53 | 46 | |
| @@ -61,7 +54,7 @@ | ||
| 61 | 54 | @Test(groups = "short") |
| 62 | 55 | public void should_have_a_different_tracingId_for_each_page() { |
| 63 | 56 | SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 64 | - ResultSet result = session.execute(st.setFetchSize(40).enableTracing()); | |
| 57 | + ResultSet result = session().execute(st.setFetchSize(40).enableTracing()); | |
| 65 | 58 | result.all(); |
| 66 | 59 | // sleep 10 seconds to make sure the trace will be complete |
| 67 | 60 | Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS); |
| @@ -93,7 +86,7 @@ | ||
| 93 | 86 | SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); |
| 94 | 87 | st.setConsistencyLevel(ConsistencyLevel.THREE).enableTracing(); |
| 95 | 88 | |
| 96 | - ResultSet result = session.execute(st); | |
| 89 | + ResultSet result = session().execute(st); | |
| 97 | 90 | // sleep 10 seconds to make sure the trace will be complete |
| 98 | 91 | Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS); |
| 99 | 92 |
modified driver-core/src/test/java/com/datastax/driver/core/TupleTest.java +61 −61
| @@ -30,8 +30,8 @@ | ||
| 30 | 30 | public class TupleTest extends CCMTestsSupport { |
| 31 | 31 | |
| 32 | 32 | @Override |
| 33 | - public Collection<String> createTestFixtures() { | |
| 34 | - return Collections.singleton("CREATE TABLE t (k int PRIMARY KEY, v frozen<tuple<int, text, float>>)"); | |
| 33 | + public void onTestContextInitialized() { | |
| 34 | + execute("CREATE TABLE t (k int PRIMARY KEY, v frozen<tuple<int, text, float>>)"); | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | @Test(groups = "short") |
| @@ -55,17 +55,17 @@ | ||
| 55 | 55 | |
| 56 | 56 | @Test(groups = "short") |
| 57 | 57 | public void simpleWriteReadTest() throws Exception { |
| 58 | - session.execute("USE " + keyspace); | |
| 59 | - PreparedStatement ins = session.prepare("INSERT INTO t(k, v) VALUES (?, ?)"); | |
| 60 | - PreparedStatement sel = session.prepare("SELECT * FROM t WHERE k=?"); | |
| 58 | + session().execute("USE " + keyspace); | |
| 59 | + PreparedStatement ins = session().prepare("INSERT INTO t(k, v) VALUES (?, ?)"); | |
| 60 | + PreparedStatement sel = session().prepare("SELECT * FROM t WHERE k=?"); | |
| 61 | 61 | |
| 62 | 62 | TupleType t = TupleType.of(DataType.cint(), DataType.text(), DataType.cfloat()); |
| 63 | 63 | |
| 64 | 64 | int k = 1; |
| 65 | 65 | TupleValue v = t.newValue(1, "a", 1.0f); |
| 66 | 66 | |
| 67 | - session.execute(ins.bind(k, v)); | |
| 68 | - TupleValue v2 = session.execute(sel.bind(k)).one().getTupleValue("v"); | |
| 67 | + session().execute(ins.bind(k, v)); | |
| 68 | + TupleValue v2 = session().execute(sel.bind(k)).one().getTupleValue("v"); | |
| 69 | 69 | |
| 70 | 70 | assertEquals(v2, v); |
| 71 | 71 | |
| @@ -73,8 +73,8 @@ | ||
| 73 | 73 | k = 2; |
| 74 | 74 | v = t.newValue(2, "b", 2.0f); |
| 75 | 75 | |
| 76 | - session.execute("INSERT INTO t(k, v) VALUES (?, ?)", k, v); | |
| 77 | - v2 = session.execute(sel.bind(k)).one().getTupleValue("v"); | |
| 76 | + session().execute("INSERT INTO t(k, v) VALUES (?, ?)", k, v); | |
| 77 | + v2 = session().execute(sel.bind(k)).one().getTupleValue("v"); | |
| 78 | 78 | |
| 79 | 79 | assertEquals(v2, v); |
| 80 | 80 | } |
| @@ -87,17 +87,17 @@ | ||
| 87 | 87 | */ |
| 88 | 88 | @Test(groups = "short") |
| 89 | 89 | public void tupleTypeTest() throws Exception { |
| 90 | - session.execute("CREATE KEYSPACE test_tuple_type " + | |
| 90 | + session().execute("CREATE KEYSPACE test_tuple_type " + | |
| 91 | 91 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 92 | - session.execute("USE test_tuple_type"); | |
| 93 | - session.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<tuple<ascii, int, boolean>>)"); | |
| 92 | + session().execute("USE test_tuple_type"); | |
| 93 | + session().execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<tuple<ascii, int, boolean>>)"); | |
| 94 | 94 | |
| 95 | 95 | TupleType t = TupleType.of(DataType.ascii(), DataType.cint(), DataType.cboolean()); |
| 96 | 96 | |
| 97 | 97 | // test non-prepared statement |
| 98 | 98 | TupleValue complete = t.newValue("foo", 123, true); |
| 99 | - session.execute("INSERT INTO mytable (a, b) VALUES (0, ?)", complete); | |
| 100 | - TupleValue r = session.execute("SELECT b FROM mytable WHERE a=0").one().getTupleValue("b"); | |
| 99 | + session().execute("INSERT INTO mytable (a, b) VALUES (0, ?)", complete); | |
| 100 | + TupleValue r = session().execute("SELECT b FROM mytable WHERE a=0").one().getTupleValue("b"); | |
| 101 | 101 | assertEquals(r, complete); |
| 102 | 102 | |
| 103 | 103 | // test incomplete tuples |
| @@ -112,8 +112,8 @@ | ||
| 112 | 112 | TupleType t1 = TupleType.of(DataType.ascii(), DataType.cint()); |
| 113 | 113 | TupleValue partial = t1.newValue("bar", 456); |
| 114 | 114 | TupleValue partionResult = t.newValue("bar", 456, null); |
| 115 | - session.execute("INSERT INTO mytable (a, b) VALUES (0, ?)", partial); | |
| 116 | - r = session.execute("SELECT b FROM mytable WHERE a=0").one().getTupleValue("b"); | |
| 115 | + session().execute("INSERT INTO mytable (a, b) VALUES (0, ?)", partial); | |
| 116 | + r = session().execute("SELECT b FROM mytable WHERE a=0").one().getTupleValue("b"); | |
| 117 | 117 | assertEquals(r, partionResult); |
| 118 | 118 | |
| 119 | 119 | // test single value tuples |
| @@ -128,20 +128,20 @@ | ||
| 128 | 128 | TupleType t2 = TupleType.of(DataType.ascii()); |
| 129 | 129 | TupleValue subpartial = t2.newValue("zoo"); |
| 130 | 130 | TupleValue subpartialResult = t.newValue("zoo", null, null); |
| 131 | - session.execute("INSERT INTO mytable (a, b) VALUES (0, ?)", subpartial); | |
| 132 | - r = session.execute("SELECT b FROM mytable WHERE a=0").one().getTupleValue("b"); | |
| 131 | + session().execute("INSERT INTO mytable (a, b) VALUES (0, ?)", subpartial); | |
| 132 | + r = session().execute("SELECT b FROM mytable WHERE a=0").one().getTupleValue("b"); | |
| 133 | 133 | assertEquals(r, subpartialResult); |
| 134 | 134 | |
| 135 | 135 | // test prepared statements |
| 136 | - PreparedStatement prepared = session.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 137 | - session.execute(prepared.bind(3, complete)); | |
| 138 | - session.execute(prepared.bind(4, partial)); | |
| 139 | - session.execute(prepared.bind(5, subpartial)); | |
| 140 | - | |
| 141 | - prepared = session.prepare("SELECT b FROM mytable WHERE a=?"); | |
| 142 | - assertEquals(session.execute(prepared.bind(3)).one().getTupleValue("b"), complete); | |
| 143 | - assertEquals(session.execute(prepared.bind(4)).one().getTupleValue("b"), partionResult); | |
| 144 | - assertEquals(session.execute(prepared.bind(5)).one().getTupleValue("b"), subpartialResult); | |
| 136 | + PreparedStatement prepared = session().prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 137 | + session().execute(prepared.bind(3, complete)); | |
| 138 | + session().execute(prepared.bind(4, partial)); | |
| 139 | + session().execute(prepared.bind(5, subpartial)); | |
| 140 | + | |
| 141 | + prepared = session().prepare("SELECT b FROM mytable WHERE a=?"); | |
| 142 | + assertEquals(session().execute(prepared.bind(3)).one().getTupleValue("b"), complete); | |
| 143 | + assertEquals(session().execute(prepared.bind(4)).one().getTupleValue("b"), partionResult); | |
| 144 | + assertEquals(session().execute(prepared.bind(5)).one().getTupleValue("b"), subpartialResult); | |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
| @@ -153,9 +153,9 @@ | ||
| 153 | 153 | */ |
| 154 | 154 | @Test(groups = "short") |
| 155 | 155 | public void tupleTestTypeVaryingLengths() throws Exception { |
| 156 | - session.execute("CREATE KEYSPACE test_tuple_type_varying_lengths " + | |
| 156 | + session().execute("CREATE KEYSPACE test_tuple_type_varying_lengths " + | |
| 157 | 157 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 158 | - session.execute("USE test_tuple_type_varying_lengths"); | |
| 158 | + session().execute("USE test_tuple_type_varying_lengths"); | |
| 159 | 159 | |
| 160 | 160 | // programmatically create the table with tuples of said sizes |
| 161 | 161 | int[] lengths = {1, 2, 3, 384}; |
| @@ -167,7 +167,7 @@ | ||
| 167 | 167 | } |
| 168 | 168 | valueSchema.add(String.format(" v_%d frozen<tuple<%s>>", i, Joiner.on(',').join(ints))); |
| 169 | 169 | } |
| 170 | - session.execute(String.format("CREATE TABLE mytable (k int PRIMARY KEY, %s)", Joiner.on(',').join(valueSchema))); | |
| 170 | + session().execute(String.format("CREATE TABLE mytable (k int PRIMARY KEY, %s)", Joiner.on(',').join(valueSchema))); | |
| 171 | 171 | |
| 172 | 172 | // insert tuples into same key using different columns |
| 173 | 173 | // and verify the results |
| @@ -183,10 +183,10 @@ | ||
| 183 | 183 | TupleValue createdTuple = t.newValue(values.toArray()); |
| 184 | 184 | |
| 185 | 185 | // write tuple |
| 186 | - session.execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 186 | + session().execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 187 | 187 | |
| 188 | 188 | // read tuple |
| 189 | - TupleValue r = session.execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)).one().getTupleValue(String.format("v_%s", i)); | |
| 189 | + TupleValue r = session().execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)).one().getTupleValue(String.format("v_%s", i)); | |
| 190 | 190 | assertEquals(r, createdTuple); |
| 191 | 191 | } |
| 192 | 192 | } |
| @@ -201,12 +201,12 @@ | ||
| 201 | 201 | public void tupleSubtypesTest() throws Exception { |
| 202 | 202 | |
| 203 | 203 | List<DataType> DATA_TYPE_PRIMITIVES = Lists.newArrayList(PrimitiveTypeSamples.ALL.keySet()); |
| 204 | - session.execute("CREATE KEYSPACE test_tuple_subtypes " + | |
| 204 | + session().execute("CREATE KEYSPACE test_tuple_subtypes " + | |
| 205 | 205 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 206 | - session.execute("USE test_tuple_subtypes"); | |
| 206 | + session().execute("USE test_tuple_subtypes"); | |
| 207 | 207 | |
| 208 | 208 | // programmatically create the table with a tuple of all datatypes |
| 209 | - session.execute(String.format("CREATE TABLE mytable (k int PRIMARY KEY, v frozen<tuple<%s>>)", Joiner.on(',').join(DATA_TYPE_PRIMITIVES))); | |
| 209 | + session().execute(String.format("CREATE TABLE mytable (k int PRIMARY KEY, v frozen<tuple<%s>>)", Joiner.on(',').join(DATA_TYPE_PRIMITIVES))); | |
| 210 | 210 | |
| 211 | 211 | // insert tuples into same key using different columns |
| 212 | 212 | // and verify the results |
| @@ -240,10 +240,10 @@ | ||
| 240 | 240 | TupleValue completeTuple = t2.newValue(completeValues.toArray()); |
| 241 | 241 | |
| 242 | 242 | // write tuple |
| 243 | - session.execute(String.format("INSERT INTO mytable (k, v) VALUES (%s, ?)", i), createdTuple); | |
| 243 | + session().execute(String.format("INSERT INTO mytable (k, v) VALUES (%s, ?)", i), createdTuple); | |
| 244 | 244 | |
| 245 | 245 | // read tuple |
| 246 | - TupleValue r = session.execute("SELECT v FROM mytable WHERE k=?", i).one().getTupleValue("v"); | |
| 246 | + TupleValue r = session().execute("SELECT v FROM mytable WHERE k=?", i).one().getTupleValue("v"); | |
| 247 | 247 | |
| 248 | 248 | assertEquals(r, completeTuple); |
| 249 | 249 | ++i; |
| @@ -260,9 +260,9 @@ | ||
| 260 | 260 | public void tupleNonPrimitiveSubTypesTest() throws Exception { |
| 261 | 261 | |
| 262 | 262 | List<DataType> DATA_TYPE_PRIMITIVES = Lists.newArrayList(PrimitiveTypeSamples.ALL.keySet()); |
| 263 | - session.execute("CREATE KEYSPACE test_tuple_non_primitive_subtypes " + | |
| 263 | + session().execute("CREATE KEYSPACE test_tuple_non_primitive_subtypes " + | |
| 264 | 264 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 265 | - session.execute("USE test_tuple_non_primitive_subtypes"); | |
| 265 | + session().execute("USE test_tuple_non_primitive_subtypes"); | |
| 266 | 266 | |
| 267 | 267 | ArrayList<String> values = new ArrayList<String>(); |
| 268 | 268 | |
| @@ -282,7 +282,7 @@ | ||
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | // create table |
| 285 | - session.execute(String.format("CREATE TABLE mytable (k int PRIMARY KEY, %s)", Joiner.on(',').join(values))); | |
| 285 | + session().execute(String.format("CREATE TABLE mytable (k int PRIMARY KEY, %s)", Joiner.on(',').join(values))); | |
| 286 | 286 | |
| 287 | 287 | |
| 288 | 288 | int i = 0; |
| @@ -299,10 +299,10 @@ | ||
| 299 | 299 | TupleValue createdTuple = t.newValue(createdValues.toArray()); |
| 300 | 300 | |
| 301 | 301 | // write tuple |
| 302 | - session.execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 302 | + session().execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 303 | 303 | |
| 304 | 304 | // read tuple |
| 305 | - TupleValue r = session.execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)) | |
| 305 | + TupleValue r = session().execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)) | |
| 306 | 306 | .one().getTupleValue(String.format("v_%s", i)); |
| 307 | 307 | |
| 308 | 308 | assertEquals(r, createdTuple); |
| @@ -322,10 +322,10 @@ | ||
| 322 | 322 | TupleValue createdTuple = t.newValue(createdValues.toArray()); |
| 323 | 323 | |
| 324 | 324 | // write tuple |
| 325 | - session.execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 325 | + session().execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 326 | 326 | |
| 327 | 327 | // read tuple |
| 328 | - TupleValue r = session.execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)) | |
| 328 | + TupleValue r = session().execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)) | |
| 329 | 329 | .one().getTupleValue(String.format("v_%s", i)); |
| 330 | 330 | |
| 331 | 331 | assertEquals(r, createdTuple); |
| @@ -348,10 +348,10 @@ | ||
| 348 | 348 | TupleValue createdTuple = t.newValue(createdValues.toArray()); |
| 349 | 349 | |
| 350 | 350 | // write tuple |
| 351 | - session.execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 351 | + session().execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (0, ?)", i), createdTuple); | |
| 352 | 352 | |
| 353 | 353 | // read tuple |
| 354 | - TupleValue r = session.execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)) | |
| 354 | + TupleValue r = session().execute(String.format("SELECT v_%s FROM mytable WHERE k=0", i)) | |
| 355 | 355 | .one().getTupleValue(String.format("v_%s", i)); |
| 356 | 356 | |
| 357 | 357 | assertEquals(r, createdTuple); |
| @@ -391,12 +391,12 @@ | ||
| 391 | 391 | */ |
| 392 | 392 | @Test(groups = "short") |
| 393 | 393 | public void nestedTuplesTest() throws Exception { |
| 394 | - session.execute("CREATE KEYSPACE test_nested_tuples " + | |
| 394 | + session().execute("CREATE KEYSPACE test_nested_tuples " + | |
| 395 | 395 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 396 | - session.execute("USE test_nested_tuples"); | |
| 396 | + session().execute("USE test_nested_tuples"); | |
| 397 | 397 | |
| 398 | 398 | // create a table with multiple sizes of nested tuples |
| 399 | - session.execute(String.format("CREATE TABLE mytable (" + | |
| 399 | + session().execute(String.format("CREATE TABLE mytable (" + | |
| 400 | 400 | "k int PRIMARY KEY, " + |
| 401 | 401 | "v_1 %s, " + |
| 402 | 402 | "v_2 %s, " + |
| @@ -411,10 +411,10 @@ | ||
| 411 | 411 | TupleValue createdTuple = nestedTuplesCreatorHelper(i); |
| 412 | 412 | |
| 413 | 413 | // write tuple |
| 414 | - session.execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (?, ?)", i), i, createdTuple); | |
| 414 | + session().execute(String.format("INSERT INTO mytable (k, v_%s) VALUES (?, ?)", i), i, createdTuple); | |
| 415 | 415 | |
| 416 | 416 | // verify tuple was written and read correctly |
| 417 | - TupleValue r = session.execute(String.format("SELECT v_%s FROM mytable WHERE k=?", i), i) | |
| 417 | + TupleValue r = session().execute(String.format("SELECT v_%s FROM mytable WHERE k=?", i), i) | |
| 418 | 418 | .one().getTupleValue(String.format("v_%s", i)); |
| 419 | 419 | |
| 420 | 420 | assertEquals(r, createdTuple); |
| @@ -430,27 +430,27 @@ | ||
| 430 | 430 | @Test(groups = "short") |
| 431 | 431 | public void testTuplesWithNulls() throws Exception { |
| 432 | 432 | // create keyspace |
| 433 | - session.execute("CREATE KEYSPACE testTuplesWithNulls " + | |
| 433 | + session().execute("CREATE KEYSPACE testTuplesWithNulls " + | |
| 434 | 434 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 435 | - session.execute("USE testTuplesWithNulls"); | |
| 435 | + session().execute("USE testTuplesWithNulls"); | |
| 436 | 436 | |
| 437 | 437 | // create UDT |
| 438 | - session.execute("CREATE TYPE user (a text, b frozen<tuple<text, int, uuid, blob>>)"); | |
| 439 | - session.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)"); | |
| 438 | + session().execute("CREATE TYPE user (a text, b frozen<tuple<text, int, uuid, blob>>)"); | |
| 439 | + session().execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)"); | |
| 440 | 440 | |
| 441 | 441 | // insert UDT data |
| 442 | - UserType userTypeDef = cluster.getMetadata().getKeyspace("testTuplesWithNulls").getUserType("user"); | |
| 442 | + UserType userTypeDef = cluster().getMetadata().getKeyspace("testTuplesWithNulls").getUserType("user"); | |
| 443 | 443 | UDTValue userType = userTypeDef.newValue(); |
| 444 | 444 | |
| 445 | 445 | TupleType t = TupleType.of(DataType.text(), DataType.cint(), DataType.uuid(), DataType.blob()); |
| 446 | 446 | TupleValue v = t.newValue(null, null, null, null); |
| 447 | 447 | userType.setTupleValue("b", v); |
| 448 | 448 | |
| 449 | - PreparedStatement ins = session.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 450 | - session.execute(ins.bind(0, userType)); | |
| 449 | + PreparedStatement ins = session().prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 450 | + session().execute(ins.bind(0, userType)); | |
| 451 | 451 | |
| 452 | 452 | // retrieve and verify data |
| 453 | - ResultSet rs = session.execute("SELECT * FROM mytable"); | |
| 453 | + ResultSet rs = session().execute("SELECT * FROM mytable"); | |
| 454 | 454 | List<Row> rows = rs.all(); |
| 455 | 455 | assertEquals(1, rows.size()); |
| 456 | 456 | |
| @@ -462,10 +462,10 @@ | ||
| 462 | 462 | // test empty strings |
| 463 | 463 | v = t.newValue("", null, null, ByteBuffer.allocate(0)); |
| 464 | 464 | userType.setTupleValue("b", v); |
| 465 | - session.execute(ins.bind(0, userType)); | |
| 465 | + session().execute(ins.bind(0, userType)); | |
| 466 | 466 | |
| 467 | 467 | // retrieve and verify data |
| 468 | - rs = session.execute("SELECT * FROM mytable"); | |
| 468 | + rs = session().execute("SELECT * FROM mytable"); | |
| 469 | 469 | rows = rs.all(); |
| 470 | 470 | assertEquals(1, rows.size()); |
| 471 | 471 |
modified driver-core/src/test/java/com/datastax/driver/core/UserTypesTest.java +80 −80
| @@ -41,11 +41,11 @@ | ||
| 41 | 41 | new ArrayList<DataType.Name>(EnumSet.of(DataType.Name.LIST, DataType.Name.SET, DataType.Name.MAP, DataType.Name.TUPLE)); |
| 42 | 42 | |
| 43 | 43 | @Override |
| 44 | - public Collection<String> createTestFixtures() { | |
| 44 | + public void onTestContextInitialized() { | |
| 45 | 45 | String type1 = "CREATE TYPE phone (alias text, number text)"; |
| 46 | 46 | String type2 = "CREATE TYPE address (street text, \"ZIP\" int, phones set<frozen<phone>>)"; |
| 47 | 47 | String table = "CREATE TABLE user (id int PRIMARY KEY, addr frozen<address>)"; |
| 48 | - return Arrays.asList(type1, type2, table); | |
| 48 | + execute(type1, type2, table); | |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
| @@ -56,20 +56,20 @@ | ||
| 56 | 56 | @Test(groups = "short") |
| 57 | 57 | public void simpleWriteReadTest() throws Exception { |
| 58 | 58 | int userId = 0; |
| 59 | - PreparedStatement ins = session.prepare("INSERT INTO user(id, addr) VALUES (?, ?)"); | |
| 60 | - PreparedStatement sel = session.prepare("SELECT * FROM user WHERE id=?"); | |
| 59 | + PreparedStatement ins = session().prepare("INSERT INTO user(id, addr) VALUES (?, ?)"); | |
| 60 | + PreparedStatement sel = session().prepare("SELECT * FROM user WHERE id=?"); | |
| 61 | 61 | |
| 62 | - UserType addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 63 | - UserType phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 62 | + UserType addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 63 | + UserType phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 64 | 64 | |
| 65 | 65 | UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); |
| 66 | 66 | UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); |
| 67 | 67 | |
| 68 | 68 | UDTValue addr = addrDef.newValue().setString("street", "1600 Pennsylvania Ave NW").setInt(quote("ZIP"), 20500).setSet("phones", ImmutableSet.of(phone1, phone2)); |
| 69 | 69 | |
| 70 | - session.execute(ins.bind(userId, addr)); | |
| 70 | + session().execute(ins.bind(userId, addr)); | |
| 71 | 71 | |
| 72 | - Row r = session.execute(sel.bind(userId)).one(); | |
| 72 | + Row r = session().execute(sel.bind(userId)).one(); | |
| 73 | 73 | |
| 74 | 74 | assertEquals(r.getInt("id"), 0); |
| 75 | 75 | assertEquals(r.getUDTValue("addr"), addr); |
| @@ -83,17 +83,17 @@ | ||
| 83 | 83 | @Test(groups = "short") |
| 84 | 84 | public void simpleUnpreparedWriteReadTest() throws Exception { |
| 85 | 85 | int userId = 1; |
| 86 | - UserType addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 87 | - UserType phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 86 | + UserType addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 87 | + UserType phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 88 | 88 | |
| 89 | 89 | UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); |
| 90 | 90 | UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); |
| 91 | 91 | |
| 92 | 92 | UDTValue addr = addrDef.newValue().setString("street", "1600 Pennsylvania Ave NW").setInt(quote("ZIP"), 20500).setSet("phones", ImmutableSet.of(phone1, phone2)); |
| 93 | 93 | |
| 94 | - session.execute("INSERT INTO user(id, addr) VALUES (?, ?)", userId, addr); | |
| 94 | + session().execute("INSERT INTO user(id, addr) VALUES (?, ?)", userId, addr); | |
| 95 | 95 | |
| 96 | - Row r = session.execute("SELECT * FROM user WHERE id=?", userId).one(); | |
| 96 | + Row r = session().execute("SELECT * FROM user WHERE id=?", userId).one(); | |
| 97 | 97 | |
| 98 | 98 | assertEquals(r.getInt("id"), 1); |
| 99 | 99 | assertEquals(r.getUDTValue("addr"), addr); |
| @@ -106,31 +106,31 @@ | ||
| 106 | 106 | */ |
| 107 | 107 | @Test(groups = "short") |
| 108 | 108 | public void nonExistingTypesTest() throws Exception { |
| 109 | - UserType addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address1"); | |
| 110 | - UserType phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone1"); | |
| 109 | + UserType addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("address1"); | |
| 110 | + UserType phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone1"); | |
| 111 | 111 | assertEquals(addrDef, null); |
| 112 | 112 | assertEquals(phoneDef, null); |
| 113 | 113 | |
| 114 | - addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 115 | - phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 114 | + addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 115 | + phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 116 | 116 | assertNotEquals(addrDef, null); |
| 117 | 117 | assertNotEquals(phoneDef, null); |
| 118 | 118 | |
| 119 | 119 | // create keyspace |
| 120 | 120 | String nonExistingKeyspace = keyspace + "_nonEx"; |
| 121 | - session.execute("CREATE KEYSPACE " + nonExistingKeyspace + " " + | |
| 121 | + session().execute("CREATE KEYSPACE " + nonExistingKeyspace + " " + | |
| 122 | 122 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 123 | - session.execute("USE " + nonExistingKeyspace); | |
| 123 | + session().execute("USE " + nonExistingKeyspace); | |
| 124 | 124 | |
| 125 | - addrDef = cluster.getMetadata().getKeyspace(nonExistingKeyspace).getUserType("address"); | |
| 126 | - phoneDef = cluster.getMetadata().getKeyspace(nonExistingKeyspace).getUserType("phone"); | |
| 125 | + addrDef = cluster().getMetadata().getKeyspace(nonExistingKeyspace).getUserType("address"); | |
| 126 | + phoneDef = cluster().getMetadata().getKeyspace(nonExistingKeyspace).getUserType("phone"); | |
| 127 | 127 | assertEquals(addrDef, null); |
| 128 | 128 | assertEquals(phoneDef, null); |
| 129 | 129 | |
| 130 | - session.execute("USE " + keyspace); | |
| 130 | + session().execute("USE " + keyspace); | |
| 131 | 131 | |
| 132 | - addrDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 133 | - phoneDef = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 132 | + addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("address"); | |
| 133 | + phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); | |
| 134 | 134 | assertNotEquals(addrDef, null); |
| 135 | 135 | assertNotEquals(phoneDef, null); |
| 136 | 136 | } |
| @@ -145,9 +145,9 @@ | ||
| 145 | 145 | public void udtSizesTest() throws Exception { |
| 146 | 146 | int MAX_TEST_LENGTH = 1024; |
| 147 | 147 | // create keyspace |
| 148 | - session.execute("CREATE KEYSPACE test_udt_sizes " + | |
| 148 | + session().execute("CREATE KEYSPACE test_udt_sizes " + | |
| 149 | 149 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 150 | - session.execute("USE test_udt_sizes"); | |
| 150 | + session().execute("USE test_udt_sizes"); | |
| 151 | 151 | |
| 152 | 152 | // create the seed udt |
| 153 | 153 | StringBuilder sb = new StringBuilder(); |
| @@ -157,13 +157,13 @@ | ||
| 157 | 157 | if (i + 1 < MAX_TEST_LENGTH) |
| 158 | 158 | sb.append(","); |
| 159 | 159 | } |
| 160 | - session.execute(String.format("CREATE TYPE lengthy_udt (%s)", sb.toString())); | |
| 160 | + session().execute(String.format("CREATE TYPE lengthy_udt (%s)", sb.toString())); | |
| 161 | 161 | |
| 162 | 162 | // create a table with multiple sizes of udts |
| 163 | - session.execute("CREATE TABLE mytable (k int PRIMARY KEY, v frozen<lengthy_udt>)"); | |
| 163 | + session().execute("CREATE TABLE mytable (k int PRIMARY KEY, v frozen<lengthy_udt>)"); | |
| 164 | 164 | |
| 165 | 165 | // hold onto the UserType for future use |
| 166 | - UserType udtDef = cluster.getMetadata().getKeyspace("test_udt_sizes").getUserType("lengthy_udt"); | |
| 166 | + UserType udtDef = cluster().getMetadata().getKeyspace("test_udt_sizes").getUserType("lengthy_udt"); | |
| 167 | 167 | |
| 168 | 168 | // verify inserts and reads |
| 169 | 169 | for (int i : Arrays.asList(0, 1, 2, 3, MAX_TEST_LENGTH)) { |
| @@ -174,10 +174,10 @@ | ||
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // write udt |
| 177 | - session.execute("INSERT INTO mytable (k, v) VALUES (0, ?)", createdUDT); | |
| 177 | + session().execute("INSERT INTO mytable (k, v) VALUES (0, ?)", createdUDT); | |
| 178 | 178 | |
| 179 | 179 | // verify udt was written and read correctly |
| 180 | - UDTValue r = session.execute("SELECT v FROM mytable WHERE k=0") | |
| 180 | + UDTValue r = session().execute("SELECT v FROM mytable WHERE k=0") | |
| 181 | 181 | .one().getUDTValue("v"); |
| 182 | 182 | assertEquals(r.toString(), createdUDT.toString()); |
| 183 | 183 | } |
| @@ -192,9 +192,9 @@ | ||
| 192 | 192 | @Test(groups = "short") |
| 193 | 193 | public void testPrimitiveDatatypes() throws Exception { |
| 194 | 194 | // create keyspace |
| 195 | - session.execute("CREATE KEYSPACE testPrimitiveDatatypes " + | |
| 195 | + session().execute("CREATE KEYSPACE testPrimitiveDatatypes " + | |
| 196 | 196 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 197 | - session.execute("USE testPrimitiveDatatypes"); | |
| 197 | + session().execute("USE testPrimitiveDatatypes"); | |
| 198 | 198 | |
| 199 | 199 | // create UDT |
| 200 | 200 | List<String> alpha_type_list = new ArrayList<String>(); |
| @@ -204,11 +204,11 @@ | ||
| 204 | 204 | DATA_TYPE_PRIMITIVES.get(i).getName())); |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - session.execute(String.format("CREATE TYPE alldatatypes (%s)", Joiner.on(',').join(alpha_type_list))); | |
| 208 | - session.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<alldatatypes>)"); | |
| 207 | + session().execute(String.format("CREATE TYPE alldatatypes (%s)", Joiner.on(',').join(alpha_type_list))); | |
| 208 | + session().execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<alldatatypes>)"); | |
| 209 | 209 | |
| 210 | 210 | // insert UDT data |
| 211 | - UserType alldatatypesDef = cluster.getMetadata().getKeyspace("testPrimitiveDatatypes").getUserType("alldatatypes"); | |
| 211 | + UserType alldatatypesDef = cluster().getMetadata().getKeyspace("testPrimitiveDatatypes").getUserType("alldatatypes"); | |
| 212 | 212 | UDTValue alldatatypes = alldatatypesDef.newValue(); |
| 213 | 213 | |
| 214 | 214 | for (int i = 0; i < DATA_TYPE_PRIMITIVES.size(); i++) { |
| @@ -265,11 +265,11 @@ | ||
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - PreparedStatement ins = session.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 269 | - session.execute(ins.bind(0, alldatatypes)); | |
| 268 | + PreparedStatement ins = session().prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 269 | + session().execute(ins.bind(0, alldatatypes)); | |
| 270 | 270 | |
| 271 | 271 | // retrieve and verify data |
| 272 | - ResultSet rs = session.execute("SELECT * FROM mytable"); | |
| 272 | + ResultSet rs = session().execute("SELECT * FROM mytable"); | |
| 273 | 273 | List<Row> rows = rs.all(); |
| 274 | 274 | assertEquals(1, rows.size()); |
| 275 | 275 | |
| @@ -288,9 +288,9 @@ | ||
| 288 | 288 | @Test(groups = "short") |
| 289 | 289 | public void testNonPrimitiveDatatypes() throws Exception { |
| 290 | 290 | // create keyspace |
| 291 | - session.execute("CREATE KEYSPACE test_nonprimitive_datatypes " + | |
| 291 | + session().execute("CREATE KEYSPACE test_nonprimitive_datatypes " + | |
| 292 | 292 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 293 | - session.execute("USE test_nonprimitive_datatypes"); | |
| 293 | + session().execute("USE test_nonprimitive_datatypes"); | |
| 294 | 294 | |
| 295 | 295 | // counters are not allowed inside collections |
| 296 | 296 | DATA_TYPE_PRIMITIVES.remove(DataType.counter()); |
| @@ -317,11 +317,11 @@ | ||
| 317 | 317 | alpha_type_list.add(typeString); |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - session.execute(String.format("CREATE TYPE alldatatypes (%s)", Joiner.on(',').join(alpha_type_list))); | |
| 321 | - session.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<alldatatypes>)"); | |
| 320 | + session().execute(String.format("CREATE TYPE alldatatypes (%s)", Joiner.on(',').join(alpha_type_list))); | |
| 321 | + session().execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<alldatatypes>)"); | |
| 322 | 322 | |
| 323 | 323 | // insert UDT data |
| 324 | - UserType alldatatypesDef = cluster.getMetadata().getKeyspace("test_nonprimitive_datatypes").getUserType("alldatatypes"); | |
| 324 | + UserType alldatatypesDef = cluster().getMetadata().getKeyspace("test_nonprimitive_datatypes").getUserType("alldatatypes"); | |
| 325 | 325 | UDTValue alldatatypes = alldatatypesDef.newValue(); |
| 326 | 326 | |
| 327 | 327 | for (int i = 0; i < DATA_TYPE_NON_PRIMITIVE_NAMES.size(); i++) |
| @@ -346,11 +346,11 @@ | ||
| 346 | 346 | } |
| 347 | 347 | } |
| 348 | 348 | |
| 349 | - PreparedStatement ins = session.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 350 | - session.execute(ins.bind(0, alldatatypes)); | |
| 349 | + PreparedStatement ins = session().prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 350 | + session().execute(ins.bind(0, alldatatypes)); | |
| 351 | 351 | |
| 352 | 352 | // retrieve and verify data |
| 353 | - ResultSet rs = session.execute("SELECT * FROM mytable"); | |
| 353 | + ResultSet rs = session().execute("SELECT * FROM mytable"); | |
| 354 | 354 | List<Row> rows = rs.all(); |
| 355 | 355 | assertEquals(1, rows.size()); |
| 356 | 356 | |
| @@ -370,41 +370,41 @@ | ||
| 370 | 370 | public void udtNestedTest() throws Exception { |
| 371 | 371 | final int MAX_NESTING_DEPTH = 4; |
| 372 | 372 | // create keyspace |
| 373 | - session.execute("CREATE KEYSPACE udtNestedTest " + | |
| 373 | + session().execute("CREATE KEYSPACE udtNestedTest " + | |
| 374 | 374 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 375 | - session.execute("USE udtNestedTest"); | |
| 375 | + session().execute("USE udtNestedTest"); | |
| 376 | 376 | |
| 377 | 377 | // create UDT |
| 378 | - session.execute("CREATE TYPE depth_0 (age int, name text)"); | |
| 378 | + session().execute("CREATE TYPE depth_0 (age int, name text)"); | |
| 379 | 379 | |
| 380 | 380 | for (int i = 1; i <= MAX_NESTING_DEPTH; i++) { |
| 381 | - session.execute(String.format("CREATE TYPE depth_%s (value frozen<depth_%s>)", String.valueOf(i), String.valueOf(i - 1))); | |
| 381 | + session().execute(String.format("CREATE TYPE depth_%s (value frozen<depth_%s>)", String.valueOf(i), String.valueOf(i - 1))); | |
| 382 | 382 | } |
| 383 | 383 | |
| 384 | - session.execute(String.format("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<depth_0>, c frozen<depth_1>, d frozen<depth_2>, e frozen<depth_3>," + | |
| 384 | + session().execute(String.format("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<depth_0>, c frozen<depth_1>, d frozen<depth_2>, e frozen<depth_3>," + | |
| 385 | 385 | "f frozen<depth_%s>)", MAX_NESTING_DEPTH)); |
| 386 | 386 | |
| 387 | 387 | // insert UDT data |
| 388 | - UserType depthZeroDef = cluster.getMetadata().getKeyspace("udtNestedTest").getUserType("depth_0"); | |
| 388 | + UserType depthZeroDef = cluster().getMetadata().getKeyspace("udtNestedTest").getUserType("depth_0"); | |
| 389 | 389 | UDTValue depthZero = depthZeroDef.newValue().setInt("age", 42).setString("name", "Bob"); |
| 390 | 390 | |
| 391 | - UserType depthOneDef = cluster.getMetadata().getKeyspace("udtNestedTest").getUserType("depth_1"); | |
| 391 | + UserType depthOneDef = cluster().getMetadata().getKeyspace("udtNestedTest").getUserType("depth_1"); | |
| 392 | 392 | UDTValue depthOne = depthOneDef.newValue().setUDTValue("value", depthZero); |
| 393 | 393 | |
| 394 | - UserType depthTwoDef = cluster.getMetadata().getKeyspace("udtNestedTest").getUserType("depth_2"); | |
| 394 | + UserType depthTwoDef = cluster().getMetadata().getKeyspace("udtNestedTest").getUserType("depth_2"); | |
| 395 | 395 | UDTValue depthTwo = depthTwoDef.newValue().setUDTValue("value", depthOne); |
| 396 | 396 | |
| 397 | - UserType depthThreeDef = cluster.getMetadata().getKeyspace("udtNestedTest").getUserType("depth_3"); | |
| 397 | + UserType depthThreeDef = cluster().getMetadata().getKeyspace("udtNestedTest").getUserType("depth_3"); | |
| 398 | 398 | UDTValue depthThree = depthThreeDef.newValue().setUDTValue("value", depthTwo); |
| 399 | 399 | |
| 400 | - UserType depthFourDef = cluster.getMetadata().getKeyspace("udtNestedTest").getUserType("depth_4"); | |
| 400 | + UserType depthFourDef = cluster().getMetadata().getKeyspace("udtNestedTest").getUserType("depth_4"); | |
| 401 | 401 | UDTValue depthFour = depthFourDef.newValue().setUDTValue("value", depthThree); |
| 402 | 402 | |
| 403 | - PreparedStatement ins = session.prepare("INSERT INTO mytable (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)"); | |
| 404 | - session.execute(ins.bind(0, depthZero, depthOne, depthTwo, depthThree, depthFour)); | |
| 403 | + PreparedStatement ins = session().prepare("INSERT INTO mytable (a, b, c, d, e, f) VALUES (?, ?, ?, ?, ?, ?)"); | |
| 404 | + session().execute(ins.bind(0, depthZero, depthOne, depthTwo, depthThree, depthFour)); | |
| 405 | 405 | |
| 406 | 406 | // retrieve and verify data |
| 407 | - ResultSet rs = session.execute("SELECT * FROM mytable"); | |
| 407 | + ResultSet rs = session().execute("SELECT * FROM mytable"); | |
| 408 | 408 | List<Row> rows = rs.all(); |
| 409 | 409 | assertEquals(1, rows.size()); |
| 410 | 410 | |
| @@ -427,23 +427,23 @@ | ||
| 427 | 427 | @Test(groups = "short") |
| 428 | 428 | public void testUdtsWithNulls() throws Exception { |
| 429 | 429 | // create keyspace |
| 430 | - session.execute("CREATE KEYSPACE testUdtsWithNulls " + | |
| 430 | + session().execute("CREATE KEYSPACE testUdtsWithNulls " + | |
| 431 | 431 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 432 | - session.execute("USE testUdtsWithNulls"); | |
| 432 | + session().execute("USE testUdtsWithNulls"); | |
| 433 | 433 | |
| 434 | 434 | // create UDT |
| 435 | - session.execute("CREATE TYPE user (a text, b int, c uuid, d blob)"); | |
| 436 | - session.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)"); | |
| 435 | + session().execute("CREATE TYPE user (a text, b int, c uuid, d blob)"); | |
| 436 | + session().execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)"); | |
| 437 | 437 | |
| 438 | 438 | // insert UDT data |
| 439 | - UserType userTypeDef = cluster.getMetadata().getKeyspace("testUdtsWithNulls").getUserType("user"); | |
| 439 | + UserType userTypeDef = cluster().getMetadata().getKeyspace("testUdtsWithNulls").getUserType("user"); | |
| 440 | 440 | UDTValue userType = userTypeDef.newValue().setString("a", null).setInt("b", 0).setUUID("c", null).setBytes("d", null); |
| 441 | 441 | |
| 442 | - PreparedStatement ins = session.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 443 | - session.execute(ins.bind(0, userType)); | |
| 442 | + PreparedStatement ins = session().prepare("INSERT INTO mytable (a, b) VALUES (?, ?)"); | |
| 443 | + session().execute(ins.bind(0, userType)); | |
| 444 | 444 | |
| 445 | 445 | // retrieve and verify data |
| 446 | - ResultSet rs = session.execute("SELECT * FROM mytable"); | |
| 446 | + ResultSet rs = session().execute("SELECT * FROM mytable"); | |
| 447 | 447 | List<Row> rows = rs.all(); |
| 448 | 448 | assertEquals(1, rows.size()); |
| 449 | 449 | |
| @@ -454,10 +454,10 @@ | ||
| 454 | 454 | |
| 455 | 455 | // test empty strings |
| 456 | 456 | userType = userTypeDef.newValue().setString("a", "").setInt("b", 0).setUUID("c", null).setBytes("d", ByteBuffer.allocate(0)); |
| 457 | - session.execute(ins.bind(0, userType)); | |
| 457 | + session().execute(ins.bind(0, userType)); | |
| 458 | 458 | |
| 459 | 459 | // retrieve and verify data |
| 460 | - rs = session.execute("SELECT * FROM mytable"); | |
| 460 | + rs = session().execute("SELECT * FROM mytable"); | |
| 461 | 461 | rows = rs.all(); |
| 462 | 462 | assertEquals(1, rows.size()); |
| 463 | 463 | |
| @@ -475,38 +475,38 @@ | ||
| 475 | 475 | @Test(groups = "short") |
| 476 | 476 | public void testUdtsWithCollectionNulls() throws Exception { |
| 477 | 477 | // create keyspace |
| 478 | - session.execute("CREATE KEYSPACE testUdtsWithCollectionNulls " + | |
| 478 | + session().execute("CREATE KEYSPACE testUdtsWithCollectionNulls " + | |
| 479 | 479 | "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); |
| 480 | - session.execute("USE testUdtsWithCollectionNulls"); | |
| 480 | + session().execute("USE testUdtsWithCollectionNulls"); | |
| 481 | 481 | |
| 482 | 482 | // create UDT |
| 483 | - session.execute("CREATE TYPE user (a List<text>, b Set<text>, c Map<text, text>, d frozen<Tuple<text>>)"); | |
| 484 | - session.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)"); | |
| 483 | + session().execute("CREATE TYPE user (a List<text>, b Set<text>, c Map<text, text>, d frozen<Tuple<text>>)"); | |
| 484 | + session().execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)"); | |
| 485 | 485 | |
| 486 | 486 | // insert null UDT data |
| 487 | - PreparedStatement ins = session.prepare("INSERT INTO mytable (a, b) " + | |
| 487 | + PreparedStatement ins = session().prepare("INSERT INTO mytable (a, b) " + | |
| 488 | 488 | "VALUES (0, { a: ?, b: ?, c: ?, d: ? })"); |
| 489 | - session.execute(ins.bind().setList(0, null).setSet(1, null).setMap(2, null).setTupleValue(3, null)); | |
| 489 | + session().execute(ins.bind().setList(0, null).setSet(1, null).setMap(2, null).setTupleValue(3, null)); | |
| 490 | 490 | |
| 491 | 491 | // retrieve and verify data |
| 492 | - ResultSet rs = session.execute("SELECT * FROM mytable"); | |
| 492 | + ResultSet rs = session().execute("SELECT * FROM mytable"); | |
| 493 | 493 | List<Row> rows = rs.all(); |
| 494 | 494 | assertEquals(1, rows.size()); |
| 495 | 495 | |
| 496 | 496 | Row row = rows.get(0); |
| 497 | 497 | assertEquals(row.getInt("a"), 0); |
| 498 | 498 | |
| 499 | - UserType userTypeDef = cluster.getMetadata().getKeyspace("testUdtsWithCollectionNulls").getUserType("user"); | |
| 499 | + UserType userTypeDef = cluster().getMetadata().getKeyspace("testUdtsWithCollectionNulls").getUserType("user"); | |
| 500 | 500 | UDTValue userType = userTypeDef.newValue().setList("a", null).setSet("b", null).setMap("c", null).setTupleValue("d", null); |
| 501 | 501 | assertEquals(row.getUDTValue("b"), userType); |
| 502 | 502 | |
| 503 | 503 | // test missing UDT args |
| 504 | - ins = session.prepare("INSERT INTO mytable (a, b) " + | |
| 504 | + ins = session().prepare("INSERT INTO mytable (a, b) " + | |
| 505 | 505 | "VALUES (1, { a: ? })"); |
| 506 | - session.execute(ins.bind().setList(0, new ArrayList<Object>())); | |
| 506 | + session().execute(ins.bind().setList(0, new ArrayList<Object>())); | |
| 507 | 507 | |
| 508 | 508 | // retrieve and verify data |
| 509 | - rs = session.execute("SELECT * FROM mytable"); | |
| 509 | + rs = session().execute("SELECT * FROM mytable"); | |
| 510 | 510 | rows = rs.all(); |
| 511 | 511 | assertEquals(2, rows.size()); |
| 512 | 512 |
modified driver-core/src/test/java/com/datastax/driver/core/exceptions/ExceptionsTest.java +10 −10
| @@ -53,35 +53,35 @@ | ||
| 53 | 53 | }; |
| 54 | 54 | |
| 55 | 55 | // Create the schema once |
| 56 | - session.execute(cqlCommands[0]); | |
| 57 | - session.execute(cqlCommands[1]); | |
| 58 | - session.execute(cqlCommands[2]); | |
| 56 | + session().execute(cqlCommands[0]); | |
| 57 | + session().execute(cqlCommands[1]); | |
| 58 | + session().execute(cqlCommands[2]); | |
| 59 | 59 | |
| 60 | 60 | // Try creating the keyspace again |
| 61 | 61 | try { |
| 62 | - session.execute(cqlCommands[0]); | |
| 62 | + session().execute(cqlCommands[0]); | |
| 63 | 63 | } catch (AlreadyExistsException e) { |
| 64 | 64 | String expected = String.format("Keyspace %s already exists", keyspace.toLowerCase()); |
| 65 | 65 | assertEquals(e.getMessage(), expected); |
| 66 | 66 | assertEquals(e.getKeyspace(), keyspace.toLowerCase()); |
| 67 | 67 | assertEquals(e.getTable(), null); |
| 68 | 68 | assertEquals(e.wasTableCreation(), false); |
| 69 | - assertEquals(e.getHost(), ccm.addressOfNode(1).getAddress()); | |
| 70 | - assertEquals(e.getAddress(), ccm.addressOfNode(1)); | |
| 69 | + assertEquals(e.getHost(), ccm().addressOfNode(1).getAddress()); | |
| 70 | + assertEquals(e.getAddress(), ccm().addressOfNode(1)); | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - session.execute(cqlCommands[1]); | |
| 73 | + session().execute(cqlCommands[1]); | |
| 74 | 74 | |
| 75 | 75 | // Try creating the table again |
| 76 | 76 | try { |
| 77 | - session.execute(cqlCommands[2]); | |
| 77 | + session().execute(cqlCommands[2]); | |
| 78 | 78 | } catch (AlreadyExistsException e) { |
| 79 | 79 | // is released |
| 80 | 80 | assertEquals(e.getKeyspace(), keyspace.toLowerCase()); |
| 81 | 81 | assertEquals(e.getTable(), table.toLowerCase()); |
| 82 | 82 | assertEquals(e.wasTableCreation(), true); |
| 83 | - assertEquals(e.getHost(), ccm.addressOfNode(1).getAddress()); | |
| 84 | - assertEquals(e.getAddress(), ccm.addressOfNode(1)); | |
| 83 | + assertEquals(e.getHost(), ccm().addressOfNode(1).getAddress()); | |
| 84 | + assertEquals(e.getAddress(), ccm().addressOfNode(1)); | |
| 85 | 85 | } |
| 86 | 86 | } |
| 87 | 87 |
modified driver-core/src/test/java/com/datastax/driver/core/policies/CloseableLoadBalancingPolicyTest.java +3 −3
| @@ -30,8 +30,8 @@ | ||
| 30 | 30 | @Test(groups = "short") |
| 31 | 31 | public void should_be_invoked_at_shutdown() { |
| 32 | 32 | try { |
| 33 | - cluster.connect(); | |
| 34 | - cluster.close(); | |
| 33 | + cluster().connect(); | |
| 34 | + cluster().close(); | |
| 35 | 35 | } finally { |
| 36 | 36 | assertThat(policy.wasClosed).isTrue(); |
| 37 | 37 | } |
| @@ -41,7 +41,7 @@ | ||
| 41 | 41 | public Cluster.Builder createClusterBuilder() { |
| 42 | 42 | policy = new CloseMonitoringPolicy(Policies.defaultLoadBalancingPolicy()); |
| 43 | 43 | return Cluster.builder() |
| 44 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 44 | + .addContactPoints(getContactPoints().get(0)) | |
| 45 | 45 | .withLoadBalancingPolicy(policy); |
| 46 | 46 | } |
| 47 | 47 |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicyIntegrationTest.java +18 −7
| @@ -37,19 +37,30 @@ | ||
| 37 | 37 | * receivedResponses in primed responses. |
| 38 | 38 | * If that becomes possible in the future, we could refactor this test. |
| 39 | 39 | */ |
| 40 | -@CCMConfig(dirtiesContext = true, createCluster = false, numberOfNodes = 3) | |
| 40 | +@CCMConfig( | |
| 41 | + dirtiesContext = true, | |
| 42 | + createCluster = false, | |
| 43 | + numberOfNodes = 3, | |
| 44 | + config = { | |
| 45 | + // tests fail often with write or read timeouts | |
| 46 | + "hinted_handoff_enabled:true", | |
| 47 | + "phi_convict_threshold:5", | |
| 48 | + "read_request_timeout_in_ms:100000", | |
| 49 | + "write_request_timeout_in_ms:100000" | |
| 50 | + } | |
| 51 | +) | |
| 41 | 52 | public class DowngradingConsistencyRetryPolicyIntegrationTest extends CCMTestsSupport { |
| 42 | 53 | |
| 43 | 54 | @Test(groups = "long") |
| 44 | 55 | public void should_downgrade_if_not_enough_replicas_for_requested_CL() { |
| 45 | 56 | Cluster cluster = register(Cluster.builder() |
| 46 | - .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 47 | - .withPort(ccm.getBinaryPort()) | |
| 57 | + .addContactPoints(getContactPoints().get(0)) | |
| 58 | + .withPort(ccm().getBinaryPort()) | |
| 48 | 59 | .withRetryPolicy(Mockito.spy(DowngradingConsistencyRetryPolicy.INSTANCE)) |
| 49 | 60 | .build()); |
| 50 | 61 | Session session = cluster.connect(); |
| 51 | 62 | |
| 52 | - String ks = TestUtils.getAvailableKeyspaceName(); | |
| 63 | + String ks = TestUtils.generateIdentifier("ks_"); | |
| 53 | 64 | session.execute(String.format("CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}", ks)); |
| 54 | 65 | // tests fail randomly here with InvalidQueryException: Keyspace 'xxx' does not exist |
| 55 | 66 | Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS); |
| @@ -79,9 +90,9 @@ | ||
| 79 | 90 | } |
| 80 | 91 | |
| 81 | 92 | private void stopAndWait(int node, Cluster cluster) { |
| 82 | - ccm.stop(node); | |
| 83 | - ccm.waitForDown(node);// this uses port ping | |
| 84 | - TestUtils.waitForDown(ipOfNode(node), cluster, 10); // this uses UP/DOWN events | |
| 93 | + ccm().stop(node); | |
| 94 | + ccm().waitForDown(node);// this uses port ping | |
| 95 | + TestUtils.waitForDown(ipOfNode(node), cluster); // this uses UP/DOWN events | |
| 85 | 96 | } |
| 86 | 97 | |
| 87 | 98 | private void checkAchievedConsistency(ConsistencyLevel requested, ConsistencyLevel expected, Session session) { |
modified driver-core/src/test/java/com/datastax/driver/core/policies/TokenAwarePolicyTest.java +3 −3
| @@ -323,13 +323,13 @@ | ||
| 323 | 323 | // given: a 3 node cluster with a keyspace with RF 1. |
| 324 | 324 | Cluster cluster = register(Cluster.builder() |
| 325 | 325 | .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) |
| 326 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 327 | - .withPort(ccm.getBinaryPort()) | |
| 326 | + .addContactPoints(getContactPoints().get(0)) | |
| 327 | + .withPort(ccm().getBinaryPort()) | |
| 328 | 328 | .build()); |
| 329 | 329 | Session session = cluster.connect(); |
| 330 | 330 | |
| 331 | 331 | String table = "composite"; |
| 332 | - String ks = TestUtils.getAvailableKeyspaceName(); | |
| 332 | + String ks = TestUtils.generateIdentifier("ks_"); | |
| 333 | 333 | session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, ks, 1)); |
| 334 | 334 | session.execute("USE " + ks); |
| 335 | 335 | session.execute(String.format("CREATE TABLE %s (k1 int, k2 int, i int, PRIMARY KEY ((k1, k2)))", table)); |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilder21ExecutionTest.java +10 −13
| @@ -22,9 +22,6 @@ | ||
| 22 | 22 | import com.datastax.driver.core.utils.CassandraVersion; |
| 23 | 23 | import org.testng.annotations.Test; |
| 24 | 24 | |
| 25 | -import java.util.Arrays; | |
| 26 | -import java.util.Collection; | |
| 27 | - | |
| 28 | 25 | import static com.datastax.driver.core.Assertions.assertThat; |
| 29 | 26 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 30 | 27 | |
| @@ -32,9 +29,9 @@ | ||
| 32 | 29 | public class QueryBuilder21ExecutionTest extends CCMTestsSupport { |
| 33 | 30 | |
| 34 | 31 | @Override |
| 35 | - public Collection<String> createTestFixtures() { | |
| 32 | + public void onTestContextInitialized() { | |
| 36 | 33 | // Taken from http://www.datastax.com/dev/blog/cql-in-2-1 |
| 37 | - return Arrays.asList( | |
| 34 | + execute( | |
| 38 | 35 | "CREATE TABLE products (id int PRIMARY KEY, description text, price int, categories set<text>, buyers list<int>, features_keys map<text, text>, features_values map<text, text>)", |
| 39 | 36 | "CREATE INDEX cat_index ON products(categories)", |
| 40 | 37 | "CREATE INDEX buyers_index ON products(buyers)", |
| @@ -51,11 +48,11 @@ | ||
| 51 | 48 | |
| 52 | 49 | @Test(groups = "short") |
| 53 | 50 | public void should_handle_contains_on_set_with_index() { |
| 54 | - PreparedStatement byCategory = session.prepare(select("id", "description", "categories") | |
| 51 | + PreparedStatement byCategory = session().prepare(select("id", "description", "categories") | |
| 55 | 52 | .from("products") |
| 56 | 53 | .where(contains("categories", bindMarker("category")))); |
| 57 | 54 | |
| 58 | - ResultSet results = session.execute(byCategory.bind().setString("category", "hdtv")); | |
| 55 | + ResultSet results = session().execute(byCategory.bind().setString("category", "hdtv")); | |
| 59 | 56 | |
| 60 | 57 | assertThat(results.getAvailableWithoutFetching()).isEqualTo(2); |
| 61 | 58 | for (Row row : results) { |
| @@ -65,11 +62,11 @@ | ||
| 65 | 62 | |
| 66 | 63 | @Test(groups = "short") |
| 67 | 64 | public void should_handle_contains_on_list_with_index() { |
| 68 | - PreparedStatement byBuyer = session.prepare(select("id", "description", "buyers") | |
| 65 | + PreparedStatement byBuyer = session().prepare(select("id", "description", "buyers") | |
| 69 | 66 | .from("products") |
| 70 | 67 | .where(contains("buyers", bindMarker("buyer")))); |
| 71 | 68 | |
| 72 | - ResultSet results = session.execute(byBuyer.bind().setInt("buyer", 4)); | |
| 69 | + ResultSet results = session().execute(byBuyer.bind().setInt("buyer", 4)); | |
| 73 | 70 | |
| 74 | 71 | Row row = results.one(); |
| 75 | 72 | assertThat(row).isNotNull(); |
| @@ -79,11 +76,11 @@ | ||
| 79 | 76 | |
| 80 | 77 | @Test(groups = "short") |
| 81 | 78 | public void should_handle_contains_on_map_with_index() { |
| 82 | - PreparedStatement byFeatures = session.prepare(select("id", "description", "features_values") | |
| 79 | + PreparedStatement byFeatures = session().prepare(select("id", "description", "features_values") | |
| 83 | 80 | .from("products") |
| 84 | 81 | .where(contains("features_values", bindMarker("feature")))); |
| 85 | 82 | |
| 86 | - ResultSet results = session.execute(byFeatures.bind().setString("feature", "LED")); | |
| 83 | + ResultSet results = session().execute(byFeatures.bind().setString("feature", "LED")); | |
| 87 | 84 | |
| 88 | 85 | Row row = results.one(); |
| 89 | 86 | assertThat(row).isNotNull(); |
| @@ -94,11 +91,11 @@ | ||
| 94 | 91 | |
| 95 | 92 | @Test(groups = "short") |
| 96 | 93 | public void should_handle_contains_key_on_map_with_index() { |
| 97 | - PreparedStatement byFeatures = session.prepare(select("id", "description", "features_keys") | |
| 94 | + PreparedStatement byFeatures = session().prepare(select("id", "description", "features_keys") | |
| 98 | 95 | .from("products") |
| 99 | 96 | .where(containsKey("features_keys", bindMarker("feature")))); |
| 100 | 97 | |
| 101 | - ResultSet results = session.execute(byFeatures.bind().setString("feature", "refresh-rate")); | |
| 98 | + ResultSet results = session().execute(byFeatures.bind().setString("feature", "refresh-rate")); | |
| 102 | 99 | |
| 103 | 100 | Row row = results.one(); |
| 104 | 101 | assertThat(row).isNotNull(); |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +34 −31
| @@ -18,7 +18,10 @@ | ||
| 18 | 18 | import com.datastax.driver.core.*; |
| 19 | 19 | import org.testng.annotations.Test; |
| 20 | 20 | |
| 21 | -import java.util.*; | |
| 21 | +import java.util.Date; | |
| 22 | +import java.util.List; | |
| 23 | +import java.util.Map; | |
| 24 | +import java.util.Set; | |
| 22 | 25 | |
| 23 | 26 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 24 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -30,8 +33,8 @@ | ||
| 30 | 33 | private static final String TABLE1 = "test1"; |
| 31 | 34 | |
| 32 | 35 | @Override |
| 33 | - public Collection<String> createTestFixtures() { | |
| 34 | - return Arrays.asList( | |
| 36 | + public void onTestContextInitialized() { | |
| 37 | + execute( | |
| 35 | 38 | String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, TABLE1), |
| 36 | 39 | "CREATE TABLE dateTest (t timestamp PRIMARY KEY)", |
| 37 | 40 | "CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>, c set<text>)"); |
| @@ -40,10 +43,10 @@ | ||
| 40 | 43 | @Test(groups = "short") |
| 41 | 44 | public void executeTest() throws Exception { |
| 42 | 45 | |
| 43 | - session.execute(insertInto(TABLE1).value("k", "k1").value("t", "This is a test").value("i", 3).value("f", 0.42)); | |
| 44 | - session.execute(update(TABLE1).with(set("t", "Another test")).where(eq("k", "k2"))); | |
| 46 | + session().execute(insertInto(TABLE1).value("k", "k1").value("t", "This is a test").value("i", 3).value("f", 0.42)); | |
| 47 | + session().execute(update(TABLE1).with(set("t", "Another test")).where(eq("k", "k2"))); | |
| 45 | 48 | |
| 46 | - List<Row> rows = session.execute(select().from(TABLE1).where(in("k", "k1", "k2"))).all(); | |
| 49 | + List<Row> rows = session().execute(select().from(TABLE1).where(in("k", "k1", "k2"))).all(); | |
| 47 | 50 | |
| 48 | 51 | assertEquals(2, rows.size()); |
| 49 | 52 | |
| @@ -64,9 +67,9 @@ | ||
| 64 | 67 | public void dateHandlingTest() throws Exception { |
| 65 | 68 | |
| 66 | 69 | Date d = new Date(); |
| 67 | - session.execute(insertInto("dateTest").value("t", d)); | |
| 70 | + session().execute(insertInto("dateTest").value("t", d)); | |
| 68 | 71 | String query = select().from("dateTest").where(eq(token("t"), fcall("token", d))).toString(); |
| 69 | - List<Row> rows = session.execute(query).all(); | |
| 72 | + List<Row> rows = session().execute(query).all(); | |
| 70 | 73 | |
| 71 | 74 | assertEquals(1, rows.size()); |
| 72 | 75 | |
| @@ -91,9 +94,9 @@ | ||
| 91 | 94 | public void batchNonBuiltStatementTest() throws Exception { |
| 92 | 95 | SimpleStatement simple = new SimpleStatement("INSERT INTO " + TABLE1 + " (k, t) VALUES ('batchTest1', 'val1')"); |
| 93 | 96 | RegularStatement built = insertInto(TABLE1).value("k", "batchTest2").value("t", "val2"); |
| 94 | - session.execute(batch().add(simple).add(built)); | |
| 97 | + session().execute(batch().add(simple).add(built)); | |
| 95 | 98 | |
| 96 | - List<Row> rows = session.execute(select().from(TABLE1).where(in("k", "batchTest1", "batchTest2"))).all(); | |
| 99 | + List<Row> rows = session().execute(select().from(TABLE1).where(in("k", "batchTest1", "batchTest2"))).all(); | |
| 97 | 100 | assertEquals(2, rows.size()); |
| 98 | 101 | |
| 99 | 102 | Row r1 = rows.get(0); |
| @@ -108,75 +111,75 @@ | ||
| 108 | 111 | @Test(groups = "short") |
| 109 | 112 | public void should_delete_list_element() throws Exception { |
| 110 | 113 | //given |
| 111 | - session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)"); | |
| 114 | + session().execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)"); | |
| 112 | 115 | //when |
| 113 | 116 | BuiltStatement statement = delete().listElt("a", 1).from("test_coll").where(eq("k", 1)); |
| 114 | - session.execute(statement); | |
| 117 | + session().execute(statement); | |
| 115 | 118 | //then |
| 116 | - List<Integer> actual = session.execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class); | |
| 119 | + List<Integer> actual = session().execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class); | |
| 117 | 120 | assertThat(actual).containsExactly(1, 3); |
| 118 | 121 | } |
| 119 | 122 | |
| 120 | 123 | @Test(groups = "short") |
| 121 | 124 | public void should_delete_list_element_with_bind_marker() throws Exception { |
| 122 | 125 | //given |
| 123 | - session.execute("INSERT INTO test_coll (k, a) VALUES (1, [1,2,3])"); | |
| 126 | + session().execute("INSERT INTO test_coll (k, a) VALUES (1, [1,2,3])"); | |
| 124 | 127 | //when |
| 125 | 128 | BuiltStatement statement = delete().listElt("a", bindMarker()).from("test_coll").where(eq("k", 1)); |
| 126 | - PreparedStatement ps = session.prepare(statement); | |
| 127 | - session.execute(ps.bind(1)); | |
| 129 | + PreparedStatement ps = session().prepare(statement); | |
| 130 | + session().execute(ps.bind(1)); | |
| 128 | 131 | //then |
| 129 | - List<Integer> actual = session.execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class); | |
| 132 | + List<Integer> actual = session().execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class); | |
| 130 | 133 | assertThat(actual).containsExactly(1, 3); |
| 131 | 134 | } |
| 132 | 135 | |
| 133 | 136 | @Test(groups = "short") |
| 134 | 137 | public void should_delete_set_element() throws Exception { |
| 135 | 138 | //given |
| 136 | - session.execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); | |
| 139 | + session().execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); | |
| 137 | 140 | //when |
| 138 | 141 | BuiltStatement statement = delete().setElt("c", "foo").from("test_coll").where(eq("k", 1)); |
| 139 | - session.execute(statement); | |
| 142 | + session().execute(statement); | |
| 140 | 143 | //then |
| 141 | - Set<String> actual = session.execute("SELECT c FROM test_coll WHERE k = 1").one().getSet("c", String.class); | |
| 144 | + Set<String> actual = session().execute("SELECT c FROM test_coll WHERE k = 1").one().getSet("c", String.class); | |
| 142 | 145 | assertThat(actual).containsOnly("bar", "qix"); |
| 143 | 146 | } |
| 144 | 147 | |
| 145 | 148 | @Test(groups = "short") |
| 146 | 149 | public void should_delete_set_element_with_bind_marker() throws Exception { |
| 147 | 150 | //given |
| 148 | - session.execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); | |
| 151 | + session().execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); | |
| 149 | 152 | //when |
| 150 | 153 | BuiltStatement statement = delete().setElt("c", bindMarker()).from("test_coll").where(eq("k", 1)); |
| 151 | - PreparedStatement ps = session.prepare(statement); | |
| 152 | - session.execute(ps.bind("foo")); | |
| 154 | + PreparedStatement ps = session().prepare(statement); | |
| 155 | + session().execute(ps.bind("foo")); | |
| 153 | 156 | //then |
| 154 | - Set<String> actual = session.execute("SELECT c FROM test_coll WHERE k = 1").one().getSet("c", String.class); | |
| 157 | + Set<String> actual = session().execute("SELECT c FROM test_coll WHERE k = 1").one().getSet("c", String.class); | |
| 155 | 158 | assertThat(actual).containsOnly("bar", "qix"); |
| 156 | 159 | } |
| 157 | 160 | |
| 158 | 161 | @Test(groups = "short") |
| 159 | 162 | public void should_delete_map_entry() throws Exception { |
| 160 | 163 | //given |
| 161 | - session.execute("INSERT INTO test_coll (k, b) VALUES (1, {1:'foo', 2:'bar'})"); | |
| 164 | + session().execute("INSERT INTO test_coll (k, b) VALUES (1, {1:'foo', 2:'bar'})"); | |
| 162 | 165 | //when |
| 163 | 166 | BuiltStatement statement = delete().mapElt("b", 1).from("test_coll").where(eq("k", 1)); |
| 164 | - session.execute(statement); | |
| 167 | + session().execute(statement); | |
| 165 | 168 | //then |
| 166 | - Map<Integer, String> actual = session.execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class); | |
| 169 | + Map<Integer, String> actual = session().execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class); | |
| 167 | 170 | assertThat(actual).containsExactly(entry(2, "bar")); |
| 168 | 171 | } |
| 169 | 172 | |
| 170 | 173 | @Test(groups = "short") |
| 171 | 174 | public void should_delete_map_entry_with_bind_marker() throws Exception { |
| 172 | 175 | //given |
| 173 | - session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); | |
| 176 | + session().execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); | |
| 174 | 177 | //when |
| 175 | 178 | BuiltStatement statement = delete().mapElt("b", bindMarker()).from("test_coll").where(eq("k", 1)); |
| 176 | - PreparedStatement ps = session.prepare(statement); | |
| 177 | - session.execute(ps.bind().setInt(0, 1)); | |
| 179 | + PreparedStatement ps = session().prepare(statement); | |
| 180 | + session().execute(ps.bind().setInt(0, 1)); | |
| 178 | 181 | //then |
| 179 | - Map<Integer, String> actual = session.execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class); | |
| 182 | + Map<Integer, String> actual = session().execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class); | |
| 180 | 183 | assertThat(actual).containsExactly(entry(2, "bar")); |
| 181 | 184 | } |
| 182 | 185 |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +22 −23
| @@ -21,7 +21,6 @@ | ||
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | 22 | |
| 23 | 23 | import java.util.Arrays; |
| 24 | -import java.util.Collection; | |
| 25 | 24 | import java.util.TreeSet; |
| 26 | 25 | |
| 27 | 26 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| @@ -34,8 +33,8 @@ | ||
| 34 | 33 | private static final String TABLE_INT = "test_int"; |
| 35 | 34 | |
| 36 | 35 | @Override |
| 37 | - public Collection<String> createTestFixtures() { | |
| 38 | - return Arrays.asList(String.format("CREATE TABLE %s (k text PRIMARY KEY, a int, b int)", TABLE_TEXT), | |
| 36 | + public void onTestContextInitialized() { | |
| 37 | + execute(String.format("CREATE TABLE %s (k text PRIMARY KEY, a int, b int)", TABLE_TEXT), | |
| 39 | 38 | String.format("CREATE TABLE %s (k int PRIMARY KEY, a int, b int)", TABLE_INT)); |
| 40 | 39 | } |
| 41 | 40 | |
| @@ -43,14 +42,14 @@ | ||
| 43 | 42 | public void remainingDeleteTests() throws Exception { |
| 44 | 43 | |
| 45 | 44 | Statement query; |
| 46 | - TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT); | |
| 45 | + TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT); | |
| 47 | 46 | assertNotNull(table); |
| 48 | 47 | |
| 49 | 48 | String expected = String.format("DELETE k FROM %s.test_text;", keyspace); |
| 50 | 49 | query = delete("k").from(table); |
| 51 | 50 | assertEquals(query.toString(), expected); |
| 52 | 51 | try { |
| 53 | - session.execute(query); | |
| 52 | + session().execute(query); | |
| 54 | 53 | fail(); |
| 55 | 54 | } catch (SyntaxError e) { |
| 56 | 55 | // Missing WHERE clause |
| @@ -65,16 +64,16 @@ | ||
| 65 | 64 | PreparedStatement ps; |
| 66 | 65 | BoundStatement bs; |
| 67 | 66 | |
| 68 | - session.execute("CREATE KEYSPACE foo WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};"); | |
| 69 | - session.execute("CREATE TABLE foo.foo ( k ascii PRIMARY KEY , i int, s ascii )"); | |
| 70 | - session.execute("USE foo"); | |
| 67 | + session().execute("CREATE KEYSPACE foo WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};"); | |
| 68 | + session().execute("CREATE TABLE foo.foo ( k ascii PRIMARY KEY , i int, s ascii )"); | |
| 69 | + session().execute("USE foo"); | |
| 71 | 70 | |
| 72 | 71 | query = "SELECT * FROM foo WHERE k=?;"; |
| 73 | 72 | select = select().all().from("foo").where(eq("k", QueryBuilder.bindMarker())); |
| 74 | - ps = session.prepare(select.toString()); | |
| 73 | + ps = session().prepare(select.toString()); | |
| 75 | 74 | bs = ps.bind(); |
| 76 | 75 | assertEquals(select.toString(), query); |
| 77 | - session.execute(bs.setString("k", "4 AND c=5")); | |
| 76 | + session().execute(bs.setString("k", "4 AND c=5")); | |
| 78 | 77 | |
| 79 | 78 | query = "SELECT * FROM foo WHERE k='4'' AND c=''5';"; |
| 80 | 79 | select = select().all().from("foo").where(eq("k", "4' AND c='5")); |
| @@ -211,58 +210,58 @@ | ||
| 211 | 210 | @Test(groups = "short") |
| 212 | 211 | @CassandraVersion(major = 2.0, minor = 7, description = "DELETE..IF EXISTS only supported in 2.0.7+ (CASSANDRA-5708)") |
| 213 | 212 | public void conditionalDeletesTest() throws Exception { |
| 214 | - session.execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace)); | |
| 213 | + session().execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace)); | |
| 215 | 214 | |
| 216 | 215 | Statement delete; |
| 217 | 216 | Row row; |
| 218 | 217 | delete = delete().from(keyspace, TABLE_INT).where(eq("k", 2)).ifExists(); |
| 219 | - row = session.execute(delete).one(); | |
| 218 | + row = session().execute(delete).one(); | |
| 220 | 219 | assertFalse(row.getBool("[applied]")); |
| 221 | 220 | |
| 222 | 221 | delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).ifExists(); |
| 223 | - row = session.execute(delete).one(); | |
| 222 | + row = session().execute(delete).one(); | |
| 224 | 223 | assertTrue(row.getBool("[applied]")); |
| 225 | 224 | |
| 226 | - session.execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace)); | |
| 225 | + session().execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace)); | |
| 227 | 226 | |
| 228 | 227 | delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 2)); |
| 229 | - row = session.execute(delete).one(); | |
| 228 | + row = session().execute(delete).one(); | |
| 230 | 229 | assertFalse(row.getBool("[applied]")); |
| 231 | 230 | |
| 232 | 231 | delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 1)); |
| 233 | - row = session.execute(delete).one(); | |
| 232 | + row = session().execute(delete).one(); | |
| 234 | 233 | assertTrue(row.getBool("[applied]")); |
| 235 | 234 | } |
| 236 | 235 | |
| 237 | 236 | @Test(groups = "short") |
| 238 | 237 | @CassandraVersion(major = 2.0, minor = 13, description = "Allow IF EXISTS for UPDATE statements (CASSANDRA-8610)") |
| 239 | 238 | public void conditionalUpdatesTest() throws Exception { |
| 240 | - session.execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace)); | |
| 239 | + session().execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace)); | |
| 241 | 240 | |
| 242 | 241 | Statement update; |
| 243 | 242 | Row row; |
| 244 | 243 | update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 2)).ifExists(); |
| 245 | - row = session.execute(update).one(); | |
| 244 | + row = session().execute(update).one(); | |
| 246 | 245 | assertFalse(row.getBool("[applied]")); |
| 247 | 246 | |
| 248 | 247 | update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 1)).ifExists(); |
| 249 | - row = session.execute(update).one(); | |
| 248 | + row = session().execute(update).one(); | |
| 250 | 249 | assertTrue(row.getBool("[applied]")); |
| 251 | 250 | |
| 252 | 251 | update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 2)).onlyIf(eq("a", 1)).and(eq("b", 2)); |
| 253 | - row = session.execute(update).one(); | |
| 252 | + row = session().execute(update).one(); | |
| 254 | 253 | assertFalse(row.getBool("[applied]")); |
| 255 | 254 | |
| 256 | 255 | update = update(TABLE_INT).with(set("a", 3)).and(set("b", 3)).where(eq("k", 1)).onlyIf(eq("a", 2)).and(eq("b", 2)); |
| 257 | - row = session.execute(update).one(); | |
| 256 | + row = session().execute(update).one(); | |
| 258 | 257 | assertTrue(row.getBool("[applied]")); |
| 259 | 258 | |
| 260 | 259 | update = update(TABLE_INT).with(set("a", 4)).and(set("b", 4)).onlyIf(eq("a", 2)).and(eq("b", 2)).where(eq("k", 1)); |
| 261 | - row = session.execute(update).one(); | |
| 260 | + row = session().execute(update).one(); | |
| 262 | 261 | assertFalse(row.getBool("[applied]")); |
| 263 | 262 | |
| 264 | 263 | update = update(TABLE_INT).with(set("a", 4)).and(set("b", 4)).onlyIf(eq("a", 3)).and(eq("b", 3)).where(eq("k", 1)); |
| 265 | - row = session.execute(update).one(); | |
| 264 | + row = session().execute(update).one(); | |
| 266 | 265 | assertTrue(row.getBool("[applied]")); |
| 267 | 266 | } |
| 268 | 267 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +13 −15
| @@ -19,8 +19,6 @@ | ||
| 19 | 19 | import org.testng.annotations.Test; |
| 20 | 20 | |
| 21 | 21 | import java.nio.ByteBuffer; |
| 22 | -import java.util.Arrays; | |
| 23 | -import java.util.Collection; | |
| 24 | 22 | |
| 25 | 23 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 26 | 24 | import static org.testng.Assert.assertEquals; |
| @@ -33,8 +31,8 @@ | ||
| 33 | 31 | private static final String TABLE_INT = "test_int"; |
| 34 | 32 | |
| 35 | 33 | @Override |
| 36 | - public Collection<String> createTestFixtures() { | |
| 37 | - return Arrays.asList(String.format("CREATE TABLE %s (k text PRIMARY KEY, a int, b int)", TABLE_TEXT), | |
| 34 | + public void onTestContextInitialized() { | |
| 35 | + execute(String.format("CREATE TABLE %s (k text PRIMARY KEY, a int, b int)", TABLE_TEXT), | |
| 38 | 36 | String.format("CREATE TABLE %s (k int PRIMARY KEY, a int, b int)", TABLE_INT)); |
| 39 | 37 | } |
| 40 | 38 | |
| @@ -42,17 +40,17 @@ | ||
| 42 | 40 | public void textRoutingKeyTest() throws Exception { |
| 43 | 41 | |
| 44 | 42 | Statement query; |
| 45 | - TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT); | |
| 43 | + TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT); | |
| 46 | 44 | assertNotNull(table); |
| 47 | 45 | |
| 48 | 46 | String txt = "If she weighs the same as a duck... she's made of wood."; |
| 49 | 47 | query = insertInto(table).values(new String[]{"k", "a", "b"}, new Object[]{txt, 1, 2}); |
| 50 | 48 | assertEquals(query.getRoutingKey(), ByteBuffer.wrap(txt.getBytes())); |
| 51 | - session.execute(query); | |
| 49 | + session().execute(query); | |
| 52 | 50 | |
| 53 | 51 | query = select().from(table).where(eq("k", txt)); |
| 54 | 52 | assertEquals(query.getRoutingKey(), ByteBuffer.wrap(txt.getBytes())); |
| 55 | - Row row = session.execute(query).one(); | |
| 53 | + Row row = session().execute(query).one(); | |
| 56 | 54 | assertEquals(row.getString("k"), txt); |
| 57 | 55 | assertEquals(row.getInt("a"), 1); |
| 58 | 56 | assertEquals(row.getInt("b"), 2); |
| @@ -62,18 +60,18 @@ | ||
| 62 | 60 | public void intRoutingKeyTest() throws Exception { |
| 63 | 61 | |
| 64 | 62 | Statement query; |
| 65 | - TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); | |
| 63 | + TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); | |
| 66 | 64 | assertNotNull(table); |
| 67 | 65 | |
| 68 | 66 | query = insertInto(table).values(new String[]{"k", "a", "b"}, new Object[]{42, 1, 2}); |
| 69 | 67 | ByteBuffer bb = ByteBuffer.allocate(4); |
| 70 | 68 | bb.putInt(0, 42); |
| 71 | 69 | assertEquals(query.getRoutingKey(), bb); |
| 72 | - session.execute(query); | |
| 70 | + session().execute(query); | |
| 73 | 71 | |
| 74 | 72 | query = select().from(table).where(eq("k", 42)); |
| 75 | 73 | assertEquals(query.getRoutingKey(), bb); |
| 76 | - Row row = session.execute(query).one(); | |
| 74 | + Row row = session().execute(query).one(); | |
| 77 | 75 | assertEquals(row.getInt("k"), 42); |
| 78 | 76 | assertEquals(row.getInt("a"), 1); |
| 79 | 77 | assertEquals(row.getInt("b"), 2); |
| @@ -83,7 +81,7 @@ | ||
| 83 | 81 | public void intRoutingBatchKeyTest() throws Exception { |
| 84 | 82 | |
| 85 | 83 | RegularStatement query; |
| 86 | - TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); | |
| 84 | + TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); | |
| 87 | 85 | assertNotNull(table); |
| 88 | 86 | |
| 89 | 87 | ByteBuffer bb = ByteBuffer.allocate(4); |
| @@ -103,7 +101,7 @@ | ||
| 103 | 101 | .add(update(table).using(ttl(400))); |
| 104 | 102 | assertEquals(batch.getRoutingKey(), bb); |
| 105 | 103 | assertEquals(batch.toString(), batch_query); |
| 106 | - // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL | |
| 104 | + // TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL | |
| 107 | 105 | |
| 108 | 106 | batch_query = "BEGIN BATCH "; |
| 109 | 107 | batch_query += String.format("SELECT * FROM %s.test_int WHERE k=42;", keyspace); |
| @@ -111,7 +109,7 @@ | ||
| 111 | 109 | batch = batch(query); |
| 112 | 110 | assertEquals(batch.getRoutingKey(), bb); |
| 113 | 111 | assertEquals(batch.toString(), batch_query); |
| 114 | - // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL | |
| 112 | + // TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL | |
| 115 | 113 | |
| 116 | 114 | batch_query = "BEGIN BATCH "; |
| 117 | 115 | batch_query += "SELECT * FROM foo WHERE k=42;"; |
| @@ -119,7 +117,7 @@ | ||
| 119 | 117 | batch = batch().add(select().from("foo").where(eq("k", 42))); |
| 120 | 118 | assertEquals(batch.getRoutingKey(), null); |
| 121 | 119 | assertEquals(batch.toString(), batch_query); |
| 122 | - // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL | |
| 120 | + // TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL | |
| 123 | 121 | |
| 124 | 122 | batch_query = "BEGIN BATCH USING TIMESTAMP 42 "; |
| 125 | 123 | batch_query += "INSERT INTO foo.bar (a) VALUES (123);"; |
| @@ -127,6 +125,6 @@ | ||
| 127 | 125 | batch = batch().using(timestamp(42)).add(insertInto("foo", "bar").value("a", 123)); |
| 128 | 126 | assertEquals(batch.getRoutingKey(), null); |
| 129 | 127 | assertEquals(batch.toString(), batch_query); |
| 130 | - // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL | |
| 128 | + // TODO: rs = session().execute(batch); // Not guaranteed to be valid CQL | |
| 131 | 129 | } |
| 132 | 130 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderUDTExecutionTest.java +10 −12
| @@ -22,8 +22,6 @@ | ||
| 22 | 22 | import org.testng.annotations.Test; |
| 23 | 23 | |
| 24 | 24 | import java.net.InetAddress; |
| 25 | -import java.util.Arrays; | |
| 26 | -import java.util.Collection; | |
| 27 | 25 | import java.util.List; |
| 28 | 26 | import java.util.Map; |
| 29 | 27 | |
| @@ -35,22 +33,22 @@ | ||
| 35 | 33 | public class QueryBuilderUDTExecutionTest extends CCMTestsSupport { |
| 36 | 34 | |
| 37 | 35 | @Override |
| 38 | - public Collection<String> createTestFixtures() { | |
| 39 | - return Arrays.asList("CREATE TYPE udt (i int, a inet)", | |
| 36 | + public void onTestContextInitialized() { | |
| 37 | + execute("CREATE TYPE udt (i int, a inet)", | |
| 40 | 38 | "CREATE TABLE udtTest(k int PRIMARY KEY, t frozen<udt>, l list<frozen<udt>>, m map<int, frozen<udt>>)"); |
| 41 | 39 | } |
| 42 | 40 | |
| 43 | 41 | @Test(groups = "short") |
| 44 | 42 | public void insertUdtTest() throws Exception { |
| 45 | - UserType udtType = cluster.getMetadata().getKeyspace(keyspace).getUserType("udt"); | |
| 43 | + UserType udtType = cluster().getMetadata().getKeyspace(keyspace).getUserType("udt"); | |
| 46 | 44 | UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); |
| 47 | 45 | |
| 48 | 46 | Statement insert = insertInto("udtTest").value("k", 1).value("t", udtValue); |
| 49 | 47 | assertEquals(insert.toString(), "INSERT INTO udtTest (k,t) VALUES (1,{i:2, a:'127.0.0.1'});"); |
| 50 | 48 | |
| 51 | - session.execute(insert); | |
| 49 | + session().execute(insert); | |
| 52 | 50 | |
| 53 | - List<Row> rows = session.execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 51 | + List<Row> rows = session().execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 54 | 52 | |
| 55 | 53 | assertEquals(rows.size(), 1); |
| 56 | 54 | |
| @@ -60,16 +58,16 @@ | ||
| 60 | 58 | |
| 61 | 59 | @Test(groups = "short") |
| 62 | 60 | public void should_handle_collections_of_UDT() throws Exception { |
| 63 | - UserType udtType = cluster.getMetadata().getKeyspace(keyspace).getUserType("udt"); | |
| 61 | + UserType udtType = cluster().getMetadata().getKeyspace(keyspace).getUserType("udt"); | |
| 64 | 62 | UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); |
| 65 | 63 | UDTValue udtValue2 = udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost")); |
| 66 | 64 | |
| 67 | 65 | Statement insert = insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue)); |
| 68 | 66 | assertThat(insert.toString()).isEqualTo("INSERT INTO udtTest (k,l) VALUES (1,[{i:2, a:'127.0.0.1'}]);"); |
| 69 | 67 | |
| 70 | - session.execute(insert); | |
| 68 | + session().execute(insert); | |
| 71 | 69 | |
| 72 | - List<Row> rows = session.execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 70 | + List<Row> rows = session().execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 73 | 71 | |
| 74 | 72 | assertThat(rows.size()).isEqualTo(1); |
| 75 | 73 | |
| @@ -83,9 +81,9 @@ | ||
| 83 | 81 | assertThat(updateMap.toString()) |
| 84 | 82 | .isEqualTo("UPDATE udtTest SET m=m+{0:{i:2, a:'127.0.0.1'},2:{i:3, a:'127.0.0.1'}} WHERE k=1;"); |
| 85 | 83 | |
| 86 | - session.execute(updateMap); | |
| 84 | + session().execute(updateMap); | |
| 87 | 85 | |
| 88 | - rows = session.execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 86 | + rows = session().execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 89 | 87 | r1 = rows.get(0); |
| 90 | 88 | assertThat(r1.getMap("m", Integer.class, UDTValue.class)).isEqualTo(map); |
| 91 | 89 | } |
modified driver-core/src/test/java/com/datastax/driver/core/schemabuilder/SchemaBuilderIT.java +22 −22
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | @CassandraVersion(major = 2.1, minor = 2) |
| 36 | 36 | public void should_modify_table_metadata() { |
| 37 | 37 | // Create a table |
| 38 | - session.execute(SchemaBuilder.createTable("ks", "TableMetadata") | |
| 38 | + session().execute(SchemaBuilder.createTable("ks", "TableMetadata") | |
| 39 | 39 | .addPartitionKey("a", DataType.cint()) |
| 40 | 40 | .addPartitionKey("b", DataType.cint()) |
| 41 | 41 | .addClusteringColumn("c", DataType.cint()) |
| @@ -45,7 +45,7 @@ | ||
| 45 | 45 | ); |
| 46 | 46 | |
| 47 | 47 | // Modify the table metadata |
| 48 | - session.execute(SchemaBuilder.alterTable("TableMetadata") | |
| 48 | + session().execute(SchemaBuilder.alterTable("TableMetadata") | |
| 49 | 49 | .withOptions() |
| 50 | 50 | .defaultTimeToLive(1337) |
| 51 | 51 | .bloomFilterFPChance(0.42) |
| @@ -68,7 +68,7 @@ | ||
| 68 | 68 | .compressionOptions(snappy())); |
| 69 | 69 | |
| 70 | 70 | // Retrieve the metadata from Cassandra |
| 71 | - ResultSet rows = session.execute("SELECT " | |
| 71 | + ResultSet rows = session().execute("SELECT " | |
| 72 | 72 | + "bloom_filter_fp_chance, " |
| 73 | 73 | + "caching, " |
| 74 | 74 | + "cf_id, " |
| @@ -131,12 +131,12 @@ | ||
| 131 | 131 | @CassandraVersion(major = 2.1) |
| 132 | 132 | public void should_create_a_table_and_a_udt() { |
| 133 | 133 | // Create a UDT and a table |
| 134 | - session.execute(SchemaBuilder.createType("MyUDT") | |
| 134 | + session().execute(SchemaBuilder.createType("MyUDT") | |
| 135 | 135 | .ifNotExists() |
| 136 | 136 | .addColumn("x", DataType.cint()) |
| 137 | 137 | ); |
| 138 | 138 | UDTType myUDT = UDTType.frozen("MyUDT"); |
| 139 | - session.execute(SchemaBuilder.createTable("ks", "CreateTable") | |
| 139 | + session().execute(SchemaBuilder.createTable("ks", "CreateTable") | |
| 140 | 140 | .ifNotExists() |
| 141 | 141 | .addPartitionKey("a", DataType.cint()) |
| 142 | 142 | .addUDTPartitionKey("b", myUDT) |
| @@ -152,7 +152,7 @@ | ||
| 152 | 152 | ); |
| 153 | 153 | |
| 154 | 154 | // Check columns a to k |
| 155 | - ResultSet rows = session.execute( | |
| 155 | + ResultSet rows = session().execute( | |
| 156 | 156 | "SELECT column_name, type, validator " |
| 157 | 157 | + "FROM system.schema_columns " |
| 158 | 158 | + "WHERE keyspace_name='ks' AND columnfamily_name='createtable'"); |
| @@ -177,22 +177,22 @@ | ||
| 177 | 177 | @Test(groups = "short") |
| 178 | 178 | public void should_add_and_drop_a_column() { |
| 179 | 179 | // Create a table, add a column to it with an alter table statement and delete that column |
| 180 | - session.execute(SchemaBuilder.createTable("ks", "DropColumn") | |
| 180 | + session().execute(SchemaBuilder.createTable("ks", "DropColumn") | |
| 181 | 181 | .ifNotExists() |
| 182 | 182 | .addPartitionKey("a", DataType.cint()) |
| 183 | 183 | ); |
| 184 | 184 | |
| 185 | 185 | // Add and then drop a column |
| 186 | - session.execute(SchemaBuilder.alterTable("ks", "DropColumn") | |
| 186 | + session().execute(SchemaBuilder.alterTable("ks", "DropColumn") | |
| 187 | 187 | .addColumn("b") |
| 188 | 188 | .type(DataType.cint()) |
| 189 | 189 | ); |
| 190 | - session.execute(SchemaBuilder.alterTable("ks", "DropColumn") | |
| 190 | + session().execute(SchemaBuilder.alterTable("ks", "DropColumn") | |
| 191 | 191 | .dropColumn("b") |
| 192 | 192 | ); |
| 193 | 193 | |
| 194 | 194 | // Check that only column a exist |
| 195 | - ResultSet rows = session.execute( | |
| 195 | + ResultSet rows = session().execute( | |
| 196 | 196 | "SELECT column_name, type, validator " |
| 197 | 197 | + "FROM system.schema_columns " |
| 198 | 198 | + "WHERE keyspace_name='ks' AND columnfamily_name='dropcolumn'"); |
| @@ -214,15 +214,15 @@ | ||
| 214 | 214 | @Test(groups = "short") |
| 215 | 215 | public void should_drop_a_table() { |
| 216 | 216 | // Create a table |
| 217 | - session.execute(SchemaBuilder.createTable("ks", "DropTable") | |
| 217 | + session().execute(SchemaBuilder.createTable("ks", "DropTable") | |
| 218 | 218 | .addPartitionKey("a", DataType.cint()) |
| 219 | 219 | ); |
| 220 | 220 | |
| 221 | 221 | // Drop the table |
| 222 | - session.execute(SchemaBuilder.dropTable("ks", "DropTable")); | |
| 223 | - session.execute(SchemaBuilder.dropTable("DropTable").ifExists()); | |
| 222 | + session().execute(SchemaBuilder.dropTable("ks", "DropTable")); | |
| 223 | + session().execute(SchemaBuilder.dropTable("DropTable").ifExists()); | |
| 224 | 224 | |
| 225 | - ResultSet rows = session.execute( | |
| 225 | + ResultSet rows = session().execute( | |
| 226 | 226 | "SELECT columnfamily_name " |
| 227 | 227 | + "FROM system.schema_columnfamilies " |
| 228 | 228 | + "WHERE keyspace_name='ks' AND columnfamily_name='droptable'"); |
| @@ -234,24 +234,24 @@ | ||
| 234 | 234 | @Test(groups = "short") |
| 235 | 235 | public void should_create_an_index() { |
| 236 | 236 | // Create a table |
| 237 | - session.execute(SchemaBuilder.createTable("ks", "CreateIndex") | |
| 237 | + session().execute(SchemaBuilder.createTable("ks", "CreateIndex") | |
| 238 | 238 | .addPartitionKey("a", DataType.cint()) |
| 239 | 239 | .addClusteringColumn("b", DataType.cint()) |
| 240 | 240 | .addColumn("c", DataType.map(DataType.cint(), DataType.cint())) |
| 241 | 241 | ); |
| 242 | 242 | |
| 243 | 243 | // Create an index on a regular column of the table |
| 244 | - session.execute(SchemaBuilder.createIndex("ks_Index") | |
| 244 | + session().execute(SchemaBuilder.createIndex("ks_Index") | |
| 245 | 245 | .onTable("ks", "CreateIndex") |
| 246 | 246 | .andColumn("b") |
| 247 | 247 | ); |
| 248 | - session.execute(SchemaBuilder.createIndex("ks_IndexOnMap") | |
| 248 | + session().execute(SchemaBuilder.createIndex("ks_IndexOnMap") | |
| 249 | 249 | .onTable("ks", "CreateIndex") |
| 250 | 250 | .andKeysOfColumn("c") |
| 251 | 251 | ); |
| 252 | 252 | |
| 253 | 253 | // Verify that the indexes exist on the right columns |
| 254 | - ResultSet rows = session.execute( | |
| 254 | + ResultSet rows = session().execute( | |
| 255 | 255 | "SELECT column_name, index_name, index_options, index_type, component_index " |
| 256 | 256 | + "FROM system.schema_columns " |
| 257 | 257 | + "WHERE keyspace_name='ks' " |
| @@ -275,7 +275,7 @@ | ||
| 275 | 275 | @Test(groups = "short") |
| 276 | 276 | public void should_drop_an_index() { |
| 277 | 277 | // Create a table |
| 278 | - session.execute(SchemaBuilder.createTable("ks", "DropIndex") | |
| 278 | + session().execute(SchemaBuilder.createTable("ks", "DropIndex") | |
| 279 | 279 | .addPartitionKey("a", DataType.cint()) |
| 280 | 280 | .addClusteringColumn("b", DataType.cint()) |
| 281 | 281 | ); |
| @@ -284,7 +284,7 @@ | ||
| 284 | 284 | // Note: we have to pick a lower-case name because Cassandra uses the CamelCase index name at creation |
| 285 | 285 | // but a lowercase index name at deletion |
| 286 | 286 | // See : https://issues.apache.org/jira/browse/CASSANDRA-8365 |
| 287 | - session.execute(SchemaBuilder.createIndex("ks_index") | |
| 287 | + session().execute(SchemaBuilder.createIndex("ks_index") | |
| 288 | 288 | .onTable("ks", "DropIndex") |
| 289 | 289 | .andColumn("b") |
| 290 | 290 | ); |
| @@ -293,14 +293,14 @@ | ||
| 293 | 293 | assertThat(numberOfIndexedColumns()).isEqualTo(1); |
| 294 | 294 | |
| 295 | 295 | // Delete the index |
| 296 | - session.execute(SchemaBuilder.dropIndex("ks", "ks_index")); | |
| 296 | + session().execute(SchemaBuilder.dropIndex("ks", "ks_index")); | |
| 297 | 297 | |
| 298 | 298 | // Verify that only the PK index exists |
| 299 | 299 | assertThat(numberOfIndexedColumns()).isEqualTo(0); |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | private int numberOfIndexedColumns() { |
| 303 | - ResultSet columns = session.execute( | |
| 303 | + ResultSet columns = session().execute( | |
| 304 | 304 | "SELECT * " |
| 305 | 305 | + "FROM system.schema_columns " |
| 306 | 306 | + "WHERE keyspace_name='ks' " |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorParamsTest.java +15 −16
| @@ -20,17 +20,16 @@ | ||
| 20 | 20 | import com.datastax.driver.core.Row; |
| 21 | 21 | import com.datastax.driver.core.utils.CassandraVersion; |
| 22 | 22 | import com.datastax.driver.mapping.annotations.*; |
| 23 | -import com.google.common.collect.Lists; | |
| 24 | 23 | import org.testng.annotations.Test; |
| 25 | 24 | |
| 26 | -import java.util.Collection; | |
| 27 | - | |
| 28 | 25 | import static org.assertj.core.api.Assertions.assertThat; |
| 29 | 26 | |
| 27 | +@SuppressWarnings("unused") | |
| 30 | 28 | public class MapperAccessorParamsTest extends CCMTestsSupport { |
| 29 | + | |
| 31 | 30 | @Override |
| 32 | - public Collection<String> createTestFixtures() { | |
| 33 | - return Lists.newArrayList( | |
| 31 | + public void onTestContextInitialized() { | |
| 32 | + execute( | |
| 34 | 33 | "CREATE TABLE user ( key int primary key, gender int, home_phone text, work_phone text)", |
| 35 | 34 | "CREATE INDEX on user(gender)", |
| 36 | 35 | "CREATE TABLE user_str ( key int primary key, gender text)", |
| @@ -40,7 +39,7 @@ | ||
| 40 | 39 | |
| 41 | 40 | @Test(groups = "short") |
| 42 | 41 | public void should_allow_enum_as_int_in_accessor_params() { |
| 43 | - UserAccessor accessor = new MappingManager(session) | |
| 42 | + UserAccessor accessor = new MappingManager(session()) | |
| 44 | 43 | .createAccessor(UserAccessor.class); |
| 45 | 44 | |
| 46 | 45 | accessor.addUser(0, Enum.FEMALE); |
| @@ -55,7 +54,7 @@ | ||
| 55 | 54 | |
| 56 | 55 | @Test(groups = "short") |
| 57 | 56 | public void should_allow_enum_as_string_in_accessor_params() { |
| 58 | - UserAccessor accessor = new MappingManager(session) | |
| 57 | + UserAccessor accessor = new MappingManager(session()) | |
| 59 | 58 | .createAccessor(UserAccessor.class); |
| 60 | 59 | |
| 61 | 60 | accessor.addUserStr(0, Enum.FEMALE); |
| @@ -71,44 +70,44 @@ | ||
| 71 | 70 | @Test(groups = "short") |
| 72 | 71 | @CassandraVersion(major = 2.0, description = "Uses named parameters") |
| 73 | 72 | public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() { |
| 74 | - UserPhoneAccessor accessor = new MappingManager(session) | |
| 73 | + UserPhoneAccessor accessor = new MappingManager(session()) | |
| 75 | 74 | .createAccessor(UserPhoneAccessor.class); |
| 76 | 75 | |
| 77 | - session.execute("delete from user where key = 0"); | |
| 76 | + session().execute("delete from user where key = 0"); | |
| 78 | 77 | accessor.updatePhones_positional("1111", "2222", 0); |
| 79 | 78 | assertPhonesEqual(0, "1111", "2222"); |
| 80 | 79 | |
| 81 | - session.execute("delete from user where key = 0"); | |
| 80 | + session().execute("delete from user where key = 0"); | |
| 82 | 81 | accessor.updatePhones_named(0, "1111", "2222"); |
| 83 | 82 | assertPhonesEqual(0, "1111", "2222"); |
| 84 | 83 | |
| 85 | - session.execute("delete from user where key = 0"); | |
| 84 | + session().execute("delete from user where key = 0"); | |
| 86 | 85 | accessor.updatePhones_fallback("1111", "2222", 0); |
| 87 | 86 | assertPhonesEqual(0, "1111", "2222"); |
| 88 | 87 | |
| 89 | - session.execute("delete from user where key = 0"); | |
| 88 | + session().execute("delete from user where key = 0"); | |
| 90 | 89 | accessor.updateBothPhones(0, "1111"); |
| 91 | 90 | assertPhonesEqual(0, "1111", "1111"); |
| 92 | 91 | |
| 93 | - session.execute("delete from user where key = 0"); | |
| 92 | + session().execute("delete from user where key = 0"); | |
| 94 | 93 | accessor.updatePhones_fallback2("1111", "2222", 0); |
| 95 | 94 | assertPhonesEqual(0, "1111", "2222"); |
| 96 | 95 | } |
| 97 | 96 | |
| 98 | 97 | @Test(groups = "short", expectedExceptions = RuntimeException.class) |
| 99 | 98 | public void should_fail_if_not_enough_parameters() { |
| 100 | - new MappingManager(session) | |
| 99 | + new MappingManager(session()) | |
| 101 | 100 | .createAccessor(UserPhoneAccessor_NotEnoughParams.class); |
| 102 | 101 | } |
| 103 | 102 | |
| 104 | 103 | @Test(groups = "short", expectedExceptions = RuntimeException.class) |
| 105 | 104 | public void should_fail_if_too_many_parameters() { |
| 106 | - new MappingManager(session) | |
| 105 | + new MappingManager(session()) | |
| 107 | 106 | .createAccessor(UserPhoneAccessor_TooManyParams.class); |
| 108 | 107 | } |
| 109 | 108 | |
| 110 | 109 | private void assertPhonesEqual(int key, String home, String work) { |
| 111 | - Row row = session.execute("select * from user where key = " + key).one(); | |
| 110 | + Row row = session().execute("select * from user where key = " + key).one(); | |
| 112 | 111 | assertThat(row.getString("home_phone")).isEqualTo(home); |
| 113 | 112 | assertThat(row.getString("work_phone")).isEqualTo(work); |
| 114 | 113 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorTest.java +10 −13
| @@ -22,23 +22,20 @@ | ||
| 22 | 22 | import com.datastax.driver.mapping.annotations.Accessor; |
| 23 | 23 | import com.datastax.driver.mapping.annotations.Param; |
| 24 | 24 | import com.datastax.driver.mapping.annotations.Query; |
| 25 | -import com.google.common.collect.Lists; | |
| 26 | 25 | import org.testng.annotations.Test; |
| 27 | 26 | |
| 28 | -import java.util.Collection; | |
| 29 | - | |
| 30 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 31 | 28 | |
| 32 | 29 | public class MapperAccessorTest extends CCMTestsSupport { |
| 33 | 30 | |
| 34 | 31 | @Override |
| 35 | - public Collection<String> createTestFixtures() { | |
| 36 | - return Lists.newArrayList("CREATE TABLE foo (k int primary key, v text)"); | |
| 32 | + public void onTestContextInitialized() { | |
| 33 | + execute("CREATE TABLE foo (k int primary key, v text)"); | |
| 37 | 34 | } |
| 38 | 35 | |
| 39 | 36 | @Test(groups = "short") |
| 40 | 37 | public void should_implement_toString() { |
| 41 | - SystemAccessor accessor = new MappingManager(session) | |
| 38 | + SystemAccessor accessor = new MappingManager(session()) | |
| 42 | 39 | .createAccessor(SystemAccessor.class); |
| 43 | 40 | |
| 44 | 41 | assertThat(accessor.toString()) |
| @@ -47,7 +44,7 @@ | ||
| 47 | 44 | |
| 48 | 45 | @Test(groups = "short") |
| 49 | 46 | public void should_implement_equals_and_hashCode() { |
| 50 | - SystemAccessor accessor = new MappingManager(session) | |
| 47 | + SystemAccessor accessor = new MappingManager(session()) | |
| 51 | 48 | .createAccessor(SystemAccessor.class); |
| 52 | 49 | |
| 53 | 50 | assertThat(accessor).isNotEqualTo(new Object()); |
| @@ -57,32 +54,32 @@ | ||
| 57 | 54 | @Test(groups = "short") |
| 58 | 55 | @CassandraVersion(major = 2.0) |
| 59 | 56 | public void should_allow_null_argument_in_accessor_when_set_by_name() { |
| 60 | - FooAccessor accessor = new MappingManager(session) | |
| 57 | + FooAccessor accessor = new MappingManager(session()) | |
| 61 | 58 | .createAccessor(FooAccessor.class); |
| 62 | 59 | |
| 63 | 60 | accessor.insert(1, null); |
| 64 | - Row row = session.execute("select * from foo where k = 1").one(); | |
| 61 | + Row row = session().execute("select * from foo where k = 1").one(); | |
| 65 | 62 | |
| 66 | 63 | assertThat(row.getString("v")).isNull(); |
| 67 | 64 | } |
| 68 | 65 | |
| 69 | 66 | @Test(groups = "short") |
| 70 | 67 | public void should_allow_null_argument_in_accessor_when_set_by_index() { |
| 71 | - FooBindMarkerAccessor accessor = new MappingManager(session) | |
| 68 | + FooBindMarkerAccessor accessor = new MappingManager(session()) | |
| 72 | 69 | .createAccessor(FooBindMarkerAccessor.class); |
| 73 | 70 | |
| 74 | 71 | accessor.insert(1, null); |
| 75 | 72 | |
| 76 | - Row row = session.execute("select * from foo where k = 1").one(); | |
| 73 | + Row row = session().execute("select * from foo where k = 1").one(); | |
| 77 | 74 | |
| 78 | 75 | assertThat(row.getString("v")).isNull(); |
| 79 | 76 | } |
| 80 | 77 | |
| 81 | 78 | @Test(groups = "short") |
| 82 | 79 | public void should_allow_void_return_type_in_accessor() { |
| 83 | - VoidAccessor accessor = new MappingManager(session).createAccessor(VoidAccessor.class); | |
| 80 | + VoidAccessor accessor = new MappingManager(session()).createAccessor(VoidAccessor.class); | |
| 84 | 81 | accessor.insert(1, "bar"); |
| 85 | - Row row = session.execute("select * from foo where k = 1").one(); | |
| 82 | + Row row = session().execute("select * from foo where k = 1").one(); | |
| 86 | 83 | assertThat(row.getInt("k")).isEqualTo(1); |
| 87 | 84 | assertThat(row.getString("v")).isEqualTo("bar"); |
| 88 | 85 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperCompositeKeyTest.java +3 −6
| @@ -21,17 +21,14 @@ | ||
| 21 | 21 | import com.datastax.driver.mapping.annotations.Table; |
| 22 | 22 | import org.testng.annotations.Test; |
| 23 | 23 | |
| 24 | -import java.util.Collection; | |
| 25 | -import java.util.Collections; | |
| 26 | - | |
| 27 | 24 | /** |
| 28 | 25 | * Tests for the mapper with composite partition keys and multiple clustering columns. |
| 29 | 26 | */ |
| 30 | 27 | public class MapperCompositeKeyTest extends CCMTestsSupport { |
| 31 | 28 | |
| 32 | 29 | @Override |
| 33 | - public Collection<String> createTestFixtures() { | |
| 34 | - return Collections.singletonList("CREATE TABLE test_table (pk1 int, pk2 int, cc1 int, cc2 int, PRIMARY KEY ((pk1, pk2), cc1, cc2))"); | |
| 30 | + public void onTestContextInitialized() { | |
| 31 | + execute("CREATE TABLE test_table (pk1 int, pk2 int, cc1 int, cc2 int, PRIMARY KEY ((pk1, pk2), cc1, cc2))"); | |
| 35 | 32 | } |
| 36 | 33 | |
| 37 | 34 | @SuppressWarnings("unused") |
| @@ -84,6 +81,6 @@ | ||
| 84 | 81 | |
| 85 | 82 | @Test(groups = "short") |
| 86 | 83 | public void testCreateMapper() throws Exception { |
| 87 | - new MappingManager(session).mapper(TestTable.class); | |
| 84 | + new MappingManager(session()).mapper(TestTable.class); | |
| 88 | 85 | } |
| 89 | 86 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperComputedFieldsTest.java +6 −9
| @@ -19,13 +19,10 @@ | ||
| 19 | 19 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 20 | 20 | import com.datastax.driver.core.utils.CassandraVersion; |
| 21 | 21 | import com.datastax.driver.mapping.annotations.*; |
| 22 | -import com.google.common.collect.Lists; | |
| 23 | 22 | import org.testng.SkipException; |
| 24 | 23 | import org.testng.annotations.BeforeMethod; |
| 25 | 24 | import org.testng.annotations.Test; |
| 26 | 25 | |
| 27 | -import java.util.Collection; | |
| 28 | - | |
| 29 | 26 | import static com.datastax.driver.core.Assertions.assertThat; |
| 30 | 27 | import static com.datastax.driver.core.ProtocolVersion.V1; |
| 31 | 28 | import static org.assertj.core.api.Assertions.fail; |
| @@ -38,8 +35,8 @@ | ||
| 38 | 35 | public class MapperComputedFieldsTest extends CCMTestsSupport { |
| 39 | 36 | |
| 40 | 37 | @Override |
| 41 | - public Collection<String> createTestFixtures() { | |
| 42 | - return Lists.newArrayList( | |
| 38 | + public void onTestContextInitialized() { | |
| 39 | + execute( | |
| 43 | 40 | "CREATE TABLE user (login text primary key, name text)", |
| 44 | 41 | "INSERT INTO user (login, name) VALUES ('testlogin', 'test name')"); |
| 45 | 42 | } |
| @@ -50,8 +47,8 @@ | ||
| 50 | 47 | |
| 51 | 48 | @BeforeMethod(groups = "short") |
| 52 | 49 | void setup() { |
| 53 | - mappingManager = new MappingManager(session); | |
| 54 | - protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum(); | |
| 50 | + mappingManager = new MappingManager(session()); | |
| 51 | + protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum(); | |
| 55 | 52 | if (protocolVersion.compareTo(V1) > 0) |
| 56 | 53 | userMapper = mappingManager.mapper(User.class); |
| 57 | 54 | } |
| @@ -81,7 +78,7 @@ | ||
| 81 | 78 | assertThat(fetched.getTtl()).isNull(); // TTL should be null since it was not set. |
| 82 | 79 | |
| 83 | 80 | // Overwrite with TTL |
| 84 | - session.execute("insert into user (login, name) values ('testlogin2', 'blah') using TTL 600"); | |
| 81 | + session().execute("insert into user (login, name) values ('testlogin2', 'blah') using TTL 600"); | |
| 85 | 82 | fetched = userMapper.get("testlogin2"); |
| 86 | 83 | assertThat(fetched.getWriteTime()).isGreaterThanOrEqualTo(writeTime).isLessThan(writeTime + 30000000L); |
| 87 | 84 | assertThat(fetched.getTtl()).isBetween(570, 600); // TTL should be within 30 secs. |
| @@ -103,7 +100,7 @@ | ||
| 103 | 100 | public void should_map_aliased_resultset_to_objects() { |
| 104 | 101 | Statement getQuery = userMapper.getQuery("testlogin"); |
| 105 | 102 | getQuery.setConsistencyLevel(ConsistencyLevel.QUORUM); |
| 106 | - ResultSet rs = session.execute(getQuery); | |
| 103 | + ResultSet rs = session().execute(getQuery); | |
| 107 | 104 | |
| 108 | 105 | Result<User> result = userMapper.mapAliased(rs); |
| 109 | 106 | User user = result.one(); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperDefaultKeyspaceTest.java +11 −13
| @@ -23,8 +23,6 @@ | ||
| 23 | 23 | import com.google.common.base.Objects; |
| 24 | 24 | import org.testng.annotations.Test; |
| 25 | 25 | |
| 26 | -import java.util.Arrays; | |
| 27 | -import java.util.Collection; | |
| 28 | 26 | import java.util.UUID; |
| 29 | 27 | |
| 30 | 28 | import static org.testng.Assert.assertEquals; |
| @@ -40,8 +38,8 @@ | ||
| 40 | 38 | private static final String KEYSPACE = "mapper_default_keyspace_test_ks"; |
| 41 | 39 | |
| 42 | 40 | @Override |
| 43 | - public Collection<String> createTestFixtures() { | |
| 44 | - return Arrays.asList( | |
| 41 | + public void onTestContextInitialized() { | |
| 42 | + execute( | |
| 45 | 43 | "CREATE TABLE groups (group_id uuid PRIMARY KEY, name text)", |
| 46 | 44 | "CREATE TYPE IF NOT EXISTS group_name (name text)"); |
| 47 | 45 | } |
| @@ -102,9 +100,9 @@ | ||
| 102 | 100 | @Test(groups = "short") |
| 103 | 101 | public void testTableWithDefaultKeyspace() throws Exception { |
| 104 | 102 | // Ensure that the test session is logged into the "ks" keyspace. |
| 105 | - session.execute("USE " + keyspace); | |
| 103 | + session().execute("USE " + keyspace); | |
| 106 | 104 | |
| 107 | - MappingManager manager = new MappingManager(session); | |
| 105 | + MappingManager manager = new MappingManager(session()); | |
| 108 | 106 | Mapper<Group> m = manager.mapper(Group.class); |
| 109 | 107 | Group group = new Group("testGroup"); |
| 110 | 108 | UUID groupId = group.getGroupId(); |
| @@ -212,13 +210,13 @@ | ||
| 212 | 210 | @Test(groups = "short") |
| 213 | 211 | public void testUDTWithDefaultKeyspace() throws Exception { |
| 214 | 212 | // Create test specific keyspace and table. |
| 215 | - session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }", KEYSPACE)); | |
| 216 | - session.execute(String.format("CREATE TYPE IF NOT EXISTS %s.group_name (name text)", KEYSPACE)); | |
| 217 | - session.execute(String.format("CREATE TABLE IF NOT EXISTS %s.groups2 (group_id uuid PRIMARY KEY, name frozen<group_name>)", KEYSPACE)); | |
| 213 | + session().execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }", KEYSPACE)); | |
| 214 | + session().execute(String.format("CREATE TYPE IF NOT EXISTS %s.group_name (name text)", KEYSPACE)); | |
| 215 | + session().execute(String.format("CREATE TABLE IF NOT EXISTS %s.groups2 (group_id uuid PRIMARY KEY, name frozen<group_name>)", KEYSPACE)); | |
| 218 | 216 | // Ensure that the test session is logged into the keyspace. |
| 219 | - session.execute("USE " + keyspace); | |
| 217 | + session().execute("USE " + keyspace); | |
| 220 | 218 | |
| 221 | - MappingManager manager = new MappingManager(session); | |
| 219 | + MappingManager manager = new MappingManager(session()); | |
| 222 | 220 | Mapper<Group2> m = manager.mapper(Group2.class); |
| 223 | 221 | Group2 group = new Group2(new GroupName("testGroup")); |
| 224 | 222 | UUID groupId = group.getGroupId(); |
| @@ -239,7 +237,7 @@ | ||
| 239 | 237 | expectedExceptions = IllegalArgumentException.class, |
| 240 | 238 | expectedExceptionsMessageRegExp = "Error creating mapper for class Group, the @Table annotation declares no default keyspace, and the session is not currently logged to any keyspace") |
| 241 | 239 | public void should_throw_a_meaningful_error_message_when_no_default_table_keyspace_and_session_not_logged() { |
| 242 | - Session session2 = cluster.connect(); | |
| 240 | + Session session2 = cluster().connect(); | |
| 243 | 241 | MappingManager manager = new MappingManager(session2); |
| 244 | 242 | manager.mapper(Group.class); |
| 245 | 243 | } |
| @@ -248,7 +246,7 @@ | ||
| 248 | 246 | expectedExceptions = IllegalArgumentException.class, |
| 249 | 247 | expectedExceptionsMessageRegExp = "Error creating UDT mapper for class GroupName, the @UDT annotation declares no default keyspace, and the session is not currently logged to any keyspace") |
| 250 | 248 | public void should_throw_a_meaningful_error_message_when_no_default_udt_keyspace_and_session_not_logged() { |
| 251 | - Session session2 = cluster.connect(); | |
| 249 | + Session session2 = cluster().connect(); | |
| 252 | 250 | MappingManager manager = new MappingManager(session2); |
| 253 | 251 | manager.udtMapper(GroupName.class); |
| 254 | 252 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperEnumTest.java +4 −7
| @@ -19,11 +19,8 @@ | ||
| 19 | 19 | import com.datastax.driver.mapping.annotations.Enumerated; |
| 20 | 20 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 21 | 21 | import com.datastax.driver.mapping.annotations.Table; |
| 22 | -import com.google.common.collect.Lists; | |
| 23 | 22 | import org.testng.annotations.Test; |
| 24 | 23 | |
| 25 | -import java.util.Collection; | |
| 26 | - | |
| 27 | 24 | import static org.junit.Assert.assertNull; |
| 28 | 25 | |
| 29 | 26 | /** |
| @@ -33,8 +30,8 @@ | ||
| 33 | 30 | public class MapperEnumTest extends CCMTestsSupport { |
| 34 | 31 | |
| 35 | 32 | @Override |
| 36 | - public Collection<String> createTestFixtures() { | |
| 37 | - return Lists.newArrayList("CREATE TABLE asOrdinal (k int primary key, v int)", | |
| 33 | + public void onTestContextInitialized() { | |
| 34 | + execute("CREATE TABLE asOrdinal (k int primary key, v int)", | |
| 38 | 35 | "CREATE TABLE asString (k int primary key, v text)"); |
| 39 | 36 | } |
| 40 | 37 | |
| @@ -44,7 +41,7 @@ | ||
| 44 | 41 | |
| 45 | 42 | @Test(groups = "short") |
| 46 | 43 | public void should_handle_null_enum_as_ordinal() { |
| 47 | - Mapper<AsOrdinal> mapper = new MappingManager(session).mapper(AsOrdinal.class); | |
| 44 | + Mapper<AsOrdinal> mapper = new MappingManager(session()).mapper(AsOrdinal.class); | |
| 48 | 45 | |
| 49 | 46 | mapper.save(new AsOrdinal(1, null)); |
| 50 | 47 | AsOrdinal o = mapper.get(1); |
| @@ -54,7 +51,7 @@ | ||
| 54 | 51 | |
| 55 | 52 | @Test(groups = "short") |
| 56 | 53 | public void should_handle_null_enum_as_string() { |
| 57 | - Mapper<AsString> mapper = new MappingManager(session).mapper(AsString.class); | |
| 54 | + Mapper<AsString> mapper = new MappingManager(session()).mapper(AsString.class); | |
| 58 | 55 | |
| 59 | 56 | mapper.save(new AsString(1, null)); |
| 60 | 57 | AsString o = mapper.get(1); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperInvalidCollectionTypesTest.java +8 −6
| @@ -20,14 +20,16 @@ | ||
| 20 | 20 | import com.datastax.driver.mapping.annotations.Table; |
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | 22 | |
| 23 | -import java.util.*; | |
| 23 | +import java.util.ArrayList; | |
| 24 | +import java.util.HashSet; | |
| 25 | +import java.util.TreeMap; | |
| 24 | 26 | |
| 25 | 27 | @SuppressWarnings("unused") |
| 26 | 28 | public class MapperInvalidCollectionTypesTest extends CCMTestsSupport { |
| 27 | 29 | |
| 28 | 30 | @Override |
| 29 | - public Collection<String> createTestFixtures() { | |
| 30 | - return Arrays.asList( | |
| 31 | + public void onTestContextInitialized() { | |
| 32 | + execute( | |
| 31 | 33 | "CREATE TABLE table_list (id int PRIMARY KEY, l list<int>)", |
| 32 | 34 | "CREATE TABLE table_set (id int PRIMARY KEY, s set<int>)", |
| 33 | 35 | "CREATE TABLE table_map (id int PRIMARY KEY, m map<int, int>)"); |
| @@ -107,16 +109,16 @@ | ||
| 107 | 109 | |
| 108 | 110 | @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) |
| 109 | 111 | public void list_type_is_enforced() { |
| 110 | - new MappingManager(session).mapper(InvalidList.class); | |
| 112 | + new MappingManager(session()).mapper(InvalidList.class); | |
| 111 | 113 | } |
| 112 | 114 | |
| 113 | 115 | @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) |
| 114 | 116 | public void map_type_is_enforced() { |
| 115 | - new MappingManager(session).mapper(InvalidMap.class); | |
| 117 | + new MappingManager(session()).mapper(InvalidMap.class); | |
| 116 | 118 | } |
| 117 | 119 | |
| 118 | 120 | @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) |
| 119 | 121 | public void set_type_is_enforced() { |
| 120 | - new MappingManager(session).mapper(InvalidSet.class); | |
| 122 | + new MappingManager(session()).mapper(InvalidSet.class); | |
| 121 | 123 | } |
| 122 | 124 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperNestedCollectionsTest.java +8 −5
| @@ -24,7 +24,10 @@ | ||
| 24 | 24 | import com.google.common.collect.Sets; |
| 25 | 25 | import org.testng.annotations.Test; |
| 26 | 26 | |
| 27 | -import java.util.*; | |
| 27 | +import java.util.ArrayList; | |
| 28 | +import java.util.List; | |
| 29 | +import java.util.Map; | |
| 30 | +import java.util.Set; | |
| 28 | 31 | |
| 29 | 32 | import static com.datastax.driver.core.Assertions.assertThat; |
| 30 | 33 | |
| @@ -33,8 +36,8 @@ | ||
| 33 | 36 | public class MapperNestedCollectionsTest extends CCMTestsSupport { |
| 34 | 37 | |
| 35 | 38 | @Override |
| 36 | - public Collection<String> createTestFixtures() { | |
| 37 | - return Lists.newArrayList( | |
| 39 | + public void onTestContextInitialized() { | |
| 40 | + execute( | |
| 38 | 41 | "CREATE TYPE testType(i int, ls list<frozen<set<int>>>)", |
| 39 | 42 | "CREATE TABLE testTable (k int primary key, " |
| 40 | 43 | + "m1 map<text, frozen<map<int, int>>>, " // nested collection |
| @@ -44,7 +47,7 @@ | ||
| 44 | 47 | |
| 45 | 48 | @Test(groups = "short") |
| 46 | 49 | public void should_map_fields_to_nested_collections() { |
| 47 | - Mapper<TestTable> mapper = new MappingManager(session).mapper(TestTable.class); | |
| 50 | + Mapper<TestTable> mapper = new MappingManager(session()).mapper(TestTable.class); | |
| 48 | 51 | |
| 49 | 52 | TestTable testTable = new TestTable(); |
| 50 | 53 | testTable.setK(1); |
| @@ -67,7 +70,7 @@ | ||
| 67 | 70 | |
| 68 | 71 | @Test(groups = "short") |
| 69 | 72 | public void should_map_accessor_params_to_nested_collections() { |
| 70 | - MappingManager manager = new MappingManager(session); | |
| 73 | + MappingManager manager = new MappingManager(session()); | |
| 71 | 74 | Mapper<TestTable> mapper = manager.mapper(TestTable.class); |
| 72 | 75 | TestAccessor accessor = manager.createAccessor(TestAccessor.class); |
| 73 | 76 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperOptionTest.java +11 −13
| @@ -21,12 +21,10 @@ | ||
| 21 | 21 | import com.datastax.driver.core.utils.CassandraVersion; |
| 22 | 22 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 23 | 23 | import com.datastax.driver.mapping.annotations.Table; |
| 24 | -import com.google.common.collect.Lists; | |
| 25 | 24 | import org.testng.SkipException; |
| 26 | 25 | import org.testng.annotations.BeforeMethod; |
| 27 | 26 | import org.testng.annotations.Test; |
| 28 | 27 | |
| 29 | -import java.util.Collection; | |
| 30 | 28 | import java.util.concurrent.ExecutionException; |
| 31 | 29 | |
| 32 | 30 | import static com.datastax.driver.core.ConsistencyLevel.*; |
| @@ -42,14 +40,14 @@ | ||
| 42 | 40 | Mapper<User> mapper; |
| 43 | 41 | |
| 44 | 42 | @Override |
| 45 | - public Collection<String> createTestFixtures() { | |
| 46 | - return Lists.newArrayList("CREATE TABLE user (key int primary key, v text)"); | |
| 43 | + public void onTestContextInitialized() { | |
| 44 | + execute("CREATE TABLE user (key int primary key, v text)"); | |
| 47 | 45 | } |
| 48 | 46 | |
| 49 | 47 | @BeforeMethod(groups = "short") |
| 50 | 48 | public void setup() { |
| 51 | - mapper = new MappingManager(session).mapper(User.class); | |
| 52 | - protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum(); | |
| 49 | + mapper = new MappingManager(session()).mapper(User.class); | |
| 50 | + protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersionEnum(); | |
| 53 | 51 | } |
| 54 | 52 | |
| 55 | 53 | @Test(groups = "short") |
| @@ -58,7 +56,7 @@ | ||
| 58 | 56 | Long tsValue = futureTimestamp(); |
| 59 | 57 | mapper.save(new User(42, "helloworld"), Option.timestamp(tsValue), Option.tracing(true)); |
| 60 | 58 | assertThat(mapper.get(42).getV()).isEqualTo("helloworld"); |
| 61 | - Long tsReturned = session.execute("SELECT writetime(v) FROM user WHERE key=" + 42).one().getLong(0); | |
| 59 | + Long tsReturned = session().execute("SELECT writetime(v) FROM user WHERE key=" + 42).one().getLong(0); | |
| 62 | 60 | assertThat(tsReturned).isEqualTo(tsValue); |
| 63 | 61 | } |
| 64 | 62 | |
| @@ -83,7 +81,7 @@ | ||
| 83 | 81 | assertThat(bs.isTracing()).isTrue(); |
| 84 | 82 | assertThat(bs.getConsistencyLevel()).isEqualTo(ALL); |
| 85 | 83 | |
| 86 | - ResultSet rs = session.execute(bs); | |
| 84 | + ResultSet rs = session().execute(bs); | |
| 87 | 85 | assertThat(rs.getExecutionInfo().getQueryTrace()).isNotNull(); |
| 88 | 86 | User us = mapper.mapAliased(rs).one(); |
| 89 | 87 | assertThat(us.getV()).isEqualTo("toget"); |
| @@ -97,7 +95,7 @@ | ||
| 97 | 95 | Long tsValue = futureTimestamp(); |
| 98 | 96 | mapper.save(new User(43, "helloworld"), Option.timestamp(tsValue)); |
| 99 | 97 | mapper.save(new User(44, "test")); |
| 100 | - Long tsReturned = session.execute("SELECT writetime(v) FROM user WHERE key=" + 44).one().getLong(0); | |
| 98 | + Long tsReturned = session().execute("SELECT writetime(v) FROM user WHERE key=" + 44).one().getLong(0); | |
| 101 | 99 | // Assuming we cannot go back in time (yet) and execute the write at ts=1 |
| 102 | 100 | assertThat(tsReturned).isNotEqualTo(tsValue); |
| 103 | 101 | } |
| @@ -165,7 +163,7 @@ | ||
| 165 | 163 | mapper.setDefaultSaveOptions(Option.timestamp(defaultTimestamp)); |
| 166 | 164 | |
| 167 | 165 | mapper.save(new User(42, "helloworld"), Option.timestamp(explicitTimestamp)); |
| 168 | - Long savedTimestamp = session.execute("SELECT writetime(v) FROM user WHERE key=" + 42).one().getLong(0); | |
| 166 | + Long savedTimestamp = session().execute("SELECT writetime(v) FROM user WHERE key=" + 42).one().getLong(0); | |
| 169 | 167 | assertThat(savedTimestamp).isEqualTo(explicitTimestamp); |
| 170 | 168 | } |
| 171 | 169 | |
| @@ -186,7 +184,7 @@ | ||
| 186 | 184 | assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp); |
| 187 | 185 | mapper.delete(user.getKey()); |
| 188 | 186 | |
| 189 | - session.execute(mapper.saveQuery(user, Option.timestamp(timestamp))); | |
| 187 | + session().execute(mapper.saveQuery(user, Option.timestamp(timestamp))); | |
| 190 | 188 | assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp); |
| 191 | 189 | mapper.delete(user.getKey()); |
| 192 | 190 | |
| @@ -201,13 +199,13 @@ | ||
| 201 | 199 | assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp); |
| 202 | 200 | mapper.delete(user.getKey()); |
| 203 | 201 | |
| 204 | - session.execute(mapper.saveQuery(user)); | |
| 202 | + session().execute(mapper.saveQuery(user)); | |
| 205 | 203 | assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp); |
| 206 | 204 | mapper.delete(user.getKey()); |
| 207 | 205 | } |
| 208 | 206 | |
| 209 | 207 | private Long getWriteTime(int key) { |
| 210 | - Row row = session.execute("SELECT writetime(v) FROM user WHERE key=" + key).one(); | |
| 208 | + Row row = session().execute("SELECT writetime(v) FROM user WHERE key=" + key).one(); | |
| 211 | 209 | return row.getLong(0); |
| 212 | 210 | } |
| 213 | 211 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperPrimitiveTypesTest.java +3 −5
| @@ -26,8 +26,6 @@ | ||
| 26 | 26 | import java.math.BigInteger; |
| 27 | 27 | import java.net.InetAddress; |
| 28 | 28 | import java.nio.ByteBuffer; |
| 29 | -import java.util.Collection; | |
| 30 | -import java.util.Collections; | |
| 31 | 29 | import java.util.Date; |
| 32 | 30 | import java.util.UUID; |
| 33 | 31 | |
| @@ -38,8 +36,8 @@ | ||
| 38 | 36 | */ |
| 39 | 37 | public class MapperPrimitiveTypesTest extends CCMTestsSupport { |
| 40 | 38 | |
| 41 | - public Collection<String> createTestFixtures() { | |
| 42 | - return Collections.singletonList("CREATE TABLE primitiveTypes (" | |
| 39 | + public void onTestContextInitialized() { | |
| 40 | + execute("CREATE TABLE primitiveTypes (" | |
| 43 | 41 | + "byteBufferCol blob primary key," |
| 44 | 42 | + "intCol int, intWrapperCol int," |
| 45 | 43 | + "longCol bigint, longWrapperCol bigint," |
| @@ -96,7 +94,7 @@ | ||
| 96 | 94 | primitiveTypes.setUuidCol(uuidCol); |
| 97 | 95 | primitiveTypes.setTimeUuidCol(timeUuidCol); |
| 98 | 96 | |
| 99 | - Mapper<PrimitiveTypes> mapper = new MappingManager(session).mapper(PrimitiveTypes.class); | |
| 97 | + Mapper<PrimitiveTypes> mapper = new MappingManager(session()).mapper(PrimitiveTypes.class); | |
| 100 | 98 | mapper.save(primitiveTypes); |
| 101 | 99 | PrimitiveTypes primitiveTypes2 = mapper.get(byteBufferCol); |
| 102 | 100 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperSaveNullFieldsTest.java +4 −7
| @@ -20,12 +20,9 @@ | ||
| 20 | 20 | import com.datastax.driver.mapping.Mapper.Option; |
| 21 | 21 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 22 | 22 | import com.datastax.driver.mapping.annotations.Table; |
| 23 | -import com.google.common.collect.Lists; | |
| 24 | 23 | import org.testng.annotations.BeforeMethod; |
| 25 | 24 | import org.testng.annotations.Test; |
| 26 | 25 | |
| 27 | -import java.util.Collection; | |
| 28 | - | |
| 29 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
| 30 | 27 | |
| 31 | 28 | @SuppressWarnings("unused") |
| @@ -34,13 +31,13 @@ | ||
| 34 | 31 | Mapper<User> mapper; |
| 35 | 32 | |
| 36 | 33 | @Override |
| 37 | - public Collection<String> createTestFixtures() { | |
| 38 | - return Lists.newArrayList("CREATE TABLE user (login text primary key, name text, phone text)"); | |
| 34 | + public void onTestContextInitialized() { | |
| 35 | + execute("CREATE TABLE user (login text primary key, name text, phone text)"); | |
| 39 | 36 | } |
| 40 | 37 | |
| 41 | 38 | @BeforeMethod(groups = "short") |
| 42 | 39 | public void setup() { |
| 43 | - mapper = new MappingManager(session).mapper(User.class); | |
| 40 | + mapper = new MappingManager(session()).mapper(User.class); | |
| 44 | 41 | } |
| 45 | 42 | |
| 46 | 43 | @Test(groups = "short") |
| @@ -74,7 +71,7 @@ | ||
| 74 | 71 | |
| 75 | 72 | private void should_save_null_fields(boolean nullName, boolean nullPhone, boolean saveExpected, Option... options) { |
| 76 | 73 | // Start with clean data |
| 77 | - session.execute("insert into user(login, name, phone) " | |
| 74 | + session().execute("insert into user(login, name, phone) " | |
| 78 | 75 | + "values ('test_login', 'previous_name', 'previous_phone')"); |
| 79 | 76 | |
| 80 | 77 | String newName = nullName ? null : "new_name"; |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +13 −10
| @@ -24,7 +24,10 @@ | ||
| 24 | 24 | import org.testng.annotations.Test; |
| 25 | 25 | |
| 26 | 26 | import java.net.InetAddress; |
| 27 | -import java.util.*; | |
| 27 | +import java.util.Arrays; | |
| 28 | +import java.util.HashSet; | |
| 29 | +import java.util.Set; | |
| 30 | +import java.util.UUID; | |
| 28 | 31 | |
| 29 | 32 | import static org.assertj.core.api.Assertions.assertThat; |
| 30 | 33 | import static org.testng.Assert.assertEquals; |
| @@ -37,10 +40,10 @@ | ||
| 37 | 40 | public class MapperTest extends CCMTestsSupport { |
| 38 | 41 | |
| 39 | 42 | @Override |
| 40 | - public Collection<String> createTestFixtures() { | |
| 43 | + public void onTestContextInitialized() { | |
| 41 | 44 | // We'll allow to generate those create statement from the annotated entities later, but it's currently |
| 42 | 45 | // a TODO |
| 43 | - return Arrays.asList("CREATE TABLE users (user_id uuid PRIMARY KEY, name text, email text, year int, gender text)", | |
| 46 | + execute("CREATE TABLE users (user_id uuid PRIMARY KEY, name text, email text, year int, gender text)", | |
| 44 | 47 | "CREATE TABLE posts (user_id uuid, post_id timeuuid, title text, content text, device inet, tags set<text>, PRIMARY KEY(user_id, post_id))"); |
| 45 | 48 | } |
| 46 | 49 | |
| @@ -301,7 +304,7 @@ | ||
| 301 | 304 | // Very simple mapping a User, saving and getting it. Note that here we |
| 302 | 305 | // don't use the Accessor stuff since the queries we use are directly |
| 303 | 306 | // supported by the Mapper object. |
| 304 | - Mapper<User> m = new MappingManager(session).mapper(User.class); | |
| 307 | + Mapper<User> m = new MappingManager(session()).mapper(User.class); | |
| 305 | 308 | |
| 306 | 309 | User u1 = new User("Paul", "paul@yahoo.com", User.Gender.MALE); |
| 307 | 310 | u1.setYear(2014); |
| @@ -317,7 +320,7 @@ | ||
| 317 | 320 | @Test(groups = "short") |
| 318 | 321 | @CassandraVersion(major = 2.0) |
| 319 | 322 | public void testDynamicEntity() throws Exception { |
| 320 | - MappingManager manager = new MappingManager(session); | |
| 323 | + MappingManager manager = new MappingManager(session()); | |
| 321 | 324 | |
| 322 | 325 | Mapper<Post> m = manager.mapper(Post.class); |
| 323 | 326 | |
| @@ -386,7 +389,7 @@ | ||
| 386 | 389 | |
| 387 | 390 | @Test(groups = "short") |
| 388 | 391 | public void should_map_objects_from_partial_queries() throws Exception { |
| 389 | - MappingManager manager = new MappingManager(session); | |
| 392 | + MappingManager manager = new MappingManager(session()); | |
| 390 | 393 | |
| 391 | 394 | Mapper<Post> m = manager.mapper(Post.class); |
| 392 | 395 | |
| @@ -404,7 +407,7 @@ | ||
| 404 | 407 | m.save(p3); |
| 405 | 408 | |
| 406 | 409 | // Retrieve posts with a projection query that only retrieves some of the fields |
| 407 | - ResultSet rs = session.execute("select user_id, post_id, title from posts where user_id = " + u1.getUserId()); | |
| 410 | + ResultSet rs = session().execute("select user_id, post_id, title from posts where user_id = " + u1.getUserId()); | |
| 408 | 411 | |
| 409 | 412 | Result<Post> result = m.map(rs); |
| 410 | 413 | for (Post post : result) { |
| @@ -417,13 +420,13 @@ | ||
| 417 | 420 | } |
| 418 | 421 | |
| 419 | 422 | // cleanup |
| 420 | - session.execute("delete from posts where user_id = " + u1.getUserId()); | |
| 423 | + session().execute("delete from posts where user_id = " + u1.getUserId()); | |
| 421 | 424 | } |
| 422 | 425 | |
| 423 | 426 | |
| 424 | 427 | @Test(groups = "short") |
| 425 | 428 | public void should_return_table_metadata() throws Exception { |
| 426 | - MappingManager manager = new MappingManager(session); | |
| 429 | + MappingManager manager = new MappingManager(session()); | |
| 427 | 430 | |
| 428 | 431 | Mapper<Post> m = manager.mapper(Post.class); |
| 429 | 432 | |
| @@ -434,7 +437,7 @@ | ||
| 434 | 437 | |
| 435 | 438 | @Test(groups = "short") |
| 436 | 439 | public void should_not_initialize_session_when_protocol_version_provided() { |
| 437 | - Session newSession = cluster.newSession(); | |
| 440 | + Session newSession = cluster().newSession(); | |
| 438 | 441 | |
| 439 | 442 | // Ensures that a Session is not initialized when a protocol version is provided. |
| 440 | 443 | MappingManager manager = new MappingManager(newSession, ProtocolVersion.V1); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTCollectionsTest.java +7 −5
| @@ -23,7 +23,9 @@ | ||
| 23 | 23 | import org.testng.annotations.Test; |
| 24 | 24 | import org.testng.collections.Lists; |
| 25 | 25 | |
| 26 | -import java.util.*; | |
| 26 | +import java.util.List; | |
| 27 | +import java.util.Map; | |
| 28 | +import java.util.Set; | |
| 27 | 29 | |
| 28 | 30 | import static org.testng.Assert.assertEquals; |
| 29 | 31 | |
| @@ -34,8 +36,8 @@ | ||
| 34 | 36 | public class MapperUDTCollectionsTest extends CCMTestsSupport { |
| 35 | 37 | |
| 36 | 38 | @Override |
| 37 | - public Collection<String> createTestFixtures() { | |
| 38 | - return Arrays.asList("CREATE TYPE sub(i int)", | |
| 39 | + public void onTestContextInitialized() { | |
| 40 | + execute("CREATE TYPE sub(i int)", | |
| 39 | 41 | "CREATE TABLE collection_examples (id int PRIMARY KEY, l list<frozen<sub>>, s set<frozen<sub>>, m1 map<int,frozen<sub>>, m2 map<frozen<sub>,int>, m3 map<frozen<sub>,frozen<sub>>)"); |
| 40 | 42 | } |
| 41 | 43 | |
| @@ -174,7 +176,7 @@ | ||
| 174 | 176 | |
| 175 | 177 | @Test(groups = "short") |
| 176 | 178 | public void testCollections() throws Exception { |
| 177 | - Mapper<CollectionExamples> m = new MappingManager(session).mapper(CollectionExamples.class); | |
| 179 | + Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class); | |
| 178 | 180 | |
| 179 | 181 | CollectionExamples c = new CollectionExamples(1, 1); |
| 180 | 182 | m.save(c); |
| @@ -184,7 +186,7 @@ | ||
| 184 | 186 | |
| 185 | 187 | @Test(groups = "short") |
| 186 | 188 | public void testNullCollection() { |
| 187 | - Mapper<CollectionExamples> m = new MappingManager(session).mapper(CollectionExamples.class); | |
| 189 | + Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class); | |
| 188 | 190 | |
| 189 | 191 | CollectionExamples c = new CollectionExamples(1, 1); |
| 190 | 192 | c.setL(null); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +7 −7
| @@ -33,14 +33,14 @@ | ||
| 33 | 33 | public class MapperUDTTest extends CCMTestsSupport { |
| 34 | 34 | |
| 35 | 35 | @Override |
| 36 | - public Collection<String> createTestFixtures() { | |
| 37 | - return Arrays.asList("CREATE TYPE address (street text, city text, zip_code int, phones set<text>)", | |
| 36 | + public void onTestContextInitialized() { | |
| 37 | + execute("CREATE TYPE address (street text, city text, zip_code int, phones set<text>)", | |
| 38 | 38 | "CREATE TABLE users (user_id uuid PRIMARY KEY, name text, mainaddress frozen<address>, other_addresses map<text,frozen<address>>)"); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | @BeforeMethod(groups = "short") |
| 42 | 42 | public void clean() { |
| 43 | - session.execute("TRUNCATE users"); | |
| 43 | + session().execute("TRUNCATE users"); | |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | @Table(name = "users", |
| @@ -208,7 +208,7 @@ | ||
| 208 | 208 | |
| 209 | 209 | @Test(groups = "short") |
| 210 | 210 | public void testSimpleEntity() throws Exception { |
| 211 | - Mapper<User> m = new MappingManager(session).mapper(User.class); | |
| 211 | + Mapper<User> m = new MappingManager(session()).mapper(User.class); | |
| 212 | 212 | |
| 213 | 213 | User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 214 | 214 | u1.addOtherAddress("work", new Address("5 Main Street", "Springfield", 12345, "23431342")); |
| @@ -219,7 +219,7 @@ | ||
| 219 | 219 | |
| 220 | 220 | @Test(groups = "short") |
| 221 | 221 | public void should_handle_null_UDT_value() throws Exception { |
| 222 | - Mapper<User> m = new MappingManager(session).mapper(User.class); | |
| 222 | + Mapper<User> m = new MappingManager(session()).mapper(User.class); | |
| 223 | 223 | |
| 224 | 224 | User u1 = new User("Paul", null); |
| 225 | 225 | m.save(u1); |
| @@ -229,9 +229,9 @@ | ||
| 229 | 229 | |
| 230 | 230 | @Test(groups = "short") |
| 231 | 231 | public void testAccessor() throws Exception { |
| 232 | - MappingManager manager = new MappingManager(session); | |
| 232 | + MappingManager manager = new MappingManager(session()); | |
| 233 | 233 | |
| 234 | - Mapper<User> m = new MappingManager(session).mapper(User.class); | |
| 234 | + Mapper<User> m = new MappingManager(session()).mapper(User.class); | |
| 235 | 235 | User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 236 | 236 | m.save(u1); |
| 237 | 237 |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/SyntheticFieldsMapperTest.java +3 −5
| @@ -25,8 +25,6 @@ | ||
| 25 | 25 | import java.io.IOException; |
| 26 | 26 | import java.io.InputStream; |
| 27 | 27 | import java.lang.reflect.Constructor; |
| 28 | -import java.util.Collection; | |
| 29 | -import java.util.Collections; | |
| 30 | 28 | |
| 31 | 29 | import static org.testng.Assert.assertEquals; |
| 32 | 30 | |
| @@ -34,8 +32,8 @@ | ||
| 34 | 32 | public class SyntheticFieldsMapperTest extends CCMTestsSupport { |
| 35 | 33 | |
| 36 | 34 | @Override |
| 37 | - public Collection<String> createTestFixtures() { | |
| 38 | - return Collections.singletonList("CREATE TABLE synthetic_fields (id int PRIMARY KEY)"); | |
| 35 | + public void onTestContextInitialized() { | |
| 36 | + execute("CREATE TABLE synthetic_fields (id int PRIMARY KEY)"); | |
| 39 | 37 | } |
| 40 | 38 | |
| 41 | 39 | /** |
| @@ -61,7 +59,7 @@ | ||
| 61 | 59 | // Instead, it is a different Class (identity + ClassLoader), a modified version of ClassWithSyntheticField |
| 62 | 60 | // Nice ClassCastException will be thrown if we try to cast it to ClassWithSyntheticField |
| 63 | 61 | @SuppressWarnings("unchecked") |
| 64 | - Mapper<Object> m = (Mapper<Object>) new MappingManager(session).mapper(classWithSyntheticFields); | |
| 62 | + Mapper<Object> m = (Mapper<Object>) new MappingManager(session()).mapper(classWithSyntheticFields); | |
| 65 | 63 | m.save(instance); |
| 66 | 64 | |
| 67 | 65 | assertEquals(m.get(42), instance); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/UDTFieldMapperTest.java +19 −19
| @@ -34,11 +34,11 @@ | ||
| 34 | 34 | |
| 35 | 35 | @Test(groups = "short") |
| 36 | 36 | public void udt_and_tables_with_ks_created_in_another_session_should_be_mapped() { |
| 37 | - Cluster cluster = register(Cluster.builder() | |
| 38 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 39 | - .withPort(ccm.getBinaryPort()) | |
| 37 | + Cluster cluster1 = register(Cluster.builder() | |
| 38 | + .addContactPoints(getContactPoints().get(0)) | |
| 39 | + .withPort(ccm().getBinaryPort()) | |
| 40 | 40 | .build()); |
| 41 | - Session session1 = cluster.connect(); | |
| 41 | + Session session1 = cluster1.connect(); | |
| 42 | 42 | // Create type and table |
| 43 | 43 | session1.execute("create schema if not exists java_509 " + |
| 44 | 44 | "with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); |
| @@ -48,14 +48,14 @@ | ||
| 48 | 48 | session1.execute("create table java_509.my_hash (" + |
| 49 | 49 | "key int primary key, " + |
| 50 | 50 | "properties map<text, frozen<java_509.my_tuple>>);"); |
| 51 | - cluster.close(); | |
| 51 | + cluster1.close(); | |
| 52 | 52 | |
| 53 | 53 | // Create entities with another connection |
| 54 | - cluster = register(Cluster.builder() | |
| 55 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 56 | - .withPort(ccm.getBinaryPort()) | |
| 54 | + Cluster cluster2 = register(Cluster.builder() | |
| 55 | + .addContactPoints(getContactPoints().get(0)) | |
| 56 | + .withPort(ccm().getBinaryPort()) | |
| 57 | 57 | .build()); |
| 58 | - Session session2 = cluster.newSession(); | |
| 58 | + Session session2 = cluster2.newSession(); | |
| 59 | 59 | Mapper<MyHashWithKeyspace> hashMapper = new MappingManager(session2).mapper(MyHashWithKeyspace.class); |
| 60 | 60 | hashMapper.save(new MyHashWithKeyspace( |
| 61 | 61 | 1, |
| @@ -76,11 +76,11 @@ | ||
| 76 | 76 | |
| 77 | 77 | @Test(groups = "short") |
| 78 | 78 | public void udt_and_tables_without_ks_created_in_another_session_should_be_mapped() { |
| 79 | - Cluster cluster = register(Cluster.builder() | |
| 80 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 81 | - .withPort(ccm.getBinaryPort()) | |
| 79 | + Cluster cluster1 = register(Cluster.builder() | |
| 80 | + .addContactPoints(getContactPoints().get(0)) | |
| 81 | + .withPort(ccm().getBinaryPort()) | |
| 82 | 82 | .build()); |
| 83 | - Session session1 = cluster.connect(); | |
| 83 | + Session session1 = cluster1.connect(); | |
| 84 | 84 | session1.execute("create schema if not exists java_509b " + |
| 85 | 85 | "with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); |
| 86 | 86 | session1.execute("use java_509b"); |
| @@ -90,14 +90,14 @@ | ||
| 90 | 90 | session1.execute("create table my_hash (" + |
| 91 | 91 | "key int primary key, " + |
| 92 | 92 | "properties map<text, frozen<my_tuple>>);"); |
| 93 | - cluster.close(); | |
| 93 | + cluster1.close(); | |
| 94 | 94 | |
| 95 | 95 | // Create entities with another connection |
| 96 | - cluster = Cluster.builder() | |
| 97 | - .addContactPointsWithPorts(ccm.addressOfNode(1)) | |
| 98 | - .withPort(ccm.getBinaryPort()) | |
| 99 | - .build(); | |
| 100 | - Session session2 = cluster.newSession(); | |
| 96 | + Cluster cluster2 = register(Cluster.builder() | |
| 97 | + .addContactPoints(getContactPoints().get(0)) | |
| 98 | + .withPort(ccm().getBinaryPort()) | |
| 99 | + .build()); | |
| 100 | + Session session2 = cluster2.newSession(); | |
| 101 | 101 | |
| 102 | 102 | session2.execute("use java_509b"); |
| 103 | 103 | Mapper<MyHash> hashMapper = new MappingManager(session2).mapper(MyHash.class); |