Skip to content

Commit 4be9437

Browse files
committed
Get rid of !!omap prepending. Check for LinkedHashMap is returned instead
1 parent f21b792 commit 4be9437

16 files changed

+124
-129
lines changed

src/main/java/org/scm4j/commons/URLContentLoader.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.scm4j.commons;
22

3-
import org.apache.commons.io.ByteOrderMark;
43
import org.apache.commons.io.IOUtils;
5-
import org.apache.commons.io.input.BOMInputStream;
64

75
import java.io.IOException;
86
import java.io.InputStream;
@@ -50,10 +48,7 @@ private String getWithDefaultProtocol(String urlStr) {
5048

5149
public String getContentFromUrl(URL url) throws IOException {
5250
try (InputStream inputStream = url.openStream()) {
53-
BOMInputStream bOMInputStream = new BOMInputStream(inputStream);
54-
ByteOrderMark bom = bOMInputStream.getBOM();
55-
String charsetName = bom == null ? StandardCharsets.UTF_8.toString() : bom.getCharsetName();
56-
return IOUtils.toString(bOMInputStream, charsetName);
51+
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
5752
}
5853
}
5954

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package org.scm4j.commons;
1+
package org.scm4j.commons.regexconfig;
22

33
public class EConfig extends RuntimeException {
4-
54
public EConfig(String message, Exception e) {
65
super(message, e);
76
}
87

9-
private static final long serialVersionUID = 1L;
10-
8+
public EConfig(String message) {
9+
super(message);
10+
}
1111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.scm4j.commons.regexconfig;
2+
3+
public class EConfigParseFailed extends EConfig {
4+
5+
public EConfigParseFailed(String message, Exception e) {
6+
super(message, e);
7+
}
8+
9+
private static final long serialVersionUID = 1L;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.scm4j.commons.regexconfig;
2+
3+
public class EConfigReadFailed extends EConfig {
4+
public EConfigReadFailed(String message, Exception e) {
5+
super(message, e);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.scm4j.commons.regexconfig;
2+
3+
public class EConfigWrongFormat extends EConfig {
4+
public EConfigWrongFormat(String message) {
5+
super(message);
6+
}
7+
}

src/main/java/org/scm4j/commons/regexconfig/RegexConfig.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package org.scm4j.commons.regexconfig;
22

3-
import java.io.IOException;
43
import java.util.LinkedHashMap;
54
import java.util.Map;
65

76
public class RegexConfig {
87

98
private final LinkedHashMap<Object, Object> content = new LinkedHashMap<>();
109

11-
public void loadFromYamlUrls(String... separatedUrls) throws IOException {
10+
public void loadFromYamlUrls(String... separatedUrls) {
1211
new RegexConfigLoaderYaml().loadFromUrls(content, separatedUrls);
1312
}
1413

1514
@SuppressWarnings("unchecked")
1615
public <T> T getPropByName(String nameToMatch, String propName, T defaultValue) {
1716
for (Object key : content.keySet()) {
1817
if (key == null || nameToMatch.matches((String) key)) {
19-
Map<?, ?> props = (Map<?, ?>) content.get(key);
18+
LinkedHashMap<?, ?> props = getPropsMap(content, key);
2019
if (props.containsKey(propName)) {
2120
return (T) props.get(propName);
2221
}
@@ -29,7 +28,7 @@ public String getPlaceholderedStringByName(String nameToMatch, Object propName,
2928
String result = defaultValue;
3029
for (Object key : content.keySet()) {
3130
if (key == null || nameToMatch.matches((String) key)) {
32-
Map<?, ?> props = (Map<?, ?>) content.get(key);
31+
LinkedHashMap<?, ?> props = getPropsMap(content, key);
3332
if (props.containsKey(propName)) {
3433
result = (String) props.get(propName);
3534
if (result != null)
@@ -41,6 +40,14 @@ public String getPlaceholderedStringByName(String nameToMatch, Object propName,
4140
return result;
4241
}
4342

43+
private LinkedHashMap<?,?> getPropsMap(LinkedHashMap<Object, Object> content, Object key) {
44+
Object res = content.get(key);
45+
if (!(Map.class.isInstance(res))) {
46+
throw new EConfigWrongFormat("Wrong config format met by key " + key + ": ordered map only is supported");
47+
}
48+
return (LinkedHashMap<?, ?>) res;
49+
}
50+
4451
public Boolean isEmpty() {
4552
return content.isEmpty();
4653
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
package org.scm4j.commons.regexconfig;
22

3-
import org.scm4j.commons.CommentedString;
4-
import org.scm4j.commons.EConfig;
53
import org.scm4j.commons.URLContentLoader;
64
import org.yaml.snakeyaml.Yaml;
7-
import org.yaml.snakeyaml.nodes.Node;
8-
import org.yaml.snakeyaml.nodes.NodeId;
95

10-
import java.io.BufferedReader;
11-
import java.io.IOException;
12-
import java.io.StringReader;
136
import java.util.LinkedHashMap;
147

158
public class RegexConfigLoaderYaml {
169

1710
public static final String URL_SEPARATOR = URLContentLoader.URL_SEPARATOR;
18-
public static final String OMAP_TAG = "!!omap";
1911

2012
@SuppressWarnings("unchecked")
21-
public void loadFromUrls(LinkedHashMap<Object, Object> content, String... separatedUrls) throws IOException {
13+
public void loadFromUrls(LinkedHashMap<Object, Object> content, String... separatedUrls) {
2214
Yaml yaml = new Yaml();
2315
URLContentLoader loader = new URLContentLoader();
2416
for (String separatedUrl : separatedUrls) {
@@ -27,50 +19,28 @@ public void loadFromUrls(LinkedHashMap<Object, Object> content, String... separa
2719
if (url.isEmpty()) {
2820
continue;
2921
}
30-
String contentStr = loader.getContentFromUrl(url);
31-
32-
if (!contentStr.isEmpty()) {
33-
contentStr = prependOmapIfNeed(contentStr, yaml);
34-
LinkedHashMap<Object, Object> map;
35-
try {
36-
map = (LinkedHashMap<Object, Object>) yaml.load(contentStr);
37-
} catch (Exception e) {
38-
throw new EConfig("failed to load config from yaml content at " + url + ": " + e.getMessage(), e);
39-
}
40-
if (map != null) {
41-
content.putAll(map);
22+
Object res;
23+
String contentStr;
24+
try {
25+
contentStr = loader.getContentFromUrl(url);
26+
} catch (Exception e) {
27+
throw new EConfigReadFailed("Failed to read config from url " + url + ": "+ e.getMessage(), e);
28+
}
29+
if (contentStr.isEmpty()) {
30+
continue;
31+
}
32+
try {
33+
res = yaml.load(contentStr);
34+
} catch (Exception e) {
35+
throw new EConfigParseFailed("Failed to parse config from yaml content at " + url + ": " + e.getMessage(), e);
36+
}
37+
if (res != null) {
38+
if (!(res instanceof LinkedHashMap)) {
39+
throw new EConfigWrongFormat("Unexpected yaml format at " + url + ": ordered map only is supported");
4240
}
41+
content.putAll((LinkedHashMap<Object, Object>) res);
4342
}
4443
}
4544
}
4645
}
47-
48-
String prependOmapIfNeed(String content, Yaml yaml) throws IOException {
49-
if (noOMAPTag(content)) {
50-
if (isSequence(content, yaml)) {
51-
return OMAP_TAG + "\r\n" + content;
52-
}
53-
}
54-
return content;
55-
}
56-
57-
private boolean isSequence(String content, Yaml yaml) {
58-
StringReader sr = new StringReader(content);
59-
Node node = yaml.compose(sr);
60-
return node != null && node.getNodeId().equals(NodeId.sequence);
61-
}
62-
63-
boolean noOMAPTag(String content) throws IOException {
64-
StringReader sr = new StringReader(content);
65-
BufferedReader br = new BufferedReader(sr);
66-
String line = br.readLine();
67-
while (line != null) {
68-
CommentedString cs = new CommentedString(line);
69-
if (cs.isValuable()) {
70-
return !cs.getStrNoComment().trim().startsWith(OMAP_TAG);
71-
}
72-
line = br.readLine();
73-
}
74-
return true;
75-
}
7646
}

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ public void testGetContentFromUrls() throws Exception {
2727
String content2 = FileUtils.readFileToString(new File(url2.toURI()), StandardCharsets.UTF_8);
2828
String content3 = FileUtils.readFileToString(new File(url3.toURI()), StandardCharsets.UTF_8);
2929
List<String> contents = loader.getContentsFromUrls(url1.toString(), url2.toString() + URLContentLoader.URL_SEPARATOR + url3.toString());
30-
assertEquals(String.join("", content1, content2.substring(1), content3), String.join("", contents));
31-
30+
assertTrue(contents.containsAll(Arrays.asList(content1, content2, content3)));
31+
assertEquals(3, contents.size());
32+
3233
contents = loader.getContentsFromUrls(Arrays.asList(url1, url2));
33-
assertEquals(String.join("", content1, content2.substring(1)), String.join("", contents));
34-
34+
assertTrue(contents.containsAll(Arrays.asList(content1, content2)));
35+
assertEquals(2, contents.size());
36+
3537
contents = loader.getContentsFromUrls("", URLContentLoader.URL_SEPARATOR + url1.toString());
36-
assertEquals(content1, String.join("", contents));
38+
assertEquals(content1, contents.get(0));
39+
assertEquals(1, contents.size());
3740
}
3841

3942
@Test
@@ -46,7 +49,8 @@ public void testFileProtocolOmitting() throws Exception {
4649
String content1 = FileUtils.readFileToString(file1, StandardCharsets.UTF_8);
4750
String content2 = FileUtils.readFileToString(file2, StandardCharsets.UTF_8);
4851
List<String> contents = loader.getContentsFromUrls(file1.toString(), file2.toString());
49-
assertEquals(String.join("", content1, content2.substring(1)), String.join("", contents));
52+
assertTrue(contents.containsAll(Arrays.asList(content1, content2)));
53+
assertEquals(2, contents.size());
5054
}
5155
}
5256

src/test/java/org/scm4j/commons/regexconfig/RegexConfigLoaderYamlTest.java

-44
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package org.scm4j.commons.regexconfig;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertTrue;
5-
import static org.junit.Assert.fail;
6-
7-
import java.io.IOException;
8-
93
import org.junit.Before;
104
import org.junit.Test;
11-
import org.scm4j.commons.EConfig;
5+
6+
import static org.junit.Assert.*;
127

138
public class RegexConfigTest {
149

@@ -17,16 +12,19 @@ public class RegexConfigTest {
1712
private String mapping = this.getClass().getResource("mapping.yml").toString();
1813
private String seq = this.getClass().getResource("sequence.yml").toString();
1914
private String empty = this.getClass().getResource("empty.yml").toString();
20-
private String wrongContent = this.getClass().getResource("wrong-content.yml").toString();
2115
private String seqBOM = this.getClass().getResource("sequence-bom.yml").toString();
16+
private String nonOmap = this.getClass().getResource("non-omap.yml").toString();
17+
private String wrongContent = this.getClass().getResource("wrong-content.yml").toString();
18+
private String emptyContent = this.getClass().getResource("empty-content.yml").toString();
19+
private String wrongContentInternal = this.getClass().getResource("wrong-internal-content.yml").toString();
2220

2321
@Before
2422
public void setUp() {
2523
config = new RegexConfig();
2624
}
2725

2826
@Test
29-
public void testGetPropByName() throws IOException {
27+
public void testGetPropByName() {
3028
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq + ";" + mapping);
3129
assertEquals("value1and2", config.getPropByName("node1", "prop1and2", null));
3230
assertEquals("value1and2", config.getPropByName("node2", "prop1and2", null));
@@ -42,30 +40,64 @@ public void testGetPropByName() throws IOException {
4240
}
4341

4442
@Test
45-
public void testGetPlaceholderedStringByName() throws IOException {
43+
public void testGetPlaceholderedStringByName() {
4644
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq);
4745
assertEquals("value4_placeholder", config.getPlaceholderedStringByName("node4placeholder", "prop4", null));
4846
assertEquals("unexisting_node", config.getPlaceholderedStringByName("unexisting_node", "placeholderedProp", null));
4947
}
5048

5149
@Test
52-
public void testEmptyConfig() throws IOException {
50+
public void testEmptyConfig() {
5351
config.loadFromYamlUrls(empty);
5452
assertTrue(config.isEmpty());
5553
}
5654

5755
@Test
58-
public void testEmptyUrls() throws IOException {
56+
public void testEmptyContent() {
57+
config.loadFromYamlUrls(emptyContent);
58+
assertTrue(config.isEmpty());
59+
}
60+
61+
@Test
62+
public void testEmptyUrls() {
5963
config.loadFromYamlUrls("");
6064
assertTrue(config.isEmpty());
6165
}
6266

6367
@Test
64-
public void testWrongContent() throws IOException {
68+
public void testNonSequenceContent() {
69+
try {
70+
config.loadFromYamlUrls(nonOmap);
71+
fail();
72+
} catch (EConfigWrongFormat e) {
73+
}
74+
}
75+
76+
@Test
77+
public void testConfigReadFailed() {
78+
try {
79+
config.loadFromYamlUrls("wrong location");
80+
fail();
81+
} catch (RuntimeException e) {
82+
}
83+
}
84+
85+
@Test
86+
public void testWrongContent() {
6587
try {
6688
config.loadFromYamlUrls(wrongContent);
6789
fail();
68-
} catch (EConfig e) {
90+
} catch (EConfigParseFailed e) {
91+
}
92+
}
93+
94+
@Test
95+
public void testWrongInternalContent() {
96+
config.loadFromYamlUrls(wrongContentInternal);
97+
try {
98+
config.getPropByName("node1", "prop1", "");
99+
fail();
100+
} catch (EConfigWrongFormat e) {
69101
}
70102
}
71103
}

src/test/resources/org/scm4j/commons/regexconfig/empty-content.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)