File: GmapsuppTest.java

package info (click to toggle)
mkgmap 0.0.0%2Bsvn4923-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,068 kB
  • sloc: java: 73,955; xml: 1,719; python: 713; sh: 240; makefile: 147; perl: 31
file content (451 lines) | stat: -rw-r--r-- 14,056 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * Copyright (C) 2008 Steve Ratcliffe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 or
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
/* Create date: 17-Feb-2009 */
package func.files;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;

import uk.me.parabola.imgfmt.Utils;
import uk.me.parabola.imgfmt.fs.DirectoryEntry;
import uk.me.parabola.imgfmt.fs.FileSystem;
import uk.me.parabola.imgfmt.fs.ImgChannel;
import uk.me.parabola.imgfmt.mps.MapBlock;
import uk.me.parabola.imgfmt.mps.MpsFileReader;
import uk.me.parabola.imgfmt.mps.ProductBlock;
import uk.me.parabola.imgfmt.sys.ImgFS;
import uk.me.parabola.mkgmap.main.Main;

import func.Base;
import func.lib.Args;
import func.lib.Outputs;
import func.lib.TestUtils;
import org.junit.Test;

import static org.junit.Assert.*;

public class GmapsuppTest extends Base {
	private static final String GMAPSUPP_IMG = "gmapsupp.img";

	@Test
	public void testBasic() throws IOException {
		File f = new File(GMAPSUPP_IMG);
		assertFalse("does not pre-exist", f.exists());

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		assertTrue("gmapsupp.img is created", f.exists());

		FileSystem fs = openFs(GMAPSUPP_IMG);
		DirectoryEntry entry = fs.lookup("63240001.TRE");
		assertNotNull("first file TRE", entry);
		assertEquals("first file TRE size", getFileSize(Args.TEST_RESOURCE_IMG + "63240001.img", "63240001.TRE"), entry.getSize());

		entry = fs.lookup("63240002.TRE");
		assertNotNull("second file TRE", entry);
		assertEquals("second file TRE size", getFileSize(Args.TEST_RESOURCE_IMG + "63240002.img", "63240002.TRE"), entry.getSize());
	}

	/**
	 * Check the values inside the MPS file, when the family id etc is
	 * common to all files.
	 */
	@Test
	public void testMpsFile() throws IOException {
		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--family-id=150",
				"--product-id=24",
				"--series-name=tst series",
				"--family-name=tst family",
				"--area-name=tst area",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		MpsFileReader reader = getMpsFile();
		List<MapBlock> list = reader.getMaps();
		reader.close();
		assertEquals("number of map blocks", 2, list.size());

		// All maps will have the same parameters apart from map name here
		int count = 0;
		for (MapBlock map : list) {
			assertEquals("map number", 63240001 + count++, map.getMapNumber());
			assertEquals("family id", 150, map.getFamilyId());
			assertEquals("product id", 24, map.getProductId());
			assertEquals("series name", "tst series", map.getSeriesName());
			assertEquals("area name", "tst area", map.getAreaName());
			assertEquals("map description", "uk test " + count, map.getMapDescription());
		}
	}

	/**
	 * Test combining gmapsupp files.  The family id etc should be taken from
	 * the MPS file in the gmapsupp.
	 */
	@Test
	public void testCombiningSupps() throws IOException {
		TestUtils.registerFile("g1.img", "g2.img");
		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--family-id=150",
				"--product-id=24",
				"--series-name=tst series",
				"--family-name=tst family",
				"--area-name=tst area",
				Args.TEST_RESOURCE_IMG + "63240001.img");

		File f = new File("gmapsupp.img");
		f.renameTo(new File("g1.img"));

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--family-id=152",
				"--product-id=26",
				"--series-name=tst series 2",
				"--family-name=tst family 2",
				"--area-name=tst area 2",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		f.renameTo(new File("g2.img"));

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--gmapsupp", "g1.img", "g2.img");


		MpsFileReader reader = getMpsFile();
		List<MapBlock> list = reader.getMaps();
		assertEquals("number of map blocks", 2, list.size());

		for (MapBlock map : list) {
			if (map.getMapNumber() == 63240001) {
				assertEquals("family id", 150, map.getFamilyId());
				assertEquals("product id", 24, map.getProductId());
				assertEquals("series name", "tst series", map.getSeriesName());
				assertEquals("area name", "tst area", map.getAreaName());
				assertEquals("hex name", 63240001, map.getHexNumber());
				assertEquals("map description", "uk test 1", map.getMapDescription());
			} else if (map.getMapNumber() == 63240002) {
				assertEquals("family id", 152, map.getFamilyId());
				assertEquals("product id", 26, map.getProductId());
				assertEquals("series name", "tst series 2", map.getSeriesName());
				assertEquals("area name", "tst area 2", map.getAreaName());
				assertEquals("hex name", 63240002, map.getHexNumber());
				assertEquals("map description", "uk test 2", map.getMapDescription());
			} else {
				assertTrue("Unexpected map found", false);
			}
		}
	}

	/**
	 * Test the case where we are combining img files with different family
	 * and product ids.
	 */
	@Test
	public void testDifferentFamilies() throws IOException {
		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--family-id=101",
				"--product-id=1",
				"--series-name=tst series1",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				"--family-id=102",
				"--product-id=2",
				"--series-name=tst series2",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		MpsFileReader reader = getMpsFile();
		List<MapBlock> list = reader.getMaps();
		reader.close();
		assertEquals("number of map blocks", 2, list.size());

		// Directly check the family id's
		assertEquals("family in map1", 101, list.get(0).getFamilyId());
		assertEquals("family in map2", 102, list.get(1).getFamilyId());

		// Check more things
		int count = 0;
		for (MapBlock map : list) {
			count++;
			assertEquals("family in map" + count, 100 + count, map.getFamilyId());
			assertEquals("product in map" + count, count, map.getProductId());
			assertEquals("series name in map" + count, "tst series" + count, map.getSeriesName());
		}
	}

	/**
	 * The mps file has a block for each family/product in the map set.
	 */
	@Test
	public void testProductBlocks() throws IOException {
		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--family-id=101",
				"--product-id=1",
				"--family-name=tst family1",
				"--series-name=tst series1",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				"--family-id=102",
				"--product-id=2",
				"--family-name=tst family2",
				"--series-name=tst series2",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		MpsFileReader reader = getMpsFile();

		List<ProductBlock> products = reader.getProducts();
		products.sort((o1, o2) -> {
			if (o1.getFamilyId() == o2.getFamilyId())
				return 0;
			else if (o1.getFamilyId() > o2.getFamilyId())
				return 1;
			else return -1;
		});

		ProductBlock block = products.get(0);
		assertEquals("product block first family", 101, block.getFamilyId());
		assertEquals("product block first product id", 1, block.getProductId());
		assertEquals("product block first family name", "tst family1", block.getDescription());
		
		block = products.get(1);
		assertEquals("product block second family", 102, block.getFamilyId());
		assertEquals("product block first product id", 2, block.getProductId());
		assertEquals("product block first family name", "tst family2", block.getDescription());
	}

	/**
	 * Make sure that if we have multiple maps in the same family, which after
	 * all is the common case, that we only get one product block.
	 */
	@Test
	public void testProductWithSeveralMaps() throws IOException {
		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--family-id=101",
				"--product-id=1",
				"--family-name=tst family1",
				"--series-name=tst series1",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		MpsFileReader reader = getMpsFile();
		assertEquals("number of map blocks", 2, reader.getMaps().size());
		assertEquals("number of product blocks", 1, reader.getProducts().size());
	}

	@Test
	public void testWithIndex() throws IOException {
		new File("osmmap_mdr.img").delete();
		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--index",
				"--latin1",
				"--family-id=101",
				"--product-id=1",
				"--family-name=tst family1",
				"--series-name=tst series1",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		assertFalse(new File("osmmap_mdr.img").exists());

		// All we are doing here is checking that the file was created and that it is
		// not completely empty.
		FileSystem fs = openFs(GMAPSUPP_IMG);
		ImgChannel r = fs.open("00000101.MDR", "r");
		r.position(2);
		ByteBuffer buf = ByteBuffer.allocate(1024);
		
		int read = r.read(buf);
		assertEquals(1024, read);

		buf.flip();
		byte[] b = new byte[3];
		buf.get(b, 0, 3);
		assertEquals('G', b[0]);
	}

	@Test
	public void testWithTwoIndexes() throws IOException {
		TestUtils.registerFile("osmmap_mdr.img", "osmmap.img", "osmmap.tbd", "osmmap.mdx");

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--index",
				"--tdbfile",
				"--latin1",
				"--family-id=101",
				"--product-id=1",
				"--family-name=tst family1",
				"--series-name=tst series1",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				Args.TEST_RESOURCE_IMG + "63240002.img");

		assertTrue(new File("osmmap_mdr.img").exists());

		// All we are doing here is checking that the file was created and that it is
		// not completely empty.
		FileSystem fs = openFs(GMAPSUPP_IMG);
		ImgChannel r = fs.open("00000101.MDR", "r");
		r.position(2);
		ByteBuffer buf = ByteBuffer.allocate(1024);

		int read = r.read(buf);
		assertEquals(1024, read);

		buf.flip();
		byte[] b = new byte[3];
		buf.get(b, 0, 3);
		assertEquals('G', b[0]);
	}

	/**
	 * If there are files in two (or more) families then there should be a MDR and SRT for each.
	 */
	@Test
	public void testTwoFamilyIndex() throws IOException {
		TestUtils.registerFile("osmmap_mdr.img", "osmmap.img", "osmmap.tbd", "osmmap.mdx");

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--index",
				"--latin1",
				"--family-id=101",
				"--product-id=1",
				"--family-name=tst family1",
				"--series-name=tst series1",
				Args.TEST_RESOURCE_OSM + "uk-test-1.osm.gz",
				"--family-id=202",
				"--family-name=tst family2",
				"--series-name=tst series2",
				Args.TEST_RESOURCE_OSM + "uk-test-2.osm.gz");

		assertFalse(new File("osmmap_mdr.img").exists());

		// All we are doing here is checking that the file was created and that it is
		// not completely empty.
		FileSystem fs = openFs(GMAPSUPP_IMG);
		ImgChannel r = fs.open("00000101.MDR", "r");
		r.position(2);
		ByteBuffer buf = ByteBuffer.allocate(1024);
		int read = r.read(buf);
		assertEquals(1024, read);

		fs = openFs(GMAPSUPP_IMG);
		r = fs.open("00000202.MDR", "r");
		r.position(2);
		buf.clear();
		read = r.read(buf);
		assertEquals(1024, read);

		r = fs.open("00000202.SRT", "r");
		buf = ByteBuffer.allocate(512);
		read = r.read(buf);
		assertEquals(512, read);
		r = fs.open("00000101.SRT", "r");
		buf.clear();
		read = r.read(buf);
		assertEquals(512, read);
	}

	/**
	 * If no code page is given for the index, it is taken from the input files.
	 */
	@Test
	public void testImplicitCodePageIndex() throws IOException {
		TestUtils.registerFile("osmmap_mdr.img", "osmmap.img", "osmmap.tbd", "osmmap.mdx");

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--code-page=1256",
				Args.TEST_RESOURCE_OSM + "uk-test-1.osm.gz");

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--gmapsupp", "--index", "63240001.img");

		assertFalse(new File("osmmap_mdr.img").exists());

		FileSystem fs = openFs(GMAPSUPP_IMG);
		ImgChannel r = fs.open("00006324.MDR", "r");

		ByteBuffer buf = ByteBuffer.allocate(1024);
		buf.order(ByteOrder.LITTLE_ENDIAN);

		r.read(buf);
		assertEquals(1256, buf.getChar(0x15));
		assertEquals(0x020009, buf.getInt(0x17));
	}

	/**
	 * If there are mis-matching code-pages in the input files there should be a warning.
	 */
	@Test
	public void testWarningOnMismatchedCodePages() {
		TestUtils.registerFile("osmmap.img");

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG, "--route", "--code-page=1256",
				Args.TEST_RESOURCE_OSM + "uk-test-1.osm.gz",
				"--latin1",
				Args.TEST_RESOURCE_OSM + "uk-test-2.osm.gz");

		Outputs outputs = TestUtils.runAsProcess(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				"--index",

				"63240001.img",
				"63240002.img"
		);

		outputs.checkError("different code page");
	}

	@Test
	public void testWithTypFile() throws IOException {
		File f = new File(GMAPSUPP_IMG);
		assertFalse("does not pre-exist", f.exists());

		Main.mainNoSystemExit(Args.TEST_STYLE_ARG,
				"--gmapsupp",
				Args.TEST_RESOURCE_IMG + "63240001.img",
				Args.TEST_RESOURCE_TYP + "test.txt");

		assertTrue("gmapsupp.img is created", f.exists());

		FileSystem fs = openFs(GMAPSUPP_IMG);
		DirectoryEntry entry = fs.lookup("63240001.TRE");
		assertNotNull("first file TRE", entry);
		assertEquals("first file TRE size", getFileSize(Args.TEST_RESOURCE_IMG + "63240001.img", "63240001.TRE"), entry.getSize());

		entry = fs.lookup("0000TEST.TYP");
		assertNotNull("contains typ file", entry);
	}

	private MpsFileReader getMpsFile() throws IOException {
		FileSystem fs = openFs(GMAPSUPP_IMG);
		MpsFileReader reader = new MpsFileReader(fs.open("MAKEGMAP.MPS", "r"), 0);
		TestUtils.registerFile(reader);
		return reader;
	}

	private int getFileSize(String imgName, String fileName) throws IOException {
		FileSystem fs = ImgFS.openFs(imgName);
		try {
			return fs.lookup(fileName).getSize();
		} finally {
			Utils.closeFile(fs);
		}
	}
}