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

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

fix remaining checkstyle issues

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