kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 6d5deb0e2ba0996a3a6e75401be2b8e0fbf56d7a234b0c8650733f2880471c67 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-02-26 19:29 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-31 17:05 +02:00 |
| Parents | b8316e077255 |
3 files changed
modified changelog/README.md +1 −0
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | - [improvement] JAVA-851: Improve UUIDs javadoc with regard to user-provided timestamps. |
| 10 | 10 | - [improvement] JAVA-979: Update javadoc for RegularStatement toString() and getQueryString() to indicate that consistency level and other parameters are not maintained in the query string. |
| 11 | 11 | - [bug] JAVA-1068: Unwrap StatementWrappers when hashing the paging state. |
| 12 | +- [improvement] JAVA-1021: Improve error message when connect() is called with an invalid keyspace name. | |
| 12 | 13 | |
| 13 | 14 | |
| 14 | 15 | ### 3.0.0 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +14 −4
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | import com.google.common.base.Functions; |
| 23 | 23 | import com.google.common.base.Predicates; |
| 24 | 24 | import com.google.common.base.Strings; |
| 25 | +import com.google.common.base.Throwables; | |
| 25 | 26 | import com.google.common.collect.*; |
| 26 | 27 | import com.google.common.util.concurrent.*; |
| 27 | 28 | import org.slf4j.Logger; |
| @@ -336,19 +337,28 @@ | ||
| 336 | 337 | if (keyspace == null) { |
| 337 | 338 | return sessionInitialized; |
| 338 | 339 | } else { |
| 340 | + final String useQuery = "USE " + keyspace; | |
| 339 | 341 | ListenableFuture<ResultSet> keyspaceSet = Futures.transform(sessionInitialized, new AsyncFunction<Session, ResultSet>() { |
| 340 | 342 | @Override |
| 341 | 343 | public ListenableFuture<ResultSet> apply(Session session) throws Exception { |
| 342 | - return session.executeAsync("USE " + keyspace); | |
| 344 | + return session.executeAsync(useQuery); | |
| 343 | 345 | } |
| 344 | 346 | }); |
| 345 | - Futures.addCallback(keyspaceSet, new MoreFutures.FailureCallback<ResultSet>() { | |
| 347 | + ListenableFuture<ResultSet> withErrorHandling = Futures.withFallback(keyspaceSet, new FutureFallback<ResultSet>() { | |
| 346 | 348 | @Override |
| 347 | - public void onFailure(Throwable t) { | |
| 349 | + public ListenableFuture<ResultSet> create(Throwable t) throws Exception { | |
| 348 | 350 | session.closeAsync(); |
| 351 | + if (t instanceof SyntaxError) { | |
| 352 | + // Give a more explicit message, because it's probably caused by a bad keyspace name | |
| 353 | + SyntaxError e = (SyntaxError) t; | |
| 354 | + t = new SyntaxError(e.getAddress(), | |
| 355 | + String.format("Error executing \"%s\" (%s). Check that your keyspace name is valid", | |
| 356 | + useQuery, e.getMessage())); | |
| 357 | + } | |
| 358 | + throw Throwables.propagate(t); | |
| 349 | 359 | } |
| 350 | 360 | }); |
| 351 | - return Futures.transform(keyspaceSet, Functions.constant(session)); | |
| 361 | + return Futures.transform(withErrorHandling, Functions.constant(session)); | |
| 352 | 362 | } |
| 353 | 363 | } |
| 354 | 364 |
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +59 −23
| @@ -15,18 +15,17 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.exceptions.SyntaxError; | |
| 18 | 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 19 | 21 | import org.testng.annotations.Test; |
| 20 | 22 | |
| 21 | 23 | import java.util.List; |
| 22 | 24 | import java.util.Locale; |
| 23 | -import java.util.concurrent.CountDownLatch; | |
| 24 | -import java.util.concurrent.ExecutorService; | |
| 25 | -import java.util.concurrent.Executors; | |
| 26 | -import java.util.concurrent.TimeUnit; | |
| 25 | +import java.util.concurrent.*; | |
| 27 | 26 | |
| 27 | +import static com.datastax.driver.core.Assertions.*; | |
| 28 | 28 | import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; |
| 29 | -import static org.testng.Assert.*; | |
| 30 | 29 | |
| 31 | 30 | /** |
| 32 | 31 | * Simple test of the Sessions methods against a one node cluster. |
| @@ -47,11 +46,11 @@ | ||
| 47 | 46 | } |
| 48 | 47 | |
| 49 | 48 | @Test(groups = "short") |
| 50 | - public void executeTest() throws Exception { | |
| 49 | + public void should_execute_simple_statements() throws Exception { | |
| 51 | 50 | // Simple calls to all versions of the execute/executeAsync methods |
| 52 | 51 | String key = "execute_test"; |
| 53 | 52 | ResultSet rs = session().execute(String.format(Locale.US, "INSERT INTO %s (k, t, i, f) VALUES ('%s', '%s', %d, %f)", TABLE1, key, "foo", 42, 24.03f)); |
| 54 | - assertTrue(rs.isExhausted()); | |
| 53 | + assertThat(rs.isExhausted()).isTrue(); | |
| 55 | 54 | |
| 56 | 55 | // execute |
| 57 | 56 | checkExecuteResultSet(session().execute(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)), key); |
| @@ -63,12 +62,12 @@ | ||
| 63 | 62 | } |
| 64 | 63 | |
| 65 | 64 | @Test(groups = "short") |
| 66 | - public void executePreparedTest() throws Exception { | |
| 65 | + public void should_execute_prepared_statements() throws Exception { | |
| 67 | 66 | // Simple calls to all versions of the execute/executeAsync methods for prepared statements |
| 68 | 67 | // Note: the goal is only to exercice the Session methods, PreparedStatementTest have better prepared statement tests. |
| 69 | 68 | String key = "execute_prepared_test"; |
| 70 | 69 | ResultSet rs = session().execute(String.format(Locale.US, "INSERT INTO %s (k, t, i, f) VALUES ('%s', '%s', %d, %f)", TABLE2, key, "foo", 42, 24.03f)); |
| 71 | - assertTrue(rs.isExhausted()); | |
| 70 | + assertThat(rs.isExhausted()).isTrue(); | |
| 72 | 71 | |
| 73 | 72 | PreparedStatement p = session().prepare(String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = ?", TABLE2)); |
| 74 | 73 | BoundStatement bs = p.bind(key); |
| @@ -83,17 +82,17 @@ | ||
| 83 | 82 | } |
| 84 | 83 | |
| 85 | 84 | private static void checkExecuteResultSet(ResultSet rs, String key) { |
| 86 | - assertTrue(!rs.isExhausted()); | |
| 85 | + assertThat(rs.isExhausted()).isFalse(); | |
| 87 | 86 | Row row = rs.one(); |
| 88 | - assertTrue(rs.isExhausted()); | |
| 89 | - assertEquals(row.getString("k"), key); | |
| 90 | - assertEquals(row.getString("t"), "foo"); | |
| 91 | - assertEquals(row.getInt("i"), 42); | |
| 92 | - assertEquals(row.getFloat("f"), 24.03f, 0.1f); | |
| 87 | + assertThat(rs.isExhausted()).isTrue(); | |
| 88 | + assertThat(row.getString("k")).isEqualTo(key); | |
| 89 | + assertThat(row.getString("t")).isEqualTo("foo"); | |
| 90 | + assertThat(row.getInt("i")).isEqualTo(42); | |
| 91 | + assertThat(row.getFloat("f")).isEqualTo(24.03f, offset(0.1f)); | |
| 93 | 92 | } |
| 94 | 93 | |
| 95 | 94 | @Test(groups = "short") |
| 96 | - public void executePreparedCounterTest() throws Exception { | |
| 95 | + public void should_execute_prepared_counter_statement() throws Exception { | |
| 97 | 96 | PreparedStatement p = session().prepare("UPDATE " + COUNTER_TABLE + " SET c = c + ? WHERE k = ?"); |
| 98 | 97 | |
| 99 | 98 | session().execute(p.bind(1L, "row")); |
| @@ -101,8 +100,8 @@ | ||
| 101 | 100 | |
| 102 | 101 | ResultSet rs = session().execute("SELECT * FROM " + COUNTER_TABLE); |
| 103 | 102 | List<Row> rows = rs.all(); |
| 104 | - assertEquals(rows.size(), 1); | |
| 105 | - assertEquals(rows.get(0).getLong("c"), 2L); | |
| 103 | + assertThat(rows).hasSize(1); | |
| 104 | + assertThat(rows.get(0).getLong("c")).isEqualTo(2L); | |
| 106 | 105 | } |
| 107 | 106 | |
| 108 | 107 | /** |
| @@ -113,7 +112,7 @@ | ||
| 113 | 112 | * @expected_result session established and queries made successfully using it. |
| 114 | 113 | */ |
| 115 | 114 | @Test(groups = "short") |
| 116 | - public void session_should_function_with_snappy_compression() throws Exception { | |
| 115 | + public void should_function_with_snappy_compression() throws Exception { | |
| 117 | 116 | compressionTest(ProtocolOptions.Compression.SNAPPY); |
| 118 | 117 | } |
| 119 | 118 | |
| @@ -126,7 +125,7 @@ | ||
| 126 | 125 | */ |
| 127 | 126 | @Test(groups = "short") |
| 128 | 127 | @CassandraVersion(major = 2.0) |
| 129 | - public void session_should_function_with_lz4_compression() throws Exception { | |
| 128 | + public void should_function_with_lz4_compression() throws Exception { | |
| 130 | 129 | compressionTest(ProtocolOptions.Compression.LZ4); |
| 131 | 130 | } |
| 132 | 131 | |
| @@ -138,7 +137,7 @@ | ||
| 138 | 137 | // Simple calls to all versions of the execute/executeAsync methods |
| 139 | 138 | String key = "execute_compressed_test_" + compression; |
| 140 | 139 | ResultSet rs = compressedSession.execute(String.format(Locale.US, "INSERT INTO %s (k, t, i, f) VALUES ('%s', '%s', %d, %f)", TABLE3, key, "foo", 42, 24.03f)); |
| 141 | - assertTrue(rs.isExhausted()); | |
| 140 | + assertThat(rs.isExhausted()).isTrue(); | |
| 142 | 141 | |
| 143 | 142 | String SELECT_ALL = String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = '%s'", TABLE3, key); |
| 144 | 143 | |
| @@ -159,7 +158,7 @@ | ||
| 159 | 158 | * Checks for deadlocks when a session shutdown races with the initialization of the cluster (JAVA-418). |
| 160 | 159 | */ |
| 161 | 160 | @Test(groups = "short") |
| 162 | - public void closeDuringClusterInitTest() throws InterruptedException { | |
| 161 | + public void should_close_properly_when_racing_with_cluster_init() throws InterruptedException { | |
| 163 | 162 | for (int i = 0; i < 500; i++) { |
| 164 | 163 | |
| 165 | 164 | // Use our own cluster and session (not the ones provided by the parent class) because we want an uninitialized cluster |
| @@ -206,7 +205,7 @@ | ||
| 206 | 205 | |
| 207 | 206 | executor.shutdown(); |
| 208 | 207 | boolean normalShutdown = executor.awaitTermination(5, TimeUnit.SECONDS); |
| 209 | - assertTrue(normalShutdown); | |
| 208 | + assertThat(normalShutdown).isTrue(); | |
| 210 | 209 | |
| 211 | 210 | } finally { |
| 212 | 211 | // The deadlock occurred here before JAVA-418 |
| @@ -214,4 +213,41 @@ | ||
| 214 | 213 | } |
| 215 | 214 | } |
| 216 | 215 | } |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Ensures that if an attempt is made to create a {@link Session} via {@link Cluster#connect} with an invalid | |
| 219 | + * keyspace that the returned exception is decorated with an indication to check that your keyspace name is valid | |
| 220 | + * and includes the original {@link SyntaxError}. | |
| 221 | + */ | |
| 222 | + @Test(groups = "short") | |
| 223 | + public void should_give_explicit_error_message_when_keyspace_name_invalid() { | |
| 224 | + try { | |
| 225 | + cluster().connect("%!;"); | |
| 226 | + fail("Expected a SyntaxError"); | |
| 227 | + } catch (SyntaxError e) { | |
| 228 | + assertThat(e.getMessage()) | |
| 229 | + .contains("Error executing \"USE %!;\"") | |
| 230 | + .contains("Check that your keyspace name is valid"); | |
| 231 | + } | |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * Ensures that if an attempt is made to create a {@link Session} via {@link Cluster#connectAsync} with an invalid | |
| 236 | + * keyspace that the returned exception is decorated with an indication to check that your keyspace name is valid | |
| 237 | + * and includes the original {@link SyntaxError}. | |
| 238 | + */ | |
| 239 | + @Test(groups = "short") | |
| 240 | + public void should_give_explicit_error_message_when_keyspace_name_invalid_async() { | |
| 241 | + ListenableFuture<Session> sessionFuture = cluster().connectAsync(""); | |
| 242 | + try { | |
| 243 | + sessionFuture.get(); | |
| 244 | + } catch (ExecutionException e) { | |
| 245 | + assertThat(e.getCause()).isInstanceOf(SyntaxError.class); | |
| 246 | + assertThat(e.getCause().getMessage()) | |
| 247 | + .contains("no viable alternative at input '<EOF>'") | |
| 248 | + .contains("Check that your keyspace name is valid"); | |
| 249 | + } catch (Exception e) { | |
| 250 | + fail("Did not expect Exception", e); | |
| 251 | + } | |
| 252 | + } | |
| 217 | 253 | } |