Skip to content

Commit d693f04

Browse files
committed
fix validation and improve test
1 parent e12d605 commit d693f04

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/main/java/io/nats/client/MessageTtl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package io.nats.client;
1515

16+
import io.nats.client.support.Validator;
17+
1618
public class MessageTtl {
1719
private final String messageTtl;
1820

@@ -35,7 +37,7 @@ public String toString() {
3537
* @return The Builder
3638
*/
3739
public static MessageTtl seconds(int msgTtlSeconds) {
38-
if (msgTtlSeconds < 0) {
40+
if (msgTtlSeconds < 1) {
3941
throw new IllegalArgumentException("msgTtlSeconds must be at least 1 second.");
4042
}
4143
return new MessageTtl(msgTtlSeconds + "s");
@@ -48,7 +50,7 @@ public static MessageTtl seconds(int msgTtlSeconds) {
4850
* @return The Builder
4951
*/
5052
public static MessageTtl custom(String messageTtlCustom) {
51-
if (messageTtlCustom == null) {
53+
if (Validator.nullOrEmpty(messageTtlCustom)) {
5254
throw new IllegalArgumentException("messageTtlCustom required.");
5355
}
5456
return new MessageTtl(messageTtlCustom);

src/test/java/io/nats/client/PublishOptionsTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public void testMessageTtl() {
115115
.build();
116116
assertEquals("never", po.getMessageTtl());
117117

118+
assertThrows(IllegalArgumentException.class, () -> MessageTtl.seconds(0));
119+
assertThrows(IllegalArgumentException.class, () -> MessageTtl.seconds(-1));
120+
assertThrows(IllegalArgumentException.class, () -> MessageTtl.custom(null));
121+
assertThrows(IllegalArgumentException.class, () -> MessageTtl.custom(""));
118122
assertTrue(MessageTtl.seconds(3).toString().contains("3s")); // COVERAGE
119123
}
120124
}

0 commit comments

Comments
 (0)