Elffuss BraidHub

kikocis/java-driver public

Update Token Integration tests for 1.2.x support.
Changesetddaa2629e3846d67356deab55224b88936f1749d87c1d379f2d4482c7f0c3f6c
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-02-20 14:46 -06:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-02-20 14:46 -06:00
Parents8a7467807397

Browse the files at this changeset

2 files changed

modified driver-core/src/test/java/com/datastax/driver/core/MetadataTest.java +1 −1
@@ -47,7 +47,7 @@
4747 Session session = cluster.connect();
4848
4949 String keyspace = "test";
50- session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
50+ session.execute("CREATE KEYSPACE " + keyspace + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
5151 Metadata metadata = cluster.getMetadata();
5252
5353 // Capture all Token data.
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +41 −27
@@ -14,6 +14,7 @@
1414 import com.datastax.driver.core.policies.LoadBalancingPolicy;
1515 import com.datastax.driver.core.policies.RoundRobinPolicy;
1616 import com.datastax.driver.core.policies.WhiteListPolicy;
17+import com.datastax.driver.core.utils.CassandraVersion;
1718
1819 import static com.datastax.driver.core.Assertions.assertThat;
1920
@@ -26,15 +27,13 @@
2627 public abstract class TokenIntegrationTest {
2728
2829 List<String> schema = Lists.newArrayList(
29- "CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}",
30- "CREATE KEYSPACE IF NOT EXISTS test2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2}",
30+ "CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}",
31+ "CREATE KEYSPACE test2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2}",
3132 "USE test",
32- "CREATE TABLE IF NOT EXISTS foo(i int primary key)",
33+ "CREATE TABLE foo(i int primary key)",
3334 "INSERT INTO foo (i) VALUES (1)",
3435 "INSERT INTO foo (i) VALUES (2)",
35- "INSERT INTO foo (i) VALUES (3)",
36- "CREATE TABLE IF NOT EXISTS foo2(\"caseSensitiveKey\" int primary key)",
37- "INSERT INTO foo2 (\"caseSensitiveKey\") VALUES (1)"
36+ "INSERT INTO foo (i) VALUES (3)"
3837 );
3938
4039 private final String ccmOptions;
@@ -75,8 +74,9 @@
7574 session.execute(statement);
7675 }
7776
78- @AfterClass(groups = "short")
77+ @AfterClass(groups = "short", alwaysRun=true)
7978 public void teardown() {
79+ System.out.println("Tearing down");
8080 if (cluster != null)
8181 cluster.close();
8282 if (ccm != null)
@@ -106,9 +106,12 @@
106106
107107 // Iterate the cluster's token ranges. For each one, use a range query to ask Cassandra which partition keys
108108 // are in this range.
109+
110+ PreparedStatement rangeStmt = session.prepare("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?");
111+
109112 TokenRange foundRange = null;
110113 for (TokenRange range : metadata.getTokenRanges()) {
111- List<Row> rows = rangeQuery("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?", range);
114+ List<Row> rows = rangeQuery(rangeStmt, range);
112115 for (Row row : rows) {
113116 if (row.getInt("i") == testKey) {
114117 // We should find our test key exactly once
@@ -124,12 +127,11 @@
124127 assertThat(foundRange).isNotNull();
125128 }
126129
127- private List<Row> rangeQuery(String query, TokenRange range) {
130+ private List<Row> rangeQuery(PreparedStatement rangeStmt, TokenRange range) {
128131 List<Row> rows = Lists.newArrayList();
129132 for (TokenRange subRange : range.unwrap()) {
130- rows.addAll(session.execute(query,
131- subRange.getStart(), subRange.getEnd())
132- .all());
133+ Statement statement = rangeStmt.bind(subRange.getStart(), subRange.getEnd());
134+ rows.addAll(session.execute(statement).all());
133135 }
134136 return rows;
135137 }
@@ -145,13 +147,10 @@
145147 *
146148 * <ol>
147149 * <li>Retrieve the token for the key with value '1', get it by index, and ensure if is of the expected token type.</li>
148- * <li>Retrieve the token for the key with value '1', get it by name, and ensure it matches the token by index.</li>
149- * <li>Retrieve the token by alias for the key '1', and ensure it matches the token by index.</li>
150- * <li>Retrieve the token for a case sensitive key.</li>
150+ * <li>Retrieve the token for the partition key with getPartitionKeyToken</li>
151151 * <li>Select data by token with a BoundStatement.</li>
152152 * <li>Select data by token using setToken by index.</li>
153- * <li>Select data by token using setToken by name.</li>
154- * <li>Select data by token with a SimpleStatement</li>
153+ * <li>Select data by token with setPartitionKeyToken.</li>
155154 * </ol>
156155 *
157156 * @test_category token
@@ -170,13 +169,6 @@
170169 row.getPartitionKeyToken()
171170 ).isEqualTo(token);
172171
173- // get by name when column is aliased:
174- assertThat(
175- session.execute("SELECT token(i) AS t FROM test.foo WHERE i = 1").one()
176- .getToken("t")
177- ).isEqualTo(token);
178-
179-
180172 PreparedStatement pst = session.prepare("SELECT * FROM test.foo WHERE token(i) = ?");
181173 row = session.execute(pst.bind(token)).one();
182174 assertThat(row.getInt(0)).isEqualTo(1);
@@ -186,9 +178,31 @@
186178
187179 row = session.execute(pst.bind().setPartitionKeyToken(token)).one();
188180 assertThat(row.getInt(0)).isEqualTo(1);
181+ }
182+
183+ /**
184+ * <p>
185+ * Validates that a {@link Token} can be retrieved and parsed by using bind variables and
186+ * aliasing.
187+ * </p>
188+ *
189+ * <p>
190+ * This test does the following:
191+ *
192+ * <ol>
193+ * <li>Retrieve the token by alias for the key '1', and ensure it matches the token by index.</li>
194+ * <li>Select data by token using setToken by name.</li>
195+ * </ol>
196+ */
197+ @Test(groups = "short")
198+ @CassandraVersion(major=2)
199+ public void should_get_token_from_row_and_set_token_in_query_with_binding_and_aliasing() {
200+ Row row = session.execute("SELECT token(i) AS t FROM test.foo WHERE i = 1").one();
201+ Token token = row.getToken("t");
202+ assertThat(token.getType()).isEqualTo(expectedTokenType);
189203
190- PreparedStatement pst2 = session.prepare("SELECT * FROM test.foo WHERE token(i) = :myToken");
191- row = session.execute(pst2.bind().setToken("myToken", token)).one();
204+ PreparedStatement pst = session.prepare("SELECT * FROM test.foo WHERE token(i) = :myToken");
205+ row = session.execute(pst.bind().setToken("myToken", token)).one();
192206 assertThat(row.getInt(0)).isEqualTo(1);
193207
194208 row = session.execute("SELECT * FROM test.foo WHERE token(i) = ?", token).one();
@@ -283,7 +297,7 @@
283297 // Note that this relies on all queries going to node 1, which is why we use a WhiteList LBP in setup().
284298 Row row = (host.listenAddress == null)
285299 ? session.execute("select tokens from system.local").one()
286- : session.execute("select tokens from system.peers where peer = ?", host.listenAddress).one();
300+ : session.execute("select tokens from system.peers where peer = '" + host.listenAddress.getHostAddress() + "'").one();
287301 Set<String> tokenStrings = row.getSet("tokens", String.class);
288302 assertThat(tokenStrings).hasSize(numTokens);
289303 Iterable<Token> tokensFromSystemTable = Iterables.transform(tokenStrings, new Function<String, Token>() {