1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNotNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 | import static org.junit.Assert.fail;
|
---|
9 |
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.FileInputStream;
|
---|
12 | import java.io.FileWriter;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.io.PrintWriter;
|
---|
15 | import java.text.MessageFormat;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Properties;
|
---|
18 | import java.util.logging.Level;
|
---|
19 | import java.util.logging.Logger;
|
---|
20 |
|
---|
21 | import org.junit.Before;
|
---|
22 | import org.junit.BeforeClass;
|
---|
23 | import org.junit.Test;
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
26 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
27 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
30 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
31 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
32 | import org.openstreetmap.josm.data.osm.Way;
|
---|
33 | import org.openstreetmap.josm.data.projection.Mercator;
|
---|
34 | import org.openstreetmap.josm.gui.io.UploadStrategy;
|
---|
35 | import org.openstreetmap.josm.gui.io.UploadStrategySpecification;
|
---|
36 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
37 |
|
---|
38 | public class MultiFetchServerObjectReaderTest {
|
---|
39 | private static Logger logger = Logger.getLogger(MultiFetchServerObjectReader.class.getName());
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * builds a large data set to be used later for testing MULTI FETCH on the server
|
---|
43 | *
|
---|
44 | * @return a large data set
|
---|
45 | */
|
---|
46 | protected static DataSet buildTestDataSet() {
|
---|
47 | DataSet ds = new DataSet();
|
---|
48 | ds.setVersion("0.6");
|
---|
49 |
|
---|
50 | int numNodes = 1000;
|
---|
51 | int numWays = 1000;
|
---|
52 | int numRelations = 1000;
|
---|
53 |
|
---|
54 | ArrayList<Node> nodes = new ArrayList<Node>();
|
---|
55 | ArrayList<Way> ways = new ArrayList<Way>();
|
---|
56 |
|
---|
57 | // create a set of nodes
|
---|
58 | //
|
---|
59 | for (int i=0; i< numNodes; i++) {
|
---|
60 | Node n = new Node();
|
---|
61 | n.setCoor(new LatLon(-36.6,47.6));
|
---|
62 | n.put("name", "node-"+i);
|
---|
63 | ds.addPrimitive(n);
|
---|
64 | nodes.add(n);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // create a set of ways, each with a random number of
|
---|
68 | // nodes
|
---|
69 | //
|
---|
70 | for (int i=0; i< numWays; i++) {
|
---|
71 | Way w = new Way();
|
---|
72 | int numNodesInWay = 2 + (int)Math.round(Math.random() * 5);
|
---|
73 | int start = (int)Math.round(Math.random() * numNodes);
|
---|
74 | for (int j = 0; j < numNodesInWay;j++) {
|
---|
75 | int idx = (start + j) % numNodes;
|
---|
76 | Node n = nodes.get(idx);
|
---|
77 | w.addNode(n);
|
---|
78 | }
|
---|
79 | w.put("name", "way-"+i);
|
---|
80 | ds.addPrimitive(w);
|
---|
81 | ways.add(w);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // create a set of relations each with a random number of nodes,
|
---|
85 | // and ways
|
---|
86 | //
|
---|
87 | for (int i=0; i< numRelations; i++) {
|
---|
88 | Relation r = new Relation();
|
---|
89 | r.put("name", "relation-" +i);
|
---|
90 | int numNodesInRelation = (int)Math.round(Math.random() * 10);
|
---|
91 | int start = (int)Math.round(Math.random() * numNodes);
|
---|
92 | for (int j = 0; j < numNodesInRelation;j++) {
|
---|
93 | int idx = (start + j) % 500;
|
---|
94 | Node n = nodes.get(idx);
|
---|
95 | r.addMember(new RelationMember("role-" + j, n));
|
---|
96 | }
|
---|
97 | int numWaysInRelation = (int)Math.round(Math.random() * 10);
|
---|
98 | start = (int)Math.round(Math.random() * numWays);
|
---|
99 | for (int j = 0; j < numWaysInRelation;j++) {
|
---|
100 | int idx = (start + j) % 500;
|
---|
101 | Way w = ways.get(idx);
|
---|
102 | r.addMember(new RelationMember("role-" + j, w));
|
---|
103 | }
|
---|
104 | ds.addPrimitive(r);
|
---|
105 | }
|
---|
106 |
|
---|
107 | return ds;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static public DataSet testDataSet;
|
---|
111 | static public Properties testProperties;
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * creates the dataset on the server.
|
---|
115 | *
|
---|
116 | * @param ds the data set
|
---|
117 | * @throws OsmTransferException
|
---|
118 | */
|
---|
119 | static public void createDataSetOnServer(DataSet ds) throws OsmTransferException {
|
---|
120 | logger.info("creating data set on the server ...");
|
---|
121 | ArrayList<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
|
---|
122 | primitives.addAll(testDataSet.getNodes());
|
---|
123 | primitives.addAll(testDataSet.getWays());
|
---|
124 | primitives.addAll(testDataSet.getRelations());
|
---|
125 |
|
---|
126 | OsmServerWriter writer = new OsmServerWriter();
|
---|
127 | Changeset cs = new Changeset();
|
---|
128 | writer.uploadOsm(new UploadStrategySpecification().setStrategy(UploadStrategy.SINGLE_REQUEST_STRATEGY), primitives,cs,NullProgressMonitor.INSTANCE);
|
---|
129 | OsmApi.getOsmApi().closeChangeset(cs, NullProgressMonitor.INSTANCE);
|
---|
130 | }
|
---|
131 |
|
---|
132 | @BeforeClass
|
---|
133 | public static void init() throws OsmTransferException {
|
---|
134 | logger.info("initializing ...");
|
---|
135 | testProperties = new Properties();
|
---|
136 |
|
---|
137 | // load properties
|
---|
138 | //
|
---|
139 | try {
|
---|
140 | testProperties.load(MultiFetchServerObjectReaderTest.class.getResourceAsStream("/test-functional-env.properties"));
|
---|
141 | } catch(Exception e){
|
---|
142 | logger.log(Level.SEVERE, MessageFormat.format("failed to load property file ''{0}''", "test-functional-env.properties"));
|
---|
143 | fail(MessageFormat.format("failed to load property file ''{0}''", "test-functional-env.properties"));
|
---|
144 | }
|
---|
145 |
|
---|
146 | // check josm.home
|
---|
147 | //
|
---|
148 | String josmHome = testProperties.getProperty("josm.home");
|
---|
149 | if (josmHome == null) {
|
---|
150 | fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
|
---|
151 | } else {
|
---|
152 | File f = new File(josmHome);
|
---|
153 | if (! f.exists() || ! f.canRead()) {
|
---|
154 | fail(MessageFormat.format("property ''{0}'' points to ''{1}'' which is either not existing or not readable", "josm.home", josmHome));
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | // check temp output dir
|
---|
159 | //
|
---|
160 | String tempOutputDir = testProperties.getProperty("test.functional.tempdir");
|
---|
161 | if (tempOutputDir == null) {
|
---|
162 | fail(MessageFormat.format("property ''{0}'' not set in test environment", "test.functional.tempdir"));
|
---|
163 | } else {
|
---|
164 | File f = new File(tempOutputDir);
|
---|
165 | if (! f.exists() || ! f.isDirectory() || ! f.canWrite()) {
|
---|
166 | fail(MessageFormat.format("property ''{0}'' points to ''{1}'' which is either not existing, not a directory, or not writeable", "test.functional.tempdir", tempOutputDir));
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | // init preferences
|
---|
172 | //
|
---|
173 | System.setProperty("josm.home", josmHome);
|
---|
174 | Main.pref.init(false);
|
---|
175 | // don't use atomic upload, the test API server can't cope with large diff uploads
|
---|
176 | //
|
---|
177 | Main.pref.put("osm-server.atomic-upload", false);
|
---|
178 | Main.proj = new Mercator();
|
---|
179 |
|
---|
180 | File dataSetCacheOutputFile = new File(tempOutputDir, MultiFetchServerObjectReaderTest.class.getName() + ".dataset");
|
---|
181 |
|
---|
182 | // make sure we don't upload to production
|
---|
183 | //
|
---|
184 | String url = OsmApi.getOsmApi().getBaseUrl().toLowerCase().trim();
|
---|
185 | if (url.startsWith("http://www.openstreetmap.org")
|
---|
186 | || url.startsWith("http://api.openstreetmap.org")) {
|
---|
187 | fail(MessageFormat.format("configured url ''{0}'' seems to be a productive url, aborting.", url));
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | String p = System.getProperties().getProperty("useCachedDataset");
|
---|
192 | if (p != null && Boolean.parseBoolean(p.trim().toLowerCase())) {
|
---|
193 | logger.info(MessageFormat.format("property ''{0}'' set, using cached dataset", "useCachedDataset"));
|
---|
194 | return;
|
---|
195 | }
|
---|
196 |
|
---|
197 | logger.info(MessageFormat.format("property ''{0}'' not set to true, creating test dataset on the server. property is ''{1}''", "useCachedDataset", p));
|
---|
198 |
|
---|
199 | // build and upload the test data set
|
---|
200 | //
|
---|
201 | logger.info("creating test data set ....");
|
---|
202 | testDataSet = buildTestDataSet();
|
---|
203 | logger.info("uploading test data set ...");
|
---|
204 | createDataSetOnServer(testDataSet);
|
---|
205 |
|
---|
206 | try {
|
---|
207 | PrintWriter pw = new PrintWriter(
|
---|
208 | new FileWriter(dataSetCacheOutputFile)
|
---|
209 | );
|
---|
210 | logger.info(MessageFormat.format("caching test data set in ''{0}'' ...", dataSetCacheOutputFile.toString()));
|
---|
211 | OsmWriter w = new OsmWriter(pw, false, testDataSet.getVersion());
|
---|
212 | w.header();
|
---|
213 | w.writeDataSources(testDataSet);
|
---|
214 | w.writeContent(testDataSet);
|
---|
215 | w.footer();
|
---|
216 | w.close();
|
---|
217 | pw.close();
|
---|
218 | } catch(IOException e) {
|
---|
219 | fail(MessageFormat.format("failed to open file ''{0}'' for writing", dataSetCacheOutputFile.toString()));
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | private DataSet ds;
|
---|
224 |
|
---|
225 |
|
---|
226 | @Before
|
---|
227 | public void setUp() throws IOException, IllegalDataException {
|
---|
228 | File f = new File(testProperties.getProperty("test.functional.tempdir"), MultiFetchServerObjectReaderTest.class.getName() + ".dataset");
|
---|
229 | logger.info(MessageFormat.format("reading cached dataset ''{0}''", f.toString()));
|
---|
230 | ds = new DataSet();
|
---|
231 | FileInputStream fis = new FileInputStream(f);
|
---|
232 | ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
233 | fis.close();
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Test
|
---|
237 | public void testMultiGet10Nodes() throws OsmTransferException {
|
---|
238 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
239 | ArrayList<Node> nodes = new ArrayList<Node>(ds.getNodes());
|
---|
240 | for (int i =0; i< 10; i++) {
|
---|
241 | reader.append(nodes.get(i));
|
---|
242 | }
|
---|
243 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
244 | assertEquals(10, out.getNodes().size());
|
---|
245 | for (Node n1:out.getNodes()) {
|
---|
246 | Node n2 = (Node)ds.getPrimitiveById(n1);
|
---|
247 | assertNotNull(n2);
|
---|
248 | assertEquals(n2.get("name"),n2.get("name"));
|
---|
249 | }
|
---|
250 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
251 | }
|
---|
252 |
|
---|
253 | @Test
|
---|
254 | public void testMultiGet10Ways() throws OsmTransferException {
|
---|
255 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
256 | ArrayList<Way> ways= new ArrayList<Way>(ds.getWays());
|
---|
257 | for (int i =0; i< 10; i++) {
|
---|
258 | reader.append(ways.get(i));
|
---|
259 | }
|
---|
260 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
261 | assertEquals(10, out.getWays().size());
|
---|
262 | for (Way w1: out.getWays()) {
|
---|
263 | Way w2 = (Way)ds.getPrimitiveById(w1);
|
---|
264 | assertNotNull(w2);
|
---|
265 | assertEquals(w2.getNodesCount(), w1.getNodesCount());
|
---|
266 | assertEquals(w2.get("name"),w1.get("name"));
|
---|
267 | }
|
---|
268 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Test
|
---|
272 | public void testMultiGet10Relations() throws OsmTransferException {
|
---|
273 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
274 | ArrayList<Relation> relations= new ArrayList<Relation>(ds.getRelations());
|
---|
275 | for (int i =0; i< 10; i++) {
|
---|
276 | reader.append(relations.get(i));
|
---|
277 | }
|
---|
278 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
279 | assertEquals(10, out.getRelations().size());
|
---|
280 | for (Relation r1: out.getRelations()) {
|
---|
281 | Relation r2 = (Relation)ds.getPrimitiveById(r1);
|
---|
282 | assertNotNull(r2);
|
---|
283 | assertEquals(r2.getMembersCount(), r1.getMembersCount());
|
---|
284 | assertEquals(r2.get("name"),r2.get("name"));
|
---|
285 | }
|
---|
286 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
287 | }
|
---|
288 |
|
---|
289 | @Test
|
---|
290 | public void testMultiGet800Nodes() throws OsmTransferException {
|
---|
291 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
292 | ArrayList<Node> nodes = new ArrayList<Node>(ds.getNodes());
|
---|
293 | for (int i =0; i< 812; i++) {
|
---|
294 | reader.append(nodes.get(i));
|
---|
295 | }
|
---|
296 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
297 | assertEquals(812, out.getNodes().size());
|
---|
298 | for (Node n1:out.getNodes()) {
|
---|
299 | Node n2 = (Node)ds.getPrimitiveById(n1);
|
---|
300 | assertNotNull(n2);
|
---|
301 | assertEquals(n2.get("name"),n2.get("name"));
|
---|
302 | }
|
---|
303 | assertTrue(reader.getMissingPrimitives().isEmpty());
|
---|
304 | }
|
---|
305 |
|
---|
306 | @Test
|
---|
307 | public void multiGetWithNonExistingNode() throws OsmTransferException {
|
---|
308 | MultiFetchServerObjectReader reader = new MultiFetchServerObjectReader();
|
---|
309 | ArrayList<Node> nodes = new ArrayList<Node>(ds.getNodes());
|
---|
310 | for (int i =0; i< 10; i++) {
|
---|
311 | reader.append(nodes.get(i));
|
---|
312 | }
|
---|
313 | Node n = new Node(9999999);
|
---|
314 | reader.append(n); // doesn't exist
|
---|
315 | DataSet out = reader.parseOsm(NullProgressMonitor.INSTANCE);
|
---|
316 | assertEquals(10, out.getNodes().size());
|
---|
317 | for (Node n1:out.getNodes()) {
|
---|
318 | Node n2 = (Node)ds.getPrimitiveById(n1);
|
---|
319 | assertNotNull(n2);
|
---|
320 | assertEquals(n2.get("name"),n2.get("name"));
|
---|
321 | }
|
---|
322 | assertFalse(reader.getMissingPrimitives().isEmpty());
|
---|
323 | assertEquals(1, reader.getMissingPrimitives().size());
|
---|
324 | assertEquals(9999999, reader.getMissingPrimitives().iterator().next().getUniqueId());
|
---|
325 | }
|
---|
326 | }
|
---|