kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ab0e361a495135e1c9c0b4950cd86f797e485443aeb341a931d6f66782110e59 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-09 22:31 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-04-01 14:16 +02:00 |
| Parents | 6c9eb909ee48 |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +44 −14
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import com.datastax.driver.core.*; |
| 19 | 19 | import com.datastax.driver.core.exceptions.CodecNotFoundException; |
| 20 | 20 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 21 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 21 | 22 | import com.datastax.driver.core.utils.Bytes; |
| 22 | 23 | import com.datastax.driver.core.utils.CassandraVersion; |
| 23 | 24 | import com.google.common.collect.ImmutableList; |
| @@ -26,14 +27,19 @@ | ||
| 26 | 27 | import com.google.common.collect.Sets; |
| 27 | 28 | import org.testng.annotations.Test; |
| 28 | 29 | |
| 30 | +import java.io.PrintWriter; | |
| 31 | +import java.io.StringWriter; | |
| 29 | 32 | import java.math.BigDecimal; |
| 30 | 33 | import java.math.BigInteger; |
| 31 | 34 | import java.net.InetAddress; |
| 35 | +import java.nio.ByteBuffer; | |
| 32 | 36 | import java.util.*; |
| 33 | 37 | |
| 34 | 38 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 35 | 39 | import static org.assertj.core.api.Assertions.assertThat; |
| 36 | -import static org.testng.Assert.*; | |
| 40 | +import static org.assertj.core.api.Assertions.fail; | |
| 41 | +import static org.testng.Assert.assertEquals; | |
| 42 | +import static org.testng.Assert.assertTrue; | |
| 37 | 43 | |
| 38 | 44 | public class QueryBuilderTest { |
| 39 | 45 | |
| @@ -139,35 +145,35 @@ | ||
| 139 | 145 | |
| 140 | 146 | try { |
| 141 | 147 | select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b")); |
| 142 | - fail(); | |
| 148 | + fail("Expected an IllegalStateException"); | |
| 143 | 149 | } catch (IllegalStateException e) { |
| 144 | 150 | assertEquals(e.getMessage(), "An ORDER BY clause has already been provided"); |
| 145 | 151 | } |
| 146 | 152 | |
| 147 | 153 | try { |
| 148 | 154 | select().column("a").all().from("foo"); |
| 149 | - fail(); | |
| 155 | + fail("Expected an IllegalStateException"); | |
| 150 | 156 | } catch (IllegalStateException e) { |
| 151 | 157 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 152 | 158 | } |
| 153 | 159 | |
| 154 | 160 | try { |
| 155 | 161 | select().column("a").countAll().from("foo"); |
| 156 | - fail(); | |
| 162 | + fail("Expected an IllegalStateException"); | |
| 157 | 163 | } catch (IllegalStateException e) { |
| 158 | 164 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 159 | 165 | } |
| 160 | 166 | |
| 161 | 167 | try { |
| 162 | 168 | select().all().from("foo").limit(-42); |
| 163 | - fail(); | |
| 169 | + fail("Expected an IllegalArgumentException"); | |
| 164 | 170 | } catch (IllegalArgumentException e) { |
| 165 | 171 | assertEquals(e.getMessage(), "Invalid LIMIT value, must be strictly positive"); |
| 166 | 172 | } |
| 167 | 173 | |
| 168 | 174 | try { |
| 169 | 175 | select().all().from("foo").limit(42).limit(42); |
| 170 | - fail(); | |
| 176 | + fail("Expected an IllegalStateException"); | |
| 171 | 177 | } catch (IllegalStateException e) { |
| 172 | 178 | assertEquals(e.getMessage(), "A LIMIT value has already been provided"); |
| 173 | 179 | } |
| @@ -243,7 +249,7 @@ | ||
| 243 | 249 | |
| 244 | 250 | try { |
| 245 | 251 | insertInto("foo").values(new String[]{"a", "b"}, new Object[]{1, 2, 3}); |
| 246 | - fail(); | |
| 252 | + fail("Expected an IllegalArgumentException"); | |
| 247 | 253 | } catch (IllegalArgumentException e) { |
| 248 | 254 | assertEquals(e.getMessage(), "Got 2 names but 3 values"); |
| 249 | 255 | } |
| @@ -323,7 +329,7 @@ | ||
| 323 | 329 | |
| 324 | 330 | try { |
| 325 | 331 | update("foo").using(ttl(-400)); |
| 326 | - fail(); | |
| 332 | + fail("Expected an IllegalArgumentException"); | |
| 327 | 333 | } catch (IllegalArgumentException e) { |
| 328 | 334 | assertEquals(e.getMessage(), "Invalid ttl, must be positive"); |
| 329 | 335 | } |
| @@ -377,14 +383,14 @@ | ||
| 377 | 383 | |
| 378 | 384 | try { |
| 379 | 385 | delete().column("a").all().from("foo"); |
| 380 | - fail(); | |
| 386 | + fail("Expected an IllegalStateException"); | |
| 381 | 387 | } catch (IllegalStateException e) { |
| 382 | 388 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 383 | 389 | } |
| 384 | 390 | |
| 385 | 391 | try { |
| 386 | 392 | delete().from("foo").using(timestamp(-1240003134L)); |
| 387 | - fail(); | |
| 393 | + fail("Expected an IllegalArgumentException"); | |
| 388 | 394 | } catch (IllegalArgumentException e) { |
| 389 | 395 | assertEquals(e.getMessage(), "Invalid timestamp, must be positive"); |
| 390 | 396 | } |
| @@ -880,7 +886,6 @@ | ||
| 880 | 886 | } |
| 881 | 887 | |
| 882 | 888 | @Test(groups = "unit") |
| 883 | - @CassandraVersion(major = 2.1) | |
| 884 | 889 | public void should_serialize_collections_of_serializable_elements() { |
| 885 | 890 | Set<UUID> set = Sets.newHashSet(UUID.randomUUID()); |
| 886 | 891 | List<Date> list = Lists.newArrayList(new Date()); |
| @@ -897,7 +902,6 @@ | ||
| 897 | 902 | } |
| 898 | 903 | |
| 899 | 904 | @Test(groups = "unit") |
| 900 | - @CassandraVersion(major = 2.1) | |
| 901 | 905 | public void should_not_attempt_to_serialize_function_calls_in_collections() { |
| 902 | 906 | BuiltStatement query = insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1))); |
| 903 | 907 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});"); |
| @@ -905,7 +909,13 @@ | ||
| 905 | 909 | } |
| 906 | 910 | |
| 907 | 911 | @Test(groups = "unit") |
| 908 | - @CassandraVersion(major = 2.1) | |
| 912 | + public void should_not_attempt_to_serialize_bind_markers_in_collections() { | |
| 913 | + BuiltStatement query = insertInto("foo").value("v", Lists.newArrayList(1, 2, bindMarker())); | |
| 914 | + assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,?]);"); | |
| 915 | + assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)).isNullOrEmpty(); | |
| 916 | + } | |
| 917 | + | |
| 918 | + @Test(groups = "unit") | |
| 909 | 919 | public void should_not_attempt_to_serialize_raw_values_in_collections() { |
| 910 | 920 | BuiltStatement query = insertInto("foo").value("v", ImmutableMap.of(1, raw("x"))); |
| 911 | 921 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});"); |
| @@ -913,7 +923,6 @@ | ||
| 913 | 923 | } |
| 914 | 924 | |
| 915 | 925 | @Test(groups = "unit") |
| 916 | - @CassandraVersion(major = 2.1) | |
| 917 | 926 | public void should_not_attempt_to_serialize_collections_containing_numbers() { |
| 918 | 927 | BuiltStatement query; |
| 919 | 928 | // lists |
| @@ -933,4 +942,25 @@ | ||
| 933 | 942 | assertThat(query.hasValues()).isFalse(); |
| 934 | 943 | } |
| 935 | 944 | |
| 945 | + @Test(groups = "unit") | |
| 946 | + public void should_include_original_cause_when_arguments_invalid() { | |
| 947 | + // Collection elements in protocol v2 must be at most 65535 bytes | |
| 948 | + ByteBuffer bb = ByteBuffer.allocate(65536); // too big | |
| 949 | + List<ByteBuffer> value = Lists.newArrayList(bb); | |
| 950 | + | |
| 951 | + BuiltStatement s = insertInto("foo").value("l", value); | |
| 952 | + try { | |
| 953 | + s.getValues(ProtocolVersion.V2, CodecRegistry.DEFAULT_INSTANCE); | |
| 954 | + fail("Expected an IllegalArgumentException"); | |
| 955 | + } catch (InvalidTypeException e) { | |
| 956 | + assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class); | |
| 957 | + StringWriter writer = new StringWriter(); | |
| 958 | + e.getCause().printStackTrace(new PrintWriter(writer)); | |
| 959 | + String stackTrace = writer.toString(); | |
| 960 | + assertThat(stackTrace).contains( | |
| 961 | + "Native protocol version 2 supports only elements with size up to 65535 bytes - " + | |
| 962 | + "but element size is 65536 bytes"); | |
| 963 | + } | |
| 964 | + } | |
| 965 | + | |
| 936 | 966 | } |