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.apache.commons.configuration2.io.FileHandler;
38  import org.junit.jupiter.api.BeforeEach;
39  import org.junit.jupiter.api.Test;
40  import org.junit.jupiter.api.io.TempDir;
41  import org.yaml.snakeyaml.Yaml;
42  
43  /**
44   * Unit test for {@link YAMLConfiguration}
45   */
46  public class TestYAMLConfiguration {
47  
48      /** A folder for temporary files. */
49      @TempDir
50      public File tempFolder;
51  
52      /** The files that we test with. */
53      private final String testYaml = ConfigurationAssert.getTestFile("test.yaml").getAbsolutePath();
54  
55      private YAMLConfiguration yamlConfiguration;
56  
57      @BeforeEach
58      public void setUp() throws Exception {
59          yamlConfiguration = new YAMLConfiguration();
60          yamlConfiguration.read(new FileReader(testYaml));
61      }
62  
63      @Test
64      void testCopyConstructor() {
65          final BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
66          c.addProperty("foo", "bar");
67  
68          yamlConfiguration = new YAMLConfiguration(c);
69          assertEquals("bar", yamlConfiguration.getString("foo"));
70      }
71  
72      @Test
73      void testCycle() throws ConfigurationException {
74          final YAMLConfiguration configuration = new YAMLConfiguration();
75          final FileHandler handler = new FileHandler(configuration);
76          handler.load(new File("src/test/resources/org/apache/commons/configuration2/yaml/cycle.yaml"));
77      }
78  
79      @Test
80      void testDoubleStringEmptyValues() {
81          final Object property = yamlConfiguration.getProperty("key5.example1");
82          assertEquals(Arrays.asList("", "", "value"), property);
83      }
84  
85      @Test
86      void testDoubleStringValues() {
87          final Object property = yamlConfiguration.getProperty("key5.example2");
88          assertEquals(Arrays.asList("a", "a", "value"), property);
89      }
90  
91      @Test
92      void testGetPropertyDictionary() {
93          assertEquals("Martin D'vloper", yamlConfiguration.getProperty("martin.name"));
94          assertEquals("Developer", yamlConfiguration.getProperty("martin.job"));
95          assertEquals("Elite", yamlConfiguration.getProperty("martin.skill"));
96      }
97  
98      @Test
99      void testGetPropertyInteger() {
100         final Object property = yamlConfiguration.getProperty("int1");
101         assertInstanceOf(Integer.class, property);
102         assertEquals(37, property);
103     }
104 
105     @Test
106     void testGetPropertyNested() {
107         assertEquals("value23", yamlConfiguration.getProperty("key2.key3"));
108     }
109 
110     @Test
111     void testGetPropertyNestedWithList() {
112         assertEquals(Arrays.asList("col1", "col2"), yamlConfiguration.getProperty("key4.key5"));
113     }
114 
115     @Test
116     void testGetPropertySimple() {
117         assertEquals("value1", yamlConfiguration.getProperty("key1"));
118     }
119 
120     @Test
121     void testGetPropertySubset() {
122         final Configuration subset = yamlConfiguration.subset("key4");
123         assertEquals(Arrays.asList("col1", "col2"), subset.getProperty("key5"));
124     }
125 
126     @Test
127     void testGetPropertyVeryNestedProperties() {
128         final Object property = yamlConfiguration.getProperty("very.nested.properties");
129         assertEquals(Arrays.asList("nested1", "nested2", "nested3"), property);
130     }
131 
132     @Test
133     void testObjectCreationFromReader() {
134         final File createdFile = new File(tempFolder, "data.txt");
135         final String yaml = "!!java.io.FileOutputStream [" + createdFile.getAbsolutePath() + "]";
136         final StringReader reader = new StringReader(yaml);
137 
138         assertThrows(ConfigurationException.class, () -> yamlConfiguration.read(reader));
139         assertFalse(createdFile.exists());
140     }
141 
142     @Test
143     void testObjectCreationFromStream() {
144         final File createdFile = new File(tempFolder, "data.txt");
145         final String yaml = "!!java.io.FileOutputStream [" + createdFile.getAbsolutePath() + "]";
146         final ByteArrayInputStream inputStream = new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8));
147 
148         assertThrows(ConfigurationException.class, () -> yamlConfiguration.read(inputStream));
149         assertFalse(createdFile.exists());
150     }
151 
152     @Test
153     void testSave() throws IOException, ConfigurationException {
154         // save the YAMLConfiguration as a String...
155         final StringWriter sw = new StringWriter();
156         yamlConfiguration.write(sw);
157         final String output = sw.toString();
158 
159         // ..and then try parsing it back as using SnakeYAML
160         final Map<?, ?> parsed = new Yaml().loadAs(output, Map.class);
161         assertEquals(7, parsed.entrySet().size());
162         assertEquals("value1", parsed.get("key1"));
163 
164         final Map<?, ?> key2 = (Map<?, ?>) parsed.get("key2");
165         assertEquals("value23", key2.get("key3"));
166 
167         final List<?> key5 = (List<?>) ((Map<?, ?>) parsed.get("key4")).get("key5");
168         assertEquals(Arrays.asList("col1", "col2"), key5);
169     }
170 }