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