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

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

javadoc/code style/minor refactorization

  • 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.FileNotFoundException;
8import java.io.FileReader;
9import java.io.FileWriter;
10import java.io.IOException;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.LinkedHashSet;
16import java.util.List;
17import java.util.Map;
18import java.util.Map.Entry;
19import java.util.Random;
20import java.util.Set;
21
22import org.junit.BeforeClass;
23import org.junit.Test;
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.data.Bounds;
26import org.openstreetmap.josm.data.coor.EastNorth;
27import org.openstreetmap.josm.data.coor.LatLon;
28import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
29import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
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-regression-test-data.csv.
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 public static final String PROJECTION_DATA_FILE = "data_nodist/projection-regression-test-data.csv";
46
47 private static class TestData {
48 public String code;
49 public LatLon ll;
50 public EastNorth en;
51 public LatLon ll2;
52 }
53
54 public static void main(String[] args) throws IOException, FileNotFoundException {
55 setUp();
56
57 Map<String, Projection> supportedCodesMap = new HashMap<String, Projection>();
58 for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
59 for (String code : pc.allCodes()) {
60 Collection<String> pref = pc.getPreferencesFromCode(code);
61 pc.setPreferences(pref);
62 Projection p = pc.getProjection();
63 supportedCodesMap.put(code, p);
64 }
65 }
66
67 List<TestData> prevData = new ArrayList<TestData>();
68 if (new File(PROJECTION_DATA_FILE).exists()) {
69 prevData = readData();
70 }
71 Map<String,TestData> prevCodesMap = new HashMap<String,TestData>();
72 for (TestData data : prevData) {
73 prevCodesMap.put(data.code, data);
74 }
75
76 Set<String> codesToWrite = new LinkedHashSet<String>();
77 for (TestData data : prevData) {
78 if (supportedCodesMap.containsKey(data.code)) {
79 codesToWrite.add(data.code);
80 }
81 }
82 for (String code : supportedCodesMap.keySet()) {
83 if (!codesToWrite.contains(code)) {
84 codesToWrite.add(code);
85 }
86 }
87
88 Random rand = new Random();
89 BufferedWriter out = new BufferedWriter(new FileWriter(PROJECTION_DATA_FILE));
90 out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
91 out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
92 for (Entry<String, Projection> e : supportedCodesMap.entrySet()) {
93 }
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 out.close();
111 }
112
113 private static List<TestData> readData() throws IOException, FileNotFoundException {
114 BufferedReader in = new BufferedReader(new FileReader(PROJECTION_DATA_FILE));
115 List<TestData> result = new ArrayList<TestData>();
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 in.close();
135 return result;
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 Main.initApplicationPreferences();
153 }
154
155 @Test
156 public void regressionTest() throws IOException, FileNotFoundException {
157 List<TestData> allData = readData();
158 Set<String> dataCodes = new HashSet<String>();
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.