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