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.assertInstanceOf;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import java.io.FileReader;
25  import java.io.IOException;
26  import java.io.StringWriter;
27  import java.util.Arrays;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.commons.configuration2.ex.ConfigurationException;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Disabled;
34  import org.junit.jupiter.api.Test;
35  
36  import com.fasterxml.jackson.databind.ObjectMapper;
37  import com.fasterxml.jackson.databind.type.MapType;
38  
39  /**
40   * Unit test for {@link JSONConfiguration} Not ideal: it uses the Jackson JSON parser just like
41   * {@link JSONConfiguration} itself
42   */
43  public class TestJSONConfiguration {
44  
45      /** The files that we test with. */
46      private final String testJson = ConfigurationAssert.getTestFile("test.json").getAbsolutePath();
47  
48      private JSONConfiguration jsonConfiguration;
49  
50      @BeforeEach
51      public void setUp() throws Exception {
52          jsonConfiguration = new JSONConfiguration();
53          jsonConfiguration.read(new FileReader(testJson));
54      }
55  
56      @Test
57      public void testCopyConstructor() {
58          final BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
59          c.addProperty("foo", "bar");
60  
61          jsonConfiguration = new JSONConfiguration(c);
62          assertEquals("bar", jsonConfiguration.getString("foo"));
63      }
64  
65      /**
66       * Tests CONFIGURATION-793.
67       */
68      public void testGetListNestedWithList() {
69          assertEquals(Arrays.asList("col1", "col2"), jsonConfiguration.getList(String.class, "key4.key5"));
70      }
71  
72      @Test
73      public void testGetPropertyDictionary() {
74          assertEquals("Martin D'vloper", jsonConfiguration.getProperty("martin.name"));
75          assertEquals("Developer", jsonConfiguration.getProperty("martin.job"));
76          assertEquals("Elite", jsonConfiguration.getProperty("martin.skill"));
77      }
78  
79      @Test
80      public void testGetPropertyDictionaryInList() {
81          assertEquals("UK", jsonConfiguration.getString("capitals(1).country"));
82          assertEquals("Washington", jsonConfiguration.getString("capitals(0).capital"));
83      }
84  
85      @Test
86      public void testGetPropertyInteger() {
87          final Object property = jsonConfiguration.getProperty("int1");
88          assertInstanceOf(Integer.class, property);
89          assertEquals(37, property);
90      }
91  
92      @Test
93      public void testGetPropertyNested() {
94          assertEquals("value23", jsonConfiguration.getProperty("key2.key3"));
95      }
96  
97      @Test
98      public void testGetPropertyNestedWithList() {
99          assertEquals(Arrays.asList("col1", "col2"), jsonConfiguration.getProperty("key4.key5"));
100     }
101 
102     @Test
103     public void testGetPropertySimple() {
104         assertEquals("value1", jsonConfiguration.getProperty("key1"));
105     }
106 
107     @Test
108     public void testGetPropertySubset() {
109         final Configuration subset = jsonConfiguration.subset("key4");
110         assertEquals(Arrays.asList("col1", "col2"), subset.getProperty("key5"));
111     }
112 
113     @Test
114     public void testGetPropertyVeryNestedProperties() {
115         final Object property = jsonConfiguration.getProperty("very.nested.properties");
116         assertEquals(Arrays.asList("nested1", "nested2", "nested3"), property);
117     }
118 
119     /**
120      * Tests CONFIGURATION-793.
121      */
122     @Disabled
123     @Test
124     public void testListOfObjects() {
125         final Configuration subset = jsonConfiguration.subset("capitals");
126         assertNotNull(subset);
127         assertEquals(2, subset.size());
128 
129         final List<Object> list = jsonConfiguration.getList("capitals");
130         assertNotNull(list);
131         assertEquals(2, list.size());
132 
133 //        assertEquals(list.get(0).get("country"), "USA");
134 //        assertEquals(list.get(0).get("capital"), "Washington");
135 //        assertEquals(list.get(1).get("country"), "UK");
136 //        assertEquals(list.get(1).get("capital"), "London");
137     }
138 
139     @Test
140     public void testSave() throws IOException, ConfigurationException {
141         // save the Configuration as a String...
142         final StringWriter sw = new StringWriter();
143         jsonConfiguration.write(sw);
144         final String output = sw.toString();
145 
146         // ..and then try parsing it back
147         final ObjectMapper mapper = new ObjectMapper();
148         final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
149         final Map<String, Object> parsed = mapper.readValue(output, type);
150         assertEquals(7, parsed.entrySet().size());
151         assertEquals("value1", parsed.get("key1"));
152 
153         final Map<?, ?> key2 = (Map<?, ?>) parsed.get("key2");
154         assertEquals("value23", key2.get("key3"));
155 
156         final List<?> key5 = (List<?>) ((Map<?, ?>) parsed.get("key4")).get("key5");
157         assertEquals(Arrays.asList("col1", "col2"), key5);
158 
159         final List<?> capitals = (List<?>) parsed.get("capitals");
160         final Map<?, ?> capUk = (Map<?, ?>) capitals.get(1);
161         assertEquals("London", capUk.get("capital"));
162     }
163 }