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

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

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

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