| 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.util.Arrays;
|
|---|
| 7 | import java.util.Comparator;
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * Various utils, useful for unit tests.
|
|---|
| 11 | */
|
|---|
| 12 | public final class TestUtils {
|
|---|
| 13 |
|
|---|
| 14 | private TestUtils() {
|
|---|
| 15 | // Hide constructor for utility classes
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Returns the path to test data root directory.
|
|---|
| 20 | * @return path to test data root directory
|
|---|
| 21 | */
|
|---|
| 22 | public static String getTestDataRoot() {
|
|---|
| 23 | String testDataRoot = System.getProperty("josm.test.data");
|
|---|
| 24 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
|---|
| 25 | testDataRoot = "test/data";
|
|---|
| 26 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
|---|
| 27 | }
|
|---|
| 28 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Gets path to test data directory for given ticket id.
|
|---|
| 33 | * @param ticketid Ticket numeric identifier
|
|---|
| 34 | * @return path to test data directory for given ticket id
|
|---|
| 35 | */
|
|---|
| 36 | public static String getRegressionDataDir(int ticketid) {
|
|---|
| 37 | return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Gets path to given file in test data directory for given ticket id.
|
|---|
| 42 | * @param ticketid Ticket numeric identifier
|
|---|
| 43 | * @param filename File name
|
|---|
| 44 | * @return path to given file in test data directory for given ticket id
|
|---|
| 45 | */
|
|---|
| 46 | public static String getRegressionDataFile(int ticketid, String filename) {
|
|---|
| 47 | return getRegressionDataDir(ticketid) + '/' + filename;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Checks that the given Comparator respects its contract on the given table.
|
|---|
| 52 | * @param comparator The comparator to test
|
|---|
| 53 | * @param array The array sorted for test purpose
|
|---|
| 54 | */
|
|---|
| 55 | public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
|
|---|
| 56 | System.out.println("Validating Comparable contract on array of "+array.length+" elements");
|
|---|
| 57 | // Check each compare possibility
|
|---|
| 58 | for (int i = 0; i < array.length; i++) {
|
|---|
| 59 | T r1 = array[i];
|
|---|
| 60 | for (int j = i; j < array.length; j++) {
|
|---|
| 61 | T r2 = array[j];
|
|---|
| 62 | int a = comparator.compare(r1, r2);
|
|---|
| 63 | int b = comparator.compare(r2, r1);
|
|---|
| 64 | if (i == j || a == b) {
|
|---|
| 65 | if (a != 0 || b != 0) {
|
|---|
| 66 | fail(getFailMessage(r1, r2, a, b));
|
|---|
| 67 | }
|
|---|
| 68 | } else {
|
|---|
| 69 | if (a != -b) {
|
|---|
| 70 | fail(getFailMessage(r1, r2, a, b));
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | for (int k = j; k < array.length; k++) {
|
|---|
| 74 | T r3 = array[k];
|
|---|
| 75 | int c = comparator.compare(r1, r3);
|
|---|
| 76 | int d = comparator.compare(r2, r3);
|
|---|
| 77 | if (a > 0 && d > 0) {
|
|---|
| 78 | if (c <= 0) {
|
|---|
| 79 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
|---|
| 80 | }
|
|---|
| 81 | } else if (a == 0 && d == 0) {
|
|---|
| 82 | if (c != 0) {
|
|---|
| 83 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
|---|
| 84 | }
|
|---|
| 85 | } else if (a < 0 && d < 0) {
|
|---|
| 86 | if (c >= 0) {
|
|---|
| 87 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | // Sort relation array
|
|---|
| 94 | Arrays.sort(array, comparator);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | private static <T> String getFailMessage(T o1, T o2, int a, int b) {
|
|---|
| 98 | return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
|
|---|
| 99 | .append(o2).append("\ngave: ").append(a).append("/").append(b)
|
|---|
| 100 | .toString();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
|
|---|
| 104 | return new StringBuilder(getFailMessage(o1, o2, a, b))
|
|---|
| 105 | .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
|
|---|
| 106 | .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
|
|---|
| 107 | .toString();
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Returns the Java version as a double value.
|
|---|
| 112 | * @return the Java version as a double value (1.7, 1.8, 1.9, etc.)
|
|---|
| 113 | */
|
|---|
| 114 | public static double getJavaVersion() {
|
|---|
| 115 | String version = System.getProperty("java.version");
|
|---|
| 116 | int pos = version.indexOf('.');
|
|---|
| 117 | pos = version.indexOf('.', pos + 1);
|
|---|
| 118 | return Double.parseDouble(version.substring(0, pos));
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|