Debian Codegraph @

test/func/files/GmapsuppTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13 /* Create date: 17-Feb-2009 */
14 package func.files;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.nio.ByteBuffer;
19 import java.nio.ByteOrder;
20 import java.util.List;
21
22 import uk.me.parabola.imgfmt.Utils;
23 import uk.me.parabola.imgfmt.fs.DirectoryEntry;
24 import uk.me.parabola.imgfmt.fs.FileSystem;
25 import uk.me.parabola.imgfmt.fs.ImgChannel;
26 import uk.me.parabola.imgfmt.mps.MapBlock;
27 import uk.me.parabola.imgfmt.mps.MpsFileReader;
28 import uk.me.parabola.imgfmt.mps.ProductBlock;
29 import uk.me.parabola.imgfmt.sys.ImgFS;
30 import uk.me.parabola.mkgmap.main.Main;
31
32 import func.Base;
33 import func.lib.Args;
34 import func.lib.Outputs;
35 import func.lib.TestUtils;
36 import org.junit.Test;
37
38 import static org.junit.Assert.*;
39
40 public class GmapsuppTest extends Base {
41 private static final String GMAPSUPP_IMG = "gmapsupp.img";
42
43 @Test
44 public void testBasic() throws IOException {
45 File f = new File(GMAPSUPP_IMG);
46 assertFalse("does not pre-exist", f.exists());
47
48 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
49 "--gmapsupp",
50 Args.TEST_RESOURCE_IMG + "63240001.img",
51 Args.TEST_RESOURCE_IMG + "63240002.img");
52
53 assertTrue("gmapsupp.img is created", f.exists());
54
55 FileSystem fs = openFs(GMAPSUPP_IMG);
56 DirectoryEntry entry = fs.lookup("63240001.TRE");
57 assertNotNull("first file TRE", entry);
58 assertEquals("first file TRE size", getFileSize(Args.TEST_RESOURCE_IMG + "63240001.img", "63240001.TRE"), entry.getSize());
59
60 entry = fs.lookup("63240002.TRE");
61 assertNotNull("second file TRE", entry);
62 assertEquals("second file TRE size", getFileSize(Args.TEST_RESOURCE_IMG + "63240002.img", "63240002.TRE"), entry.getSize());
63 }
64
65 /**
66 * Check the values inside the MPS file, when the family id etc is
67 * common to all files.
68 */
69 @Test
70 public void testMpsFile() throws IOException {
71 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
72 "--gmapsupp",
73 "--family-id=150",
74 "--product-id=24",
75 "--series-name=tst series",
76 "--family-name=tst family",
77 "--area-name=tst area",
78 Args.TEST_RESOURCE_IMG + "63240001.img",
79 Args.TEST_RESOURCE_IMG + "63240002.img");
80
81 MpsFileReader reader = getMpsFile();
82 List<MapBlock> list = reader.getMaps();
83 reader.close();
84 assertEquals("number of map blocks", 2, list.size());
85
86 // All maps will have the same parameters apart from map name here
87 int count = 0;
88 for (MapBlock map : list) {
89 assertEquals("map number", 63240001 + count++, map.getMapNumber());
90 assertEquals("family id", 150, map.getFamilyId());
91 assertEquals("product id", 24, map.getProductId());
92 assertEquals("series name", "tst series", map.getSeriesName());
93 assertEquals("area name", "tst area", map.getAreaName());
94 assertEquals("map description", "uk test " + count, map.getMapDescription());
95 }
96 }
97
98 /**
99 * Test combining gmapsupp files. The family id etc should be taken from
100 * the MPS file in the gmapsupp.
101 */
102 @Test
103 public void testCombiningSupps() throws IOException {
104 TestUtils.registerFile("g1.img", "g2.img");
105 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
106 "--gmapsupp",
107 "--family-id=150",
108 "--product-id=24",
109 "--series-name=tst series",
110 "--family-name=tst family",
111 "--area-name=tst area",
112 Args.TEST_RESOURCE_IMG + "63240001.img");
113
114 File f = new File("gmapsupp.img");
115 f.renameTo(new File("g1.img"));
116
117 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
118 "--gmapsupp",
119 "--family-id=152",
120 "--product-id=26",
121 "--series-name=tst series 2",
122 "--family-name=tst family 2",
123 "--area-name=tst area 2",
124 Args.TEST_RESOURCE_IMG + "63240002.img");
125
126 f.renameTo(new File("g2.img"));
127
128 Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--gmapsupp", "g1.img", "g2.img");
129
130
131 MpsFileReader reader = getMpsFile();
132 List<MapBlock> list = reader.getMaps();
133 assertEquals("number of map blocks", 2, list.size());
134
135 for (MapBlock map : list) {
136 if (map.getMapNumber() == 63240001) {
137 assertEquals("family id", 150, map.getFamilyId());
138 assertEquals("product id", 24, map.getProductId());
139 assertEquals("series name", "tst series", map.getSeriesName());
140 assertEquals("area name", "tst area", map.getAreaName());
141 assertEquals("hex name", 63240001, map.getHexNumber());
142 assertEquals("map description", "uk test 1", map.getMapDescription());
143 } else if (map.getMapNumber() == 63240002) {
144 assertEquals("family id", 152, map.getFamilyId());
145 assertEquals("product id", 26, map.getProductId());
146 assertEquals("series name", "tst series 2", map.getSeriesName());
147 assertEquals("area name", "tst area 2", map.getAreaName());
148 assertEquals("hex name", 63240002, map.getHexNumber());
149 assertEquals("map description", "uk test 2", map.getMapDescription());
150 } else {
151 assertTrue("Unexpected map found", false);
152 }
153 }
154 }
155
156 /**
157 * Test the case where we are combining img files with different family
158 * and product ids.
159 */
160 @Test
161 public void testDifferentFamilies() throws IOException {
162 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
163 "--gmapsupp",
164 "--family-id=101",
165 "--product-id=1",
166 "--series-name=tst series1",
167 Args.TEST_RESOURCE_IMG + "63240001.img",
168 "--family-id=102",
169 "--product-id=2",
170 "--series-name=tst series2",
171 Args.TEST_RESOURCE_IMG + "63240002.img");
172
173 MpsFileReader reader = getMpsFile();
174 List<MapBlock> list = reader.getMaps();
175 reader.close();
176 assertEquals("number of map blocks", 2, list.size());
177
178 // Directly check the family id's
179 assertEquals("family in map1", 101, list.get(0).getFamilyId());
180 assertEquals("family in map2", 102, list.get(1).getFamilyId());
181
182 // Check more things
183 int count = 0;
184 for (MapBlock map : list) {
185 count++;
186 assertEquals("family in map" + count, 100 + count, map.getFamilyId());
187 assertEquals("product in map" + count, count, map.getProductId());
188 assertEquals("series name in map" + count, "tst series" + count, map.getSeriesName());
189 }
190 }
191
192 /**
193 * The mps file has a block for each family/product in the map set.
194 */
195 @Test
196 public void testProductBlocks() throws IOException {
197 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
198 "--gmapsupp",
199 "--family-id=101",
200 "--product-id=1",
201 "--family-name=tst family1",
202 "--series-name=tst series1",
203 Args.TEST_RESOURCE_IMG + "63240001.img",
204 "--family-id=102",
205 "--product-id=2",
206 "--family-name=tst family2",
207 "--series-name=tst series2",
208 Args.TEST_RESOURCE_IMG + "63240002.img");
209
210 MpsFileReader reader = getMpsFile();
211
212 List<ProductBlock> products = reader.getProducts();
213 products.sort((o1, o2) -> {
214 if (o1.getFamilyId() == o2.getFamilyId())
215 return 0;
216 else if (o1.getFamilyId() > o2.getFamilyId())
217 return 1;
218 else return -1;
219 });
220
221 ProductBlock block = products.get(0);
222 assertEquals("product block first family", 101, block.getFamilyId());
223 assertEquals("product block first product id", 1, block.getProductId());
224 assertEquals("product block first family name", "tst family1", block.getDescription());
225
226 block = products.get(1);
227 assertEquals("product block second family", 102, block.getFamilyId());
228 assertEquals("product block first product id", 2, block.getProductId());
229 assertEquals("product block first family name", "tst family2", block.getDescription());
230 }
231
232 /**
233 * Make sure that if we have multiple maps in the same family, which after
234 * all is the common case, that we only get one product block.
235 */
236 @Test
237 public void testProductWithSeveralMaps() throws IOException {
238 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
239 "--gmapsupp",
240 "--family-id=101",
241 "--product-id=1",
242 "--family-name=tst family1",
243 "--series-name=tst series1",
244 Args.TEST_RESOURCE_IMG + "63240001.img",
245 Args.TEST_RESOURCE_IMG + "63240002.img");
246
247 MpsFileReader reader = getMpsFile();
248 assertEquals("number of map blocks", 2, reader.getMaps().size());
249 assertEquals("number of product blocks", 1, reader.getProducts().size());
250 }
251
252 @Test
253 public void testWithIndex() throws IOException {
254 new File("osmmap_mdr.img").delete();
255 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
256 "--gmapsupp",
257 "--index",
258 "--latin1",
259 "--family-id=101",
260 "--product-id=1",
261 "--family-name=tst family1",
262 "--series-name=tst series1",
263 Args.TEST_RESOURCE_IMG + "63240001.img",
264 Args.TEST_RESOURCE_IMG + "63240002.img");
265
266 assertFalse(new File("osmmap_mdr.img").exists());
267
268 // All we are doing here is checking that the file was created and that it is
269 // not completely empty.
270 FileSystem fs = openFs(GMAPSUPP_IMG);
271 ImgChannel r = fs.open("00000101.MDR", "r");
272 r.position(2);
273 ByteBuffer buf = ByteBuffer.allocate(1024);
274
275 int read = r.read(buf);
276 assertEquals(1024, read);
277
278 buf.flip();
279 byte[] b = new byte[3];
280 buf.get(b, 0, 3);
281 assertEquals('G', b[0]);
282 }
283
284 @Test
285 public void testWithTwoIndexes() throws IOException {
286 TestUtils.registerFile("osmmap_mdr.img", "osmmap.img", "osmmap.tbd", "osmmap.mdx");
287
288 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
289 "--gmapsupp",
290 "--index",
291 "--tdbfile",
292 "--latin1",
293 "--family-id=101",
294 "--product-id=1",
295 "--family-name=tst family1",
296 "--series-name=tst series1",
297 Args.TEST_RESOURCE_IMG + "63240001.img",
298 Args.TEST_RESOURCE_IMG + "63240002.img");
299
300 assertTrue(new File("osmmap_mdr.img").exists());
301
302 // All we are doing here is checking that the file was created and that it is
303 // not completely empty.
304 FileSystem fs = openFs(GMAPSUPP_IMG);
305 ImgChannel r = fs.open("00000101.MDR", "r");
306 r.position(2);
307 ByteBuffer buf = ByteBuffer.allocate(1024);
308
309 int read = r.read(buf);
310 assertEquals(1024, read);
311
312 buf.flip();
313 byte[] b = new byte[3];
314 buf.get(b, 0, 3);
315 assertEquals('G', b[0]);
316 }
317
318 /**
319 * If there are files in two (or more) families then there should be a MDR and SRT for each.
320 */
321 @Test
322 public void testTwoFamilyIndex() throws IOException {
323 TestUtils.registerFile("osmmap_mdr.img", "osmmap.img", "osmmap.tbd", "osmmap.mdx");
324
325 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
326 "--gmapsupp",
327 "--index",
328 "--latin1",
329 "--family-id=101",
330 "--product-id=1",
331 "--family-name=tst family1",
332 "--series-name=tst series1",
333 Args.TEST_RESOURCE_OSM + "uk-test-1.osm.gz",
334 "--family-id=202",
335 "--family-name=tst family2",
336 "--series-name=tst series2",
337 Args.TEST_RESOURCE_OSM + "uk-test-2.osm.gz");
338
339 assertFalse(new File("osmmap_mdr.img").exists());
340
341 // All we are doing here is checking that the file was created and that it is
342 // not completely empty.
343 FileSystem fs = openFs(GMAPSUPP_IMG);
344 ImgChannel r = fs.open("00000101.MDR", "r");
345 r.position(2);
346 ByteBuffer buf = ByteBuffer.allocate(1024);
347 int read = r.read(buf);
348 assertEquals(1024, read);
349
350 fs = openFs(GMAPSUPP_IMG);
351 r = fs.open("00000202.MDR", "r");
352 r.position(2);
353 buf.clear();
354 read = r.read(buf);
355 assertEquals(1024, read);
356
357 r = fs.open("00000202.SRT", "r");
358 buf = ByteBuffer.allocate(512);
359 read = r.read(buf);
360 assertEquals(512, read);
361 r = fs.open("00000101.SRT", "r");
362 buf.clear();
363 read = r.read(buf);
364 assertEquals(512, read);
365 }
366
367 /**
368 * If no code page is given for the index, it is taken from the input files.
369 */
370 @Test
371 public void testImplicitCodePageIndex() throws IOException {
372 TestUtils.registerFile("osmmap_mdr.img", "osmmap.img", "osmmap.tbd", "osmmap.mdx");
373
374 Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--code-page=1256",
375 Args.TEST_RESOURCE_OSM + "uk-test-1.osm.gz");
376
377 Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--gmapsupp", "--index", "63240001.img");
378
379 assertFalse(new File("osmmap_mdr.img").exists());
380
381 FileSystem fs = openFs(GMAPSUPP_IMG);
382 ImgChannel r = fs.open("00006324.MDR", "r");
383
384 ByteBuffer buf = ByteBuffer.allocate(1024);
385 buf.order(ByteOrder.LITTLE_ENDIAN);
386
387 r.read(buf);
388 assertEquals(1256, buf.getChar(0x15));
389 assertEquals(0x020009, buf.getInt(0x17));
390 }
391
392 /**
393 * If there are mis-matching code-pages in the input files there should be a warning.
394 */
395 @Test
396 public void testWarningOnMismatchedCodePages() {
397 TestUtils.registerFile("osmmap.img");
398
399 Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--route", "--code-page=1256",
400 Args.TEST_RESOURCE_OSM + "uk-test-1.osm.gz",
401 "--latin1",
402 Args.TEST_RESOURCE_OSM + "uk-test-2.osm.gz");
403
404 Outputs outputs = TestUtils.runAsProcess(Args.TEST_STYLE_ARG,
405 "--gmapsupp",
406 "--index",
407
408 "63240001.img",
409 "63240002.img"
410 );
411
412 outputs.checkError("different code page");
413 }
414
415 @Test
416 public void testWithTypFile() throws IOException {
417 File f = new File(GMAPSUPP_IMG);
418 assertFalse("does not pre-exist", f.exists());
419
420 Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
421 "--gmapsupp",
422 Args.TEST_RESOURCE_IMG + "63240001.img",
423 Args.TEST_RESOURCE_TYP + "test.txt");
424
425 assertTrue("gmapsupp.img is created", f.exists());
426
427 FileSystem fs = openFs(GMAPSUPP_IMG);
428 DirectoryEntry entry = fs.lookup("63240001.TRE");
429 assertNotNull("first file TRE", entry);
430 assertEquals("first file TRE size", getFileSize(Args.TEST_RESOURCE_IMG + "63240001.img", "63240001.TRE"), entry.getSize());
431
432 entry = fs.lookup("0000TEST.TYP");
433 assertNotNull("contains typ file", entry);
434 }
435
436 private MpsFileReader getMpsFile() throws IOException {
437 FileSystem fs = openFs(GMAPSUPP_IMG);
438 MpsFileReader reader = new MpsFileReader(fs.open("MAKEGMAP.MPS", "r"), 0);
439 TestUtils.registerFile(reader);
440 return reader;
441 }
442
443 private int getFileSize(String imgName, String fileName) throws IOException {
444 FileSystem fs = ImgFS.openFs(imgName);
445 try {
446 return fs.lookup(fileName).getSize();
447 } finally {
448 Utils.closeFile(fs);
449 }
450 }
451 }