source: josm/trunk/test/unit/org/openstreetmap/josm/TestUtils.java@ 13078

Last change on this file since 13078 was 13078, checked in by Don-vip, 6 years ago

fix #15508 - add TileSourceRule, use in MinimapDialogTest (patch by ris, modified) + update WireMock to 2.10.1

  • Property svn:eol-style set to native
File size: 11.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.fail;
6
7import java.awt.Component;
8import java.awt.Container;
9import java.awt.Graphics2D;
10import java.io.File;
11import java.io.IOException;
12import java.io.InputStream;
13import java.lang.reflect.Field;
14import java.lang.reflect.Method;
15import java.security.AccessController;
16import java.security.PrivilegedAction;
17import java.util.Arrays;
18import java.util.Collection;
19import java.util.Comparator;
20import java.util.Objects;
21import java.util.stream.Stream;
22
23import org.openstreetmap.josm.command.Command;
24import org.openstreetmap.josm.data.osm.DataSet;
25import org.openstreetmap.josm.data.osm.Node;
26import org.openstreetmap.josm.data.osm.OsmPrimitive;
27import org.openstreetmap.josm.data.osm.OsmUtils;
28import org.openstreetmap.josm.data.osm.Relation;
29import org.openstreetmap.josm.data.osm.RelationMember;
30import org.openstreetmap.josm.data.osm.Way;
31import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
32import org.openstreetmap.josm.gui.progress.CancelHandler;
33import org.openstreetmap.josm.gui.progress.ProgressMonitor;
34import org.openstreetmap.josm.gui.progress.ProgressTaskId;
35import org.openstreetmap.josm.io.Compression;
36import org.openstreetmap.josm.testutils.FakeGraphics;
37import org.openstreetmap.josm.tools.JosmRuntimeException;
38import org.openstreetmap.josm.tools.Utils;
39
40import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
41
42/**
43 * Various utils, useful for unit tests.
44 */
45public final class TestUtils {
46
47 private TestUtils() {
48 // Hide constructor for utility classes
49 }
50
51 /**
52 * Returns the path to test data root directory.
53 * @return path to test data root directory
54 */
55 public static String getTestDataRoot() {
56 String testDataRoot = System.getProperty("josm.test.data");
57 if (testDataRoot == null || testDataRoot.isEmpty()) {
58 testDataRoot = "test/data";
59 System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
60 }
61 return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
62 }
63
64 /**
65 * Gets path to test data directory for given ticket id.
66 * @param ticketid Ticket numeric identifier
67 * @return path to test data directory for given ticket id
68 */
69 public static String getRegressionDataDir(int ticketid) {
70 return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
71 }
72
73 /**
74 * Gets path to given file in test data directory for given ticket id.
75 * @param ticketid Ticket numeric identifier
76 * @param filename File name
77 * @return path to given file in test data directory for given ticket id
78 */
79 public static String getRegressionDataFile(int ticketid, String filename) {
80 return getRegressionDataDir(ticketid) + '/' + filename;
81 }
82
83 /**
84 * Gets input stream to given file in test data directory for given ticket id.
85 * @param ticketid Ticket numeric identifier
86 * @param filename File name
87 * @return path to given file in test data directory for given ticket id
88 * @throws IOException if any I/O error occurs
89 */
90 public static InputStream getRegressionDataStream(int ticketid, String filename) throws IOException {
91 return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid), filename));
92 }
93
94 /**
95 * Checks that the given Comparator respects its contract on the given table.
96 * @param <T> type of elements
97 * @param comparator The comparator to test
98 * @param array The array sorted for test purpose
99 */
100 @SuppressFBWarnings(value = "RV_NEGATING_RESULT_OF_COMPARETO")
101 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
102 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
103 // Check each compare possibility
104 for (int i = 0; i < array.length; i++) {
105 T r1 = array[i];
106 for (int j = i; j < array.length; j++) {
107 T r2 = array[j];
108 int a = comparator.compare(r1, r2);
109 int b = comparator.compare(r2, r1);
110 if (i == j || a == b) {
111 if (a != 0 || b != 0) {
112 fail(getFailMessage(r1, r2, a, b));
113 }
114 } else {
115 if (a != -b) {
116 fail(getFailMessage(r1, r2, a, b));
117 }
118 }
119 for (int k = j; k < array.length; k++) {
120 T r3 = array[k];
121 int c = comparator.compare(r1, r3);
122 int d = comparator.compare(r2, r3);
123 if (a > 0 && d > 0) {
124 if (c <= 0) {
125 fail(getFailMessage(r1, r2, r3, a, b, c, d));
126 }
127 } else if (a == 0 && d == 0) {
128 if (c != 0) {
129 fail(getFailMessage(r1, r2, r3, a, b, c, d));
130 }
131 } else if (a < 0 && d < 0) {
132 if (c >= 0) {
133 fail(getFailMessage(r1, r2, r3, a, b, c, d));
134 }
135 }
136 }
137 }
138 }
139 // Sort relation array
140 Arrays.sort(array, comparator);
141 }
142
143 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
144 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
145 .append(o2).append("\ngave: ").append(a).append("/").append(b)
146 .toString();
147 }
148
149 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
150 return new StringBuilder(getFailMessage(o1, o2, a, b))
151 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
152 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
153 .toString();
154 }
155
156 /**
157 * Returns a private field value.
158 * @param obj object
159 * @param fieldName private field name
160 * @return private field value
161 * @throws ReflectiveOperationException if a reflection operation error occurs
162 */
163 public static Object getPrivateField(Object obj, String fieldName) throws ReflectiveOperationException {
164 Field f = obj.getClass().getDeclaredField(fieldName);
165 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
166 f.setAccessible(true);
167 return null;
168 });
169 return f.get(obj);
170 }
171
172 /**
173 * Returns a private static field value.
174 * @param cls object class
175 * @param fieldName private field name
176 * @return private field value
177 * @throws ReflectiveOperationException if a reflection operation error occurs
178 */
179 public static Object getPrivateStaticField(Class<?> cls, String fieldName) throws ReflectiveOperationException {
180 Field f = cls.getDeclaredField(fieldName);
181 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
182 f.setAccessible(true);
183 return null;
184 });
185 return f.get(null);
186 }
187
188 /**
189 * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
190 * but does not show the progress.
191 * @return a progress monitor
192 */
193 public static ProgressMonitor newTestProgressMonitor() {
194 return new AbstractProgressMonitor(new CancelHandler()) {
195
196 @Override
197 protected void doBeginTask() {
198 }
199
200 @Override
201 protected void doFinishTask() {
202 }
203
204 @Override
205 protected void doSetIntermediate(boolean value) {
206 }
207
208 @Override
209 protected void doSetTitle(String title) {
210 }
211
212 @Override
213 protected void doSetCustomText(String title) {
214 }
215
216 @Override
217 protected void updateProgress(double value) {
218 }
219
220 @Override
221 public void setProgressTaskId(ProgressTaskId taskId) {
222 }
223
224 @Override
225 public ProgressTaskId getProgressTaskId() {
226 return null;
227 }
228
229 @Override
230 public Component getWindowParent() {
231 return null;
232 }
233 };
234 }
235
236 /**
237 * Returns an instance of {@link Graphics2D}.
238 * @return a mockup graphics instance
239 */
240 public static Graphics2D newGraphics() {
241 return new FakeGraphics();
242 }
243
244 /**
245 * Creates a new way with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the nodes added
246 *
247 * @param tags the tags to set
248 * @param nodes the nodes to add
249 * @return a new way
250 */
251 public static Way newWay(String tags, Node... nodes) {
252 final Way way = (Way) OsmUtils.createPrimitive("way " + tags);
253 for (Node node : nodes) {
254 way.addNode(node);
255 }
256 return way;
257 }
258
259 /**
260 * Creates a new relation with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the members added
261 *
262 * @param tags the tags to set
263 * @param members the members to add
264 * @return a new relation
265 */
266 public static Relation newRelation(String tags, RelationMember... members) {
267 final Relation relation = (Relation) OsmUtils.createPrimitive("relation " + tags);
268 for (RelationMember member : members) {
269 relation.addMember(member);
270 }
271 return relation;
272 }
273
274 /**
275 * Creates a new empty command.
276 * @param ds data set
277 * @return a new empty command
278 */
279 public static Command newCommand(DataSet ds) {
280 return new Command(ds) {
281 @Override
282 public String getDescriptionText() {
283 return "";
284 }
285
286 @Override
287 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
288 Collection<OsmPrimitive> added) {
289 // Do nothing
290 }
291 };
292 }
293
294 /**
295 * Ensures 100% code coverage for enums.
296 * @param enumClass enum class to cover
297 */
298 public static void superficialEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
299 try {
300 Method values = enumClass.getMethod("values");
301 Method valueOf = enumClass.getMethod("valueOf", String.class);
302 Utils.setObjectsAccessible(values, valueOf);
303 for (Object o : (Object[]) values.invoke(null)) {
304 assertEquals(o, valueOf.invoke(null, ((Enum<?>) o).name()));
305 }
306 } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
307 throw new JosmRuntimeException(e);
308 }
309 }
310
311 /**
312 * Get a descendant component by name.
313 * @param root The root component to start searching from.
314 * @param name The component name
315 * @return The component with that name or null if it does not exist.
316 * @since 12045
317 */
318 public static Component getComponentByName(Component root, String name) {
319 if (name.equals(root.getName())) {
320 return root;
321 } else if (root instanceof Container) {
322 Container container = (Container) root;
323 return Stream.of(container.getComponents())
324 .map(child -> getComponentByName(child, name))
325 .filter(Objects::nonNull)
326 .findFirst().orElse(null);
327 } else {
328 return null;
329 }
330 }
331}
Note: See TracBrowser for help on using the repository browser.