Skip to content

Commit 4a233cf

Browse files
author
Joey Yang
committed
improve code quality, add test case for coverage
1 parent 8712f3b commit 4a233cf

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

src/main/java/top/code2life/config/DynamicConfigBeanPostProcessor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
100100
@SuppressWarnings("unchecked")
101101
public void handleEvent(ConfigurationChangedEvent event) {
102102
try {
103-
Map<Object, OriginTrackedValue> prev = (Map<Object, OriginTrackedValue>) event.getPrevious().getSource();
104-
Map<Object, OriginTrackedValue> current = (Map<Object, OriginTrackedValue>) event.getCurrent().getSource();
105-
Map<Object, Object> diff = getPropertyDiff(prev, current);
103+
Map<Object, Object> diff = getPropertyDiff((Map<Object, OriginTrackedValue>) event.getPrevious().getSource(), (Map<Object, OriginTrackedValue>) event.getCurrent().getSource());
106104
Map<String, ValueBeanFieldBinder> toRefreshProps = new HashMap<>(4);
107105
for (Map.Entry<Object, Object> entry : diff.entrySet()) {
108106
String key = entry.getKey().toString();

src/main/java/top/code2life/config/DynamicConfigPropertiesWatcher.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.concurrent.Executors;
24-
import java.util.stream.StreamSupport;
2524

2625
/**
2726
* Enhance PropertySource when spring.config.location is specified, it will start directory-watch,
@@ -72,32 +71,35 @@ public void watchConfigDirectory() {
7271
return;
7372
}
7473
MutablePropertySources propertySources = env.getPropertySources();
75-
StreamSupport.stream(propertySources.spliterator(), false)
76-
.filter(ps -> ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN) || ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN_LEGACY))
77-
.forEach(ps -> {
78-
String name = ps.getName();
79-
int beginIndex = name.indexOf("[") + 1;
80-
int endIndex = name.indexOf("]");
81-
if (beginIndex < 1 && endIndex < 1) {
82-
log.warn("unrecognized config location, property source name is: {}", name);
83-
}
84-
String pathStr = name.substring(beginIndex, endIndex);
85-
if (pathStr.contains(FILE_COLON_SYMBOL)) {
86-
pathStr = pathStr.replace(FILE_COLON_SYMBOL, "");
87-
}
88-
PROPERTY_SOURCE_META_MAP.put(pathStr.replaceAll("\\\\", "/"), new PropertySourceMeta(ps, Paths.get(pathStr), 0L));
89-
if (log.isDebugEnabled()) {
90-
log.debug("configuration file found: {}", pathStr);
91-
}
92-
});
74+
for (PropertySource<?> ps : propertySources) {
75+
boolean isFilePropSource = ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN_LEGACY) || ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN);
76+
if (isFilePropSource) {
77+
normalizeAndRecordPropSource(ps);
78+
}
79+
}
9380
Executors.newSingleThreadExecutor(r -> new Thread(r, "config-watcher")).submit(this::startWatchDir);
9481
}
9582

83+
private void normalizeAndRecordPropSource(PropertySource<?> ps) {
84+
String name = ps.getName();
85+
int beginIndex = name.indexOf("[") + 1;
86+
int endIndex = name.indexOf("]");
87+
if (beginIndex < 1 && endIndex < 1) {
88+
log.warn("unrecognized config location, property source name is: {}", name);
89+
}
90+
String pathStr = name.substring(beginIndex, endIndex);
91+
if (pathStr.contains(FILE_COLON_SYMBOL)) {
92+
pathStr = pathStr.replace(FILE_COLON_SYMBOL, "");
93+
}
94+
PROPERTY_SOURCE_META_MAP.put(pathStr.replaceAll("\\\\", "/"), new PropertySourceMeta(ps, Paths.get(pathStr), 0L));
95+
log.debug("configuration file found: {}", pathStr);
96+
}
97+
9698
private void startWatchDir() {
9799
try {
98100
log.info("start watching configuration directory: {}", configLocation);
99101
watchService = FileSystems.getDefault().newWatchService();
100-
Paths.get(configLocation).register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
102+
Paths.get(configLocation).register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE);
101103
WatchKey key;
102104
while ((key = watchService.take()) != null) {
103105
// avoid receiving two ENTRY_MODIFY events: file modified and timestamp updated
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package top.code2life.config;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Set;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
/**
10+
* @author Code2Life
11+
**/
12+
public class FeatureGateTest {
13+
14+
private final FeatureGate featureGate = new FeatureGate(null);
15+
16+
@Test
17+
public void testConvertEmptyConfigValue() {
18+
Set<String> configVal = featureGate.convert("");
19+
assertEquals(0, configVal.size());
20+
}
21+
22+
@Test
23+
public void testGetBetaListOfFeature() {
24+
Set<String> configVal = featureGate.convert("a, b ,c");
25+
assertTrue(featureGate.isFeatureEnabled(configVal, "a"));
26+
assertFalse(featureGate.isFeatureEnabled(configVal, "d"));
27+
}
28+
29+
@Test
30+
public void testFullyOpenFeature() {
31+
Set<String> configVal = featureGate.convert("a, b ,all");
32+
assertTrue(featureGate.isFeatureEnabled(configVal, "a"));
33+
assertTrue(featureGate.isFeatureEnabled(configVal, "e"));
34+
}
35+
}

0 commit comments

Comments
 (0)