Skip to content

Commit ed3dd78

Browse files
committed
add constraint on bizconfig
1 parent 7f0fc9d commit ed3dd78

File tree

3 files changed

+106
-9
lines changed

3 files changed

+106
-9
lines changed

apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/config/BizConfig.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,18 @@ public List<String> eurekaServiceUrls() {
4444
}
4545

4646
public int grayReleaseRuleScanInterval() {
47-
return getIntProperty("apollo.gray-release-rule-scan.interval", 60);
47+
int interval = getIntProperty("apollo.gray-release-rule-scan.interval", 60);
48+
return checkInt(interval, 1, Integer.MAX_VALUE, 60);
4849
}
4950

5051
public int itemKeyLengthLimit() {
51-
return getIntProperty("item.key.length.limit", 128);
52+
int limit = getIntProperty("item.key.length.limit", 128);
53+
return checkInt(limit, 5, Integer.MAX_VALUE, 128);
5254
}
5355

5456
public int itemValueLengthLimit() {
55-
return getIntProperty("item.value.length.limit", 20000);
57+
int limit = getIntProperty("item.value.length.limit", 20000);
58+
return checkInt(limit, 5, Integer.MAX_VALUE, 20000);
5659
}
5760

5861
public Map<Long, Integer> namespaceValueLengthLimitOverride() {
@@ -82,34 +85,46 @@ public String cloggingPort() {
8285
}
8386

8487
public int appNamespaceCacheScanInterval() {
85-
return getIntProperty("apollo.app-namespace-cache-scan.interval", 1);
88+
int interval = getIntProperty("apollo.app-namespace-cache-scan.interval", 1);
89+
return checkInt(interval, 1, Integer.MAX_VALUE, 1);
8690
}
8791

8892
public TimeUnit appNamespaceCacheScanIntervalTimeUnit() {
8993
return TimeUnit.SECONDS;
9094
}
9195

9296
public int appNamespaceCacheRebuildInterval() {
93-
return getIntProperty("apollo.app-namespace-cache-rebuild.interval", 60);
97+
int interval = getIntProperty("apollo.app-namespace-cache-rebuild.interval", 60);
98+
return checkInt(interval, 1, Integer.MAX_VALUE, 60);
9499
}
95100

96101
public TimeUnit appNamespaceCacheRebuildIntervalTimeUnit() {
97102
return TimeUnit.SECONDS;
98103
}
99104

100105
public int releaseMessageCacheScanInterval() {
101-
return getIntProperty("apollo.release-message-cache-scan.interval", 1);
106+
int interval = getIntProperty("apollo.release-message-cache-scan.interval", 1);
107+
return checkInt(interval, 1, Integer.MAX_VALUE, 1);
102108
}
103109

104110
public TimeUnit releaseMessageCacheScanIntervalTimeUnit() {
105111
return TimeUnit.SECONDS;
106112
}
107113

108114
public int releaseMessageNotificationBatch() {
109-
return getIntProperty("apollo.release-message.notification.batch", 100);
115+
int batch = getIntProperty("apollo.release-message.notification.batch", 100);
116+
return checkInt(batch, 1, Integer.MAX_VALUE, 100);
110117
}
111118

112119
public int releaseMessageNotificationBatchIntervalInMilli() {
113-
return getIntProperty("apollo.release-message.notification.batch.interval", 100);
120+
int interval = getIntProperty("apollo.release-message.notification.batch.interval", 100);
121+
return checkInt(interval, 1, Integer.MAX_VALUE, 100);
122+
}
123+
124+
int checkInt(int value, int min, int max, int defaultValue) {
125+
if (value >= min && value <= max) {
126+
return value;
127+
}
128+
return defaultValue;
114129
}
115130
}

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/AllTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ctrip.framework.apollo.biz;
22

3+
import com.ctrip.framework.apollo.biz.config.BizConfigTest;
34
import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolderTest;
45
import com.ctrip.framework.apollo.biz.message.DatabaseMessageSenderTest;
56
import com.ctrip.framework.apollo.biz.message.ReleaseMessageScannerTest;
@@ -38,7 +39,8 @@
3839
NamespaceBranchServiceTest.class,
3940
ReleaseCreationTest.class,
4041
NamespacePublishInfoTest.class,
41-
NamespaceServiceTest.class
42+
NamespaceServiceTest.class,
43+
BizConfigTest.class
4244
})
4345
public class AllTests {
4446

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.ctrip.framework.apollo.biz.config;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.mockito.Mock;
7+
import org.mockito.runners.MockitoJUnitRunner;
8+
import org.springframework.core.env.ConfigurableEnvironment;
9+
import org.springframework.test.util.ReflectionTestUtils;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.mockito.Mockito.when;
13+
14+
/**
15+
* @author Jason Song(song_s@ctrip.com)
16+
*/
17+
@RunWith(MockitoJUnitRunner.class)
18+
public class BizConfigTest {
19+
20+
@Mock
21+
private ConfigurableEnvironment environment;
22+
23+
private BizConfig bizConfig;
24+
25+
@Before
26+
public void setUp() throws Exception {
27+
bizConfig = new BizConfig();
28+
ReflectionTestUtils.setField(bizConfig, "environment", environment);
29+
}
30+
31+
@Test
32+
public void testReleaseMessageNotificationBatch() throws Exception {
33+
int someBatch = 20;
34+
when(environment.getProperty("apollo.release-message.notification.batch")).thenReturn(String.valueOf(someBatch));
35+
36+
assertEquals(someBatch, bizConfig.releaseMessageNotificationBatch());
37+
}
38+
39+
@Test
40+
public void testReleaseMessageNotificationBatchWithDefaultValue() throws Exception {
41+
int defaultBatch = 100;
42+
43+
assertEquals(defaultBatch, bizConfig.releaseMessageNotificationBatch());
44+
}
45+
46+
@Test
47+
public void testReleaseMessageNotificationBatchWithInvalidNumber() throws Exception {
48+
int someBatch = -20;
49+
int defaultBatch = 100;
50+
when(environment.getProperty("apollo.release-message.notification.batch")).thenReturn(String.valueOf(someBatch));
51+
52+
assertEquals(defaultBatch, bizConfig.releaseMessageNotificationBatch());
53+
}
54+
55+
@Test
56+
public void testReleaseMessageNotificationBatchWithNAN() throws Exception {
57+
String someNAN = "someNAN";
58+
int defaultBatch = 100;
59+
when(environment.getProperty("apollo.release-message.notification.batch")).thenReturn(someNAN);
60+
61+
assertEquals(defaultBatch, bizConfig.releaseMessageNotificationBatch());
62+
}
63+
64+
@Test
65+
public void testCheckInt() throws Exception {
66+
int someInvalidValue = 1;
67+
int anotherInvalidValue = 2;
68+
int someValidValue = 3;
69+
int someDefaultValue = 10;
70+
71+
int someMin = someInvalidValue + 1;
72+
int someMax = anotherInvalidValue - 1;
73+
74+
assertEquals(someDefaultValue, bizConfig.checkInt(someInvalidValue, someMin, Integer.MAX_VALUE, someDefaultValue));
75+
assertEquals(someDefaultValue, bizConfig.checkInt(anotherInvalidValue, Integer.MIN_VALUE, someMax,
76+
someDefaultValue));
77+
assertEquals(someValidValue, bizConfig.checkInt(someValidValue, Integer.MIN_VALUE, Integer.MAX_VALUE,
78+
someDefaultValue));
79+
}
80+
}

0 commit comments

Comments
 (0)