Skip to content

Commit 27618cd

Browse files
Thomas Straußmp911de
authored andcommitted
Fixes assertion for TTL duration in WriteOptionsBuilder.
Closes #1248 Original pull request: #1249.
1 parent 1acdef8 commit 27618cd

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/WriteOptions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public int hashCode() {
151151
*
152152
* @author Mark Paluch
153153
* @author Lukasz Antoniak
154+
* @author Thomas Strauß
154155
* @since 1.5
155156
*/
156157
public static class WriteOptionsBuilder extends QueryOptionsBuilder {
@@ -344,7 +345,7 @@ public WriteOptionsBuilder ttl(int ttl) {
344345
public WriteOptionsBuilder ttl(Duration ttl) {
345346

346347
Assert.notNull(ttl, "TTL must not be null");
347-
Assert.isTrue(!ttl.isNegative(), "TTL must be greater than equal to zero");
348+
Assert.isTrue(!ttl.isNegative() && !ttl.isZero(), "TTL must be greater than equal to zero");
348349

349350
this.ttl = ttl;
350351

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/cql/WriteOptionsUnitTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.Instant;
2323
import java.time.LocalDateTime;
2424
import java.time.ZoneOffset;
25+
import java.time.temporal.ChronoUnit;
2526

2627
import org.junit.jupiter.api.Test;
2728

@@ -33,6 +34,7 @@
3334
*
3435
* @author Mark Paluch
3536
* @author Sam Lightfoot
37+
* @author Thomas Strauß
3638
*/
3739
class WriteOptionsUnitTests {
3840

@@ -106,4 +108,32 @@ void buildWriteOptionsMutate() {
106108
CqlIdentifier.fromCql("routing_keyspace"));
107109
assertThat(writeOptions.getRoutingKey()).isEqualTo(ByteBuffer.allocate(1));
108110
}
111+
112+
@Test // GH-1248
113+
void buildWriteOptionsWithTtlDurationZero() {
114+
try {
115+
WriteOptions writeOptions = WriteOptions.builder()
116+
.ttl(Duration.ZERO)
117+
.build();
118+
119+
fail("WiteOptionsBuilder must not allow zero TTL");
120+
}
121+
catch (Exception e) {
122+
// expected behavior
123+
}
124+
}
125+
126+
@Test // GH-1248
127+
void buildWriteOptionsWithTtlNegativeDuration() {
128+
try {
129+
WriteOptions writeOptions = WriteOptions.builder()
130+
.ttl(Duration.of(-1, ChronoUnit.MICROS))
131+
.build();
132+
133+
fail("WiteOptionsBuilder must not allow negative TTL");
134+
}
135+
catch (Exception e) {
136+
// expected behavior
137+
}
138+
}
109139
}

0 commit comments

Comments
 (0)