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