1 | public class PlumNeedleTests extends TestCase {↵ | | 1 | public class ShipNeedleTests extends TestCase {↵
|
2 | ↵ | | |
|
3 | /**↵ | | 2 | /**↵
|
4 | * Returns the tests as a test suite.↵ | | 3 | * Returns the tests as a test suite.↵
|
5 | *↵ | | 4 | *↵
|
6 | * @return The test suite.↵ | | 5 | * @return The test suite.↵
|
7 | */↵ | | 6 | */↵
|
8 | public static Test suite() {↵ | | 7 | public static Test suite() {↵
|
9 | return new TestSuite(PlumNeedleTests.class);↵ | | 8 | return new TestSuite(ShipNeedleTests.class);↵
|
10 | }↵ | | 9 | }↵
|
|
11 | /**↵ | | 10 | /**↵
|
12 | * Constructs a new set of tests.↵ | | 11 | * Constructs a new set of tests.↵
|
13 | *↵ | | 12 | *↵
|
14 | * @param name the name of the tests.↵ | | 13 | * @param name the name of the tests.↵
|
15 | */↵ | | 14 | */↵
|
16 | public PlumNeedleTests(String name) {↵ | | 15 | public ShipNeedleTests(String name) {↵
|
17 | super(name);↵ | | 16 | super(name);↵
|
18 | }↵ | | 17 | }↵
|
|
19 | /**↵ | | 18 | /**↵
|
20 | * Check that the equals() method can distinguish all fields.↵ | | 19 | * Check that the equals() method can distinguish all fields.↵
|
21 | */↵ | | 20 | */↵
|
22 | public void testEquals() {↵ | | 21 | public void testEquals() {↵
|
23 | PlumNeedle n1 = new PlumNeedle();↵ | | 22 | ShipNeedle n1 = new ShipNeedle();↵
|
24 | PlumNeedle n2 = new PlumNeedle();↵ | | 23 | ShipNeedle n2 = new ShipNeedle();↵
|
25 | assertTrue(n1.equals(n2));↵ | | 24 | assertTrue(n1.equals(n2));↵
|
26 | assertTrue(n2.equals(n1));↵ | | 25 | assertTrue(n2.equals(n1));↵
|
27 | }↵ | | 26 | }↵
|
|
28 | /**↵ | | 27 | /**↵
|
29 | * Check that cloning works.↵ | | 28 | * Check that cloning works.↵
|
30 | */↵ | | 29 | */↵
|
31 | public void testCloning() {↵ | | 30 | public void testCloning() {↵
|
32 | PlumNeedle n1 = new PlumNeedle();↵ | | 31 | ShipNeedle n1 = new ShipNeedle();↵
|
33 | PlumNeedle n2 = null;↵ | | 32 | ShipNeedle n2 = null;↵
|
34 | try {↵ | | 33 | try {↵
|
35 | n2 = (PlumNeedle) n1.clone();↵ | | 34 | n2 = (ShipNeedle) n1.clone();↵
|
36 | }↵ | | 35 | }↵
|
37 | catch (CloneNotSupportedException e) {↵ | | 36 | catch (CloneNotSupportedException e) {↵
|
38 | e.printStackTrace();↵ | | 37 | e.printStackTrace();↵
|
39 | System.err.println("Failed to clone.");↵ | | 38 | System.err.println("Failed to clone.");↵
|
40 | }↵ | | 39 | }↵
|
41 | assertTrue(n1 != n2);↵ | | 40 | assertTrue(n1 != n2);↵
|
42 | assertTrue(n1.getClass() == n2.getClass());↵ | | 41 | assertTrue(n1.getClass() == n2.getClass());↵
|
43 | assertTrue(n1.equals(n2));↵ | | 42 | assertTrue(n1.equals(n2));↵
|
44 | }↵ | | 43 | }↵
|
|
45 | /**↵ | | 44 | /**↵
|
46 | * Serialize an instance, restore it, and check for equality.↵ | | 45 | * Serialize an instance, restore it, and check for equality.↵
|
47 | */↵ | | 46 | */↵
|
48 | public void testSerialization() {↵ | | 47 | public void testSerialization() {↵
|
49 | PlumNeedle n1 = new PlumNeedle();↵ | | 48 | ShipNeedle n1 = new ShipNeedle();↵
|
50 | PlumNeedle n2 = null;↵ | | 49 | ShipNeedle n2 = null;↵
|
51 | try {↵ | | 50 | try {↵
|
52 | ByteArrayOutputStream buffer = new ByteArrayOutputStream();↵ | | 51 | ByteArrayOutputStream buffer = new ByteArrayOutputStream();↵
|
53 | ObjectOutput out = new ObjectOutputStream(buffer);↵ | | 52 | ObjectOutput out = new ObjectOutputStream(buffer);↵
|
54 | out.writeObject(n1);↵ | | 53 | out.writeObject(n1);↵
|
55 | out.close();↵ | | 54 | out.close();↵
|
56 | ObjectInput in = new ObjectInputStream(↵ | | 55 | ObjectInput in = new ObjectInputStream(↵
|
57 | new ByteArrayInputStream(buffer.toByteArray())↵ | | 56 | new ByteArrayInputStream(buffer.toByteArray())↵
|
58 | );↵ | | 57 | );↵
|
59 | n2 = (PlumNeedle) in.readObject();↵ | | 58 | n2 = (ShipNeedle) in.readObject();↵
|
60 | in.close();↵ | | 59 | in.close();↵
|
61 | }↵ | | 60 | }↵
|
62 | catch (Exception e) {↵ | | 61 | catch (Exception e) {↵
|
63 | e.printStackTrace();↵ | | 62 | e.printStackTrace();↵
|
64 | }↵ | | 63 | }↵
|
65 | assertTrue(n1.equals(n2)) | | 64 | assertTrue(n1.equals(n2))
|