Skip to content

Commit 444eb07

Browse files
committed
Added tests for tolerant-reader pattern
1 parent fd8c058 commit 444eb07

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.iluwatar.tolerantreader;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.rules.TemporaryFolder;
6+
7+
import java.io.File;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertNotSame;
11+
12+
/**
13+
* Date: 12/30/15 - 18:39 PM
14+
*
15+
* @author Jeroen Meulemeester
16+
*/
17+
public class RainbowFishSerializerTest {
18+
19+
/**
20+
* Create a temporary folder, used to generate files in during this test
21+
*/
22+
@Rule
23+
public final TemporaryFolder testFolder = new TemporaryFolder();
24+
25+
/**
26+
* Rainbow fish version 1 used during the tests
27+
*/
28+
private static final RainbowFish V1 = new RainbowFish("version1", 1, 2, 3);
29+
30+
/**
31+
* Rainbow fish version 2 used during the tests
32+
*/
33+
private static final RainbowFishV2 V2 = new RainbowFishV2("version2", 4, 5, 6, true, false, true);
34+
35+
/**
36+
* Verify if a fish, written as version 1 can be read back as version 1
37+
*/
38+
@Test
39+
public void testWriteV1ReadV1() throws Exception {
40+
final File outputFile = this.testFolder.newFile();
41+
RainbowFishSerializer.writeV1(V1, outputFile.getPath());
42+
43+
final RainbowFish fish = RainbowFishSerializer.readV1(outputFile.getPath());
44+
assertNotSame(V1, fish);
45+
assertEquals(V1.getName(), fish.getName());
46+
assertEquals(V1.getAge(), fish.getAge());
47+
assertEquals(V1.getLengthMeters(), fish.getLengthMeters());
48+
assertEquals(V1.getWeightTons(), fish.getWeightTons());
49+
50+
}
51+
52+
/**
53+
* Verify if a fish, written as version 2 can be read back as version 1
54+
*/
55+
@Test
56+
public void testWriteV2ReadV1() throws Exception {
57+
final File outputFile = this.testFolder.newFile();
58+
RainbowFishSerializer.writeV2(V2, outputFile.getPath());
59+
60+
final RainbowFish fish = RainbowFishSerializer.readV1(outputFile.getPath());
61+
assertNotSame(V2, fish);
62+
assertEquals(V2.getName(), fish.getName());
63+
assertEquals(V2.getAge(), fish.getAge());
64+
assertEquals(V2.getLengthMeters(), fish.getLengthMeters());
65+
assertEquals(V2.getWeightTons(), fish.getWeightTons());
66+
}
67+
68+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.iluwatar.tolerantreader;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Date: 12/30/15 - 18:34 PM
9+
*
10+
* @author Jeroen Meulemeester
11+
*/
12+
public class RainbowFishTest {
13+
14+
/**
15+
* Verify if the getters of a {@link RainbowFish} return the expected values
16+
*/
17+
@Test
18+
public void testValues() {
19+
final RainbowFish fish = new RainbowFish("name", 1, 2, 3);
20+
assertEquals("name", fish.getName());
21+
assertEquals(1, fish.getAge());
22+
assertEquals(2, fish.getLengthMeters());
23+
assertEquals(3, fish.getWeightTons());
24+
}
25+
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iluwatar.tolerantreader;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Date: 12/30/15 - 18:35 PM
9+
*
10+
* @author Jeroen Meulemeester
11+
*/
12+
public class RainbowFishV2Test {
13+
14+
/**
15+
* Verify if the getters of a {@link RainbowFish} return the expected values
16+
*/
17+
@Test
18+
public void testValues() {
19+
final RainbowFishV2 fish = new RainbowFishV2("name", 1, 2, 3, false, true, false);
20+
assertEquals("name", fish.getName());
21+
assertEquals(1, fish.getAge());
22+
assertEquals(2, fish.getLengthMeters());
23+
assertEquals(3, fish.getWeightTons());
24+
assertEquals(false, fish.getSleeping());
25+
assertEquals(true, fish.getHungry());
26+
assertEquals(false, fish.getAngry());
27+
}
28+
29+
}

0 commit comments

Comments
 (0)