kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8ae5bc7181fe93f28e0ffbf5ea3f0e200741b75af381011f1f2ca50691071446 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-02-26 19:29 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-02-26 19:29 +01:00 |
| Parents | 3439c5c67fc1, 9bbd20a64b85 |
3 files changed
modified changelog/README.md +1 −0
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | - [improvement] JAVA-974: Validate accessor parameter types against bound statement. |
| 21 | 21 | - [bug] JAVA-1068: Unwrap StatementWrappers when hashing the paging state. |
| 22 | 22 | - [bug] JAVA-831: Mapper can't load an entity where the PK is a UDT. |
| 23 | +- [improvement] JAVA-1021: Improve error message when connect() is called with an invalid keyspace name. | |
| 23 | 24 | |
| 24 | 25 | Merged from 2.0 branch: |
| 25 | 26 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +15 −8
| @@ -15,16 +15,14 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.exceptions.AuthenticationException; | |
| 19 | -import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 20 | -import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 21 | -import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 18 | +import com.datastax.driver.core.exceptions.*; | |
| 22 | 19 | import com.datastax.driver.core.policies.*; |
| 23 | 20 | import com.datastax.driver.core.utils.MoreFutures; |
| 24 | 21 | import com.google.common.annotations.VisibleForTesting; |
| 25 | 22 | import com.google.common.base.Functions; |
| 26 | 23 | import com.google.common.base.Predicates; |
| 27 | 24 | import com.google.common.base.Strings; |
| 25 | +import com.google.common.base.Throwables; | |
| 28 | 26 | import com.google.common.collect.*; |
| 29 | 27 | import com.google.common.util.concurrent.*; |
| 30 | 28 | import org.slf4j.Logger; |
| @@ -334,19 +332,28 @@ | ||
| 334 | 332 | if (keyspace == null) { |
| 335 | 333 | return sessionInitialized; |
| 336 | 334 | } else { |
| 335 | + final String useQuery = "USE " + keyspace; | |
| 337 | 336 | ListenableFuture<ResultSet> keyspaceSet = Futures.transform(sessionInitialized, new AsyncFunction<Session, ResultSet>() { |
| 338 | 337 | @Override |
| 339 | 338 | public ListenableFuture<ResultSet> apply(Session session) throws Exception { |
| 340 | - return session.executeAsync("USE " + keyspace); | |
| 339 | + return session.executeAsync(useQuery); | |
| 341 | 340 | } |
| 342 | 341 | }); |
| 343 | - Futures.addCallback(keyspaceSet, new MoreFutures.FailureCallback<ResultSet>() { | |
| 342 | + ListenableFuture<ResultSet> withErrorHandling = Futures.withFallback(keyspaceSet, new FutureFallback<ResultSet>() { | |
| 344 | 343 | @Override |
| 345 | - public void onFailure(Throwable t) { | |
| 344 | + public ListenableFuture<ResultSet> create(Throwable t) throws Exception { | |
| 346 | 345 | session.closeAsync(); |
| 346 | + if (t instanceof SyntaxError) { | |
| 347 | + // Give a more explicit message, because it's probably caused by a bad keyspace name | |
| 348 | + SyntaxError e = (SyntaxError) t; | |
| 349 | + t = new SyntaxError(e.getAddress(), | |
| 350 | + String.format("Error executing \"%s\" (%s). Check that your keyspace name is valid", | |
| 351 | + useQuery, e.getMessage())); | |
| 352 | + } | |
| 353 | + throw Throwables.propagate(t); | |
| 347 | 354 | } |
| 348 | 355 | }); |
| 349 | - return Futures.transform(keyspaceSet, Functions.<Session>constant(session)); | |
| 356 | + return Futures.transform(withErrorHandling, Functions.<Session>constant(session)); | |
| 350 | 357 | } |
| 351 | 358 | } |
| 352 | 359 |
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, TestUtils.INSERT_FORMAT, 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, TestUtils.INSERT_FORMAT, 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, TestUtils.INSERT_FORMAT, 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 | } |