[6562] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[7068] | 2 | package org.openstreetmap.josm;
|
---|
[6562] | 3 |
|
---|
[6881] | 4 | import static org.hamcrest.CoreMatchers.is;
|
---|
| 5 | import static org.junit.Assert.assertThat;
|
---|
| 6 | import static org.junit.Assert.assertTrue;
|
---|
[7109] | 7 | import static org.junit.Assert.fail;
|
---|
[6881] | 8 |
|
---|
[7109] | 9 | import java.util.Arrays;
|
---|
| 10 | import java.util.Comparator;
|
---|
[6881] | 11 |
|
---|
[6592] | 12 | import org.junit.Test;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[7356] | 14 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
[6592] | 15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[6562] | 16 |
|
---|
[7356] | 17 | /**
|
---|
| 18 | * Various utils, useful for unit tests.
|
---|
| 19 | */
|
---|
[6562] | 20 | public class TestUtils {
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Returns the path to test data root directory.
|
---|
[7365] | 24 | * @return path to test data root directory
|
---|
[6562] | 25 | */
|
---|
| 26 | public static String getTestDataRoot() {
|
---|
| 27 | String testDataRoot = System.getProperty("josm.test.data");
|
---|
| 28 | if (testDataRoot == null || testDataRoot.isEmpty()) {
|
---|
| 29 | testDataRoot = "test/data";
|
---|
| 30 | System.out.println("System property josm.test.data is not set, using '" + testDataRoot + "'");
|
---|
| 31 | }
|
---|
| 32 | return testDataRoot.endsWith("/") ? testDataRoot : testDataRoot + "/";
|
---|
| 33 | }
|
---|
[6592] | 34 |
|
---|
[7365] | 35 | /**
|
---|
| 36 | * Gets path to test data directory for given ticket id.
|
---|
| 37 | * @param ticketid Ticket numeric identifier
|
---|
| 38 | * @return path to test data directory for given ticket id
|
---|
| 39 | */
|
---|
| 40 | public static String getRegressionDataDir(int ticketid) {
|
---|
| 41 | return TestUtils.getTestDataRoot() + "/regress/" + ticketid;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Gets path to given file in test data directory for given ticket id.
|
---|
| 46 | * @param ticketid Ticket numeric identifier
|
---|
| 47 | * @param filename File name
|
---|
| 48 | * @return path to given file in test data directory for given ticket id
|
---|
| 49 | */
|
---|
| 50 | public static String getRegressionDataFile(int ticketid, String filename) {
|
---|
| 51 | return getRegressionDataDir(ticketid) + '/' + filename;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[6592] | 54 | @Test
|
---|
| 55 | public void testCreatePrimitive() throws Exception {
|
---|
[7356] | 56 | final OsmPrimitive p = OsmUtils.createPrimitive("way name=Foo railway=rail");
|
---|
[6592] | 57 | assertTrue(p instanceof Way);
|
---|
| 58 | assertThat(p.keySet().size(), is(2));
|
---|
| 59 | assertThat(p.get("name"), is("Foo"));
|
---|
| 60 | assertThat(p.get("railway"), is("rail"));
|
---|
| 61 | }
|
---|
[6601] | 62 |
|
---|
| 63 | @Test(expected = IllegalArgumentException.class)
|
---|
| 64 | public void testCreatePrimitiveFail() throws Exception {
|
---|
[7356] | 65 | OsmUtils.createPrimitive("noway name=Foo");
|
---|
[6601] | 66 | }
|
---|
| 67 |
|
---|
[7109] | 68 | /**
|
---|
| 69 | * Checks that the given Comparator respects its contract on the given table.
|
---|
| 70 | * @param comparator The comparator to test
|
---|
| 71 | * @param array The array sorted for test purpose
|
---|
| 72 | */
|
---|
| 73 | public static <T> void checkComparableContract(Comparator<T> comparator, T[] array) {
|
---|
| 74 | System.out.println("Validating Comparable contract on array of "+array.length+" elements");
|
---|
| 75 | // Check each compare possibility
|
---|
| 76 | for (int i=0; i<array.length; i++) {
|
---|
| 77 | T r1 = array[i];
|
---|
| 78 | for (int j=i; j<array.length; j++) {
|
---|
| 79 | T r2 = array[j];
|
---|
| 80 | int a = comparator.compare(r1, r2);
|
---|
| 81 | int b = comparator.compare(r2, r1);
|
---|
| 82 | if (i==j || a==b) {
|
---|
| 83 | if (a != 0 || b != 0) {
|
---|
| 84 | fail(getFailMessage(r1, r2, a, b));
|
---|
| 85 | }
|
---|
| 86 | } else {
|
---|
| 87 | if (a != -b) {
|
---|
| 88 | fail(getFailMessage(r1, r2, a, b));
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | for (int k=j; k<array.length; k++) {
|
---|
| 92 | T r3 = array[k];
|
---|
| 93 | int c = comparator.compare(r1, r3);
|
---|
| 94 | int d = comparator.compare(r2, r3);
|
---|
| 95 | if (a > 0 && d > 0) {
|
---|
| 96 | if (c <= 0) {
|
---|
| 97 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
| 98 | }
|
---|
| 99 | } else if (a == 0 && d == 0) {
|
---|
| 100 | if (c != 0) {
|
---|
| 101 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
| 102 | }
|
---|
| 103 | } else if (a < 0 && d < 0) {
|
---|
| 104 | if (c >= 0) {
|
---|
| 105 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | // Sort relation array
|
---|
| 112 | Arrays.sort(array, comparator);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private static <T> String getFailMessage(T o1, T o2, int a, int b) {
|
---|
| 116 | return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
|
---|
| 117 | .append(o2).append("\ngave: ").append(a).append("/").append(b)
|
---|
| 118 | .toString();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | private static <T> String getFailMessage(T o1, T o2, T o3, int a, int b, int c, int d) {
|
---|
| 122 | return new StringBuilder(getFailMessage(o1, o2, a, b))
|
---|
| 123 | .append("\nCompared\no1: ").append(o1).append("\no3: ").append(o3).append("\ngave: ").append(c)
|
---|
| 124 | .append("\nCompared\no2: ").append(o2).append("\no3: ").append(o3).append("\ngave: ").append(d)
|
---|
| 125 | .toString();
|
---|
| 126 | }
|
---|
[6562] | 127 | }
|
---|