| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm;
|
|---|
| 3 |
|
|---|
| 4 | import static org.junit.Assert.assertEquals;
|
|---|
| 5 | import static org.junit.Assert.fail;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.Component;
|
|---|
| 8 | import java.awt.Container;
|
|---|
| 9 | import java.awt.Graphics2D;
|
|---|
| 10 | import java.io.File;
|
|---|
| 11 | import java.io.IOException;
|
|---|
| 12 | import java.io.InputStream;
|
|---|
| 13 | import java.lang.reflect.Field;
|
|---|
| 14 | import java.lang.reflect.Method;
|
|---|
| 15 | import java.security.AccessController;
|
|---|
| 16 | import java.security.PrivilegedAction;
|
|---|
| 17 | import java.util.Arrays;
|
|---|
| 18 | import java.util.Collection;
|
|---|
| 19 | import java.util.Comparator;
|
|---|
| 20 | import java.util.Objects;
|
|---|
| 21 | import java.util.stream.Stream;
|
|---|
| 22 |
|
|---|
| 23 | import org.junit.Assume;
|
|---|
| 24 | import org.openstreetmap.josm.command.Command;
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 26 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 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;
|
|---|
| 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;
|
|---|
| 36 | import org.openstreetmap.josm.io.Compression;
|
|---|
| 37 | import org.openstreetmap.josm.testutils.FakeGraphics;
|
|---|
| 38 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
|---|
| 39 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 40 |
|
|---|
| 41 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Various utils, useful for unit tests.
|
|---|
| 45 | */
|
|---|
| 46 | public final class TestUtils {
|
|---|
| 47 |
|
|---|
| 48 | private TestUtils() {
|
|---|
| 49 | // Hide constructor for utility classes
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Returns the path to test data root directory.
|
|---|
| 54 | * @return path to test data root directory
|
|---|
| 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 | }
|
|---|
| 64 |
|
|---|
| 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 |
|
|---|
| 84 | /**
|
|---|
| 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 {
|
|---|
| 92 | return Compression.getUncompressedFileInputStream(new File(getRegressionDataDir(ticketid), filename));
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Checks that the given Comparator respects its contract on the given table.
|
|---|
| 97 | * @param <T> type of elements
|
|---|
| 98 | * @param comparator The comparator to test
|
|---|
| 99 | * @param array The array sorted for test purpose
|
|---|
| 100 | */
|
|---|
| 101 | @SuppressFBWarnings(value = "RV_NEGATING_RESULT_OF_COMPARETO")
|
|---|
| 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
|
|---|
| 105 | for (int i = 0; i < array.length; i++) {
|
|---|
| 106 | T r1 = array[i];
|
|---|
| 107 | for (int j = i; j < array.length; j++) {
|
|---|
| 108 | T r2 = array[j];
|
|---|
| 109 | int a = comparator.compare(r1, r2);
|
|---|
| 110 | int b = comparator.compare(r2, r1);
|
|---|
| 111 | if (i == j || a == b) {
|
|---|
| 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 | }
|
|---|
| 120 | for (int k = j; k < array.length; k++) {
|
|---|
| 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 | }
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 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);
|
|---|
| 166 | AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
|
|---|
| 167 | f.setAccessible(true);
|
|---|
| 168 | return null;
|
|---|
| 169 | });
|
|---|
| 170 | return f.get(obj);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 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 | /**
|
|---|
| 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 | /**
|
|---|
| 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 | }
|
|---|
| 250 | };
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * Returns an instance of {@link Graphics2D}.
|
|---|
| 255 | * @return a mockup graphics instance
|
|---|
| 256 | */
|
|---|
| 257 | public static Graphics2D newGraphics() {
|
|---|
| 258 | return new FakeGraphics();
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /**
|
|---|
| 262 | * Creates a new node with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)})
|
|---|
| 263 | *
|
|---|
| 264 | * @param tags the tags to set
|
|---|
| 265 | * @return a new node
|
|---|
| 266 | */
|
|---|
| 267 | public static Node newNode(String tags) {
|
|---|
| 268 | return (Node) OsmUtils.createPrimitive("node " + tags);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /**
|
|---|
| 272 | * Creates a new way with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the nodes added
|
|---|
| 273 | *
|
|---|
| 274 | * @param tags the tags to set
|
|---|
| 275 | * @param nodes the nodes to add
|
|---|
| 276 | * @return a new way
|
|---|
| 277 | */
|
|---|
| 278 | public static Way newWay(String tags, Node... nodes) {
|
|---|
| 279 | final Way way = (Way) OsmUtils.createPrimitive("way " + tags);
|
|---|
| 280 | for (Node node : nodes) {
|
|---|
| 281 | way.addNode(node);
|
|---|
| 282 | }
|
|---|
| 283 | return way;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /**
|
|---|
| 287 | * Creates a new relation with the given tags (see {@link OsmUtils#createPrimitive(java.lang.String)}) and the members added
|
|---|
| 288 | *
|
|---|
| 289 | * @param tags the tags to set
|
|---|
| 290 | * @param members the members to add
|
|---|
| 291 | * @return a new relation
|
|---|
| 292 | */
|
|---|
| 293 | public static Relation newRelation(String tags, RelationMember... members) {
|
|---|
| 294 | final Relation relation = (Relation) OsmUtils.createPrimitive("relation " + tags);
|
|---|
| 295 | for (RelationMember member : members) {
|
|---|
| 296 | relation.addMember(member);
|
|---|
| 297 | }
|
|---|
| 298 | return relation;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | /**
|
|---|
| 302 | * Creates a new empty command.
|
|---|
| 303 | * @param ds data set
|
|---|
| 304 | * @return a new empty command
|
|---|
| 305 | */
|
|---|
| 306 | public static Command newCommand(DataSet ds) {
|
|---|
| 307 | return new Command(ds) {
|
|---|
| 308 | @Override
|
|---|
| 309 | public String getDescriptionText() {
|
|---|
| 310 | return "";
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | @Override
|
|---|
| 314 | public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
|
|---|
| 315 | Collection<OsmPrimitive> added) {
|
|---|
| 316 | // Do nothing
|
|---|
| 317 | }
|
|---|
| 318 | };
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /**
|
|---|
| 322 | * Ensures 100% code coverage for enums.
|
|---|
| 323 | * @param enumClass enum class to cover
|
|---|
| 324 | */
|
|---|
| 325 | public static void superficialEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
|
|---|
| 326 | try {
|
|---|
| 327 | Method values = enumClass.getMethod("values");
|
|---|
| 328 | Method valueOf = enumClass.getMethod("valueOf", String.class);
|
|---|
| 329 | Utils.setObjectsAccessible(values, valueOf);
|
|---|
| 330 | for (Object o : (Object[]) values.invoke(null)) {
|
|---|
| 331 | assertEquals(o, valueOf.invoke(null, ((Enum<?>) o).name()));
|
|---|
| 332 | }
|
|---|
| 333 | } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
|
|---|
| 334 | throw new JosmRuntimeException(e);
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /**
|
|---|
| 339 | * Get a descendant component by name.
|
|---|
| 340 | * @param root The root component to start searching from.
|
|---|
| 341 | * @param name The component name
|
|---|
| 342 | * @return The component with that name or null if it does not exist.
|
|---|
| 343 | * @since 12045
|
|---|
| 344 | */
|
|---|
| 345 | public static Component getComponentByName(Component root, String name) {
|
|---|
| 346 | if (name.equals(root.getName())) {
|
|---|
| 347 | return root;
|
|---|
| 348 | } else if (root instanceof Container) {
|
|---|
| 349 | Container container = (Container) root;
|
|---|
| 350 | return Stream.of(container.getComponents())
|
|---|
| 351 | .map(child -> getComponentByName(child, name))
|
|---|
| 352 | .filter(Objects::nonNull)
|
|---|
| 353 | .findFirst().orElse(null);
|
|---|
| 354 | } else {
|
|---|
| 355 | return null;
|
|---|
| 356 | }
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | /**
|
|---|
| 360 | * Use to assume that EqualsVerifier is working with the current JVM.
|
|---|
| 361 | */
|
|---|
| 362 | public static void assumeWorkingEqualsVerifier() {
|
|---|
| 363 | try {
|
|---|
| 364 | // Workaround to https://github.com/jqno/equalsverifier/issues/177
|
|---|
| 365 | // Inspired by https://issues.apache.org/jira/browse/SOLR-11606
|
|---|
| 366 | nl.jqno.equalsverifier.internal.lib.bytebuddy.ClassFileVersion.ofThisVm();
|
|---|
| 367 | } catch (IllegalArgumentException e) {
|
|---|
| 368 | Assume.assumeNoException(e);
|
|---|
| 369 | }
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|