Skip to content

Commit d46d6a4

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

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
@@ -147,6 +147,7 @@ public int hashCode() {
147147
*
148148
* @author Mark Paluch
149149
* @author Lukasz Antoniak
150+
* @author Thomas Strauß
150151
* @since 1.5
151152
*/
152153
public static class WriteOptionsBuilder extends QueryOptionsBuilder {
@@ -310,7 +311,7 @@ public WriteOptionsBuilder ttl(int ttl) {
310311
public WriteOptionsBuilder ttl(Duration ttl) {
311312

312313
Assert.notNull(ttl, "TTL must not be null");
313-
Assert.isTrue(!ttl.isNegative(), "TTL must be greater than equal to zero");
314+
Assert.isTrue(!ttl.isNegative() && !ttl.isZero(), "TTL must be greater than equal to zero");
314315

315316
this.ttl = ttl;
316317

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
@@ -21,6 +21,7 @@
2121
import java.time.Instant;
2222
import java.time.LocalDateTime;
2323
import java.time.ZoneOffset;
24+
import java.time.temporal.ChronoUnit;
2425

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

@@ -31,6 +32,7 @@
3132
* Unit tests for {@link WriteOptions}.
3233
*
3334
* @author Mark Paluch
35+
* @author Thomas Strauß
3436
*/
3537
class WriteOptionsUnitTests {
3638

@@ -89,4 +91,32 @@ void buildWriteOptionsMutate() {
8991
assertThat(mutated.getPageSize()).isEqualTo(10);
9092
assertThat(mutated.getTracing()).isTrue();
9193
}
94+
95+
@Test // GH-1248
96+
void buildWriteOptionsWithTtlDurationZero() {
97+
try {
98+
WriteOptions writeOptions = WriteOptions.builder()
99+
.ttl(Duration.ZERO)
100+
.build();
101+
102+
fail("WiteOptionsBuilder must not allow zero TTL");
103+
}
104+
catch (Exception e) {
105+
// expected behavior
106+
}
107+
}
108+
109+
@Test // GH-1248
110+
void buildWriteOptionsWithTtlNegativeDuration() {
111+
try {
112+
WriteOptions writeOptions = WriteOptions.builder()
113+
.ttl(Duration.of(-1, ChronoUnit.MICROS))
114+
.build();
115+
116+
fail("WiteOptionsBuilder must not allow negative TTL");
117+
}
118+
catch (Exception e) {
119+
// expected behavior
120+
}
121+
}
92122
}

0 commit comments

Comments
 (0)