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

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

Sonar - remove warnings related to the static preferences initialization in instance methods

  • 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
4
5import java.io.BufferedReader;
6import java.io.BufferedWriter;
7import java.io.File;
8import java.io.FileNotFoundException;
9import java.io.FileReader;
10import java.io.FileWriter;
11import java.io.IOException;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.HashMap;
15import java.util.HashSet;
16import java.util.LinkedHashSet;
17import java.util.List;
18import java.util.Map;
19import java.util.Map.Entry;
20import java.util.Random;
21import java.util.Set;
22
23import org.junit.BeforeClass;
24import org.junit.Test;
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.data.Bounds;
27import org.openstreetmap.josm.data.Preferences;
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<String, Projection>();
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<TestData>();
70 if (new File(PROJECTION_DATA_FILE).exists()) {
71 prevData = readData();
72 }
73 Map<String,TestData> prevCodesMap = new HashMap<String,TestData>();
74 for (TestData data : prevData) {
75 prevCodesMap.put(data.code, data);
76 }
77
78 Set<String> codesToWrite = new LinkedHashSet<String>();
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 BufferedWriter out = new BufferedWriter(new FileWriter(PROJECTION_DATA_FILE));
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 (Entry<String, Projection> e : supportedCodesMap.entrySet()) {
95 }
96 for (String code : codesToWrite) {
97 Projection proj = supportedCodesMap.get(code);
98 Bounds b = proj.getWorldBoundsLatLon();
99 double lat, lon;
100 TestData prev = prevCodesMap.get(proj.toCode());
101 if (prev != null) {
102 lat = prev.ll.lat();
103 lon = prev.ll.lon();
104 } else {
105 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
106 lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
107 }
108 EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
109 LatLon ll2 = proj.eastNorth2latlon(en);
110 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()));
111 }
112 out.close();
113 }
114
115 private static List<TestData> readData() throws IOException, FileNotFoundException {
116 BufferedReader in = new BufferedReader(new FileReader(PROJECTION_DATA_FILE));
117 List<TestData> result = new ArrayList<TestData>();
118 String line;
119 while ((line = in.readLine()) != null) {
120 if (line.startsWith("#")) {
121 continue;
122 }
123 TestData next = new TestData();
124
125 Pair<Double,Double> ll = readLine("ll", in.readLine());
126 Pair<Double,Double> en = readLine("en", in.readLine());
127 Pair<Double,Double> ll2 = readLine("ll2", in.readLine());
128
129 next.code = line;
130 next.ll = new LatLon(ll.a, ll.b);
131 next.en = new EastNorth(en.a, en.b);
132 next.ll2 = new LatLon(ll2.a, ll2.b);
133
134 result.add(next);
135 }
136 in.close();
137 return result;
138 }
139
140 private static Pair<Double,Double> readLine(String expectedName, String input) {
141 String[] fields = input.trim().split("[ ]+");
142 if (fields.length != 3) throw new AssertionError();
143 if (!fields[0].equals(expectedName)) throw new AssertionError();
144 double a = Double.parseDouble(fields[1]);
145 double b = Double.parseDouble(fields[2]);
146 return Pair.create(a, b);
147 }
148
149 @BeforeClass
150 public static void setUp() {
151 Main.initApplicationPreferences();
152 }
153
154 @Test
155 public void regressionTest() throws IOException, FileNotFoundException {
156 List<TestData> allData = readData();
157 Set<String> dataCodes = new HashSet<String>();
158 for (TestData data : allData) {
159 dataCodes.add(data.code);
160 }
161
162 StringBuilder fail = new StringBuilder();
163
164 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
165 for (String code : pc.allCodes()) {
166 if (!dataCodes.contains(code)) {
167 fail.append("Did not find projection "+code+" in test data!\n");
168 }
169 }
170 }
171
172 for (TestData data : allData) {
173 Projection proj = Projections.getProjectionByCode(data.code);
174 if (proj == null) {
175 fail.append("Projection "+data.code+" from test data was not found!\n");
176 continue;
177 }
178 EastNorth en = proj.latlon2eastNorth(data.ll);
179 if (!en.equals(data.en)) {
180 String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
181 " expected: eastnorth(%s,%s),%n" +
182 " but got: eastnorth(%s,%s)!%n",
183 proj.toString(), data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
184 fail.append(error);
185 }
186 LatLon ll2 = proj.eastNorth2latlon(data.en);
187 if (!ll2.equals(data.ll2)) {
188 String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
189 " expected: latlon(%s,%s),%n" +
190 " but got: latlon(%s,%s)!%n",
191 proj.toString(), data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
192 fail.append(error);
193 }
194 }
195
196 if (fail.length() > 0) {
197 System.err.println(fail.toString());
198 throw new AssertionError(fail.toString());
199 }
200
201 }
202}
Note: See TracBrowser for help on using the repository browser.