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    *     http://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      /** A folder for temporary files. */
47      @TempDir
48      public File tempFolder;
49  
50      /** The files that we test with. */
51      private final String testYaml = ConfigurationAssert.getTestFile("test.yaml").getAbsolutePath();
52  
53      private YAMLConfiguration yamlConfiguration;
54  
55      @BeforeEach
56      public void setUp() throws Exception {
57          yamlConfiguration = new YAMLConfiguration();
58          yamlConfiguration.read(new FileReader(testYaml));
59      }
60  
61      @Test
62      public void testCopyConstructor() {
63          final BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
64          c.addProperty("foo", "bar");
65  
66          yamlConfiguration = new YAMLConfiguration(c);
67          assertEquals("bar", yamlConfiguration.getString("foo"));
68      }
69  
70      @Test
71      public void testGetProperty_dictionary() {
72          assertEquals("Martin D'vloper", yamlConfiguration.getProperty("martin.name"));
73          assertEquals("Developer", yamlConfiguration.getProperty("martin.job"));
74          assertEquals("Elite", yamlConfiguration.getProperty("martin.skill"));
75      }
76  
77      @Test
78      public void testGetProperty_integer() {
79          final Object property = yamlConfiguration.getProperty("int1");
80          assertInstanceOf(Integer.class, property);
81          assertEquals(37, property);
82      }
83  
84      @Test
85      public void testGetProperty_nested() {
86          assertEquals("value23", yamlConfiguration.getProperty("key2.key3"));
87      }
88  
89      @Test
90      public void testGetProperty_nested_with_list() {
91          assertEquals(Arrays.asList("col1", "col2"), yamlConfiguration.getProperty("key4.key5"));
92      }
93  
94      @Test
95      public void testGetProperty_simple() {
96          assertEquals("value1", yamlConfiguration.getProperty("key1"));
97      }
98  
99      @Test
100     public void testGetProperty_subset() {
101         final Configuration subset = yamlConfiguration.subset("key4");
102         assertEquals(Arrays.asList("col1", "col2"), subset.getProperty("key5"));
103     }
104 
105     @Test
106     public void testGetProperty_very_nested_properties() {
107         final Object property = yamlConfiguration.getProperty("very.nested.properties");
108         assertEquals(Arrays.asList("nested1", "nested2", "nested3"), property);
109     }
110 
111     @Test
112     public void testObjectCreationFromReader() {
113         final File createdFile = new File(tempFolder, "data.txt");
114         final String yaml = "!!java.io.FileOutputStream [" + createdFile.getAbsolutePath() + "]";
115         final StringReader reader = new StringReader(yaml);
116 
117         assertThrows(ConfigurationException.class, () -> yamlConfiguration.read(reader));
118         assertFalse(createdFile.exists());
119     }
120 
121     @Test
122     public void testObjectCreationFromStream() {
123         final File createdFile = new File(tempFolder, "data.txt");
124         final String yaml = "!!java.io.FileOutputStream [" + createdFile.getAbsolutePath() + "]";
125         final ByteArrayInputStream inputStream = new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8));
126 
127         assertThrows(ConfigurationException.class, () -> yamlConfiguration.read(inputStream));
128         assertFalse(createdFile.exists());
129     }
130 
131     @Test
132     public void testSave() throws IOException, ConfigurationException {
133         // save the YAMLConfiguration as a String...
134         final StringWriter sw = new StringWriter();
135         yamlConfiguration.write(sw);
136         final String output = sw.toString();
137 
138         // ..and then try parsing it back as using SnakeYAML
139         final Map<?, ?> parsed = new Yaml().loadAs(output, Map.class);
140         assertEquals(6, parsed.entrySet().size());
141         assertEquals("value1", parsed.get("key1"));
142 
143         final Map<?, ?> key2 = (Map<?, ?>) parsed.get("key2");
144         assertEquals("value23", key2.get("key3"));
145 
146         final List<?> key5 = (List<?>) ((Map<?, ?>) parsed.get("key4")).get("key5");
147         assertEquals(Arrays.asList("col1", "col2"), key5);
148     }
149 }