Skip to content

Commit 627135a

Browse files
committed
refactor, RegexConfigLoaderYaml is implemented
1 parent 056b90f commit 627135a

File tree

11 files changed

+182
-166
lines changed

11 files changed

+182
-166
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.scm4j.commons.regexconfig;
2+
3+
import java.io.IOException;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
public class RegexConfig {
8+
9+
private final LinkedHashMap<Object, Object> content = new LinkedHashMap<>();
10+
11+
public void loadFromYamlUrls(String... separatedUrls) throws IOException {
12+
new RegexConfigLoaderYaml().loadFromUrls(content, separatedUrls);
13+
}
14+
15+
@SuppressWarnings("unchecked")
16+
public <T> T getPropByName(String nameToMatch, String propName, T defaultValue) {
17+
for (Object key : content.keySet()) {
18+
if (key == null || nameToMatch.matches((String) key)) {
19+
Map<?, ?> props = (Map<?, ?>) content.get(key);
20+
if (props.containsKey(propName)) {
21+
return (T) props.get(propName);
22+
}
23+
}
24+
}
25+
return defaultValue;
26+
}
27+
28+
public String getPlaceholderedStringByName(String nameToMatch, Object propName, String defaultValue) {
29+
String result = defaultValue;
30+
for (Object key : content.keySet()) {
31+
if (key == null || nameToMatch.matches((String) key)) {
32+
Map<?, ?> props = (Map<?, ?>) content.get(key);
33+
if (props.containsKey(propName)) {
34+
result = (String) props.get(propName);
35+
if (result != null)
36+
result = nameToMatch.replaceFirst(key == null ? ".*" : (String) key, result);
37+
break;
38+
}
39+
}
40+
}
41+
return result;
42+
}
43+
44+
public Boolean isEmpty() {
45+
return content.isEmpty();
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
package org.scm4j.commons;
1+
package org.scm4j.commons.regexconfig;
22

3+
import org.scm4j.commons.CommentedString;
4+
import org.scm4j.commons.EConfig;
5+
import org.scm4j.commons.URLContentLoader;
36
import org.yaml.snakeyaml.Yaml;
47
import org.yaml.snakeyaml.nodes.Node;
58
import org.yaml.snakeyaml.nodes.NodeId;
@@ -8,16 +11,14 @@
811
import java.io.IOException;
912
import java.io.StringReader;
1013
import java.util.LinkedHashMap;
11-
import java.util.Map;
1214

13-
public class RegexConfig {
15+
public class RegexConfigLoaderYaml {
16+
1417
public static final String URL_SEPARATOR = URLContentLoader.URL_SEPARATOR;
1518
public static final String OMAP_TAG = "!!omap";
1619

17-
private final LinkedHashMap<Object, Object> content = new LinkedHashMap<>();
18-
1920
@SuppressWarnings("unchecked")
20-
public void loadFromYamlUrls(String... separatedUrls) throws IOException {
21+
public void loadFromUrls(LinkedHashMap<Object, Object> content, String... separatedUrls) throws IOException {
2122
Yaml yaml = new Yaml();
2223
URLContentLoader loader = new URLContentLoader();
2324
for (String separatedUrl : separatedUrls) {
@@ -26,18 +27,18 @@ public void loadFromYamlUrls(String... separatedUrls) throws IOException {
2627
if (url.isEmpty()) {
2728
continue;
2829
}
29-
String content = loader.getContentFromUrl(url);
30+
String contentStr = loader.getContentFromUrl(url);
3031

31-
if (!content.isEmpty()) {
32-
content = prependOmapIfNeed(content, yaml);
32+
if (!contentStr.isEmpty()) {
33+
contentStr = prependOmapIfNeed(contentStr, yaml);
3334
LinkedHashMap<Object, Object> map;
3435
try {
35-
map = (LinkedHashMap<Object, Object>) yaml.load(content);
36+
map = (LinkedHashMap<Object, Object>) yaml.load(contentStr);
3637
} catch (Exception e) {
3738
throw new EConfig("failed to load config from yaml content at " + url + ": " + e.getMessage(), e);
3839
}
3940
if (map != null) {
40-
this.content.putAll(map);
41+
content.putAll(map);
4142
}
4243
}
4344
}
@@ -72,37 +73,4 @@ boolean noOMAPTag(String content) throws IOException {
7273
}
7374
return true;
7475
}
75-
76-
@SuppressWarnings("unchecked")
77-
public <T> T getPropByName(String nameToMatch, String propName, T defaultValue) {
78-
for (Object key : content.keySet()) {
79-
if (key == null || nameToMatch.matches((String) key)) {
80-
Map<?, ?> props = (Map<?, ?>) content.get(key);
81-
if (props.containsKey(propName)) {
82-
return (T) props.get(propName);
83-
}
84-
}
85-
}
86-
return defaultValue;
87-
}
88-
89-
public String getPlaceholderedStringByName(String nameToMatch, Object propName, String defaultValue) {
90-
String result = defaultValue;
91-
for (Object key : content.keySet()) {
92-
if (key == null || nameToMatch.matches((String) key)) {
93-
Map<?, ?> props = (Map<?, ?>) content.get(key);
94-
if (props.containsKey(propName)) {
95-
result = (String) props.get(propName);
96-
if (result != null)
97-
result = nameToMatch.replaceFirst(key == null ? ".*" : (String) key, result);
98-
break;
99-
}
100-
}
101-
}
102-
return result;
103-
}
104-
105-
public Boolean isEmpty() {
106-
return content.isEmpty();
107-
}
10876
}

src/test/java/org/scm4j/commons/URLContentLoaderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
public class URLContentLoaderTest {
1515

16-
private static final String SEQ_BOM = "sequence-bom.yml";
17-
private static final String SEQ_OMAP = "sequence-omap.yml";
18-
private static final String SEQ = "sequence.yml";
16+
private static final String SEQ_BOM = "regexconfig/sequence-bom.yml";
17+
private static final String SEQ_OMAP = "regexconfig/sequence-omap.yml";
18+
private static final String SEQ = "regexconfig/sequence.yml";
1919

2020
@Test
2121
public void testGetContentFromUrls() throws Exception {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.scm4j.commons.regexconfig;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.junit.Test;
6+
import org.yaml.snakeyaml.Yaml;
7+
8+
import java.io.File;
9+
import java.nio.charset.StandardCharsets;
10+
11+
import static org.junit.Assert.assertTrue;
12+
13+
public class RegexConfigLoaderYamlTest {
14+
15+
@Test
16+
public void testPrependOmap() throws Exception {
17+
RegexConfigLoaderYaml loader = new RegexConfigLoaderYaml();
18+
Yaml yaml = new Yaml();
19+
File seqOmap = new File(this.getClass().getResource("sequence-omap.yml").toURI());
20+
File mapping = new File(this.getClass().getResource("mapping.yml").toURI());
21+
File seq = new File(this.getClass().getResource("sequence.yml").toURI());
22+
File empty = new File(this.getClass().getResource("empty.yml").toURI());
23+
24+
String content = loader.prependOmapIfNeed(FileUtils.readFileToString(seqOmap, StandardCharsets.UTF_8), yaml);
25+
assertTrue(StringUtils.countMatches(content, RegexConfigLoaderYaml.OMAP_TAG) == 1);
26+
27+
content = loader.prependOmapIfNeed(FileUtils.readFileToString(mapping, StandardCharsets.UTF_8), yaml);
28+
assertTrue(StringUtils.countMatches(content, RegexConfigLoaderYaml.OMAP_TAG) == 0);
29+
30+
content = loader.prependOmapIfNeed(FileUtils.readFileToString(seq, StandardCharsets.UTF_8), yaml);
31+
assertTrue(StringUtils.countMatches(content, RegexConfigLoaderYaml.OMAP_TAG) == 1);
32+
33+
content = loader.prependOmapIfNeed(FileUtils.readFileToString(empty, StandardCharsets.UTF_8), yaml);
34+
assertTrue(StringUtils.countMatches(content, RegexConfigLoaderYaml.OMAP_TAG) == 0);
35+
}
36+
37+
@Test
38+
public void testNoOmapTagOnEmptyFile() throws Exception {
39+
RegexConfigLoaderYaml loader = new RegexConfigLoaderYaml();
40+
File empty = new File(this.getClass().getResource("empty.yml").toURI());
41+
assertTrue(loader.noOMAPTag(FileUtils.readFileToString(empty, StandardCharsets.UTF_8)));
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,77 @@
1-
package org.scm4j.commons;
2-
3-
import org.apache.commons.io.FileUtils;
4-
import org.apache.commons.lang3.StringUtils;
5-
import org.junit.Before;
6-
import org.junit.Test;
7-
import org.yaml.snakeyaml.Yaml;
8-
9-
import java.io.File;
10-
import java.io.IOException;
11-
import java.nio.charset.StandardCharsets;
12-
13-
import static org.junit.Assert.*;
14-
15-
public class RegexConfigTest {
16-
17-
private RegexConfig config;
18-
private String seqOmap = this.getClass().getResource("sequence-omap.yml").toString();
19-
private String mapping = this.getClass().getResource("mapping.yml").toString();
20-
private String seq = this.getClass().getResource("sequence.yml").toString();
21-
private String empty = this.getClass().getResource("empty.yml").toString();
22-
private String wrongContent = this.getClass().getResource("wrong-content.yml").toString();
23-
private String seqBOM = this.getClass().getResource("sequence-bom.yml").toString();
24-
25-
@Before
26-
public void setUp() {
27-
config = new RegexConfig();
28-
}
29-
30-
@Test
31-
public void testGetPropByName() throws IOException {
32-
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq + ";" + mapping);
33-
assertEquals("value1and2", config.getPropByName("node1", "prop1and2", null));
34-
assertEquals("value1and2", config.getPropByName("node2", "prop1and2", null));
35-
assertEquals("default value", config.getPropByName("node1", "unexisting_prop", "default value"));
36-
assertEquals("default value", config.getPropByName("unexisting_node", "unexisting_prop", "default value"));
37-
assertEquals("value3", config.getPropByName("node3", "prop3", null));
38-
assertEquals("value3", config.getPropByName("node3AnySuffix", "prop3", null));
39-
assertEquals("defaultValue", config.getPropByName("unexisting_node", "defaultProp", null));
40-
int intValue = config.getPropByName("unexisting_node", "intProp", null);
41-
assertEquals(1, intValue);
42-
Boolean boolValue = config.getPropByName("unexisting_node", "booleanProp", null);
43-
assertEquals(true, boolValue);
44-
}
45-
46-
@Test
47-
public void testGetPlaceholderedStringByName() throws IOException {
48-
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq);
49-
assertEquals("value4_placeholder", config.getPlaceholderedStringByName("node4placeholder", "prop4", null));
50-
assertEquals("unexisting_node", config.getPlaceholderedStringByName("unexisting_node", "placeholderedProp", null));
51-
}
52-
53-
@Test
54-
public void testEmptyConfig() throws IOException {
55-
config.loadFromYamlUrls(empty);
56-
assertTrue(config.isEmpty());
57-
}
58-
59-
@Test
60-
public void testEmptyUrls() throws IOException {
61-
config.loadFromYamlUrls("");
62-
assertTrue(config.isEmpty());
63-
}
64-
65-
@Test
66-
public void testWrongContent() throws IOException {
67-
try {
68-
config.loadFromYamlUrls(wrongContent);
69-
fail();
70-
} catch (EConfig e) {
71-
72-
}
73-
}
74-
75-
@Test
76-
public void testPrependOmap() throws Exception {
77-
Yaml yaml = new Yaml();
78-
File seqOmap = new File(this.getClass().getResource("sequence-omap.yml").toURI());
79-
File mapping = new File(this.getClass().getResource("mapping.yml").toURI());
80-
File seq = new File(this.getClass().getResource("sequence.yml").toURI());
81-
File empty = new File(this.getClass().getResource("empty.yml").toURI());
82-
83-
String content = config.prependOmapIfNeed(FileUtils.readFileToString(seqOmap, StandardCharsets.UTF_8), yaml);
84-
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 1);
85-
86-
content = config.prependOmapIfNeed(FileUtils.readFileToString(mapping, StandardCharsets.UTF_8), yaml);
87-
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 0);
88-
89-
content = config.prependOmapIfNeed(FileUtils.readFileToString(seq, StandardCharsets.UTF_8), yaml);
90-
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 1);
91-
92-
content = config.prependOmapIfNeed(FileUtils.readFileToString(empty, StandardCharsets.UTF_8), yaml);
93-
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 0);
94-
}
95-
96-
@Test
97-
public void testNoOmapTagOnEmptyFile() throws Exception {
98-
File empty = new File(this.getClass().getResource("empty.yml").toURI());
99-
assertTrue(config.noOMAPTag(FileUtils.readFileToString(empty, StandardCharsets.UTF_8)));
100-
}
1+
package org.scm4j.commons.regexconfig;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.scm4j.commons.EConfig;
8+
import org.yaml.snakeyaml.Yaml;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.charset.StandardCharsets;
13+
14+
import static org.junit.Assert.*;
15+
16+
public class RegexConfigTest {
17+
18+
private RegexConfig config;
19+
private String seqOmap = this.getClass().getResource("sequence-omap.yml").toString();
20+
private String mapping = this.getClass().getResource("mapping.yml").toString();
21+
private String seq = this.getClass().getResource("sequence.yml").toString();
22+
private String empty = this.getClass().getResource("empty.yml").toString();
23+
private String wrongContent = this.getClass().getResource("wrong-content.yml").toString();
24+
private String seqBOM = this.getClass().getResource("sequence-bom.yml").toString();
25+
26+
@Before
27+
public void setUp() {
28+
config = new RegexConfig();
29+
}
30+
31+
@Test
32+
public void testGetPropByName() throws IOException {
33+
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq + ";" + mapping);
34+
assertEquals("value1and2", config.getPropByName("node1", "prop1and2", null));
35+
assertEquals("value1and2", config.getPropByName("node2", "prop1and2", null));
36+
assertEquals("default value", config.getPropByName("node1", "unexisting_prop", "default value"));
37+
assertEquals("default value", config.getPropByName("unexisting_node", "unexisting_prop", "default value"));
38+
assertEquals("value3", config.getPropByName("node3", "prop3", null));
39+
assertEquals("value3", config.getPropByName("node3AnySuffix", "prop3", null));
40+
assertEquals("defaultValue", config.getPropByName("unexisting_node", "defaultProp", null));
41+
int intValue = config.getPropByName("unexisting_node", "intProp", null);
42+
assertEquals(1, intValue);
43+
Boolean boolValue = config.getPropByName("unexisting_node", "booleanProp", null);
44+
assertEquals(true, boolValue);
45+
}
46+
47+
@Test
48+
public void testGetPlaceholderedStringByName() throws IOException {
49+
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq);
50+
assertEquals("value4_placeholder", config.getPlaceholderedStringByName("node4placeholder", "prop4", null));
51+
assertEquals("unexisting_node", config.getPlaceholderedStringByName("unexisting_node", "placeholderedProp", null));
52+
}
53+
54+
@Test
55+
public void testEmptyConfig() throws IOException {
56+
config.loadFromYamlUrls(empty);
57+
assertTrue(config.isEmpty());
58+
}
59+
60+
@Test
61+
public void testEmptyUrls() throws IOException {
62+
config.loadFromYamlUrls("");
63+
assertTrue(config.isEmpty());
64+
}
65+
66+
@Test
67+
public void testWrongContent() throws IOException {
68+
try {
69+
config.loadFromYamlUrls(wrongContent);
70+
fail();
71+
} catch (EConfig e) {
72+
73+
}
74+
}
75+
76+
10177
}

src/test/resources/org/scm4j/commons/empty.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/test/resources/org/scm4j/commons/mapping.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/test/resources/org/scm4j/commons/sequence-bom.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/test/resources/org/scm4j/commons/sequence-omap.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)