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

Last change on this file since 12884 was 12726, checked in by Don-vip, 7 years ago

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

  • Property svn:eol-style set to native
File size: 11.0 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 an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
174 * but does not show the progress.
175 * @return a progress monitor
176 */
177 public static ProgressMonitor newTestProgressMonitor() {
178 return new AbstractProgressMonitor(new CancelHandler()) {
179
180 @Override
181 protected void doBeginTask() {
182 }
183
184 @Override
185 protected void doFinishTask() {
186 }
187
188 @Override
189 protected void doSetIntermediate(boolean value) {
190 }
191
192 @Override
193 protected void doSetTitle(String title) {
194 }
195
196 @Override
197 protected void doSetCustomText(String title) {
198 }
199
200 @Override
201 protected void updateProgress(double value) {
202 }
203
204 @Override
205 public void setProgressTaskId(ProgressTaskId taskId) {
206 }
207
208 @Override
209 public ProgressTaskId getProgressTaskId() {
210 return null;
211 }
212
213 @Override
214 public Component getWindowParent() {
215 return null;
216 }
217 };
218 }
219
220 /**
221 * Returns an instance of {@link Graphics2D}.
222 * @return a mockup graphics instance
223 */
224 public static Graphics2D newGraphics() {
225 return new FakeGraphics();
226 }
227
228 /**
229 * Creates a new way with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the nodes added
230 *
231 * @param tags the tags to set
232 * @param nodes the nodes to add
233 * @return a new way
234 */
235 public static Way newWay(String tags, Node... nodes) {
236 final Way way = (Way) OsmUtils.createPrimitive("way " + tags);
237 for (Node node : nodes) {
238 way.addNode(node);
239 }
240 return way;
241 }
242
243 /**
244 * Creates a new relation with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the members added
245 *
246 * @param tags the tags to set
247 * @param members the members to add
248 * @return a new relation
249 */
250 public static Relation newRelation(String tags, RelationMember... members) {
251 final Relation relation = (Relation) OsmUtils.createPrimitive("relation " + tags);
252 for (RelationMember member : members) {
253 relation.addMember(member);
254 }
255 return relation;
256 }
257
258 /**
259 * Creates a new empty command.
260 * @param ds data set
261 * @return a new empty command
262 */
263 public static Command newCommand(DataSet ds) {
264 return new Command(ds) {
265 @Override
266 public String getDescriptionText() {
267 return "";
268 }
269
270 @Override
271 public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
272 Collection<OsmPrimitive> added) {
273 // Do nothing
274 }
275 };
276 }
277
278 /**
279 * Ensures 100% code coverage for enums.
280 * @param enumClass enum class to cover
281 */
282 public static void superficialEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
283 try {
284 Method values = enumClass.getMethod("values");
285 Method valueOf = enumClass.getMethod("valueOf", String.class);
286 Utils.setObjectsAccessible(values, valueOf);
287 for (Object o : (Object[]) values.invoke(null)) {
288 assertEquals(o, valueOf.invoke(null, ((Enum<?>) o).name()));
289 }
290 } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
291 throw new JosmRuntimeException(e);
292 }
293 }
294
295 /**
296 * Get a descendant component by name.
297 * @param root The root component to start searching from.
298 * @param name The component name
299 * @return The component with that name or null if it does not exist.
300 * @since 12045
301 */
302 public static Component getComponentByName(Component root, String name) {
303 if (name.equals(root.getName())) {
304 return root;
305 } else if (root instanceof Container) {
306 Container container = (Container) root;
307 return Stream.of(container.getComponents())
308 .map(child -> getComponentByName(child, name))
309 .filter(Objects::nonNull)
310 .findFirst().orElse(null);
311 } else {
312 return null;
313 }
314 }
315}
Note: See TracBrowser for help on using the repository browser.