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}
0 commit comments