kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | b1aae6a0cc9230e22986845a7bb0e518b9a16c11aae2a6b9b1720024ce7ee7b6 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-02-20 14:55 -06:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-02-20 14:55 -06:00 |
| Parents | 688aaba7241c, ddaa2629e384 |
2 files changed
modified driver-core/src/test/java/com/datastax/driver/core/MetadataTest.java +1 −1
| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | Session session = cluster.connect(); |
| 48 | 48 | |
| 49 | 49 | 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}"); | |
| 51 | 51 | Metadata metadata = cluster.getMetadata(); |
| 52 | 52 | |
| 53 | 53 | // Capture all Token data. |
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +41 −27
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| 15 | 15 | import com.datastax.driver.core.policies.RoundRobinPolicy; |
| 16 | 16 | import com.datastax.driver.core.policies.WhiteListPolicy; |
| 17 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 17 | 18 | |
| 18 | 19 | import static com.datastax.driver.core.Assertions.assertThat; |
| 19 | 20 | |
| @@ -26,15 +27,13 @@ | ||
| 26 | 27 | public abstract class TokenIntegrationTest { |
| 27 | 28 | |
| 28 | 29 | 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}", | |
| 31 | 32 | "USE test", |
| 32 | - "CREATE TABLE IF NOT EXISTS foo(i int primary key)", | |
| 33 | + "CREATE TABLE foo(i int primary key)", | |
| 33 | 34 | "INSERT INTO foo (i) VALUES (1)", |
| 34 | 35 | "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)" | |
| 38 | 37 | ); |
| 39 | 38 | |
| 40 | 39 | private final String ccmOptions; |
| @@ -75,8 +74,9 @@ | ||
| 75 | 74 | session.execute(statement); |
| 76 | 75 | } |
| 77 | 76 | |
| 78 | - @AfterClass(groups = "short") | |
| 77 | + @AfterClass(groups = "short", alwaysRun=true) | |
| 79 | 78 | public void teardown() { |
| 79 | + System.out.println("Tearing down"); | |
| 80 | 80 | if (cluster != null) |
| 81 | 81 | cluster.close(); |
| 82 | 82 | if (ccm != null) |
| @@ -106,9 +106,12 @@ | ||
| 106 | 106 | |
| 107 | 107 | // Iterate the cluster's token ranges. For each one, use a range query to ask Cassandra which partition keys |
| 108 | 108 | // are in this range. |
| 109 | + | |
| 110 | + PreparedStatement rangeStmt = session.prepare("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?"); | |
| 111 | + | |
| 109 | 112 | TokenRange foundRange = null; |
| 110 | 113 | 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); | |
| 112 | 115 | for (Row row : rows) { |
| 113 | 116 | if (row.getInt("i") == testKey) { |
| 114 | 117 | // We should find our test key exactly once |
| @@ -124,12 +127,11 @@ | ||
| 124 | 127 | assertThat(foundRange).isNotNull(); |
| 125 | 128 | } |
| 126 | 129 | |
| 127 | - private List<Row> rangeQuery(String query, TokenRange range) { | |
| 130 | + private List<Row> rangeQuery(PreparedStatement rangeStmt, TokenRange range) { | |
| 128 | 131 | List<Row> rows = Lists.newArrayList(); |
| 129 | 132 | 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()); | |
| 133 | 135 | } |
| 134 | 136 | return rows; |
| 135 | 137 | } |
| @@ -145,13 +147,10 @@ | ||
| 145 | 147 | * |
| 146 | 148 | * <ol> |
| 147 | 149 | * <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> | |
| 151 | 151 | * <li>Select data by token with a BoundStatement.</li> |
| 152 | 152 | * <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> | |
| 155 | 154 | * </ol> |
| 156 | 155 | * |
| 157 | 156 | * @test_category token |
| @@ -170,13 +169,6 @@ | ||
| 170 | 169 | row.getPartitionKeyToken() |
| 171 | 170 | ).isEqualTo(token); |
| 172 | 171 | |
| 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 | - | |
| 180 | 172 | PreparedStatement pst = session.prepare("SELECT * FROM test.foo WHERE token(i) = ?"); |
| 181 | 173 | row = session.execute(pst.bind(token)).one(); |
| 182 | 174 | assertThat(row.getInt(0)).isEqualTo(1); |
| @@ -186,9 +178,31 @@ | ||
| 186 | 178 | |
| 187 | 179 | row = session.execute(pst.bind().setPartitionKeyToken(token)).one(); |
| 188 | 180 | 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); | |
| 189 | 203 | |
| 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(); | |
| 192 | 206 | assertThat(row.getInt(0)).isEqualTo(1); |
| 193 | 207 | |
| 194 | 208 | row = session.execute("SELECT * FROM test.foo WHERE token(i) = ?", token).one(); |
| @@ -283,7 +297,7 @@ | ||
| 283 | 297 | // Note that this relies on all queries going to node 1, which is why we use a WhiteList LBP in setup(). |
| 284 | 298 | Row row = (host.listenAddress == null) |
| 285 | 299 | ? 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(); | |
| 287 | 301 | Set<String> tokenStrings = row.getSet("tokens", String.class); |
| 288 | 302 | assertThat(tokenStrings).hasSize(numTokens); |
| 289 | 303 | Iterable<Token> tokensFromSystemTable = Iterables.transform(tokenStrings, new Function<String, Token>() { |