Skip to content

Commit c1bd710

Browse files
author
Javen
committed
Fix bug - cannot set time_to_live to 0
1 parent 78c4ed6 commit c1bd710

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/cn/jpush/api/push/model/Options.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class Options implements PushModel {
1313
private static final String TIME_TO_LIVE = "time_to_live";
1414
private static final String APNS_PRODUCTION = "apns_production";
1515

16+
private static final long NONE_TIME_TO_LIVE = -1;
17+
1618
private final int sendno;
1719
private final long overrideMsgId;
1820
private long timeToLive;
@@ -58,7 +60,7 @@ public JsonElement toJSON() {
5860
if (overrideMsgId > 0) {
5961
json.add(OVERRIDE_MSG_ID, new JsonPrimitive(overrideMsgId));
6062
}
61-
if (timeToLive > 0) {
63+
if (timeToLive >= 0) {
6264
json.add(TIME_TO_LIVE, new JsonPrimitive(timeToLive));
6365
}
6466

@@ -70,7 +72,7 @@ public JsonElement toJSON() {
7072
public static class Builder {
7173
private int sendno = 0;
7274
private long overrideMsgId = 0;
73-
private long timeToLive = 0;
75+
private long timeToLive = NONE_TIME_TO_LIVE;
7476
private boolean apnsProduction = false;
7577

7678
public Builder setSendno(int sendno) {
@@ -96,7 +98,7 @@ public Builder setApnsProduction(boolean apnsProduction) {
9698
public Options build() {
9799
Preconditions.checkArgument(sendno >= 0, "sendno should be greater than 0.");
98100
Preconditions.checkArgument(overrideMsgId >= 0, "override_msg_id should be greater than 0.");
99-
Preconditions.checkArgument(timeToLive >= 0, "time_to_live should be greater than 0.");
101+
Preconditions.checkArgument(timeToLive >= NONE_TIME_TO_LIVE, "time_to_live should be greater than 0.");
100102
if (sendno <= 0) {
101103
sendno = ServiceHelper.generateSendno();
102104
}

test/cn/jpush/api/push/model/OptionsTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void testIllegalOverrideMsgId() {
2222

2323
@Test(expected = IllegalArgumentException.class)
2424
public void testIllegalTimeToLive() {
25-
Options.newBuilder().setTimeToLive(-1).build();
25+
Options.newBuilder().setTimeToLive(-2).build();
2626
}
2727

2828
@Test
@@ -34,6 +34,44 @@ public void testSendno() {
3434
Assert.assertEquals("", json, options.toJSON());
3535
}
3636

37+
@Test
38+
public void testTimeToLive_int() {
39+
JsonObject json = new JsonObject();
40+
json.add("sendno", new JsonPrimitive(111));
41+
json.add("apns_production", new JsonPrimitive(false));
42+
json.add("time_to_live", new JsonPrimitive(640));
43+
44+
Options options = Options.newBuilder()
45+
.setSendno(111)
46+
.setTimeToLive(640).build();
47+
Assert.assertEquals("", json, options.toJSON());
48+
}
49+
50+
@Test
51+
public void testTimeToLive_0() {
52+
JsonObject json = new JsonObject();
53+
json.add("sendno", new JsonPrimitive(111));
54+
json.add("apns_production", new JsonPrimitive(false));
55+
json.add("time_to_live", new JsonPrimitive(0));
56+
57+
Options options = Options.newBuilder()
58+
.setSendno(111)
59+
.setTimeToLive(0).build();
60+
Assert.assertEquals("", json, options.toJSON());
61+
}
62+
63+
@Test
64+
public void testTimeToLive_default() {
65+
JsonObject json = new JsonObject();
66+
json.add("sendno", new JsonPrimitive(111));
67+
json.add("apns_production", new JsonPrimitive(false));
68+
69+
Options options = Options.newBuilder()
70+
.setSendno(111)
71+
.setTimeToLive(-1).build();
72+
Assert.assertEquals("", json, options.toJSON());
73+
}
74+
3775
@Test
3876
public void testApnsProduction_defaultFalse() {
3977
int sendno = ServiceHelper.generateSendno();

0 commit comments

Comments
 (0)