Browse code

add unit test

devnewton authored on 23/01/2014 at 10:27:48
Showing 5 changed files
... ...
@@ -1,3 +1,5 @@
1 1
 libtiled-jpurexml is a java library to parse [Tiled](http://www.mapeditor.org) tmx maps.
2 2
 
3
-It use [jpurexml](https://github.com/asilvestre/jpurexml), a pure java xml parser in order to be usable with [gwt](http://www.gwtproject.org/) compiler.
4 3
\ No newline at end of file
4
+It use [jpurexml](https://github.com/asilvestre/jpurexml), a pure java xml parser in order to be usable with [gwt](http://www.gwtproject.org/) compiler.
5
+
6
+Usage sample: [TestTmxLoader.java](src/test/java/im/bci/test/TestTmxLoader.java)
5 7
\ No newline at end of file
... ...
@@ -63,6 +63,12 @@ THE SOFTWARE. -->
63 63
             <artifactId>jpurexml</artifactId>
64 64
             <version>0.10</version>
65 65
         </dependency>
66
+        <dependency>
67
+            <groupId>junit</groupId>
68
+            <artifactId>junit</artifactId>
69
+            <version>4.10</version>
70
+            <scope>test</scope>
71
+        </dependency>
66 72
     </dependencies>
67 73
     <build>
68 74
         <resources>
69 75
new file mode 100644
... ...
@@ -0,0 +1,81 @@
0
+/*
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 devnewton <devnewton@bci.im>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+ */
23
+package im.bci.test;
24
+
25
+import im.bci.tmxloader.TmxLoader;
26
+import im.bci.tmxloader.TmxMap;
27
+import im.bci.tmxloader.TmxTile;
28
+import im.bci.tmxloader.TmxTileset;
29
+import java.io.File;
30
+import java.io.FileInputStream;
31
+import java.io.IOException;
32
+import java.io.InputStream;
33
+import java.util.Scanner;
34
+import org.junit.Test;
35
+import org.junit.Assert;
36
+
37
+/**
38
+ *
39
+ * @author devnewton
40
+ */
41
+public class TestTmxLoader {
42
+
43
+    @Test
44
+    public void simpleTmx() throws IOException {
45
+        final File mapFile = new File("test-data/desert.tmx");
46
+        final File mapParentDir = mapFile.getParentFile().getCanonicalFile();
47
+        TmxLoader loader = new TmxLoader();
48
+        TmxMap map = new TmxMap();
49
+        loader.parseTmx(map, loadText(mapFile));
50
+        for (TmxTileset tileset : map.getTilesets()) {
51
+            File tilesetParentDir;
52
+            if (null != tileset.getSource()) {
53
+                final File tilesetFile = new File(mapParentDir, tileset.getSource());
54
+                tilesetParentDir = tilesetFile.getParentFile().getCanonicalFile();
55
+                loader.parseTsx(map, tileset, loadText(tilesetFile));
56
+            } else {
57
+                tilesetParentDir = mapParentDir;
58
+            }
59
+            if (null != tileset.getImage()) {
60
+                tileset.getImage().setSource(new File(tilesetParentDir, tileset.getImage().getSource()).getCanonicalPath());
61
+            }
62
+            for (TmxTile tile : tileset.getTiles()) {
63
+                tile.getFrame().getImage().setSource(new File(tilesetParentDir, tile.getFrame().getImage().getSource()).getCanonicalPath());
64
+            }
65
+        }
66
+        loader.decode(map);
67
+
68
+        Assert.assertEquals(40, map.getWidth());
69
+        Assert.assertEquals(40, map.getHeight());
70
+        Assert.assertEquals(32, map.getTilewidth());
71
+        Assert.assertEquals(32, map.getTileheight());
72
+        Assert.assertEquals(1, map.getLayers().size());
73
+    }
74
+
75
+    private String loadText(File f) throws IOException {
76
+        try (InputStream is = new FileInputStream(f); Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\Z")) {
77
+            return s.next();
78
+        }
79
+    }
80
+}
0 81
new file mode 100644
... ...
@@ -0,0 +1,50 @@
0
+<?xml version="1.0" encoding="UTF-8"?>
1
+<map version="1.0" orientation="orthogonal" width="40" height="40" tilewidth="32" tileheight="32">
2
+ <tileset firstgid="1" name="Desert" tilewidth="32" tileheight="32" spacing="1" margin="1">
3
+  <image source="tmw_desert_spacing.png" width="265" height="199"/>
4
+ </tileset>
5
+ <layer name="Ground" width="40" height="40">
6
+  <data encoding="csv">
7
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,14,15,16,30,30,30,30,30,30,30,30,30,30,30,30,30,
8
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,46,14,15,16,30,31,30,30,30,30,30,30,30,30,30,30,30,
9
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,14,15,16,30,30,30,30,30,30,30,30,30,30,30,30,30,
10
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,31,22,23,24,30,30,30,30,30,30,30,30,30,30,30,30,30,
11
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,1,2,3,30,30,31,30,30,30,30,30,30,30,30,30,30,
12
+30,30,30,30,30,30,30,30,30,30,30,25,26,26,26,26,26,26,26,26,26,26,26,27,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
13
+30,30,30,30,30,30,30,30,30,30,30,33,34,34,34,34,34,34,34,34,34,34,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
14
+30,30,30,30,30,30,40,30,30,30,30,33,34,36,42,37,34,34,34,34,34,34,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
15
+30,30,30,30,30,30,30,30,30,30,30,33,34,35,30,33,34,34,34,34,34,34,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
16
+30,30,30,30,30,30,30,30,38,30,30,33,34,35,30,33,34,34,34,34,34,34,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
17
+30,30,30,30,30,48,38,30,30,30,30,33,34,44,26,45,34,34,34,34,34,34,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
18
+30,30,30,40,30,30,30,40,30,30,30,33,34,34,34,34,34,34,34,36,42,37,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
19
+40,30,30,30,30,40,38,30,30,38,30,33,34,34,34,34,34,34,34,44,26,45,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
20
+30,39,30,30,30,38,30,40,30,30,30,33,34,34,34,34,34,34,34,34,34,34,34,35,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
21
+30,30,30,39,30,30,30,30,30,30,30,41,42,42,42,42,42,42,42,42,42,42,42,43,9,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
22
+7,7,8,1,2,2,2,2,2,2,2,2,3,1,2,2,2,2,2,2,2,2,2,2,29,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
23
+15,15,16,9,10,10,10,10,10,10,10,10,11,9,10,10,10,10,10,10,10,10,10,10,10,10,11,30,30,30,30,30,30,30,30,30,30,30,30,30,
24
+23,23,24,17,18,18,18,18,18,18,18,18,19,17,18,18,18,18,18,18,18,18,18,18,18,18,19,30,30,30,30,30,30,30,30,30,30,30,30,30,
25
+30,30,39,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,32,31,
26
+30,30,30,30,39,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,32,
27
+39,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,25,26,26,26,26,26,26,26,26,26,26,26,27,30,
28
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,46,30,33,34,34,34,34,34,34,34,34,36,42,37,35,30,
29
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,33,34,34,34,34,34,34,34,34,35,48,33,35,30,
30
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,47,33,34,34,34,34,34,34,34,34,35,48,33,35,30,
31
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,39,30,30,30,33,34,34,34,34,34,34,34,34,35,48,33,35,30,
32
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,33,34,34,34,34,34,34,34,34,44,26,45,35,30,
33
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,47,30,33,34,34,34,34,34,34,34,34,34,34,34,35,30,
34
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,33,34,34,34,34,34,34,34,34,34,34,34,35,48,
35
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,33,34,34,34,34,34,34,34,34,34,34,34,35,30,
36
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,41,42,42,42,42,42,42,42,42,42,42,42,43,30,
37
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
38
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,32,30,30,30,
39
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
40
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,32,30,30,30,31,30,30,30,30,
41
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,32,30,30,
42
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
43
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
44
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
45
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
46
+30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30
47
+</data>
48
+ </layer>
49
+</map>
0 50
new file mode 100644
1 51
Binary files /dev/null and b/test-data/tmw_desert_spacing.png differ