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

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

fix #11512 - update unit test

  • Property svn:eol-style set to native
File size: 7.9 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(new FileOutputStream(PROJECTION_DATA_FILE), StandardCharsets.UTF_8))) {
86 out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
87 out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
88 for (String code : codesToWrite) {
89 Projection proj = supportedCodesMap.get(code);
90 Bounds b = proj.getWorldBoundsLatLon();
91 double lat, lon;
92 TestData prev = prevCodesMap.get(proj.toCode());
93 if (prev != null) {
94 lat = prev.ll.lat();
95 lon = prev.ll.lon();
96 } else {
97 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
98 lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
99 }
100 EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
101 LatLon ll2 = proj.eastNorth2latlon(en);
102 out.write(String.format("%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()));
103 }
104 }
105 }
106
107 private static List<TestData> readData() throws IOException, FileNotFoundException {
108 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(PROJECTION_DATA_FILE), StandardCharsets.UTF_8))) {
109 List<TestData> result = new ArrayList<>();
110 String line;
111 while ((line = in.readLine()) != null) {
112 if (line.startsWith("#")) {
113 continue;
114 }
115 TestData next = new TestData();
116
117 Pair<Double,Double> ll = readLine("ll", in.readLine());
118 Pair<Double,Double> en = readLine("en", in.readLine());
119 Pair<Double,Double> ll2 = readLine("ll2", in.readLine());
120
121 next.code = line;
122 next.ll = new LatLon(ll.a, ll.b);
123 next.en = new EastNorth(en.a, en.b);
124 next.ll2 = new LatLon(ll2.a, ll2.b);
125
126 result.add(next);
127 }
128 return result;
129 }
130 }
131
132 private static Pair<Double,Double> readLine(String expectedName, String input) {
133 String[] fields = input.trim().split("[ ]+");
134 if (fields.length != 3) throw new AssertionError();
135 if (!fields[0].equals(expectedName)) throw new AssertionError();
136 double a = Double.parseDouble(fields[1]);
137 double b = Double.parseDouble(fields[2]);
138 return Pair.create(a, b);
139 }
140
141 /**
142 * Setup test.
143 */
144 @BeforeClass
145 public static void setUp() {
146 JOSMFixture.createUnitTestFixture().init();
147 }
148
149 @Test
150 public void regressionTest() throws IOException, FileNotFoundException {
151 List<TestData> allData = readData();
152 Set<String> dataCodes = new HashSet<>();
153 for (TestData data : allData) {
154 dataCodes.add(data.code);
155 }
156
157 StringBuilder fail = new StringBuilder();
158
159 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
160 for (String code : pc.allCodes()) {
161 if (!dataCodes.contains(code)) {
162 fail.append("Did not find projection "+code+" in test data!\n");
163 }
164 }
165 }
166
167 for (TestData data : allData) {
168 Projection proj = Projections.getProjectionByCode(data.code);
169 if (proj == null) {
170 fail.append("Projection "+data.code+" from test data was not found!\n");
171 continue;
172 }
173 EastNorth en = proj.latlon2eastNorth(data.ll);
174 if (!en.equals(data.en)) {
175 String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
176 " expected: eastnorth(%s,%s),%n" +
177 " but got: eastnorth(%s,%s)!%n",
178 proj.toString(), data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
179 fail.append(error);
180 }
181 LatLon ll2 = proj.eastNorth2latlon(data.en);
182 if (!ll2.equals(data.ll2)) {
183 String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
184 " expected: latlon(%s,%s),%n" +
185 " but got: latlon(%s,%s)!%n",
186 proj.toString(), data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
187 fail.append(error);
188 }
189 }
190
191 if (fail.length() > 0) {
192 System.err.println(fail.toString());
193 throw new AssertionError(fail.toString());
194 }
195 }
196}
Note: See TracBrowser for help on using the repository browser.