source: josm/trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java@ 8926

Last change on this file since 8926 was 8793, checked in by Don-vip, 9 years ago

fix #11889 - adapt projection regression test to Java 9. Safe approach: keep exact comparison but duplicate data files.

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import java.io.BufferedReader;
5import java.io.BufferedWriter;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.IOException;
11import java.io.InputStreamReader;
12import java.io.OutputStreamWriter;
13import java.nio.charset.StandardCharsets;
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.HashSet;
17import java.util.List;
18import java.util.Map;
19import java.util.Random;
20import java.util.Set;
21import java.util.TreeSet;
22
23import org.junit.BeforeClass;
24import org.junit.Test;
25import org.openstreetmap.josm.JOSMFixture;
26import org.openstreetmap.josm.TestUtils;
27import org.openstreetmap.josm.data.Bounds;
28import org.openstreetmap.josm.data.coor.EastNorth;
29import org.openstreetmap.josm.data.coor.LatLon;
30import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
31import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
32import org.openstreetmap.josm.tools.Pair;
33
34/**
35 * This test is used to monitor changes in projection code.
36 *
37 * It keeps a record of test data in the file data_nodist/projection-regression-test-data.csv.
38 * This record is generated from the current Projection classes available in JOSM. It needs to
39 * be updated, whenever a projection is added / removed or an algorithm is changed, such that
40 * the computed values are numerically different. There is no error threshold, every change is reported.
41 *
42 * So when this test fails, first check if the change is intended. Then update the regression
43 * test data, by running the main method of this class and commit the new data file.
44 */
45public class ProjectionRegressionTest {
46
47 private static final String PROJECTION_DATA_FILE = "data_nodist/projection-regression-test-data.csv";
48 private static final String PROJECTION_DATA_FILE_JAVA_9 = "data_nodist/projection-regression-test-data-java9.csv";
49
50 private static class TestData {
51 public String code;
52 public LatLon ll;
53 public EastNorth en;
54 public LatLon ll2;
55 }
56
57 private static String getProjectionDataFile() {
58 return TestUtils.getJavaVersion() >= 1.9 ? PROJECTION_DATA_FILE_JAVA_9 : PROJECTION_DATA_FILE;
59 }
60
61 public static void main(String[] args) throws IOException {
62 setUp();
63
64 Map<String, Projection> supportedCodesMap = new HashMap<>();
65 for (String code : Projections.getAllProjectionCodes()) {
66 supportedCodesMap.put(code, Projections.getProjectionByCode(code));
67 }
68
69 List<TestData> prevData = new ArrayList<>();
70 if (new File(getProjectionDataFile()).exists()) {
71 prevData = readData();
72 }
73 Map<String, TestData> prevCodesMap = new HashMap<>();
74 for (TestData data : prevData) {
75 prevCodesMap.put(data.code, data);
76 }
77
78 Set<String> codesToWrite = new TreeSet<>();
79 for (TestData data : prevData) {
80 if (supportedCodesMap.containsKey(data.code)) {
81 codesToWrite.add(data.code);
82 }
83 }
84 for (String code : supportedCodesMap.keySet()) {
85 if (!codesToWrite.contains(code)) {
86 codesToWrite.add(code);
87 }
88 }
89
90 Random rand = new Random();
91 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
92 new FileOutputStream(getProjectionDataFile()), StandardCharsets.UTF_8))) {
93 out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
94 out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
95 for (String code : codesToWrite) {
96 Projection proj = supportedCodesMap.get(code);
97 Bounds b = proj.getWorldBoundsLatLon();
98 double lat, lon;
99 TestData prev = prevCodesMap.get(proj.toCode());
100 if (prev != null) {
101 lat = prev.ll.lat();
102 lon = prev.ll.lon();
103 } else {
104 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
105 lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
106 }
107 EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
108 LatLon ll2 = proj.eastNorth2latlon(en);
109 out.write(String.format(
110 "%s%n ll %s %s%n en %s %s%n ll2 %s %s%n", proj.toCode(), lat, lon, en.east(), en.north(), ll2.lat(), ll2.lon()));
111 }
112 }
113 }
114
115 private static List<TestData> readData() throws IOException, FileNotFoundException {
116 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(getProjectionDataFile()),
117 StandardCharsets.UTF_8))) {
118 List<TestData> result = new ArrayList<>();
119 String line;
120 while ((line = in.readLine()) != null) {
121 if (line.startsWith("#")) {
122 continue;
123 }
124 TestData next = new TestData();
125
126 Pair<Double, Double> ll = readLine("ll", in.readLine());
127 Pair<Double, Double> en = readLine("en", in.readLine());
128 Pair<Double, Double> ll2 = readLine("ll2", in.readLine());
129
130 next.code = line;
131 next.ll = new LatLon(ll.a, ll.b);
132 next.en = new EastNorth(en.a, en.b);
133 next.ll2 = new LatLon(ll2.a, ll2.b);
134
135 result.add(next);
136 }
137 return result;
138 }
139 }
140
141 private static Pair<Double, Double> readLine(String expectedName, String input) {
142 String[] fields = input.trim().split("[ ]+");
143 if (fields.length != 3) throw new AssertionError();
144 if (!fields[0].equals(expectedName)) throw new AssertionError();
145 double a = Double.parseDouble(fields[1]);
146 double b = Double.parseDouble(fields[2]);
147 return Pair.create(a, b);
148 }
149
150 /**
151 * Setup test.
152 */
153 @BeforeClass
154 public static void setUp() {
155 JOSMFixture.createUnitTestFixture().init();
156 }
157
158 @Test
159 public void regressionTest() throws IOException, FileNotFoundException {
160 List<TestData> allData = readData();
161 Set<String> dataCodes = new HashSet<>();
162 for (TestData data : allData) {
163 dataCodes.add(data.code);
164 }
165
166 StringBuilder fail = new StringBuilder();
167
168 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
169 for (String code : pc.allCodes()) {
170 if (!dataCodes.contains(code)) {
171 fail.append("Did not find projection "+code+" in test data!\n");
172 }
173 }
174 }
175
176 for (TestData data : allData) {
177 Projection proj = Projections.getProjectionByCode(data.code);
178 if (proj == null) {
179 fail.append("Projection "+data.code+" from test data was not found!\n");
180 continue;
181 }
182 EastNorth en = proj.latlon2eastNorth(data.ll);
183 if (!en.equals(data.en)) {
184 String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
185 " expected: eastnorth(%s,%s),%n" +
186 " but got: eastnorth(%s,%s)!%n",
187 proj.toString(), data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
188 fail.append(error);
189 }
190 LatLon ll2 = proj.eastNorth2latlon(data.en);
191 if (!ll2.equals(data.ll2)) {
192 String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
193 " expected: latlon(%s,%s),%n" +
194 " but got: latlon(%s,%s)!%n",
195 proj.toString(), data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
196 fail.append(error);
197 }
198 }
199
200 if (fail.length() > 0) {
201 System.err.println(fail.toString());
202 throw new AssertionError(fail.toString());
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.