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.assertTrue;
22
23 import java.io.File;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.nio.file.Path;
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Set;
33
34 import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
35
36 /**
37 * Assertions on configurations for the unit tests. This class also provides access to test files.
38 */
39 public final class ConfigurationAssert {
40
41 /** Constant for the name of the directory with the test files. */
42 public static final String TEST_DIR_NAME = "target/test-classes";
43
44 /** Constant for the name of the directory with the output files. */
45 public static final String OUT_DIR_NAME = "target";
46
47 /** The directory with the test files. */
48 public static final File TEST_DIR = new File(TEST_DIR_NAME);
49
50 /** The directory with the output files. */
51 public static final File OUT_DIR = new File(OUT_DIR_NAME);
52
53 /**
54 * Appends all keys in the specified configuration to the given collection.
55 *
56 * @param config the configuration
57 * @param collection the target collection
58 */
59 public static void appendKeys(final ImmutableConfiguration config, final Collection<String> collection) {
60 for (final Iterator<String> it = config.getKeys(); it.hasNext();) {
61 collection.add(it.next());
62 }
63 }
64
65 /**
66 * Checks the content of a configuration.
67 *
68 * @param expected the expected properties
69 * @param actual the configuration to check
70 */
71 public static void assertConfigurationEquals(final ImmutableConfiguration expected, final ImmutableConfiguration actual) {
72 // check that the actual configuration contains all the properties of the expected configuration
73 for (final Iterator<String> it = expected.getKeys(); it.hasNext();) {
74 final String key = it.next();
75 assertTrue(actual.containsKey(key), "The actual configuration doesn't contain the expected key '" + key + "'");
76 assertEquals(expected.getProperty(key), actual.getProperty(key), "Value of the '" + key + "' property");
77 }
78
79 // check that the actual configuration has no extra properties
80 for (final Iterator<String> it = actual.getKeys(); it.hasNext();) {
81 final String key = it.next();
82 assertTrue(expected.containsKey(key), "The actual configuration contains an extra key '" + key + "'");
83 }
84 }
85
86 /**
87 * Helper method for testing the equals() implementation of a class. It is also checked, whether hashCode() is
88 * compatible with equals().
89 *
90 * @param o1 test object 1
91 * @param o2 test object 2
92 * @param expEquals the expected result of equals()
93 */
94 public static void checkEquals(final Object o1, final Object o2, final boolean expEquals) {
95 assertEquals(expEquals, o1.equals(o2));
96 if (o2 != null) {
97 assertEquals(expEquals, o2.equals(o1));
98 }
99 if (expEquals) {
100 assertEquals(o1.hashCode(), o2.hashCode());
101 }
102 }
103
104 /**
105 * Returns a {@code File} object for the specified out file.
106 *
107 * @param name the name of the out file
108 * @return a {@code File} object pointing to that out file
109 */
110 public static File getOutFile(final String name) {
111 return new File(OUT_DIR, name);
112 }
113
114 /**
115 * Returns a URL pointing to the specified output file. If the URL cannot be constructed, a runtime exception is thrown.
116 *
117 * @param name the name of the output file
118 * @return the corresponding URL
119 */
120 public static URL getOutURL(final String name) {
121 return urlFromFile(getOutFile(name));
122 }
123
124 /**
125 * Returns a {@code File} object for the specified test file.
126 *
127 * @param name the name of the test file
128 * @return a {@code File} object pointing to that test file
129 */
130 public static File getTestFile(final String name) {
131 return new File(TEST_DIR, name);
132 }
133
134 /**
135 * Returns a {@code File} object for the specified test file.
136 *
137 * @param name the name of the test file
138 * @return a {@code File} object pointing to that test file
139 */
140 public static Path getTestPath(final String name) {
141 return TEST_DIR.toPath().resolve(name);
142 }
143
144 /**
145 * Returns a URL pointing to the specified test file. If the URL cannot be constructed, a runtime exception is thrown.
146 *
147 * @param name the name of the test file
148 * @return the corresponding URL
149 */
150 public static URL getTestURL(final String name) {
151 return urlFromFile(getTestFile(name));
152 }
153
154 /**
155 * Returns a list with all keys defined for the specified configuration.
156 *
157 * @param config the configuration
158 * @return a list with all keys of this configuration
159 */
160 public static List<String> keysToList(final ImmutableConfiguration config) {
161 final List<String> keyList = new LinkedList<>();
162 appendKeys(config, keyList);
163 return keyList;
164 }
165
166 /**
167 * Returns a set with all keys defined for the specified configuration.
168 *
169 * @param config the configuration
170 * @return a set with all keys of this configuration
171 */
172 public static Set<String> keysToSet(final ImmutableConfiguration config) {
173 final Set<String> keySet = new HashSet<>();
174 appendKeys(config, keySet);
175 return keySet;
176 }
177
178 /**
179 * Helper method for converting a file to a URL.
180 *
181 * @param file the file
182 * @return the corresponding URL
183 * @throws ConfigurationRuntimeException if the URL cannot be constructed
184 */
185 private static URL urlFromFile(final File file) {
186 try {
187 return file.toURI().toURL();
188 } catch (final MalformedURLException mex) {
189 throw new ConfigurationRuntimeException(mex);
190 }
191 }
192
193 private ConfigurationAssert() {
194 // empty
195 }
196 }