1 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 32 public class TestUtils { 33 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 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 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 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 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 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 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 } 112 | Popular Tags |