[6562] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[7068] | 2 | package org.openstreetmap.josm;
|
---|
[6562] | 3 |
|
---|
[11978] | 4 | import static org.junit.Assert.assertEquals;
|
---|
[7109] | 5 | import static org.junit.Assert.fail;
|
---|
[6881] | 6 |
|
---|
[9529] | 7 | import java.awt.Component;
|
---|
[12045] | 8 | import java.awt.Container;
|
---|
[9907] | 9 | import java.awt.Graphics2D;
|
---|
[9489] | 10 | import java.io.File;
|
---|
| 11 | import java.io.IOException;
|
---|
| 12 | import java.io.InputStream;
|
---|
[11104] | 13 | import java.lang.reflect.Field;
|
---|
[11978] | 14 | import java.lang.reflect.Method;
|
---|
[11324] | 15 | import java.security.AccessController;
|
---|
| 16 | import java.security.PrivilegedAction;
|
---|
[7109] | 17 | import java.util.Arrays;
|
---|
[11102] | 18 | import java.util.Collection;
|
---|
[7109] | 19 | import java.util.Comparator;
|
---|
[12045] | 20 | import java.util.Objects;
|
---|
| 21 | import java.util.stream.Stream;
|
---|
[6881] | 22 |
|
---|
[13079] | 23 | import org.junit.Assume;
|
---|
[11102] | 24 | import org.openstreetmap.josm.command.Command;
|
---|
[12726] | 25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[9933] | 26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[11102] | 27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[9933] | 28 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
| 29 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 30 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 31 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[9529] | 32 | import org.openstreetmap.josm.gui.progress.AbstractProgressMonitor;
|
---|
| 33 | import org.openstreetmap.josm.gui.progress.CancelHandler;
|
---|
| 34 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 35 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
[9489] | 36 | import org.openstreetmap.josm.io.Compression;
|
---|
[10390] | 37 | import org.openstreetmap.josm.testutils.FakeGraphics;
|
---|
[11978] | 38 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
| 39 | import org.openstreetmap.josm.tools.Utils;
|
---|
[9489] | 40 |
|
---|
[10222] | 41 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
| 42 |
|
---|
[7356] | 43 | /**
|
---|
| 44 | * Various utils, useful for unit tests.
|
---|
| 45 | */
|
---|
[8514] | 46 | public final class TestUtils {
|
---|
[6562] | 47 |
|
---|
[8514] | 48 | private TestUtils() {
|
---|
| 49 | // Hide constructor for utility classes
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[6562] | 52 | /**
|
---|
| 53 | * Returns the path to test data root directory.
|
---|
[7365] | 54 | * @return path to test data root directory
|
---|
[6562] | 55 | */
|
---|
| 56 | public static String getTestDataRoot() {
|
---|
| 57 | String testDataRoot = System.getProperty("josm.test.data");
|
---|
| 58 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
---|
| 59 | testDataRoot = "test/data";
|
---|
| 60 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
---|
| 61 | }
|
---|
| 62 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
---|
| 63 | }
|
---|
[6592] | 64 |
|
---|
[7365] | 65 | /**
|
---|
| 66 | * Gets path to test data directory for given ticket id.
|
---|
| 67 | * @param ticketid Ticket numeric identifier
|
---|
| 68 | * @return path to test data directory for given ticket id
|
---|
| 69 | */
|
---|
| 70 | public static String getRegressionDataDir(int ticketid) {
|
---|
| 71 | return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Gets path to given file in test data directory for given ticket id.
|
---|
| 76 | * @param ticketid Ticket numeric identifier
|
---|
| 77 | * @param filename File name
|
---|
| 78 | * @return path to given file in test data directory for given ticket id
|
---|
| 79 | */
|
---|
| 80 | public static String getRegressionDataFile(int ticketid, String filename) {
|
---|
| 81 | return getRegressionDataDir(ticketid) + '/' + filename;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[7109] | 84 | /**
|
---|
[9489] | 85 | * Gets input stream to given file in test data directory for given ticket id.
|
---|
| 86 | * @param ticketid Ticket numeric identifier
|
---|
| 87 | * @param filename File name
|
---|
| 88 | * @return path to given file in test data directory for given ticket id
|
---|
| 89 | * @throws IOException if any I/O error occurs
|
---|
| 90 | */
|
---|
| 91 | public static InputStream getRegressionDataStream(int ticketid, String filename) throws IOException {
|
---|
[11324] | 92 | return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid), filename));
|
---|
[9489] | 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
[7109] | 96 | * Checks that the given Comparator respects its contract on the given table.
|
---|
[9246] | 97 | * @param <T> type of elements
|
---|
[7109] | 98 | * @param comparator The comparator to test
|
---|
| 99 | * @param array The array sorted for test purpose
|
---|
| 100 | */
|
---|
[10222] | 101 | @SuppressFBWarnings(value = "RV_NEGATING_RESULT_OF_COMPARETO")
|
---|
[7109] | 102 | public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
|
---|
| 103 | System.out.println("Validating Comparable contract on array of "+array.length+" elements");
|
---|
| 104 | // Check each compare possibility
|
---|
[8510] | 105 | for (int i = 0; i < array.length; i++) {
|
---|
[7109] | 106 | T r1 = array[i];
|
---|
[8510] | 107 | for (int j = i; j < array.length; j++) {
|
---|
[7109] | 108 | T r2 = array[j];
|
---|
| 109 | int a = comparator.compare(r1, r2);
|
---|
| 110 | int b = comparator.compare(r2, r1);
|
---|
[8510] | 111 | if (i == j || a == b) {
|
---|
[7109] | 112 | if (a != 0 || b != 0) {
|
---|
| 113 | fail(getFailMessage(r1, r2, a, b));
|
---|
| 114 | }
|
---|
| 115 | } else {
|
---|
| 116 | if (a != -b) {
|
---|
| 117 | fail(getFailMessage(r1, r2, a, b));
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[8510] | 120 | for (int k = j; k < array.length; k++) {
|
---|
[7109] | 121 | T r3 = array[k];
|
---|
| 122 | int c = comparator.compare(r1, r3);
|
---|
| 123 | int d = comparator.compare(r2, r3);
|
---|
| 124 | if (a > 0 && d > 0) {
|
---|
| 125 | if (c <= 0) {
|
---|
| 126 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
| 127 | }
|
---|
| 128 | } else if (a == 0 && d == 0) {
|
---|
| 129 | if (c != 0) {
|
---|
| 130 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
| 131 | }
|
---|
| 132 | } else if (a < 0 && d < 0) {
|
---|
| 133 | if (c >= 0) {
|
---|
| 134 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | // Sort relation array
|
---|
| 141 | Arrays.sort(array, comparator);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private static <T> String getFailMessage(T o1, T o2, int a, int b) {
|
---|
| 145 | return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
|
---|
| 146 | .append(o2).append("\ngave: ").append(a).append("/").append(b)
|
---|
| 147 | .toString();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
|
---|
| 151 | return new StringBuilder(getFailMessage(o1, o2, a, b))
|
---|
| 152 | .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
|
---|
| 153 | .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
|
---|
| 154 | .toString();
|
---|
| 155 | }
|
---|
[8793] | 156 |
|
---|
| 157 | /**
|
---|
[11104] | 158 | * Returns a private field value.
|
---|
| 159 | * @param obj object
|
---|
| 160 | * @param fieldName private field name
|
---|
| 161 | * @return private field value
|
---|
| 162 | * @throws ReflectiveOperationException if a reflection operation error occurs
|
---|
| 163 | */
|
---|
| 164 | public static Object getPrivateField(Object obj, String fieldName) throws ReflectiveOperationException {
|
---|
| 165 | Field f = obj.getClass().getDeclaredField(fieldName);
|
---|
[11324] | 166 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
---|
| 167 | f.setAccessible(true);
|
---|
| 168 | return null;
|
---|
| 169 | });
|
---|
[11104] | 170 | return f.get(obj);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
[13078] | 174 | * Returns a private static field value.
|
---|
| 175 | * @param cls object class
|
---|
| 176 | * @param fieldName private field name
|
---|
| 177 | * @return private field value
|
---|
| 178 | * @throws ReflectiveOperationException if a reflection operation error occurs
|
---|
| 179 | */
|
---|
| 180 | public static Object getPrivateStaticField(Class<?> cls, String fieldName) throws ReflectiveOperationException {
|
---|
| 181 | Field f = cls.getDeclaredField(fieldName);
|
---|
| 182 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
---|
| 183 | f.setAccessible(true);
|
---|
| 184 | return null;
|
---|
| 185 | });
|
---|
| 186 | return f.get(null);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
[13300] | 190 | * Sets a private static field value.
|
---|
| 191 | * @param cls object class
|
---|
| 192 | * @param fieldName private field name
|
---|
| 193 | * @param value replacement value
|
---|
| 194 | * @throws ReflectiveOperationException if a reflection operation error occurs
|
---|
| 195 | */
|
---|
| 196 | public static void setPrivateStaticField(Class<?> cls, String fieldName, final Object value) throws ReflectiveOperationException {
|
---|
| 197 | Field f = cls.getDeclaredField(fieldName);
|
---|
| 198 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
---|
| 199 | f.setAccessible(true);
|
---|
| 200 | return null;
|
---|
| 201 | });
|
---|
| 202 | f.set(null, value);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
[9529] | 206 | * Returns an instance of {@link AbstractProgressMonitor} which keeps track of the monitor state,
|
---|
| 207 | * but does not show the progress.
|
---|
| 208 | * @return a progress monitor
|
---|
| 209 | */
|
---|
| 210 | public static ProgressMonitor newTestProgressMonitor() {
|
---|
| 211 | return new AbstractProgressMonitor(new CancelHandler()) {
|
---|
| 212 |
|
---|
| 213 | @Override
|
---|
| 214 | protected void doBeginTask() {
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | @Override
|
---|
| 218 | protected void doFinishTask() {
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | @Override
|
---|
| 222 | protected void doSetIntermediate(boolean value) {
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | @Override
|
---|
| 226 | protected void doSetTitle(String title) {
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | @Override
|
---|
| 230 | protected void doSetCustomText(String title) {
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | @Override
|
---|
| 234 | protected void updateProgress(double value) {
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | @Override
|
---|
| 238 | public void setProgressTaskId(ProgressTaskId taskId) {
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | @Override
|
---|
| 242 | public ProgressTaskId getProgressTaskId() {
|
---|
| 243 | return null;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | @Override
|
---|
| 247 | public Component getWindowParent() {
|
---|
| 248 | return null;
|
---|
| 249 | }
|
---|
[9907] | 250 | };
|
---|
| 251 | }
|
---|
[9529] | 252 |
|
---|
[9907] | 253 | /**
|
---|
| 254 | * Returns an instance of {@link Graphics2D}.
|
---|
| 255 | * @return a mockup graphics instance
|
---|
| 256 | */
|
---|
| 257 | public static Graphics2D newGraphics() {
|
---|
[10390] | 258 | return new FakeGraphics();
|
---|
[9529] | 259 | }
|
---|
[9907] | 260 |
|
---|
[9933] | 261 | /**
|
---|
[13489] | 262 | * Makes sure the given primitive belongs to a data set.
|
---|
| 263 | * @param <T> OSM primitive type
|
---|
| 264 | * @param osm OSM primitive
|
---|
| 265 | * @return OSM primitive, attached to a new {@code DataSet}
|
---|
| 266 | */
|
---|
| 267 | public static <T extends OsmPrimitive> T addFakeDataSet(T osm) {
|
---|
| 268 | new DataSet(osm);
|
---|
| 269 | return osm;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /**
|
---|
[13413] | 273 | * Creates a new node with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)})
|
---|
| 274 | *
|
---|
| 275 | * @param tags the tags to set
|
---|
| 276 | * @return a new node
|
---|
| 277 | */
|
---|
| 278 | public static Node newNode(String tags) {
|
---|
| 279 | return (Node) OsmUtils.createPrimitive("node " + tags);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /**
|
---|
[9933] | 283 | * Creates a new way with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the nodes added
|
---|
| 284 | *
|
---|
| 285 | * @param tags the tags to set
|
---|
| 286 | * @param nodes the nodes to add
|
---|
| 287 | * @return a new way
|
---|
| 288 | */
|
---|
| 289 | public static Way newWay(String tags, Node... nodes) {
|
---|
| 290 | final Way way = (Way) OsmUtils.createPrimitive("way " + tags);
|
---|
| 291 | for (Node node : nodes) {
|
---|
| 292 | way.addNode(node);
|
---|
| 293 | }
|
---|
| 294 | return way;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /**
|
---|
| 298 | * Creates a new relation with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the members added
|
---|
| 299 | *
|
---|
| 300 | * @param tags the tags to set
|
---|
| 301 | * @param members the members to add
|
---|
| 302 | * @return a new relation
|
---|
| 303 | */
|
---|
| 304 | public static Relation newRelation(String tags, RelationMember... members) {
|
---|
| 305 | final Relation relation = (Relation) OsmUtils.createPrimitive("relation " + tags);
|
---|
| 306 | for (RelationMember member : members) {
|
---|
| 307 | relation.addMember(member);
|
---|
| 308 | }
|
---|
| 309 | return relation;
|
---|
| 310 | }
|
---|
[11102] | 311 |
|
---|
| 312 | /**
|
---|
| 313 | * Creates a new empty command.
|
---|
[12726] | 314 | * @param ds data set
|
---|
[11102] | 315 | * @return a new empty command
|
---|
| 316 | */
|
---|
[12726] | 317 | public static Command newCommand(DataSet ds) {
|
---|
| 318 | return new Command(ds) {
|
---|
[11102] | 319 | @Override
|
---|
| 320 | public String getDescriptionText() {
|
---|
| 321 | return "";
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | @Override
|
---|
| 325 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
---|
| 326 | Collection<OsmPrimitive> added) {
|
---|
| 327 | // Do nothing
|
---|
| 328 | }
|
---|
| 329 | };
|
---|
| 330 | }
|
---|
[11978] | 331 |
|
---|
| 332 | /**
|
---|
| 333 | * Ensures 100% code coverage for enums.
|
---|
| 334 | * @param enumClass enum class to cover
|
---|
| 335 | */
|
---|
| 336 | public static void superficialEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
|
---|
| 337 | try {
|
---|
| 338 | Method values = enumClass.getMethod("values");
|
---|
| 339 | Method valueOf = enumClass.getMethod("valueOf", String.class);
|
---|
| 340 | Utils.setObjectsAccessible(values, valueOf);
|
---|
| 341 | for (Object o : (Object[]) values.invoke(null)) {
|
---|
| 342 | assertEquals(o, valueOf.invoke(null, ((Enum<?>) o).name()));
|
---|
| 343 | }
|
---|
| 344 | } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
|
---|
| 345 | throw new JosmRuntimeException(e);
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
[12045] | 348 |
|
---|
| 349 | /**
|
---|
| 350 | * Get a descendant component by name.
|
---|
| 351 | * @param root The root component to start searching from.
|
---|
| 352 | * @param name The component name
|
---|
| 353 | * @return The component with that name or null if it does not exist.
|
---|
| 354 | * @since 12045
|
---|
| 355 | */
|
---|
| 356 | public static Component getComponentByName(Component root, String name) {
|
---|
| 357 | if (name.equals(root.getName())) {
|
---|
| 358 | return root;
|
---|
| 359 | } else if (root instanceof Container) {
|
---|
| 360 | Container container = (Container) root;
|
---|
| 361 | return Stream.of(container.getComponents())
|
---|
| 362 | .map(child -> getComponentByName(child, name))
|
---|
| 363 | .filter(Objects::nonNull)
|
---|
| 364 | .findFirst().orElse(null);
|
---|
| 365 | } else {
|
---|
| 366 | return null;
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
[13079] | 369 |
|
---|
| 370 | /**
|
---|
| 371 | * Use to assume that EqualsVerifier is working with the current JVM.
|
---|
| 372 | */
|
---|
| 373 | public static void assumeWorkingEqualsVerifier() {
|
---|
| 374 | try {
|
---|
| 375 | // Workaround to https://github.com/jqno/equalsverifier/issues/177
|
---|
| 376 | // Inspired by https://issues.apache.org/jira/browse/SOLR-11606
|
---|
| 377 | nl.jqno.equalsverifier.internal.lib.bytebuddy.ClassFileVersion.ofThisVm();
|
---|
| 378 | } catch (IllegalArgumentException e) {
|
---|
| 379 | Assume.assumeNoException(e);
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
[6562] | 382 | }
|
---|