Browse code

add unit test

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