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;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  
25  import junit.framework.Assert;
26  
27  import org.apache.commons.math.complex.Complex;
28  
29  /***
30   * @version $Revision: 1.14 $ $Date: 2004/06/17 21:41:56 $
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 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 serializeAndRecover(Object o){
73          
74          Object result = null;
75          
76          File tmp = null;
77          
78          try {
79              
80              // serialize the Object
81              tmp = File.createTempFile("test",".ser");
82              FileOutputStream fo = new FileOutputStream(tmp);
83              ObjectOutputStream so = new ObjectOutputStream(fo);
84              so.writeObject(o);
85              so.flush();
86  
87              // deserialize the Book
88              FileInputStream fi = new FileInputStream(tmp);
89              ObjectInputStream si = new ObjectInputStream(fi);  
90              result = si.readObject();
91              
92          }catch (Exception 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 object) {
107         Object object2 = serializeAndRecover(object);
108         Assert.assertEquals("Equals check", object, object2);
109         Assert.assertEquals("HashCode check", object.hashCode(), object2.hashCode());
110     }
111 }