1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38 public class ConfigurationAssert {
39
40 public static final String TEST_DIR_NAME = "target/test-classes";
41
42
43 public static final String OUT_DIR_NAME = "target";
44
45
46 public static final File TEST_DIR = new File(TEST_DIR_NAME);
47
48
49 public static final File OUT_DIR = new File(OUT_DIR_NAME);
50
51
52
53
54
55
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
65
66
67
68
69 public static void assertConfigurationEquals(final ImmutableConfiguration expected, final ImmutableConfiguration actual) {
70
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
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
86
87
88
89
90
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
104
105
106
107
108 public static File getOutFile(final String name) {
109 return new File(OUT_DIR, name);
110 }
111
112
113
114
115
116
117
118 public static URL getOutURL(final String name) {
119 return urlFromFile(getOutFile(name));
120 }
121
122
123
124
125
126
127
128 public static File getTestFile(final String name) {
129 return new File(TEST_DIR, name);
130 }
131
132
133
134
135
136
137
138 public static URL getTestURL(final String name) {
139 return urlFromFile(getTestFile(name));
140 }
141
142
143
144
145
146
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
156
157
158
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
168
169
170
171
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 }