Source code: jtemporal/util/AllTests.java
1 /*
2 Copyright (C) 2002 Thomas A Beck (http://thomas.beck.name)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18
19 package jtemporal.util;
20
21 import junit.framework.*;
22 import java.io.*;
23
24
25 /**
26 * Package JUnit test suite
27 * @author Thomas@Beck.name
28 * @version $Id$
29 */
30 public class AllTests extends TestCase
31 {
32
33 public static void main(String[] args) {
34 junit.textui.TestRunner.run(suite());
35 }
36
37
38 public static Test suite() {
39 TestSuite suite = new TestSuite() {
40 public String toString() {
41 return "jtemporal.util test suite";
42 }
43 };
44 return suite;
45 }
46
47 public void testEmptySet() {
48 testSingleton(EmptySet.getInstance());
49 }
50
51 public void testEmptyIterator() {
52 testSingleton(EmptyIterator.getInstance());
53 }
54
55 private void testSingleton(Serializable singleton) {
56 assertTrue(singleton == this.cloneBySerialisation(singleton));
57 }
58
59 private Serializable cloneBySerialisation(Serializable singleton) {
60 ByteArrayOutputStream aos;
61 ObjectOutputStream oos;
62 Serializable clone = null;
63 byte[] array = null;
64
65 // write the object
66 try {
67 aos = new ByteArrayOutputStream(1000);
68 oos = new ObjectOutputStream( aos );
69 oos.writeObject( singleton );
70 oos.flush();
71 array = aos.toByteArray();
72 oos.close();
73 aos.close();
74 } catch (java.io.IOException ioex) {
75 fail("The singleton "+singleton+" cannot be written." + ioex.getMessage());
76 }
77
78 // read the serialized calendar
79 try {
80 ByteArrayInputStream ais = new ByteArrayInputStream(array);
81 ObjectInputStream ois = new ObjectInputStream( ais );
82 clone = (Serializable) ois.readObject();
83 ois.close();
84 ais.close();
85 } catch (ClassNotFoundException cnex) {
86 fail("The serializable "+singleton+" class cannot be read." + cnex.getMessage());
87 } catch (java.io.IOException ioex) {
88 fail("The serializable "+singleton+" cannot be read." + ioex.getMessage());
89 }
90 if (clone == null) fail("clone == null");
91 return clone;
92 }
93
94 }