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

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

convert unit tests from Groovy to Java

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