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

Last change on this file since 9908 was 9908, checked in by Don-vip, 8 years ago

see #12186, #11924 - update projection regression test file for Java 9

generated on Windows with:

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