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

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

add more unit tests

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