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

Last change on this file since 10467 was 10390, checked in by Don-vip, 8 years ago

cleanup unit tests

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.junit.Assert.fail;
5
6import java.awt.Component;
7import java.awt.Graphics2D;
8import java.io.File;
9import java.io.IOException;
10import java.io.InputStream;
11import java.util.Arrays;
12import java.util.Comparator;
13
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmUtils;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.RelationMember;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
20import org.openstreetmap.josm.gui.progress.CancelHandler;
21import org.openstreetmap.josm.gui.progress.ProgressMonitor;
22import org.openstreetmap.josm.gui.progress.ProgressTaskId;
23import org.openstreetmap.josm.io.Compression;
24import org.openstreetmap.josm.testutils.FakeGraphics;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Various utils, useful for unit tests.
30 */
31public final class TestUtils {
32
33 private TestUtils() {
34 // Hide constructor for utility classes
35 }
36
37 /**
38 * Returns the path to test data root directory.
39 * @return path to test data root directory
40 */
41 public static String getTestDataRoot() {
42 String testDataRoot = System.getProperty("josm.test.data");
43 if (testDataRoot == null || testDataRoot.isEmpty()) {
44 testDataRoot = "test/data";
45 System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
46 }
47 return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
48 }
49
50 /**
51 * Gets path to test data directory for given ticket id.
52 * @param ticketid Ticket numeric identifier
53 * @return path to test data directory for given ticket id
54 */
55 public static String getRegressionDataDir(int ticketid) {
56 return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
57 }
58
59 /**
60 * Gets path to given file in test data directory for given ticket id.
61 * @param ticketid Ticket numeric identifier
62 * @param filename File name
63 * @return path to given file in test data directory for given ticket id
64 */
65 public static String getRegressionDataFile(int ticketid, String filename) {
66 return getRegressionDataDir(ticketid) + '/' + filename;
67 }
68
69 /**
70 * Gets input stream 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 * @throws IOException if any I/O error occurs
75 */
76 public static InputStream getRegressionDataStream(int ticketid, String filename) throws IOException {
77 return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid) + '/' + filename));
78 }
79
80 /**
81 * Checks that the given Comparator respects its contract on the given table.
82 * @param <T> type of elements
83 * @param comparator The comparator to test
84 * @param array The array sorted for test purpose
85 */
86 @SuppressFBWarnings(value = "RV_NEGATING_RESULT_OF_COMPARETO")
87 public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
88 System.out.println("Validating Comparable contract on array of "+array.length+" elements");
89 // Check each compare possibility
90 for (int i = 0; i < array.length; i++) {
91 T r1 = array[i];
92 for (int j = i; j < array.length; j++) {
93 T r2 = array[j];
94 int a = comparator.compare(r1, r2);
95 int b = comparator.compare(r2, r1);
96 if (i == j || a == b) {
97 if (a != 0 || b != 0) {
98 fail(getFailMessage(r1, r2, a, b));
99 }
100 } else {
101 if (a != -b) {
102 fail(getFailMessage(r1, r2, a, b));
103 }
104 }
105 for (int k = j; k < array.length; k++) {
106 T r3 = array[k];
107 int c = comparator.compare(r1, r3);
108 int d = comparator.compare(r2, r3);
109 if (a > 0 && d > 0) {
110 if (c <= 0) {
111 fail(getFailMessage(r1, r2, r3, a, b, c, d));
112 }
113 } else if (a == 0 && d == 0) {
114 if (c != 0) {
115 fail(getFailMessage(r1, r2, r3, a, b, c, d));
116 }
117 } else if (a < 0 && d < 0) {
118 if (c >= 0) {
119 fail(getFailMessage(r1, r2, r3, a, b, c, d));
120 }
121 }
122 }
123 }
124 }
125 // Sort relation array
126 Arrays.sort(array, comparator);
127 }
128
129 private static <T> String getFailMessage(T o1, T o2, int a, int b) {
130 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
131 .append(o2).append("\ngave: ").append(a).append("/").append(b)
132 .toString();
133 }
134
135 private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
136 return new StringBuilder(getFailMessage(o1, o2, a, b))
137 .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
138 .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
139 .toString();
140 }
141
142 /**
143 * Returns the Java version as an int value.
144 * @return the Java version as an int value (7, 8, 9, etc.)
145 */
146 public static int getJavaVersion() {
147 String version = System.getProperty("java.version");
148 if (version.startsWith("1.")) {
149 version = version.substring(2);
150 }
151 // Allow these formats:
152 // 1.7.0_91
153 // 1.8.0_72-ea
154 // 9-ea
155 // 9
156 // 9.0.1
157 int dotPos = version.indexOf('.');
158 int dashPos = version.indexOf('-');
159 return Integer.parseInt(version.substring(0,
160 dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
161 }
162
163 /**
164 * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
165 * but does not show the progress.
166 * @return a progress monitor
167 */
168 public static ProgressMonitor newTestProgressMonitor() {
169 return new AbstractProgressMonitor(new CancelHandler()) {
170
171 @Override
172 protected void doBeginTask() {
173 }
174
175 @Override
176 protected void doFinishTask() {
177 }
178
179 @Override
180 protected void doSetIntermediate(boolean value) {
181 }
182
183 @Override
184 protected void doSetTitle(String title) {
185 }
186
187 @Override
188 protected void doSetCustomText(String title) {
189 }
190
191 @Override
192 protected void updateProgress(double value) {
193 }
194
195 @Override
196 public void setProgressTaskId(ProgressTaskId taskId) {
197 }
198
199 @Override
200 public ProgressTaskId getProgressTaskId() {
201 return null;
202 }
203
204 @Override
205 public Component getWindowParent() {
206 return null;
207 }
208 };
209 }
210
211 /**
212 * Returns an instance of {@link Graphics2D}.
213 * @return a mockup graphics instance
214 */
215 public static Graphics2D newGraphics() {
216 return new FakeGraphics();
217 }
218
219 /**
220 * Creates a new way with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the nodes added
221 *
222 * @param tags the tags to set
223 * @param nodes the nodes to add
224 * @return a new way
225 */
226 public static Way newWay(String tags, Node... nodes) {
227 final Way way = (Way) OsmUtils.createPrimitive("way " + tags);
228 for (Node node : nodes) {
229 way.addNode(node);
230 }
231 return way;
232 }
233
234 /**
235 * Creates a new relation with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the members added
236 *
237 * @param tags the tags to set
238 * @param members the members to add
239 * @return a new relation
240 */
241 public static Relation newRelation(String tags, RelationMember... members) {
242 final Relation relation = (Relation) OsmUtils.createPrimitive("relation " + tags);
243 for (RelationMember member : members) {
244 relation.addMember(member);
245 }
246 return relation;
247 }
248}
Note: See TracBrowser for help on using the repository browser.