1 10 package org.jgap; 11 12 import java.util.*; 13 import java.io.*; 14 import junit.framework.*; 15 import junitx.util.*; 16 import org.jgap.impl.*; 17 18 24 public abstract class JGAPTestCase 25 extends TestCase { 26 27 private final static String CVS_REVISION = "$Revision: 1.15 $"; 28 29 protected static final double DELTA = 0.0000001; 31 32 public final static PrivateAccessor privateAccessor = null; 33 public Configuration conf; 34 35 public JGAPTestCase(String a_name) { 36 super(a_name); 37 } 38 39 public JGAPTestCase() { 40 super(); 41 } 42 43 public void setUp() { 44 Genotype.setStaticConfiguration(null); 45 System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS, ""); 47 conf.resetProperty(conf.PROPERTY_FITEVAL_INST); 48 conf.resetProperty(conf.PROPERTY_EVENT_MGR_INST); 49 conf = new DefaultConfiguration(); 50 } 51 52 60 public static boolean isChromosomesEqual(IChromosome[] a_list1, 61 IChromosome[] a_list2) { 62 if (a_list1 == null) { 63 return (a_list2 == null); 64 } 65 else if (a_list2 == null) { 66 return false; 67 } 68 else { 69 if (a_list1.length != a_list2.length) { 70 return false; 71 } 72 else { 73 for (int i = 0; i < a_list1.length; i++) { 74 IChromosome c1 = (IChromosome) a_list1[i]; 75 IChromosome c2 = (IChromosome) a_list2[i]; 76 if (!c1.equals(c2)) { 77 return false; 78 } 79 } 80 return true; 81 } 82 } 83 } 84 85 public static void assertEqualsMap(Map a_map1, Map a_map2) { 86 87 } 88 89 public class TestFitnessFunction 90 extends FitnessFunction { 91 96 protected double evaluate(IChromosome a_subject) { 97 return 1.0000000d; 99 } 100 } 101 public static void assertInList(final Map a_list, Object a_object) { 102 if (a_list.containsKey(a_object)) { 103 a_list.remove(a_object); 104 } 105 else { 106 if (a_list.containsKey("java.lang." + a_object)) { 109 a_list.remove("java.lang." + a_object); 110 } 111 else { 112 fail("Object " + a_object + " not in list!"); 113 } 114 } 115 } 116 117 124 public boolean isSerializable(Object a_obj) { 125 return Serializable.class.isInstance(a_obj); 126 } 127 128 137 public Object doSerialize(Object a_obj) 138 throws Exception { 139 File f = File.createTempFile("object", "ser"); 141 OutputStream os = new FileOutputStream(f); 142 ObjectOutputStream oos = new ObjectOutputStream(os); 143 oos.writeObject(a_obj); 144 oos.flush(); 145 oos.close(); 146 InputStream oi = new FileInputStream(f); 147 ObjectInputStream ois = new ObjectInputStream(oi); 148 Object result = ois.readObject(); 149 ois.close(); 150 return result; 151 } 152 153 164 public Object getNestedField(Object a_instance, String a_parentFieldName, 165 String a_childFieldName) 166 throws NoSuchFieldException { 167 Object parentField = privateAccessor.getField(a_instance, a_parentFieldName); 168 Object childField = privateAccessor.getField(parentField, a_childFieldName); 169 return childField; 170 } 171 172 183 public void setNestedField(Object a_instance, String a_parentFieldName, 184 String a_childFieldName, Object a_value) 185 throws NoSuchFieldException { 186 Object parentField = privateAccessor.getField(a_instance, a_parentFieldName); 187 privateAccessor.setField(parentField, a_childFieldName, a_value); 188 } 189 } 190 | Popular Tags |