1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.fail;
|
---|
6 |
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Graphics2D;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.lang.reflect.Field;
|
---|
13 | import java.lang.reflect.Method;
|
---|
14 | import java.security.AccessController;
|
---|
15 | import java.security.PrivilegedAction;
|
---|
16 | import java.util.Arrays;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Comparator;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.command.Command;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
24 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
25 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
|
---|
28 | import org.openstreetmap.josm.gui.progress.CancelHandler;
|
---|
29 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
30 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
31 | import org.openstreetmap.josm.io.Compression;
|
---|
32 | import org.openstreetmap.josm.testutils.FakeGraphics;
|
---|
33 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
34 | import org.openstreetmap.josm.tools.Utils;
|
---|
35 |
|
---|
36 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Various utils, useful for unit tests.
|
---|
40 | */
|
---|
41 | public final class TestUtils {
|
---|
42 |
|
---|
43 | private TestUtils() {
|
---|
44 | // Hide constructor for utility classes
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Returns the path to test data root directory.
|
---|
49 | * @return path to test data root directory
|
---|
50 | */
|
---|
51 | public static String getTestDataRoot() {
|
---|
52 | String testDataRoot = System.getProperty("josm.test.data");
|
---|
53 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
---|
54 | testDataRoot = "test/data";
|
---|
55 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
---|
56 | }
|
---|
57 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Gets path to test data directory for given ticket id.
|
---|
62 | * @param ticketid Ticket numeric identifier
|
---|
63 | * @return path to test data directory for given ticket id
|
---|
64 | */
|
---|
65 | public static String getRegressionDataDir(int ticketid) {
|
---|
66 | return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Gets path to given file in test data directory for given ticket id.
|
---|
71 | * @param ticketid Ticket numeric identifier
|
---|
72 | * @param filename File name
|
---|
73 | * @return path to given file in test data directory for given ticket id
|
---|
74 | */
|
---|
75 | public static String getRegressionDataFile(int ticketid, String filename) {
|
---|
76 | return getRegressionDataDir(ticketid) + '/' + filename;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Gets input stream to given file in test data directory for given ticket id.
|
---|
81 | * @param ticketid Ticket numeric identifier
|
---|
82 | * @param filename File name
|
---|
83 | * @return path to given file in test data directory for given ticket id
|
---|
84 | * @throws IOException if any I/O error occurs
|
---|
85 | */
|
---|
86 | public static InputStream getRegressionDataStream(int ticketid, String filename) throws IOException {
|
---|
87 | return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid), filename));
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Checks that the given Comparator respects its contract on the given table.
|
---|
92 | * @param <T> type of elements
|
---|
93 | * @param comparator The comparator to test
|
---|
94 | * @param array The array sorted for test purpose
|
---|
95 | */
|
---|
96 | @SuppressFBWarnings(value = "RV_NEGATING_RESULT_OF_COMPARETO")
|
---|
97 | public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
|
---|
98 | System.out.println("Validating Comparable contract on array of "+array.length+" elements");
|
---|
99 | // Check each compare possibility
|
---|
100 | for (int i = 0; i < array.length; i++) {
|
---|
101 | T r1 = array[i];
|
---|
102 | for (int j = i; j < array.length; j++) {
|
---|
103 | T r2 = array[j];
|
---|
104 | int a = comparator.compare(r1, r2);
|
---|
105 | int b = comparator.compare(r2, r1);
|
---|
106 | if (i == j || a == b) {
|
---|
107 | if (a != 0 || b != 0) {
|
---|
108 | fail(getFailMessage(r1, r2, a, b));
|
---|
109 | }
|
---|
110 | } else {
|
---|
111 | if (a != -b) {
|
---|
112 | fail(getFailMessage(r1, r2, a, b));
|
---|
113 | }
|
---|
114 | }
|
---|
115 | for (int k = j; k < array.length; k++) {
|
---|
116 | T r3 = array[k];
|
---|
117 | int c = comparator.compare(r1, r3);
|
---|
118 | int d = comparator.compare(r2, r3);
|
---|
119 | if (a > 0 && d > 0) {
|
---|
120 | if (c <= 0) {
|
---|
121 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
122 | }
|
---|
123 | } else 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 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | // Sort relation array
|
---|
136 | Arrays.sort(array, comparator);
|
---|
137 | }
|
---|
138 |
|
---|
139 | private static <T> String getFailMessage(T o1, T o2, int a, int b) {
|
---|
140 | return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
|
---|
141 | .append(o2).append("\ngave: ").append(a).append("/").append(b)
|
---|
142 | .toString();
|
---|
143 | }
|
---|
144 |
|
---|
145 | private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
|
---|
146 | return new StringBuilder(getFailMessage(o1, o2, a, b))
|
---|
147 | .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
|
---|
148 | .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
|
---|
149 | .toString();
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns the Java version as an int value.
|
---|
154 | * @return the Java version as an int value (8, 9, etc.)
|
---|
155 | */
|
---|
156 | public static int getJavaVersion() {
|
---|
157 | String version = System.getProperty("java.version");
|
---|
158 | if (version.startsWith("1.")) {
|
---|
159 | version = version.substring(2);
|
---|
160 | }
|
---|
161 | // Allow these formats:
|
---|
162 | // 1.8.0_72-ea
|
---|
163 | // 9-ea
|
---|
164 | // 9
|
---|
165 | // 9.0.1
|
---|
166 | int dotPos = version.indexOf('.');
|
---|
167 | int dashPos = version.indexOf('-');
|
---|
168 | return Integer.parseInt(version.substring(0,
|
---|
169 | dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Returns a private field value.
|
---|
174 | * @param obj object
|
---|
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 getPrivateField(Object obj, String fieldName) throws ReflectiveOperationException {
|
---|
180 | Field f = obj.getClass().getDeclaredField(fieldName);
|
---|
181 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
---|
182 | f.setAccessible(true);
|
---|
183 | return null;
|
---|
184 | });
|
---|
185 | return f.get(obj);
|
---|
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 | * @return a new empty command
|
---|
277 | */
|
---|
278 | public static Command newCommand() {
|
---|
279 | return new Command() {
|
---|
280 | @Override
|
---|
281 | public String getDescriptionText() {
|
---|
282 | return "";
|
---|
283 | }
|
---|
284 |
|
---|
285 | @Override
|
---|
286 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
287 | Collection<OsmPrimitive> added) {
|
---|
288 | // Do nothing
|
---|
289 | }
|
---|
290 | };
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Ensures 100% code coverage for enums.
|
---|
295 | * @param enumClass enum class to cover
|
---|
296 | */
|
---|
297 | public static void superficialEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
|
---|
298 | try {
|
---|
299 | Method values = enumClass.getMethod("values");
|
---|
300 | Method valueOf = enumClass.getMethod("valueOf", String.class);
|
---|
301 | Utils.setObjectsAccessible(values, valueOf);
|
---|
302 | for (Object o : (Object[]) values.invoke(null)) {
|
---|
303 | assertEquals(o, valueOf.invoke(null, ((Enum<?>) o).name()));
|
---|
304 | }
|
---|
305 | } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
|
---|
306 | throw new JosmRuntimeException(e);
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|