kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 70bd5c21cb6ee6867d87e2f663f22d48e2661af25f282e46c793ad553d96c42c |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-04-05 18:46 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-04-05 18:46 +02:00 |
| Parents | 2adc07b50845, 01295193b436 |
5 files changed
modified changelog/README.md +1 −0
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | ### 3.0.1 (in progress) |
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1. |
| 6 | +- [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger. | |
| 6 | 7 | |
| 7 | 8 | Merged from 2.1 branch: |
| 8 | 9 |
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +87 −6
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | import org.slf4j.LoggerFactory; |
| 22 | 22 | |
| 23 | 23 | import java.nio.ByteBuffer; |
| 24 | +import java.util.Iterator; | |
| 24 | 25 | import java.util.List; |
| 25 | 26 | |
| 26 | 27 | import static java.util.concurrent.TimeUnit.NANOSECONDS; |
| @@ -148,8 +149,10 @@ | ||
| 148 | 149 | * within a configurable threshold in milliseconds. |
| 149 | 150 | * <p/> |
| 150 | 151 | * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}. |
| 151 | - * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}, | |
| 152 | - * then the query parameters (if any) will be logged as well (names and actual values). | |
| 152 | + * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement} | |
| 153 | + * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement} | |
| 154 | + * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order | |
| 155 | + * and named values are logged with names and value. | |
| 153 | 156 | * <p/> |
| 154 | 157 | * The name of this logger is {@code com.datastax.driver.core.QueryLogger.NORMAL}. |
| 155 | 158 | */ |
| @@ -160,8 +163,10 @@ | ||
| 160 | 163 | * but whose execution time exceeded a configurable threshold in milliseconds. |
| 161 | 164 | * <p/> |
| 162 | 165 | * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}. |
| 163 | - * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}, | |
| 164 | - * then the query parameters (if any) will be logged as well (names and actual values). | |
| 166 | + * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement} | |
| 167 | + * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement} | |
| 168 | + * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order | |
| 169 | + * and named values are logged with names and value. | |
| 165 | 170 | * <p/> |
| 166 | 171 | * The name of this logger is {@code com.datastax.driver.core.QueryLogger.SLOW}. |
| 167 | 172 | */ |
| @@ -171,8 +176,10 @@ | ||
| 171 | 176 | * The logger used to log unsuccessful queries, i.e., queries that did not complete normally and threw an exception. |
| 172 | 177 | * <p/> |
| 173 | 178 | * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}. |
| 174 | - * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}, | |
| 175 | - * then the query parameters (if any) will be logged as well (names and actual values). | |
| 179 | + * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement} | |
| 180 | + * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement} | |
| 181 | + * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order | |
| 182 | + * and named values are logged with names and value. | |
| 176 | 183 | * Note this this logger will also print the full stack trace of the reported exception. |
| 177 | 184 | * <p/> |
| 178 | 185 | * The name of this logger is {@code com.datastax.driver.core.QueryLogger.ERROR}. |
| @@ -650,12 +657,16 @@ | ||
| 650 | 657 | StringBuilder params = new StringBuilder(); |
| 651 | 658 | if (statement instanceof BoundStatement) { |
| 652 | 659 | appendParameters((BoundStatement) statement, params, maxLoggedParameters); |
| 660 | + } else if (statement instanceof SimpleStatement) { | |
| 661 | + appendParameters((SimpleStatement) statement, params, maxLoggedParameters); | |
| 653 | 662 | } else if (statement instanceof BatchStatement) { |
| 654 | 663 | BatchStatement batchStatement = (BatchStatement) statement; |
| 655 | 664 | int remaining = maxLoggedParameters; |
| 656 | 665 | for (Statement inner : batchStatement.getStatements()) { |
| 657 | 666 | if (inner instanceof BoundStatement) { |
| 658 | 667 | remaining = appendParameters((BoundStatement) inner, params, remaining); |
| 668 | + } else if (inner instanceof SimpleStatement) { | |
| 669 | + remaining = appendParameters((SimpleStatement) inner, params, remaining); | |
| 659 | 670 | } |
| 660 | 671 | } |
| 661 | 672 | } |
| @@ -677,6 +688,9 @@ | ||
| 677 | 688 | } else if (statement instanceof BoundStatement) { |
| 678 | 689 | int boundValues = ((BoundStatement) statement).wrapper.values.length; |
| 679 | 690 | sb.append("[" + boundValues + " bound values] "); |
| 691 | + } else if (statement instanceof SimpleStatement) { | |
| 692 | + int boundValues = ((SimpleStatement) statement).valuesCount(); | |
| 693 | + sb.append("[" + boundValues + " bound values] "); | |
| 680 | 694 | } |
| 681 | 695 | |
| 682 | 696 | append(statement, sb, maxQueryStringLength); |
| @@ -688,6 +702,8 @@ | ||
| 688 | 702 | for (Statement s : bs.getStatements()) { |
| 689 | 703 | if (s instanceof BoundStatement) |
| 690 | 704 | count += ((BoundStatement) s).wrapper.values.length; |
| 705 | + else if (s instanceof SimpleStatement) | |
| 706 | + count += ((SimpleStatement) s).valuesCount(); | |
| 691 | 707 | } |
| 692 | 708 | return count; |
| 693 | 709 | } |
| @@ -755,6 +771,71 @@ | ||
| 755 | 771 | return valueStr; |
| 756 | 772 | } |
| 757 | 773 | |
| 774 | + protected int appendParameters(SimpleStatement statement, StringBuilder buffer, int remaining) { | |
| 775 | + if (remaining == 0) | |
| 776 | + return 0; | |
| 777 | + int numberOfParameters = statement.valuesCount(); | |
| 778 | + if (numberOfParameters > 0) { | |
| 779 | + int numberOfLoggedParameters; | |
| 780 | + if (remaining == -1) { | |
| 781 | + numberOfLoggedParameters = numberOfParameters; | |
| 782 | + } else { | |
| 783 | + numberOfLoggedParameters = remaining > numberOfParameters ? numberOfParameters : remaining; | |
| 784 | + remaining -= numberOfLoggedParameters; | |
| 785 | + } | |
| 786 | + Iterator<String> valueNames = null; | |
| 787 | + if (statement.usesNamedValues()) { | |
| 788 | + valueNames = statement.getValueNames().iterator(); | |
| 789 | + } | |
| 790 | + for (int i = 0; i < numberOfLoggedParameters; i++) { | |
| 791 | + if (buffer.length() == 0) | |
| 792 | + buffer.append(" ["); | |
| 793 | + else | |
| 794 | + buffer.append(", "); | |
| 795 | + if (valueNames != null && valueNames.hasNext()) { | |
| 796 | + String valueName = valueNames.next(); | |
| 797 | + buffer.append(String.format("%s:%s", valueName, parameterValueAsString(statement.getObject(valueName)))); | |
| 798 | + } else { | |
| 799 | + buffer.append(parameterValueAsString(statement.getObject(i))); | |
| 800 | + } | |
| 801 | + } | |
| 802 | + if (numberOfLoggedParameters < numberOfParameters) { | |
| 803 | + buffer.append(FURTHER_PARAMS_OMITTED); | |
| 804 | + } | |
| 805 | + } | |
| 806 | + return remaining; | |
| 807 | + } | |
| 808 | + | |
| 809 | + protected String parameterValueAsString(Object value) { | |
| 810 | + String valueStr; | |
| 811 | + if (value == null) { | |
| 812 | + valueStr = "NULL"; | |
| 813 | + } else { | |
| 814 | + CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); | |
| 815 | + TypeCodec<Object> codec = codecRegistry.codecFor(value); | |
| 816 | + int maxParameterValueLength = this.maxParameterValueLength; | |
| 817 | + if (codec.cqlType.equals(DataType.blob()) && maxParameterValueLength != -1) { | |
| 818 | + // prevent large blobs from being converted to strings | |
| 819 | + ByteBuffer buf = (ByteBuffer) value; | |
| 820 | + int maxBufferLength = Math.max(2, (maxParameterValueLength - 2) / 2); | |
| 821 | + boolean bufferTooLarge = buf.remaining() > maxBufferLength; | |
| 822 | + if (bufferTooLarge) { | |
| 823 | + value = (ByteBuffer) buf.duplicate().limit(maxBufferLength); | |
| 824 | + } | |
| 825 | + valueStr = codec.format(value); | |
| 826 | + if (bufferTooLarge) { | |
| 827 | + valueStr = valueStr + TRUNCATED_OUTPUT; | |
| 828 | + } | |
| 829 | + } else { | |
| 830 | + valueStr = codec.format(value); | |
| 831 | + if (maxParameterValueLength != -1 && valueStr.length() > maxParameterValueLength) { | |
| 832 | + valueStr = valueStr.substring(0, maxParameterValueLength) + TRUNCATED_OUTPUT; | |
| 833 | + } | |
| 834 | + } | |
| 835 | + } | |
| 836 | + return valueStr; | |
| 837 | + } | |
| 838 | + | |
| 758 | 839 | private ProtocolVersion protocolVersion() { |
| 759 | 840 | // Since the QueryLogger can be registered before the Cluster was initialized, we can't retrieve |
| 760 | 841 | // it at construction time. Cache it field at first use (a volatile field is good enough since we |
modified driver-core/src/main/java/com/datastax/driver/core/SimpleStatement.java +35 −4
| @@ -18,8 +18,10 @@ | ||
| 18 | 18 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 19 | 19 | |
| 20 | 20 | import java.nio.ByteBuffer; |
| 21 | +import java.util.Collections; | |
| 21 | 22 | import java.util.HashMap; |
| 22 | 23 | import java.util.Map; |
| 24 | +import java.util.Set; | |
| 23 | 25 | |
| 24 | 26 | /** |
| 25 | 27 | * A simple {@code RegularStatement} implementation built directly from a query |
| @@ -177,21 +179,50 @@ | ||
| 177 | 179 | } |
| 178 | 180 | |
| 179 | 181 | /** |
| 180 | - * Returns the {@code i}th value as the Java type matching its CQL type. | |
| 182 | + * Returns the {@code i}th positional value as the Java type matching its CQL type. | |
| 183 | + * <p/> | |
| 184 | + * Note that, unlike with other driver types like {@link Row}, you can't retrieve named values by position. This | |
| 185 | + * getter will throw an exception if the statement was created with named values (or no values at all). Call | |
| 186 | + * {@link #usesNamedValues()} to check the type of values, and {@link #getObject(String)} if they are positional. | |
| 181 | 187 | * |
| 182 | 188 | * @param i the index to retrieve. |
| 183 | - * @return the value of the {@code i}th value of this statement. | |
| 184 | - * @throws IllegalStateException if this statement does not have values. | |
| 189 | + * @return the {@code i}th value of this statement. | |
| 190 | + * @throws IllegalStateException if this statement does not have positional values. | |
| 185 | 191 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 186 | 192 | */ |
| 187 | 193 | public Object getObject(int i) { |
| 188 | 194 | if (values == null) |
| 189 | - throw new IllegalStateException("This statement does not have values"); | |
| 195 | + throw new IllegalStateException("This statement does not have positional values"); | |
| 190 | 196 | if (i < 0 || i >= values.length) |
| 191 | 197 | throw new ArrayIndexOutOfBoundsException(i); |
| 192 | 198 | return values[i]; |
| 193 | 199 | } |
| 194 | 200 | |
| 201 | + /** | |
| 202 | + * Returns a named value as the Java type matching its CQL type. | |
| 203 | + * | |
| 204 | + * @param name the name of the value to retrieve. | |
| 205 | + * @return the value that matches the name, or {@code null} if there is no such name. | |
| 206 | + * @throws IllegalStateException if this statement does not have named values. | |
| 207 | + */ | |
| 208 | + public Object getObject(String name) { | |
| 209 | + if (namedValues == null) | |
| 210 | + throw new IllegalStateException("This statement does not have named values"); | |
| 211 | + return namedValues.get(name); | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Returns the names of the named values of this statement. | |
| 216 | + * | |
| 217 | + * @return the names of the named values of this statement. | |
| 218 | + * @throws IllegalStateException if this statement does not have named values. | |
| 219 | + */ | |
| 220 | + public Set<String> getValueNames() { | |
| 221 | + if (namedValues == null) | |
| 222 | + throw new IllegalStateException("This statement does not have named values"); | |
| 223 | + return Collections.unmodifiableSet(namedValues.keySet()); | |
| 224 | + } | |
| 225 | + | |
| 195 | 226 | /** |
| 196 | 227 | * Returns the routing key for the query. |
| 197 | 228 | * <p/> |
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java +304 −11
| @@ -31,7 +31,9 @@ | ||
| 31 | 31 | |
| 32 | 32 | import java.nio.ByteBuffer; |
| 33 | 33 | import java.util.ArrayList; |
| 34 | +import java.util.HashMap; | |
| 34 | 35 | import java.util.List; |
| 36 | +import java.util.Map; | |
| 35 | 37 | |
| 36 | 38 | import static com.datastax.driver.core.BatchStatement.Type.COUNTER; |
| 37 | 39 | import static com.datastax.driver.core.BatchStatement.Type.UNLOGGED; |
| @@ -377,7 +379,7 @@ | ||
| 377 | 379 | |
| 378 | 380 | @Test(groups = "short") |
| 379 | 381 | @CassandraVersion(major = 2.0) |
| 380 | - public void should_log_non_null_named_parameter() throws Exception { | |
| 382 | + public void should_log_non_null_named_parameter_bound_statements() throws Exception { | |
| 381 | 383 | // given |
| 382 | 384 | normal.setLevel(TRACE); |
| 383 | 385 | queryLogger = QueryLogger.builder() |
| @@ -403,7 +405,7 @@ | ||
| 403 | 405 | } |
| 404 | 406 | |
| 405 | 407 | @Test(groups = "short") |
| 406 | - public void should_log_non_null_positional_parameter() throws Exception { | |
| 408 | + public void should_log_non_null_positional_parameter_bound_statements() throws Exception { | |
| 407 | 409 | // given |
| 408 | 410 | normal.setLevel(TRACE); |
| 409 | 411 | queryLogger = QueryLogger.builder().build(); |
| @@ -426,7 +428,28 @@ | ||
| 426 | 428 | } |
| 427 | 429 | |
| 428 | 430 | @Test(groups = "short") |
| 429 | - public void should_log_null_parameter() throws Exception { | |
| 431 | + @CassandraVersion(major = 2.0) | |
| 432 | + public void should_log_non_null_positional_parameter_simple_statements() throws Exception { | |
| 433 | + // given | |
| 434 | + normal.setLevel(TRACE); | |
| 435 | + queryLogger = QueryLogger.builder().build(); | |
| 436 | + cluster().register(queryLogger); | |
| 437 | + // when | |
| 438 | + String query = "UPDATE test SET c_text = ? WHERE pk = ?"; | |
| 439 | + SimpleStatement ss = new SimpleStatement(query, "foo", 42); | |
| 440 | + session().execute(ss); | |
| 441 | + // then | |
| 442 | + String line = normalAppender.waitAndGet(10000); | |
| 443 | + assertThat(line) | |
| 444 | + .contains("Query completed normally") | |
| 445 | + .contains(ipOfNode(1)) | |
| 446 | + .contains(query) | |
| 447 | + .contains("42") | |
| 448 | + .contains("'foo'"); | |
| 449 | + } | |
| 450 | + | |
| 451 | + @Test(groups = "short") | |
| 452 | + public void should_log_null_parameter_bound_statements() throws Exception { | |
| 430 | 453 | // given |
| 431 | 454 | normal.setLevel(TRACE); |
| 432 | 455 | queryLogger = QueryLogger.builder().build(); |
| @@ -448,6 +471,27 @@ | ||
| 448 | 471 | .contains("c_text:NULL"); |
| 449 | 472 | } |
| 450 | 473 | |
| 474 | + @Test(groups = "short") | |
| 475 | + @CassandraVersion(major = 2.0) | |
| 476 | + public void should_log_null_parameter_simple_statements() throws Exception { | |
| 477 | + // given | |
| 478 | + normal.setLevel(TRACE); | |
| 479 | + queryLogger = QueryLogger.builder().build(); | |
| 480 | + cluster().register(queryLogger); | |
| 481 | + // when | |
| 482 | + String query = "UPDATE test SET c_text = ? WHERE pk = ?"; | |
| 483 | + SimpleStatement ss = new SimpleStatement(query, null, 42); | |
| 484 | + session().execute(ss); | |
| 485 | + // then | |
| 486 | + String line = normalAppender.waitAndGet(10000); | |
| 487 | + assertThat(line) | |
| 488 | + .contains("Query completed normally") | |
| 489 | + .contains(ipOfNode(1)) | |
| 490 | + .contains(query) | |
| 491 | + .contains("42") | |
| 492 | + .contains("NULL"); | |
| 493 | + } | |
| 494 | + | |
| 451 | 495 | @Test(groups = "short") |
| 452 | 496 | @CassandraVersion(major = 3.0) |
| 453 | 497 | public void should_log_unset_parameter() throws Exception { |
| @@ -497,10 +541,38 @@ | ||
| 497 | 541 | .contains("c_text:'foo'") |
| 498 | 542 | .contains("c_int:12345"); |
| 499 | 543 | } |
| 544 | + | |
| 545 | + @Test(groups = "short") | |
| 546 | + @CassandraVersion(major = 2.0) | |
| 547 | + public void should_log_simple_statement_parameters_inside_batch_statement() throws Exception { | |
| 548 | + // given | |
| 549 | + normal.setLevel(TRACE); | |
| 550 | + queryLogger = QueryLogger.builder().build(); | |
| 551 | + cluster().register(queryLogger); | |
| 552 | + // when | |
| 553 | + String query1 = "UPDATE test SET c_text = ? WHERE pk = ?"; | |
| 554 | + String query2 = "UPDATE test SET c_int = ? WHERE pk = ?"; | |
| 555 | + BatchStatement batch = new BatchStatement(); | |
| 556 | + batch.add(new SimpleStatement(query1, "foo", 42)); | |
| 557 | + batch.add(new SimpleStatement(query2, 12345, 43)); | |
| 558 | + session().execute(batch); | |
| 559 | + // then | |
| 560 | + String line = normalAppender.waitAndGet(10000); | |
| 561 | + assertThat(line) | |
| 562 | + .contains("Query completed normally") | |
| 563 | + .contains(ipOfNode(1)) | |
| 564 | + .contains(query1) | |
| 565 | + .contains(query2) | |
| 566 | + .contains("42") | |
| 567 | + .contains("43") | |
| 568 | + .contains("'foo'") | |
| 569 | + .contains("12345"); | |
| 570 | + } | |
| 571 | + | |
| 500 | 572 | // Test different CQL types |
| 501 | 573 | |
| 502 | 574 | @Test(groups = "short") |
| 503 | - public void should_log_all_parameter_types() throws Exception { | |
| 575 | + public void should_log_all_parameter_types_bound_statements() throws Exception { | |
| 504 | 576 | // given |
| 505 | 577 | normal.setLevel(TRACE); |
| 506 | 578 | queryLogger = QueryLogger.builder() |
| @@ -525,6 +597,37 @@ | ||
| 525 | 597 | } |
| 526 | 598 | } |
| 527 | 599 | |
| 600 | + @Test(groups = "short") | |
| 601 | + @CassandraVersion(major = 2.0) | |
| 602 | + public void should_log_all_parameter_types_simple_statements() throws Exception { | |
| 603 | + // given | |
| 604 | + normal.setLevel(TRACE); | |
| 605 | + queryLogger = QueryLogger.builder() | |
| 606 | + .withMaxParameterValueLength(Integer.MAX_VALUE) | |
| 607 | + .build(); | |
| 608 | + cluster().register(queryLogger); | |
| 609 | + // when | |
| 610 | + String query = "UPDATE test SET " + assignments + " WHERE pk = 42"; | |
| 611 | + SimpleStatement ss = new SimpleStatement(query, values.toArray()); | |
| 612 | + session().execute(ss); | |
| 613 | + // then | |
| 614 | + String line = normalAppender.waitAndGet(10000); | |
| 615 | + assertThat(line) | |
| 616 | + .contains("Query completed normally") | |
| 617 | + .contains(ipOfNode(1)) | |
| 618 | + .contains(query); | |
| 619 | + CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry(); | |
| 620 | + for (DataType type : dataTypes) { | |
| 621 | + TypeCodec<Object> codec; | |
| 622 | + if (type.equals(DataType.time())) { | |
| 623 | + codec = codecRegistry.codecFor(DataType.bigint()); | |
| 624 | + } else { | |
| 625 | + codec = codecRegistry.codecFor(type); | |
| 626 | + } | |
| 627 | + assertThat(line).contains(codec.format(getFixedValue(type))); | |
| 628 | + } | |
| 629 | + } | |
| 630 | + | |
| 528 | 631 | // Tests for truncation of query strings and parameter values |
| 529 | 632 | |
| 530 | 633 | @Test(groups = "short") |
| @@ -596,7 +699,7 @@ | ||
| 596 | 699 | |
| 597 | 700 | @CassandraVersion(major = 2.0) |
| 598 | 701 | @Test(groups = "short") |
| 599 | - public void should_truncate_parameter_when_max_length_exceeded() throws Exception { | |
| 702 | + public void should_truncate_parameter_when_max_length_exceeded_bound_statements() throws Exception { | |
| 600 | 703 | // given |
| 601 | 704 | normal.setLevel(TRACE); |
| 602 | 705 | queryLogger = QueryLogger.builder() |
| @@ -619,8 +722,30 @@ | ||
| 619 | 722 | .doesNotContain("123456"); |
| 620 | 723 | } |
| 621 | 724 | |
| 725 | + @CassandraVersion(major = 2.0) | |
| 726 | + @Test(groups = "short") | |
| 727 | + public void should_truncate_parameter_when_max_length_exceeded_simple_statements() throws Exception { | |
| 728 | + // given | |
| 729 | + normal.setLevel(TRACE); | |
| 730 | + queryLogger = QueryLogger.builder() | |
| 731 | + .withMaxParameterValueLength(5) | |
| 732 | + .build(); | |
| 733 | + cluster().register(queryLogger); | |
| 734 | + // when | |
| 735 | + String query = "UPDATE test SET c_int = ? WHERE pk = ?"; | |
| 736 | + SimpleStatement ss = new SimpleStatement(query, 123456, 42); | |
| 737 | + session().execute(ss); | |
| 738 | + // then | |
| 739 | + String line = normalAppender.waitAndGet(10000); | |
| 740 | + assertThat(line) | |
| 741 | + .contains("Query completed normally") | |
| 742 | + .contains(ipOfNode(1)) | |
| 743 | + .contains("12345" + TRUNCATED_OUTPUT) | |
| 744 | + .doesNotContain("123456"); | |
| 745 | + } | |
| 746 | + | |
| 622 | 747 | @Test(groups = "short") |
| 623 | - public void should_truncate_blob_parameter_when_max_length_exceeded() throws Exception { | |
| 748 | + public void should_truncate_blob_parameter_when_max_length_exceeded_bound_statements() throws Exception { | |
| 624 | 749 | // given |
| 625 | 750 | normal.setLevel(TRACE); |
| 626 | 751 | queryLogger = QueryLogger.builder() |
| @@ -640,11 +765,33 @@ | ||
| 640 | 765 | .contains("Query completed normally") |
| 641 | 766 | .contains(ipOfNode(1)) |
| 642 | 767 | .contains("c_blob:0x0102" + TRUNCATED_OUTPUT) |
| 643 | - .doesNotContain("123456"); | |
| 768 | + .doesNotContain("0x010203"); | |
| 769 | + } | |
| 770 | + | |
| 771 | + @Test(groups = "short") | |
| 772 | + @CassandraVersion(major = 2.0) | |
| 773 | + public void should_truncate_blob_parameter_when_max_length_exceeded_simple_statements() throws Exception { | |
| 774 | + // given | |
| 775 | + normal.setLevel(TRACE); | |
| 776 | + queryLogger = QueryLogger.builder() | |
| 777 | + .withMaxParameterValueLength(6) | |
| 778 | + .build(); | |
| 779 | + cluster().register(queryLogger); | |
| 780 | + // when | |
| 781 | + String query = "UPDATE test SET c_blob = ? WHERE pk = ?"; | |
| 782 | + SimpleStatement ss = new SimpleStatement(query, ByteBuffer.wrap(Bytes.toArray(Lists.newArrayList(1, 2, 3))), 42); | |
| 783 | + session().execute(ss); | |
| 784 | + // then | |
| 785 | + String line = normalAppender.waitAndGet(10000); | |
| 786 | + assertThat(line) | |
| 787 | + .contains("Query completed normally") | |
| 788 | + .contains(ipOfNode(1)) | |
| 789 | + .contains("0x0102" + TRUNCATED_OUTPUT) | |
| 790 | + .doesNotContain("0x010203"); | |
| 644 | 791 | } |
| 645 | 792 | |
| 646 | 793 | @Test(groups = "short") |
| 647 | - public void should_not_truncate_parameter_when_max_length_unlimited() throws Exception { | |
| 794 | + public void should_not_truncate_parameter_when_max_length_unlimited_bound_statements() throws Exception { | |
| 648 | 795 | // given |
| 649 | 796 | normal.setLevel(TRACE); |
| 650 | 797 | queryLogger = QueryLogger.builder() |
| @@ -668,7 +815,29 @@ | ||
| 668 | 815 | } |
| 669 | 816 | |
| 670 | 817 | @Test(groups = "short") |
| 671 | - public void should_not_log_exceeding_number_of_parameters() throws Exception { | |
| 818 | + @CassandraVersion(major = 2.0) | |
| 819 | + public void should_not_truncate_parameter_when_max_length_unlimited_simple_statements() throws Exception { | |
| 820 | + // given | |
| 821 | + normal.setLevel(TRACE); | |
| 822 | + queryLogger = QueryLogger.builder() | |
| 823 | + .withMaxParameterValueLength(-1) | |
| 824 | + .build(); | |
| 825 | + cluster().register(queryLogger); | |
| 826 | + // when | |
| 827 | + String query = "UPDATE test SET c_int = ? WHERE pk = ?"; | |
| 828 | + SimpleStatement ss = new SimpleStatement(query, 123456, 42); | |
| 829 | + session().execute(ss); | |
| 830 | + // then | |
| 831 | + String line = normalAppender.waitAndGet(10000); | |
| 832 | + assertThat(line) | |
| 833 | + .contains("Query completed normally") | |
| 834 | + .contains(ipOfNode(1)) | |
| 835 | + .contains("123456") | |
| 836 | + .doesNotContain(TRUNCATED_OUTPUT); | |
| 837 | + } | |
| 838 | + | |
| 839 | + @Test(groups = "short") | |
| 840 | + public void should_not_log_exceeding_number_of_parameters_bound_statements() throws Exception { | |
| 672 | 841 | // given |
| 673 | 842 | normal.setLevel(TRACE); |
| 674 | 843 | queryLogger = QueryLogger.builder() |
| @@ -694,7 +863,56 @@ | ||
| 694 | 863 | |
| 695 | 864 | @Test(groups = "short") |
| 696 | 865 | @CassandraVersion(major = 2.0) |
| 697 | - public void should_not_log_exceeding_number_of_parameters_in_batch_statement() throws Exception { | |
| 866 | + public void should_not_log_exceeding_number_of_parameters_simple_statements() throws Exception { | |
| 867 | + // given | |
| 868 | + normal.setLevel(TRACE); | |
| 869 | + queryLogger = QueryLogger.builder() | |
| 870 | + .withMaxLoggedParameters(1) | |
| 871 | + .build(); | |
| 872 | + cluster().register(queryLogger); | |
| 873 | + // when | |
| 874 | + String query = "UPDATE test SET c_int = ? WHERE pk = ?"; | |
| 875 | + SimpleStatement ss = new SimpleStatement(query, 123456, 42); | |
| 876 | + session().execute(ss); | |
| 877 | + // then | |
| 878 | + String line = normalAppender.waitAndGet(10000); | |
| 879 | + assertThat(line) | |
| 880 | + .contains("Query completed normally") | |
| 881 | + .contains(ipOfNode(1)) | |
| 882 | + .contains("123456") | |
| 883 | + .doesNotContain("123456, 42") | |
| 884 | + .contains(FURTHER_PARAMS_OMITTED); | |
| 885 | + } | |
| 886 | + | |
| 887 | + @Test(groups = "short") | |
| 888 | + @CassandraVersion(major = 2.1) | |
| 889 | + public void should_not_log_exceeding_number_of_parameters_simple_statements_with_named_values() throws Exception { | |
| 890 | + // given | |
| 891 | + normal.setLevel(TRACE); | |
| 892 | + queryLogger = QueryLogger.builder() | |
| 893 | + .withMaxLoggedParameters(1) | |
| 894 | + .build(); | |
| 895 | + cluster().register(queryLogger); | |
| 896 | + // when | |
| 897 | + String query = "UPDATE test SET c_int = :c_int WHERE pk = :pk"; | |
| 898 | + Map<String, Object> namedValues = new HashMap<String, Object>(); | |
| 899 | + namedValues.put("c_int", 123456); | |
| 900 | + namedValues.put("pk", 42); | |
| 901 | + SimpleStatement ss = new SimpleStatement(query, namedValues); | |
| 902 | + session().execute(ss); | |
| 903 | + // then | |
| 904 | + String line = normalAppender.waitAndGet(10000); | |
| 905 | + assertThat(line) | |
| 906 | + .contains("Query completed normally") | |
| 907 | + .contains(ipOfNode(1)) | |
| 908 | + .contains("pk:42") | |
| 909 | + .doesNotContain("c_int:123456") | |
| 910 | + .contains(FURTHER_PARAMS_OMITTED); | |
| 911 | + } | |
| 912 | + | |
| 913 | + @Test(groups = "short") | |
| 914 | + @CassandraVersion(major = 2.0) | |
| 915 | + public void should_not_log_exceeding_number_of_parameters_in_batch_statement_bound_statements() throws Exception { | |
| 698 | 916 | // given |
| 699 | 917 | normal.setLevel(TRACE); |
| 700 | 918 | queryLogger = QueryLogger.builder() |
| @@ -723,7 +941,35 @@ | ||
| 723 | 941 | } |
| 724 | 942 | |
| 725 | 943 | @Test(groups = "short") |
| 726 | - public void should_log_all_parameters_when_max_unlimited() throws Exception { | |
| 944 | + @CassandraVersion(major = 2.0) | |
| 945 | + public void should_not_log_exceeding_number_of_parameters_in_batch_statement_simple_statements() throws Exception { | |
| 946 | + // given | |
| 947 | + normal.setLevel(TRACE); | |
| 948 | + queryLogger = QueryLogger.builder() | |
| 949 | + .withMaxLoggedParameters(1) | |
| 950 | + .build(); | |
| 951 | + cluster().register(queryLogger); | |
| 952 | + // when | |
| 953 | + String query1 = "UPDATE test SET c_text = ? WHERE pk = ?"; | |
| 954 | + String query2 = "UPDATE test SET c_int = ? WHERE pk = ?"; | |
| 955 | + BatchStatement batch = new BatchStatement(); | |
| 956 | + batch.add(new SimpleStatement(query1, "foo", 42)); | |
| 957 | + batch.add(new SimpleStatement(query2, 12345, 43)); | |
| 958 | + session().execute(batch); | |
| 959 | + // then | |
| 960 | + String line = normalAppender.waitAndGet(10000); | |
| 961 | + assertThat(line) | |
| 962 | + .contains("Query completed normally") | |
| 963 | + .contains(ipOfNode(1)) | |
| 964 | + .contains(query1) | |
| 965 | + .contains(query2) | |
| 966 | + .contains("'foo'") | |
| 967 | + .doesNotContain("42, 12345, 43") | |
| 968 | + .contains(FURTHER_PARAMS_OMITTED); | |
| 969 | + } | |
| 970 | + | |
| 971 | + @Test(groups = "short") | |
| 972 | + public void should_log_all_parameters_when_max_unlimited_bound_statements() throws Exception { | |
| 727 | 973 | // given |
| 728 | 974 | normal.setLevel(TRACE); |
| 729 | 975 | queryLogger = QueryLogger.builder() |
| @@ -746,6 +992,53 @@ | ||
| 746 | 992 | .contains("pk:42"); |
| 747 | 993 | } |
| 748 | 994 | |
| 995 | + @Test(groups = "short") | |
| 996 | + @CassandraVersion(major = 2.0) | |
| 997 | + public void should_log_all_parameters_when_max_unlimited_simple_statements() throws Exception { | |
| 998 | + // given | |
| 999 | + normal.setLevel(TRACE); | |
| 1000 | + queryLogger = QueryLogger.builder() | |
| 1001 | + .withMaxLoggedParameters(-1) | |
| 1002 | + .build(); | |
| 1003 | + cluster().register(queryLogger); | |
| 1004 | + // when | |
| 1005 | + String query = "UPDATE test SET c_int = ? WHERE pk = ?"; | |
| 1006 | + SimpleStatement ss = new SimpleStatement(query, 123456, 42); | |
| 1007 | + session().execute(ss); | |
| 1008 | + // then | |
| 1009 | + String line = normalAppender.waitAndGet(10000); | |
| 1010 | + assertThat(line) | |
| 1011 | + .contains("Query completed normally") | |
| 1012 | + .contains(ipOfNode(1)) | |
| 1013 | + .contains("123456") | |
| 1014 | + .contains("42"); | |
| 1015 | + } | |
| 1016 | + | |
| 1017 | + @Test(groups = "short") | |
| 1018 | + @CassandraVersion(major = 2.1) | |
| 1019 | + public void should_log_all_parameters_when_max_unlimited_simple_statements_with_named_values() throws Exception { | |
| 1020 | + // given | |
| 1021 | + normal.setLevel(TRACE); | |
| 1022 | + queryLogger = QueryLogger.builder() | |
| 1023 | + .withMaxLoggedParameters(-1) | |
| 1024 | + .build(); | |
| 1025 | + cluster().register(queryLogger); | |
| 1026 | + // when | |
| 1027 | + String query = "UPDATE test SET c_int = :c_int WHERE pk = :pk"; | |
| 1028 | + Map<String, Object> namedValues = new HashMap<String, Object>(); | |
| 1029 | + namedValues.put("c_int", 123456); | |
| 1030 | + namedValues.put("pk", 42); | |
| 1031 | + SimpleStatement ss = new SimpleStatement(query, namedValues); | |
| 1032 | + session().execute(ss); | |
| 1033 | + // then | |
| 1034 | + String line = normalAppender.waitAndGet(10000); | |
| 1035 | + assertThat(line) | |
| 1036 | + .contains("Query completed normally") | |
| 1037 | + .contains(ipOfNode(1)) | |
| 1038 | + .contains("123456") | |
| 1039 | + .contains("42"); | |
| 1040 | + } | |
| 1041 | + | |
| 749 | 1042 | @Override |
| 750 | 1043 | public void onTestContextInitialized() { |
| 751 | 1044 | execute("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")"); |
modified driver-core/src/test/java/com/datastax/driver/core/SimpleStatementTest.java +39 −0
| @@ -18,7 +18,9 @@ | ||
| 18 | 18 | import org.testng.annotations.Test; |
| 19 | 19 | |
| 20 | 20 | import java.util.Collections; |
| 21 | +import java.util.HashMap; | |
| 21 | 22 | import java.util.List; |
| 23 | +import java.util.Map; | |
| 22 | 24 | |
| 23 | 25 | import static org.assertj.core.api.Assertions.assertThat; |
| 24 | 26 | |
| @@ -47,6 +49,43 @@ | ||
| 47 | 49 | assertThat(actual).isSameAs(expected); |
| 48 | 50 | } |
| 49 | 51 | |
| 52 | + @Test(groups = "unit", expectedExceptions = {IllegalStateException.class}) | |
| 53 | + public void should_throw_ISE_if_getObject_called_on_statement_without_named_values() { | |
| 54 | + new SimpleStatement("doesn't matter").getObject("name"); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Test(groups = "unit") | |
| 58 | + public void should_return_null_if_getObject_called_on_statement_with_wrong_name() { | |
| 59 | + Map<String, Object> namedVales = new HashMap<String, Object>(); | |
| 60 | + namedVales.put("name", new Object()); | |
| 61 | + Object actual = new SimpleStatement("doesn't matter", namedVales).getObject("wrong name"); | |
| 62 | + assertThat(actual).isNull(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Test(groups = "unit") | |
| 66 | + public void should_return_object_with_name() { | |
| 67 | + Object expected = new Object(); | |
| 68 | + String valueName = "name"; | |
| 69 | + Map<String, Object> namedVales = new HashMap<String, Object>(); | |
| 70 | + namedVales.put(valueName, expected); | |
| 71 | + Object actual = new SimpleStatement("doesn't matter", namedVales).getObject(valueName); | |
| 72 | + assertThat(actual).isSameAs(expected); | |
| 73 | + } | |
| 74 | + | |
| 75 | + @Test(groups = "unit", expectedExceptions = {IllegalStateException.class}) | |
| 76 | + public void should_throw_ISE_if_getValueNames_called_on_statement_without_named_values() { | |
| 77 | + new SimpleStatement("doesn't matter").getValueNames(); | |
| 78 | + } | |
| 79 | + | |
| 80 | + @Test(groups = "unit") | |
| 81 | + public void should_return_named_value() { | |
| 82 | + Object expected = new Object(); | |
| 83 | + Map<String, Object> namedValues = new HashMap<String, Object>(); | |
| 84 | + namedValues.put("name", expected); | |
| 85 | + Object actual = new SimpleStatement("doesn't matter", namedValues).getObject("name"); | |
| 86 | + assertThat(actual).isEqualTo(expected); | |
| 87 | + } | |
| 88 | + | |
| 50 | 89 | @Test(groups = "unit") |
| 51 | 90 | public void should_return_number_of_values() { |
| 52 | 91 | assertThat( |