KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > JGAPTestCase


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

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 /**
19  * Abstract test case for all JGAP test cases providing a common infrastructure.
20  *
21  * @author Klaus Meffert
22  * @since 2.4
23  */

24 public abstract class JGAPTestCase
25     extends TestCase {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.15 $";
28
29   //delta for distinguishing whether a value is to be interpreted as zero
30
protected static final double DELTA = 0.0000001;
31
32   public final static PrivateAccessor privateAccessor = null;
33   public Configuration conf;
34
35   public JGAPTestCase(String JavaDoc a_name) {
36     super(a_name);
37   }
38
39   public JGAPTestCase() {
40     super();
41   }
42
43   public void setUp() {
44     Genotype.setStaticConfiguration(null);
45     // reset property --> use JGAPFactory
46
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   /**
53    *
54    * @param a_list1 first list of chromosomes
55    * @param a_list2 second list of chromosomes
56    * @return true lists of chromosomes are equal
57    *
58    * @author Klaus Meffert
59    */

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     /**@todo implement*/
87   }
88
89   public class TestFitnessFunction
90       extends FitnessFunction {
91     /**
92      * @param a_subject Chromosome
93      * @return double
94      * @since 2.0 (until 1.1: return type int)
95      */

96     protected double evaluate(IChromosome a_subject) {
97       //result does not matter here
98
return 1.0000000d;
99     }
100   }
101   public static void assertInList(final Map a_list, Object JavaDoc a_object) {
102     if (a_list.containsKey(a_object)) {
103       a_list.remove(a_object);
104     }
105     else {
106       // Because only source code is browsed (also non-compilable code!),
107
// there is no disctinction between class java.lang.X and class X
108
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   /**
118    * @param a_obj object to verify
119    * @return true: object implements serializable
120    *
121    * @author Klaus Meffert
122    * @since 2.6
123    */

124   public boolean isSerializable(Object JavaDoc a_obj) {
125     return Serializable.class.isInstance(a_obj);
126   }
127
128   /**
129    *
130    * @param a_obj object to serialize, then deserialize
131    * @return deserialized object that has previously been serialized
132    * @throws Exception
133    *
134    * @author Klaus Meffert
135    * @since 2.6
136    */

137   public Object JavaDoc doSerialize(Object JavaDoc a_obj)
138       throws Exception JavaDoc {
139     // serialize object to a file
140
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 JavaDoc result = ois.readObject();
149     ois.close();
150     return result;
151   }
152
153   /**
154    * Retrieves a nested (private) field, that is field2 from "field1.field2".
155    * @param a_instance the instance the parent field is located in
156    * @param a_parentFieldName the name of the parent field (case sensitive!)
157    * @param a_childFieldName the name of the child field (case sensitive!)
158    * @throws NoSuchFieldException
159    * @return the value of the child field
160    *
161    * @author Klaus Meffert
162    * @since 2.6
163    */

164   public Object JavaDoc getNestedField(Object JavaDoc a_instance, String JavaDoc a_parentFieldName,
165                                String JavaDoc a_childFieldName)
166       throws NoSuchFieldException JavaDoc {
167     Object JavaDoc parentField = privateAccessor.getField(a_instance, a_parentFieldName);
168     Object JavaDoc childField = privateAccessor.getField(parentField, a_childFieldName);
169     return childField;
170   }
171
172   /**
173    * Sets a nested (private) field, that is field2 from "field1.field2".
174    * @param a_instance the instance the parent field is located in
175    * @param a_parentFieldName the name of the parent field (case sensitive!)
176    * @param a_childFieldName the name of the child field (case sensitive!)
177    * @param a_value the value to set the child field to
178    * @throws NoSuchFieldException
179    *
180    * @author Klaus Meffert
181    * @since 2.6
182    */

183   public void setNestedField(Object JavaDoc a_instance, String JavaDoc a_parentFieldName,
184                                String JavaDoc a_childFieldName, Object JavaDoc a_value)
185       throws NoSuchFieldException JavaDoc {
186     Object JavaDoc parentField = privateAccessor.getField(a_instance, a_parentFieldName);
187     privateAccessor.setField(parentField, a_childFieldName, a_value);
188   }
189 }
190
Popular Tags