KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > TestUtils


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.math;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24
25 import junit.framework.Assert;
26
27 import org.apache.commons.math.complex.Complex;
28
29 /**
30  * @version $Revision$ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
31  */

32 public class TestUtils {
33     /**
34      *
35      */

36     private TestUtils() {
37         super();
38     }
39
40     public static void assertEquals(double expected, double actual, double delta) {
41         assertEquals(null, expected, actual, delta);
42     }
43
44     public static void assertEquals(String JavaDoc msg, double expected, double actual, double delta) {
45         // check for NaN
46
if(Double.isNaN(expected)){
47             Assert.assertTrue("" + actual + " is not NaN.",
48                 Double.isNaN(actual));
49         } else {
50             Assert.assertEquals(msg, expected, actual, delta);
51         }
52     }
53     
54     /**
55      *
56      */

57     public static void assertEquals(Complex expected, Complex actual, double delta) {
58         assertEquals(expected.getReal(), actual.getReal(), delta);
59         assertEquals(expected.getImaginary(), actual.getImaginary(), delta);
60     }
61     
62     /**
63      * Verifies that two double arrays have equal entries, up to tolerance
64      */

65     public static void assertEquals(double a[], double b[], double tolerance) {
66         Assert.assertEquals(a.length, b.length);
67         for (int i = 0; i < a.length; i++) {
68             Assert.assertEquals(a[i], b[i], tolerance);
69         }
70     }
71     
72     public static Object JavaDoc serializeAndRecover(Object JavaDoc o){
73         
74         Object JavaDoc result = null;
75         
76         File JavaDoc tmp = null;
77         
78         try {
79             
80             // serialize the Object
81
tmp = File.createTempFile("test",".ser");
82             FileOutputStream JavaDoc fo = new FileOutputStream JavaDoc(tmp);
83             ObjectOutputStream JavaDoc so = new ObjectOutputStream JavaDoc(fo);
84             so.writeObject(o);
85             so.flush();
86
87             // deserialize the Book
88
FileInputStream JavaDoc fi = new FileInputStream JavaDoc(tmp);
89             ObjectInputStream JavaDoc si = new ObjectInputStream JavaDoc(fi);
90             result = si.readObject();
91             
92         }catch (Exception JavaDoc e) {
93             e.printStackTrace();
94         }finally{
95             if(tmp != null) tmp.delete();
96         }
97         
98         return result;
99     }
100     
101     /**
102      * Verifies that serialization preserves equals and hashCode
103      *
104      * @param object
105      */

106     public static void checkSerializedEquality(Object JavaDoc object) {
107         Object JavaDoc object2 = serializeAndRecover(object);
108         Assert.assertEquals("Equals check", object, object2);
109         Assert.assertEquals("HashCode check", object.hashCode(), object2.hashCode());
110     }
111 }
112
Popular Tags