Skip to content

Commit ce7999e

Browse files
committed
Updates tests to work with version 1.1.0.
1 parent ce6482b commit ce7999e

File tree

4 files changed

+45
-106
lines changed

4 files changed

+45
-106
lines changed

test/com/valkryst/V2DSprite/Driver.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ public void windowClosing(final WindowEvent e) {
3535
frame.setVisible(true);
3636

3737
// Load Atlas
38-
final FileInputStream atlasImageStream = new FileInputStream("test_res/Atlas.png");
39-
final FileInputStream atlasJSONStream = new FileInputStream("test_res/Atlas.json");
40-
final SpriteAtlas atlas = new SpriteAtlas(atlasImageStream, atlasJSONStream);
38+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas("test_res/Atlas.png", "test_res/Atlas.json");
4139

4240
final List<SpriteAnimation> animations = new ArrayList<>();
4341
int tmp = 0;

test/com/valkryst/V2DSprite/SpriteAnimationTest.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,24 @@
1313
import java.io.InputStreamReader;
1414

1515
public class SpriteAnimationTest {
16-
private SpriteAtlas atlas;
17-
private JSONObject animationJSONData;
18-
19-
@Before
20-
public void loadAtlas() throws IOException, ParseException {
21-
final FileInputStream atlasImageStream = new FileInputStream("test_res/Atlas.png");
22-
FileInputStream atlasJSONStream = new FileInputStream("test_res/Atlas.json");
23-
atlas = new SpriteAtlas(atlasImageStream, atlasJSONStream);
24-
25-
atlasJSONStream = new FileInputStream("test_res/Atlas.json");
26-
final JSONParser parser = new JSONParser();
27-
final JSONObject atlasData = (JSONObject) parser.parse(new InputStreamReader(atlasJSONStream));
28-
final JSONArray sheetDataArray = (JSONArray) atlasData.get("Sheets");
29-
final JSONObject spriteJSONData = (JSONObject) sheetDataArray.get(0);
30-
final JSONArray animDataArray = (JSONArray) spriteJSONData.get("Animations");
31-
animationJSONData = (JSONObject) animDataArray.get(0);
32-
atlasJSONStream.close();
33-
}
16+
private final String pngFilePath = "test_res/Atlas.png";
17+
private final String jsonFilePath = "test_res/Atlas.json";
3418

3519
@Test
36-
public void testConstructor_withValidParams() {
37-
final SpriteAnimation animation = new SpriteAnimation(atlas, animationJSONData);
20+
public void testConstructor_withValidParams() throws IOException, ParseException {
21+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
22+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
23+
final SpriteAnimation animation = sheet.getAnimation("Standing");
3824
Assert.assertEquals("Standing", animation.getName());
3925
Assert.assertEquals(3, animation.getTotalFrames());
4026
Assert.assertEquals(0, animation.getCurFrame());
4127
}
4228

43-
@Test(expected=NullPointerException.class)
44-
public void testConstructor_withNullAtlas() {
45-
new SpriteAnimation(null, animationJSONData);
46-
}
47-
48-
@Test(expected=NullPointerException.class)
49-
public void testConstructor_withNullAnimationData() {
50-
new SpriteAnimation(atlas, null);
51-
}
52-
5329
@Test
54-
public void testReset() {
55-
final SpriteAnimation animation = new SpriteAnimation(atlas, animationJSONData);
30+
public void testReset() throws IOException, ParseException {
31+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
32+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
33+
final SpriteAnimation animation = sheet.getAnimation("Standing");
5634
Assert.assertEquals(3, animation.getTotalFrames());
5735
Assert.assertEquals(0, animation.getCurFrame());
5836

@@ -64,8 +42,10 @@ public void testReset() {
6442
}
6543

6644
@Test
67-
public void testToNextFrame() {
68-
final SpriteAnimation animation = new SpriteAnimation(atlas, animationJSONData);
45+
public void testToNextFrame() throws IOException, ParseException {
46+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
47+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
48+
final SpriteAnimation animation = sheet.getAnimation("Standing");
6949
Assert.assertEquals(3, animation.getTotalFrames());
7050
Assert.assertEquals(0, animation.getCurFrame());
7151

@@ -80,8 +60,10 @@ public void testToNextFrame() {
8060
}
8161

8262
@Test
83-
public void testToPreviousFrame() {
84-
final SpriteAnimation animation = new SpriteAnimation(atlas, animationJSONData);
63+
public void testToPreviousFrame() throws IOException, ParseException {
64+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
65+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
66+
final SpriteAnimation animation = sheet.getAnimation("Standing");
8567
Assert.assertEquals(3, animation.getTotalFrames());
8668
Assert.assertEquals(0, animation.getCurFrame());
8769

@@ -96,8 +78,10 @@ public void testToPreviousFrame() {
9678
}
9779

9880
@Test
99-
public void testTotalFrames() {
100-
final SpriteAnimation animation = new SpriteAnimation(atlas, animationJSONData);
81+
public void testTotalFrames() throws IOException, ParseException {
82+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
83+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
84+
final SpriteAnimation animation = sheet.getAnimation("Standing");
10185
Assert.assertEquals(3, animation.getTotalFrames());
10286
}
10387
}

test/com/valkryst/V2DSprite/SpriteAtlasTest.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
package com.valkryst.V2DSprite;
22

33
import org.json.simple.parser.ParseException;
4-
import org.junit.After;
54
import org.junit.Assert;
6-
import org.junit.Before;
75
import org.junit.Test;
86

9-
import java.io.FileInputStream;
10-
import java.io.FileNotFoundException;
117
import java.io.IOException;
128

139
public class SpriteAtlasTest {
14-
private FileInputStream atlasImageStream;
15-
private FileInputStream atlasJSONStream;
16-
17-
@Before
18-
public void openStreams() throws FileNotFoundException {
19-
atlasImageStream = new FileInputStream("test_res/Atlas.png");
20-
atlasJSONStream = new FileInputStream("test_res/Atlas.json");
21-
}
22-
23-
@After
24-
public void closeStreams() throws IOException {
25-
atlasImageStream.close();
26-
atlasJSONStream.close();
27-
}
10+
private final String pngFilePath = "test_res/Atlas.png";
11+
private final String jsonFilePath = "test_res/Atlas.json";
2812

2913
@Test
30-
public void testConstructor_withValidParams() throws IOException, ParseException {
31-
final SpriteAtlas atlas = new SpriteAtlas(atlasImageStream, atlasJSONStream);
14+
public void testCreateSpriteAtlas_withValidParams() throws IOException, ParseException {
15+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
3216

3317
// Ensure Image Exists
3418
Assert.assertNotNull(atlas.getAtlasImage());
@@ -71,30 +55,30 @@ public void testConstructor_withValidParams() throws IOException, ParseException
7155
}
7256

7357
@Test(expected=NullPointerException.class)
74-
public void testConstructor_withNullImageStream() throws IOException, ParseException {
75-
new SpriteAtlas(null, atlasJSONStream);
58+
public void testCreateSpriteAtlas_withNullImagePath() throws IOException, ParseException {
59+
SpriteAtlas.createSpriteAtlas(null, jsonFilePath);
7660
}
7761

7862
@Test(expected=NullPointerException.class)
79-
public void testConstructor_withNullJSONStream() throws IOException, ParseException {
80-
new SpriteAtlas(atlasImageStream, null);
63+
public void testCreateSpriteAtlas_withNullJSONPath() throws IOException, ParseException {
64+
SpriteAtlas.createSpriteAtlas(pngFilePath, null);
8165
}
8266

8367
@Test
8468
public void testGetAtlasImage() throws IOException, ParseException {
85-
final SpriteAtlas atlas = new SpriteAtlas(atlasImageStream, atlasJSONStream);
69+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
8670
Assert.assertNotNull(atlas.getAtlasImage());
8771
}
8872

8973
@Test
9074
public void testGetSpriteSheet_withExistingSpriteSheet() throws IOException, ParseException {
91-
final SpriteAtlas atlas = new SpriteAtlas(atlasImageStream, atlasJSONStream);
75+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
9276
Assert.assertNotNull(atlas.getSpriteSheet("Player"));
9377
}
9478

9579
@Test
9680
public void testGetSpriteSheet_withNonExistingSpriteSheet() throws IOException, ParseException {
97-
final SpriteAtlas atlas = new SpriteAtlas(atlasImageStream, atlasJSONStream);
81+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
9882
Assert.assertNull(atlas.getSpriteSheet("SomeRandomWords"));
9983
}
10084
}
Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
11
package com.valkryst.V2DSprite;
22

3-
import org.json.simple.JSONArray;
4-
import org.json.simple.JSONObject;
5-
import org.json.simple.parser.JSONParser;
63
import org.json.simple.parser.ParseException;
74
import org.junit.Assert;
8-
import org.junit.Before;
95
import org.junit.Test;
106

11-
import java.io.FileInputStream;
127
import java.io.IOException;
13-
import java.io.InputStreamReader;
148

159
public class SpriteSheetTest {
16-
private SpriteAtlas atlas;
17-
private JSONObject spriteJSONData;
18-
19-
@Before
20-
public void loadAtlas() throws IOException, ParseException {
21-
final FileInputStream atlasImageStream = new FileInputStream("test_res/Atlas.png");
22-
FileInputStream atlasJSONStream = new FileInputStream("test_res/Atlas.json");
23-
atlas = new SpriteAtlas(atlasImageStream, atlasJSONStream);
24-
25-
atlasJSONStream = new FileInputStream("test_res/Atlas.json");
26-
final JSONParser parser = new JSONParser();
27-
final JSONObject atlasData = (JSONObject) parser.parse(new InputStreamReader(atlasJSONStream));
28-
final JSONArray sheetDataArray = (JSONArray) atlasData.get("Sheets");
29-
spriteJSONData = (JSONObject) sheetDataArray.get(0);
30-
atlasJSONStream.close();
31-
}
10+
private final String pngFilePath = "test_res/Atlas.png";
11+
private final String jsonFilePath = "test_res/Atlas.json";
3212

3313
@Test
34-
public void testConstructor_withValidParams() {
35-
final SpriteSheet sheet = new SpriteSheet(atlas, spriteJSONData);
14+
public void testConstructor_withValidParams() throws IOException, ParseException {
15+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
16+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
3617
Assert.assertEquals("Player", sheet.getName());
3718

3819
// Ensure Sprite Animation Exists & Data Is Correct
@@ -43,25 +24,17 @@ public void testConstructor_withValidParams() {
4324
Assert.assertEquals(3, animation.getTotalFrames());
4425
}
4526

46-
@Test(expected=NullPointerException.class)
47-
public void testConstructor_withNullAtlas() {
48-
new SpriteSheet(null, spriteJSONData);
49-
}
50-
51-
@Test(expected=NullPointerException.class)
52-
public void testConstructor_withNullSheetData() {
53-
new SpriteSheet(atlas, null);
54-
}
55-
5627
@Test
57-
public void testGetAnimation_withExistingAnimation() {
58-
final SpriteSheet sheet = new SpriteSheet(atlas, spriteJSONData);
28+
public void testGetAnimation_withExistingAnimation() throws IOException, ParseException {
29+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
30+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
5931
Assert.assertNotNull(sheet.getAnimation("Standing"));
6032
}
6133

6234
@Test
63-
public void testGetAnimation_WithNonExistingAnimation() {
64-
final SpriteSheet sheet = new SpriteSheet(atlas, spriteJSONData);
35+
public void testGetAnimation_WithNonExistingAnimation() throws IOException, ParseException {
36+
final SpriteAtlas atlas = SpriteAtlas.createSpriteAtlas(pngFilePath, jsonFilePath);
37+
final SpriteSheet sheet = atlas.getSpriteSheet("Player");
6538
Assert.assertNull(sheet.getAnimation("SomeRandomWords"));
6639
}
6740
}

0 commit comments

Comments
 (0)