1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.fail;
|
---|
5 |
|
---|
6 | import java.awt.Canvas;
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Composite;
|
---|
10 | import java.awt.Font;
|
---|
11 | import java.awt.FontMetrics;
|
---|
12 | import java.awt.Graphics;
|
---|
13 | import java.awt.Graphics2D;
|
---|
14 | import java.awt.GraphicsConfiguration;
|
---|
15 | import java.awt.Image;
|
---|
16 | import java.awt.Paint;
|
---|
17 | import java.awt.Rectangle;
|
---|
18 | import java.awt.RenderingHints;
|
---|
19 | import java.awt.RenderingHints.Key;
|
---|
20 | import java.awt.Shape;
|
---|
21 | import java.awt.Stroke;
|
---|
22 | import java.awt.font.FontRenderContext;
|
---|
23 | import java.awt.font.GlyphVector;
|
---|
24 | import java.awt.geom.AffineTransform;
|
---|
25 | import java.awt.image.BufferedImage;
|
---|
26 | import java.awt.image.BufferedImageOp;
|
---|
27 | import java.awt.image.ImageObserver;
|
---|
28 | import java.awt.image.RenderedImage;
|
---|
29 | import java.awt.image.renderable.RenderableImage;
|
---|
30 | import java.io.File;
|
---|
31 | import java.io.IOException;
|
---|
32 | import java.io.InputStream;
|
---|
33 | import java.text.AttributedCharacterIterator;
|
---|
34 | import java.util.Arrays;
|
---|
35 | import java.util.Comparator;
|
---|
36 | import java.util.Map;
|
---|
37 |
|
---|
38 | import org.openstreetmap.josm.data.osm.Node;
|
---|
39 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
40 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
41 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
42 | import org.openstreetmap.josm.data.osm.Way;
|
---|
43 | import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
|
---|
44 | import org.openstreetmap.josm.gui.progress.CancelHandler;
|
---|
45 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
46 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
47 | import org.openstreetmap.josm.io.Compression;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Various utils, useful for unit tests.
|
---|
51 | */
|
---|
52 | public final class TestUtils {
|
---|
53 |
|
---|
54 | private TestUtils() {
|
---|
55 | // Hide constructor for utility classes
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Returns the path to test data root directory.
|
---|
60 | * @return path to test data root directory
|
---|
61 | */
|
---|
62 | public static String getTestDataRoot() {
|
---|
63 | String testDataRoot = System.getProperty("josm.test.data");
|
---|
64 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
---|
65 | testDataRoot = "test/data";
|
---|
66 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
---|
67 | }
|
---|
68 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Gets path to test data directory for given ticket id.
|
---|
73 | * @param ticketid Ticket numeric identifier
|
---|
74 | * @return path to test data directory for given ticket id
|
---|
75 | */
|
---|
76 | public static String getRegressionDataDir(int ticketid) {
|
---|
77 | return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Gets path to given file in test data directory for given ticket id.
|
---|
82 | * @param ticketid Ticket numeric identifier
|
---|
83 | * @param filename File name
|
---|
84 | * @return path to given file in test data directory for given ticket id
|
---|
85 | */
|
---|
86 | public static String getRegressionDataFile(int ticketid, String filename) {
|
---|
87 | return getRegressionDataDir(ticketid) + '/' + filename;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Gets input stream to given file in test data directory for given ticket id.
|
---|
92 | * @param ticketid Ticket numeric identifier
|
---|
93 | * @param filename File name
|
---|
94 | * @return path to given file in test data directory for given ticket id
|
---|
95 | * @throws IOException if any I/O error occurs
|
---|
96 | */
|
---|
97 | public static InputStream getRegressionDataStream(int ticketid, String filename) throws IOException {
|
---|
98 | return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid) + '/' + filename));
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Checks that the given Comparator respects its contract on the given table.
|
---|
103 | * @param <T> type of elements
|
---|
104 | * @param comparator The comparator to test
|
---|
105 | * @param array The array sorted for test purpose
|
---|
106 | */
|
---|
107 | public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
|
---|
108 | System.out.println("Validating Comparable contract on array of "+array.length+" elements");
|
---|
109 | // Check each compare possibility
|
---|
110 | for (int i = 0; i < array.length; i++) {
|
---|
111 | T r1 = array[i];
|
---|
112 | for (int j = i; j < array.length; j++) {
|
---|
113 | T r2 = array[j];
|
---|
114 | int a = comparator.compare(r1, r2);
|
---|
115 | int b = comparator.compare(r2, r1);
|
---|
116 | if (i == j || a == b) {
|
---|
117 | if (a != 0 || b != 0) {
|
---|
118 | fail(getFailMessage(r1, r2, a, b));
|
---|
119 | }
|
---|
120 | } else {
|
---|
121 | if (a != -b) {
|
---|
122 | fail(getFailMessage(r1, r2, a, b));
|
---|
123 | }
|
---|
124 | }
|
---|
125 | for (int k = j; k < array.length; k++) {
|
---|
126 | T r3 = array[k];
|
---|
127 | int c = comparator.compare(r1, r3);
|
---|
128 | int d = comparator.compare(r2, r3);
|
---|
129 | if (a > 0 && d > 0) {
|
---|
130 | if (c <= 0) {
|
---|
131 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
132 | }
|
---|
133 | } else if (a == 0 && d == 0) {
|
---|
134 | if (c != 0) {
|
---|
135 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
136 | }
|
---|
137 | } else if (a < 0 && d < 0) {
|
---|
138 | if (c >= 0) {
|
---|
139 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | // Sort relation array
|
---|
146 | Arrays.sort(array, comparator);
|
---|
147 | }
|
---|
148 |
|
---|
149 | private static <T> String getFailMessage(T o1, T o2, int a, int b) {
|
---|
150 | return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
|
---|
151 | .append(o2).append("\ngave: ").append(a).append("/").append(b)
|
---|
152 | .toString();
|
---|
153 | }
|
---|
154 |
|
---|
155 | private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
|
---|
156 | return new StringBuilder(getFailMessage(o1, o2, a, b))
|
---|
157 | .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
|
---|
158 | .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
|
---|
159 | .toString();
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Returns the Java version as an int value.
|
---|
164 | * @return the Java version as an int value (7, 8, 9, etc.)
|
---|
165 | */
|
---|
166 | public static int getJavaVersion() {
|
---|
167 | String version = System.getProperty("java.version");
|
---|
168 | if (version.startsWith("1.")) {
|
---|
169 | version = version.substring(2);
|
---|
170 | }
|
---|
171 | // Allow these formats:
|
---|
172 | // 1.7.0_91
|
---|
173 | // 1.8.0_72-ea
|
---|
174 | // 9-ea
|
---|
175 | // 9
|
---|
176 | // 9.0.1
|
---|
177 | int dotPos = version.indexOf('.');
|
---|
178 | int dashPos = version.indexOf('-');
|
---|
179 | return Integer.parseInt(version.substring(0,
|
---|
180 | dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
|
---|
185 | * but does not show the progress.
|
---|
186 | * @return a progress monitor
|
---|
187 | */
|
---|
188 | public static ProgressMonitor newTestProgressMonitor() {
|
---|
189 | return new AbstractProgressMonitor(new CancelHandler()) {
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | protected void doBeginTask() {
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | protected void doFinishTask() {
|
---|
197 | }
|
---|
198 |
|
---|
199 | @Override
|
---|
200 | protected void doSetIntermediate(boolean value) {
|
---|
201 | }
|
---|
202 |
|
---|
203 | @Override
|
---|
204 | protected void doSetTitle(String title) {
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override
|
---|
208 | protected void doSetCustomText(String title) {
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | protected void updateProgress(double value) {
|
---|
213 | }
|
---|
214 |
|
---|
215 | @Override
|
---|
216 | public void setProgressTaskId(ProgressTaskId taskId) {
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public ProgressTaskId getProgressTaskId() {
|
---|
221 | return null;
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public Component getWindowParent() {
|
---|
226 | return null;
|
---|
227 | }
|
---|
228 | };
|
---|
229 | }
|
---|
230 |
|
---|
231 | // CHECKSTYLE.OFF: AnonInnerLength
|
---|
232 | // CHECKSTYLE.OFF: MethodLength
|
---|
233 | // CHECKSTYLE.OFF: ParameterNumber
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Returns an instance of {@link Graphics2D}.
|
---|
237 | * @return a mockup graphics instance
|
---|
238 | */
|
---|
239 | public static Graphics2D newGraphics() {
|
---|
240 | return new Graphics2D() {
|
---|
241 |
|
---|
242 | @Override
|
---|
243 | public void setXORMode(Color c1) {
|
---|
244 | }
|
---|
245 |
|
---|
246 | @Override
|
---|
247 | public void setPaintMode() {
|
---|
248 | }
|
---|
249 |
|
---|
250 | @Override
|
---|
251 | public void setFont(Font font) {
|
---|
252 | }
|
---|
253 |
|
---|
254 | @Override
|
---|
255 | public void setColor(Color c) {
|
---|
256 | }
|
---|
257 |
|
---|
258 | @Override
|
---|
259 | public void setClip(int x, int y, int width, int height) {
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public void setClip(Shape clip) {
|
---|
264 | }
|
---|
265 |
|
---|
266 | @Override
|
---|
267 | public FontMetrics getFontMetrics(Font f) {
|
---|
268 | return new Canvas().getFontMetrics(getFont());
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Override
|
---|
272 | public Font getFont() {
|
---|
273 | return new Font(null, 0, 0);
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | public Color getColor() {
|
---|
278 | return null;
|
---|
279 | }
|
---|
280 |
|
---|
281 | @Override
|
---|
282 | public Rectangle getClipBounds() {
|
---|
283 | return null;
|
---|
284 | }
|
---|
285 |
|
---|
286 | @Override
|
---|
287 | public Shape getClip() {
|
---|
288 | return null;
|
---|
289 | }
|
---|
290 |
|
---|
291 | @Override
|
---|
292 | public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
|
---|
293 | }
|
---|
294 |
|
---|
295 | @Override
|
---|
296 | public void fillRect(int x, int y, int width, int height) {
|
---|
297 | }
|
---|
298 |
|
---|
299 | @Override
|
---|
300 | public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
|
---|
301 | }
|
---|
302 |
|
---|
303 | @Override
|
---|
304 | public void fillOval(int x, int y, int width, int height) {
|
---|
305 | }
|
---|
306 |
|
---|
307 | @Override
|
---|
308 | public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
|
---|
309 | }
|
---|
310 |
|
---|
311 | @Override
|
---|
312 | public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Override
|
---|
316 | public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
|
---|
317 | }
|
---|
318 |
|
---|
319 | @Override
|
---|
320 | public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
|
---|
321 | }
|
---|
322 |
|
---|
323 | @Override
|
---|
324 | public void drawOval(int x, int y, int width, int height) {
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Override
|
---|
328 | public void drawLine(int x1, int y1, int x2, int y2) {
|
---|
329 | }
|
---|
330 |
|
---|
331 | @Override
|
---|
332 | public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
|
---|
333 | Color bgcolor, ImageObserver observer) {
|
---|
334 | return false;
|
---|
335 | }
|
---|
336 |
|
---|
337 | @Override
|
---|
338 | public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
|
---|
339 | ImageObserver observer) {
|
---|
340 | return false;
|
---|
341 | }
|
---|
342 |
|
---|
343 | @Override
|
---|
344 | public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
|
---|
345 | return false;
|
---|
346 | }
|
---|
347 |
|
---|
348 | @Override
|
---|
349 | public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
|
---|
350 | return false;
|
---|
351 | }
|
---|
352 |
|
---|
353 | @Override
|
---|
354 | public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
|
---|
355 | return false;
|
---|
356 | }
|
---|
357 |
|
---|
358 | @Override
|
---|
359 | public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
|
---|
360 | return false;
|
---|
361 | }
|
---|
362 |
|
---|
363 | @Override
|
---|
364 | public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
|
---|
365 | }
|
---|
366 |
|
---|
367 | @Override
|
---|
368 | public void dispose() {
|
---|
369 | }
|
---|
370 |
|
---|
371 | @Override
|
---|
372 | public Graphics create() {
|
---|
373 | return this;
|
---|
374 | }
|
---|
375 |
|
---|
376 | @Override
|
---|
377 | public void copyArea(int x, int y, int width, int height, int dx, int dy) {
|
---|
378 | }
|
---|
379 |
|
---|
380 | @Override
|
---|
381 | public void clipRect(int x, int y, int width, int height) {
|
---|
382 | }
|
---|
383 |
|
---|
384 | @Override
|
---|
385 | public void clearRect(int x, int y, int width, int height) {
|
---|
386 | }
|
---|
387 |
|
---|
388 | @Override
|
---|
389 | public void translate(double tx, double ty) {
|
---|
390 | }
|
---|
391 |
|
---|
392 | @Override
|
---|
393 | public void translate(int x, int y) {
|
---|
394 | }
|
---|
395 |
|
---|
396 | @Override
|
---|
397 | public void transform(AffineTransform Tx) {
|
---|
398 | }
|
---|
399 |
|
---|
400 | @Override
|
---|
401 | public void shear(double shx, double shy) {
|
---|
402 | }
|
---|
403 |
|
---|
404 | @Override
|
---|
405 | public void setTransform(AffineTransform Tx) {
|
---|
406 | }
|
---|
407 |
|
---|
408 | @Override
|
---|
409 | public void setStroke(Stroke s) {
|
---|
410 | }
|
---|
411 |
|
---|
412 | @Override
|
---|
413 | public void setRenderingHints(Map<?, ?> hints) {
|
---|
414 | }
|
---|
415 |
|
---|
416 | @Override
|
---|
417 | public void setRenderingHint(Key hintKey, Object hintValue) {
|
---|
418 | }
|
---|
419 |
|
---|
420 | @Override
|
---|
421 | public void setPaint(Paint paint) {
|
---|
422 | }
|
---|
423 |
|
---|
424 | @Override
|
---|
425 | public void setComposite(Composite comp) {
|
---|
426 | }
|
---|
427 |
|
---|
428 | @Override
|
---|
429 | public void setBackground(Color color) {
|
---|
430 | }
|
---|
431 |
|
---|
432 | @Override
|
---|
433 | public void scale(double sx, double sy) {
|
---|
434 | }
|
---|
435 |
|
---|
436 | @Override
|
---|
437 | public void rotate(double theta, double x, double y) {
|
---|
438 | }
|
---|
439 |
|
---|
440 | @Override
|
---|
441 | public void rotate(double theta) {
|
---|
442 | }
|
---|
443 |
|
---|
444 | @Override
|
---|
445 | public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
|
---|
446 | return false;
|
---|
447 | }
|
---|
448 |
|
---|
449 | @Override
|
---|
450 | public AffineTransform getTransform() {
|
---|
451 | return null;
|
---|
452 | }
|
---|
453 |
|
---|
454 | @Override
|
---|
455 | public Stroke getStroke() {
|
---|
456 | return null;
|
---|
457 | }
|
---|
458 |
|
---|
459 | @Override
|
---|
460 | public RenderingHints getRenderingHints() {
|
---|
461 | return null;
|
---|
462 | }
|
---|
463 |
|
---|
464 | @Override
|
---|
465 | public Object getRenderingHint(Key hintKey) {
|
---|
466 | return null;
|
---|
467 | }
|
---|
468 |
|
---|
469 | @Override
|
---|
470 | public Paint getPaint() {
|
---|
471 | return null;
|
---|
472 | }
|
---|
473 |
|
---|
474 | @Override
|
---|
475 | public FontRenderContext getFontRenderContext() {
|
---|
476 | return new FontRenderContext(null, false, false);
|
---|
477 | }
|
---|
478 |
|
---|
479 | @Override
|
---|
480 | public GraphicsConfiguration getDeviceConfiguration() {
|
---|
481 | return null;
|
---|
482 | }
|
---|
483 |
|
---|
484 | @Override
|
---|
485 | public Composite getComposite() {
|
---|
486 | return null;
|
---|
487 | }
|
---|
488 |
|
---|
489 | @Override
|
---|
490 | public Color getBackground() {
|
---|
491 | return null;
|
---|
492 | }
|
---|
493 |
|
---|
494 | @Override
|
---|
495 | public void fill(Shape s) {
|
---|
496 | }
|
---|
497 |
|
---|
498 | @Override
|
---|
499 | public void drawString(AttributedCharacterIterator iterator, float x, float y) {
|
---|
500 | }
|
---|
501 |
|
---|
502 | @Override
|
---|
503 | public void drawString(AttributedCharacterIterator iterator, int x, int y) {
|
---|
504 | }
|
---|
505 |
|
---|
506 | @Override
|
---|
507 | public void drawString(String str, float x, float y) {
|
---|
508 | }
|
---|
509 |
|
---|
510 | @Override
|
---|
511 | public void drawString(String str, int x, int y) {
|
---|
512 | }
|
---|
513 |
|
---|
514 | @Override
|
---|
515 | public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
|
---|
516 | }
|
---|
517 |
|
---|
518 | @Override
|
---|
519 | public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
|
---|
520 | }
|
---|
521 |
|
---|
522 | @Override
|
---|
523 | public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
|
---|
524 | }
|
---|
525 |
|
---|
526 | @Override
|
---|
527 | public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
|
---|
528 | return false;
|
---|
529 | }
|
---|
530 |
|
---|
531 | @Override
|
---|
532 | public void drawGlyphVector(GlyphVector g, float x, float y) {
|
---|
533 | }
|
---|
534 |
|
---|
535 | @Override
|
---|
536 | public void draw(Shape s) {
|
---|
537 | }
|
---|
538 |
|
---|
539 | @Override
|
---|
540 | public void clip(Shape s) {
|
---|
541 | }
|
---|
542 |
|
---|
543 | @Override
|
---|
544 | public void addRenderingHints(Map<?, ?> hints) {
|
---|
545 | }
|
---|
546 | };
|
---|
547 | }
|
---|
548 |
|
---|
549 | // CHECKSTYLE.ON: ParameterNumber
|
---|
550 | // CHECKSTYLE.ON: MethodLength
|
---|
551 | // CHECKSTYLE.ON: AnonInnerLength
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Creates a new way with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the nodes added
|
---|
555 | *
|
---|
556 | * @param tags the tags to set
|
---|
557 | * @param nodes the nodes to add
|
---|
558 | * @return a new way
|
---|
559 | */
|
---|
560 | public static Way newWay(String tags, Node... nodes) {
|
---|
561 | final Way way = (Way) OsmUtils.createPrimitive("way " + tags);
|
---|
562 | for (Node node : nodes) {
|
---|
563 | way.addNode(node);
|
---|
564 | }
|
---|
565 | return way;
|
---|
566 | }
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * Creates a new relation with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the members added
|
---|
570 | *
|
---|
571 | * @param tags the tags to set
|
---|
572 | * @param members the members to add
|
---|
573 | * @return a new relation
|
---|
574 | */
|
---|
575 | public static Relation newRelation(String tags, RelationMember... members) {
|
---|
576 | final Relation relation = (Relation) OsmUtils.createPrimitive("relation " + tags);
|
---|
577 | for (RelationMember member : members) {
|
---|
578 | relation.addMember(member);
|
---|
579 | }
|
---|
580 | return relation;
|
---|
581 | }
|
---|
582 | }
|
---|