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