View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration2;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  import java.io.StringReader;
30  import java.io.StringWriter;
31  import java.nio.charset.StandardCharsets;
32  import java.util.Arrays;
33  import java.util.List;
34  import java.util.Map;
35  
36  import org.apache.commons.configuration2.ex.ConfigurationException;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  import org.junit.jupiter.api.io.TempDir;
40  import org.yaml.snakeyaml.Yaml;
41  
42  /**
43   * Unit test for {@link YAMLConfiguration}
44   */
45  public class TestYAMLConfiguration {
46  
47      /** A folder for temporary files. */
48      @TempDir
49      public File tempFolder;
50  
51      /** The files that we test with. */
52      private final String testYaml = ConfigurationAssert.getTestFile("test.yaml").getAbsolutePath();
53  
54      private YAMLConfiguration yamlConfiguration;
55  
56      @BeforeEach
57      public void setUp() throws Exception {
58          yamlConfiguration = new YAMLConfiguration();
59          yamlConfiguration.read(new FileReader(testYaml));
60      }
61  
62      @Test
63      void testCopyConstructor() {
64          final BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
65          c.addProperty("foo", "bar");
66  
67          yamlConfiguration = new YAMLConfiguration(c);
68          assertEquals("bar", yamlConfiguration.getString("foo"));
69      }
70  
71      @Test
72      void testDoubleStringValues() {
73          final Object property = yamlConfiguration.getProperty("key5.example");
74          assertEquals(Arrays.asList("", "", "value"), property);
75      }
76  
77      @Test
78      void testGetPropertyDictionary() {
79          assertEquals("Martin D'vloper", yamlConfiguration.getProperty("martin.name"));
80          assertEquals("Developer", yamlConfiguration.getProperty("martin.job"));
81          assertEquals("Elite", yamlConfiguration.getProperty("martin.skill"));
82      }
83  
84      @Test
85      void testGetPropertyInteger() {
86          final Object property = yamlConfiguration.getProperty("int1");
87          assertInstanceOf(Integer.class, property);
88          assertEquals(37, property);
89      }
90  
91      @Test
92      void testGetPropertyNested() {
93          assertEquals("value23", yamlConfiguration.getProperty("key2.key3"));
94      }
95  
96      @Test
97      void testGetPropertyNestedWithList() {
98          assertEquals(Arrays.asList("col1", "col2"), yamlConfiguration.getProperty("key4.key5"));
99      }
100 
101     @Test
102     void testGetPropertySimple() {
103         assertEquals("value1", yamlConfiguration.getProperty("key1"));
104     }
105 
106     @Test
107     void testGetPropertySubset() {
108         final Configuration subset = yamlConfiguration.subset("key4");
109         assertEquals(Arrays.asList("col1", "col2"), subset.getProperty("key5"));
110     }
111 
112     @Test
113     void testGetPropertyVeryNestedProperties() {
114         final Object property = yamlConfiguration.getProperty("very.nested.properties");
115         assertEquals(Arrays.asList("nested1", "nested2", "nested3"), property);
116     }
117 
118     @Test
119     void testObjectCreationFromReader() {
120         final File createdFile = new File(tempFolder, "data.txt");
121         final String yaml = "!!java.io.FileOutputStream [" + createdFile.getAbsolutePath() + "]";
122         final StringReader reader = new StringReader(yaml);
123 
124         assertThrows(ConfigurationException.class, () -> yamlConfiguration.read(reader));
125         assertFalse(createdFile.exists());
126     }
127 
128     @Test
129     void testObjectCreationFromStream() {
130         final File createdFile = new File(tempFolder, "data.txt");
131         final String yaml = "!!java.io.FileOutputStream [" + createdFile.getAbsolutePath() + "]";
132         final ByteArrayInputStream inputStream = new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8));
133 
134         assertThrows(ConfigurationException.class, () -> yamlConfiguration.read(inputStream));
135         assertFalse(createdFile.exists());
136     }
137 
138     @Test
139     void testSave() throws IOException, ConfigurationException {
140         // save the YAMLConfiguration as a String...
141         final StringWriter sw = new StringWriter();
142         yamlConfiguration.write(sw);
143         final String output = sw.toString();
144 
145         // ..and then try parsing it back as using SnakeYAML
146         final Map<?, ?> parsed = new Yaml().loadAs(output, Map.class);
147         assertEquals(7, parsed.entrySet().size());
148         assertEquals("value1", parsed.get("key1"));
149 
150         final Map<?, ?> key2 = (Map<?, ?>) parsed.get("key2");
151         assertEquals("value23", key2.get("key3"));
152 
153         final List<?> key5 = (List<?>) ((Map<?, ?>) parsed.get("key4")).get("key5");
154         assertEquals(Arrays.asList("col1", "col2"), key5);
155     }
156 }