[6562] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[7068] | 2 | package org.openstreetmap.josm;
|
---|
[6562] | 3 |
|
---|
[7109] | 4 | import static org.junit.Assert.fail;
|
---|
[6881] | 5 |
|
---|
[7109] | 6 | import java.util.Arrays;
|
---|
| 7 | import java.util.Comparator;
|
---|
[6881] | 8 |
|
---|
[7356] | 9 | /**
|
---|
| 10 | * Various utils, useful for unit tests.
|
---|
| 11 | */
|
---|
[8514] | 12 | public final class TestUtils {
|
---|
[6562] | 13 |
|
---|
[8514] | 14 | private TestUtils() {
|
---|
| 15 | // Hide constructor for utility classes
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[6562] | 18 | /**
|
---|
| 19 | * Returns the path to test data root directory.
|
---|
[7365] | 20 | * @return path to test data root directory
|
---|
[6562] | 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 | }
|
---|
[6592] | 30 |
|
---|
[7365] | 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 |
|
---|
[7109] | 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
|
---|
[8510] | 58 | for (int i = 0; i < array.length; i++) {
|
---|
[7109] | 59 | T r1 = array[i];
|
---|
[8510] | 60 | for (int j = i; j < array.length; j++) {
|
---|
[7109] | 61 | T r2 = array[j];
|
---|
| 62 | int a = comparator.compare(r1, r2);
|
---|
| 63 | int b = comparator.compare(r2, r1);
|
---|
[8510] | 64 | if (i == j || a == b) {
|
---|
[7109] | 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 | }
|
---|
[8510] | 73 | for (int k = j; k < array.length; k++) {
|
---|
[7109] | 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 | }
|
---|
[8793] | 109 |
|
---|
| 110 | /**
|
---|
[9199] | 111 | * Returns the Java version as an int value.
|
---|
| 112 | * @return the Java version as an int value (7, 8, 9, etc.)
|
---|
[8793] | 113 | */
|
---|
[9199] | 114 | public static int getJavaVersion() {
|
---|
[8793] | 115 | String version = System.getProperty("java.version");
|
---|
[9199] | 116 | if (version.startsWith("1.")) {
|
---|
| 117 | version = version.substring(2);
|
---|
| 118 | }
|
---|
[9202] | 119 | // Allow these formats:
|
---|
| 120 | // 1.7.0_91
|
---|
| 121 | // 1.8.0_72-ea
|
---|
| 122 | // 9-ea
|
---|
| 123 | // 9
|
---|
| 124 | // 9.0.1
|
---|
| 125 | int dotPos = version.indexOf('.');
|
---|
| 126 | int dashPos = version.indexOf('-');
|
---|
| 127 | return Integer.parseInt(version.substring(0,
|
---|
| 128 | dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
|
---|
[8793] | 129 | }
|
---|
[6562] | 130 | }
|
---|