KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > BaseGeneTest


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 junit.framework.*;
13
14 /**
15  * Tests the BaseGene class.
16  *
17  * @author Klaus Meffert
18  * @since 2.3
19  */

20 public class BaseGeneTest
21     extends JGAPTestCase {
22   /** String containing the CVS revision. Read out via reflection!*/
23   private final static String JavaDoc CVS_REVISION = "$Revision: 1.20 $";
24
25   public static Test suite() {
26     TestSuite suite = new TestSuite(BaseGeneTest.class);
27     return suite;
28   }
29
30   /**
31    * Following should be possible without exception.
32    * @throws Exception
33    *
34    * @author Klaus Meffert
35    * @since 2.3
36    */

37   public void testConstruct_0()
38       throws Exception JavaDoc {
39     assertNotNull(new BaseGeneImpl(conf));
40   }
41
42   /**
43    * @throws Exception
44    *
45    * @author Klaus Meffert
46    * @since 2.3
47    */

48   public void testToString_0()
49       throws Exception JavaDoc {
50     Gene gene = new BaseGeneImpl(conf);
51     assertEquals("null, " + BaseGene.S_APPLICATION_DATA + ":null",
52                  gene.toString());
53   }
54
55   /**
56    * @throws Exception
57    *
58    * @author Klaus Meffert
59    * @since 2.3
60    */

61   public void testToString_1()
62       throws Exception JavaDoc {
63     Gene gene = new BaseGeneImpl(conf);
64     gene.setAllele(new Integer JavaDoc(98));
65     assertEquals("98, " + BaseGene.S_APPLICATION_DATA + ":null",
66                  gene.toString());
67   }
68
69   /**
70    * @throws Exception
71    *
72    * @author Klaus Meffert
73    * @since 2.6
74    */

75   public void testToString_2()
76       throws Exception JavaDoc {
77     Gene gene = new BaseGeneImpl(conf);
78     gene.setAllele(new Integer JavaDoc(98));
79     gene.setApplicationData("myAppData");
80     assertEquals("98, " + BaseGene.S_APPLICATION_DATA + ":myAppData",
81                  gene.toString());
82   }
83
84   /**
85    * @throws Exception
86    *
87    * @author Klaus Meffert
88    * @since 2.4
89    */

90   public void testGetAllele_0()
91       throws Exception JavaDoc {
92     Gene gene = new BaseGeneImpl(conf);
93     gene.setAllele(new Double JavaDoc(75));
94     assertEquals(new Double JavaDoc(75), gene.getAllele());
95   }
96
97   /**
98    * @throws Exception
99    *
100    * @author Klaus Meffert
101    * @since 2.4
102    */

103   public void testSize_0()
104       throws Exception JavaDoc {
105     Gene gene = new BaseGeneImpl(conf);
106     assertEquals(1, gene.size());
107   }
108
109   /**
110    * @throws Exception
111    *
112    * @author Klaus Meffert
113    * @since 2.4
114    */

115   public void testEquals_0()
116       throws Exception JavaDoc {
117     BaseGeneImpl gene = new BaseGeneImpl(conf);
118     gene.m_compareTo_result = 0;
119     assertTrue(gene.equals(null));
120     assertTrue(gene.equals(gene));
121     assertTrue(gene.equals(new Integer JavaDoc(2)));
122   }
123
124   /**
125    * @throws Exception
126    *
127    * @author Klaus Meffert
128    * @since 2.4
129    */

130   public void testEquals_1()
131       throws Exception JavaDoc {
132     BaseGeneImpl gene = new BaseGeneImpl(conf);
133     gene.m_compareTo_result = -1;
134     assertFalse(gene.equals(null));
135     assertFalse(gene.equals(gene));
136     assertFalse(gene.equals(new Integer JavaDoc(2)));
137   }
138
139   /**
140    * @throws Exception
141    *
142    * @author Klaus Meffert
143    * @since 2.4
144    */

145   public void testEquals_2()
146       throws Exception JavaDoc {
147     BaseGeneImpl gene = new BaseGeneImpl(conf);
148     gene.m_compareTo_result = 1;
149     assertFalse(gene.equals(null));
150     assertFalse(gene.equals(gene));
151     assertFalse(gene.equals(new Integer JavaDoc(2)));
152   }
153
154   /**
155    * Compare application data.
156    * @throws Exception
157    *
158    * @author Klaus Meffert
159    * @since 2.6
160    */

161   public void testEquals_3()
162       throws Exception JavaDoc {
163     Configuration conf = new ConfigurationForTest();
164 // Genotype.setConfiguration(new ConfigurationForTest());
165
// Genotype.getConfiguration().getJGAPFactory();
166
BaseGeneImpl gene = new BaseGeneImpl(conf);
167     gene.m_compareTo_result = 0;
168     gene.setApplicationData(new AppDataForTest());
169     BaseGeneImpl gene2 = new BaseGeneImpl(conf);
170     gene2.m_compareTo_result = 0;
171     gene2.setApplicationData(new AppDataForTest());
172     gene.setCompareApplicationData(true);
173     assertTrue(gene.equals(gene2));
174     /**@todo use other than JGAPFactory to be able to receive a null
175      * CompareToHandler for the application data object
176      */

177   }
178
179   /**
180    * Simple cleanup should be possible without exception.
181    * @throws Exception
182    *
183    *
184    * @author Klaus Meffert
185    * @since 2.4
186    */

187   public void testCleanup_0()
188       throws Exception JavaDoc {
189     Gene gene = new BaseGeneImpl(conf);
190     gene.setAllele(new Double JavaDoc(75));
191     gene.cleanup();
192   }
193
194   /**
195    * @throws Exception
196    *
197    * @author Klaus Meffert
198    * @since 2.3
199    */

200   public void testHashCode_0()
201       throws Exception JavaDoc {
202     BaseGeneImpl gene = new BaseGeneImpl(conf);
203     assertEquals( -79, gene.hashCode());
204   }
205
206   /**
207    * @throws Exception
208    *
209    * @author Klaus Meffert
210    * @since 2.4
211    */

212   public void testHashCode_1()
213       throws Exception JavaDoc {
214     BaseGeneImpl gene = new BaseGeneImpl(conf);
215     gene.setAllele(new Double JavaDoc(1.5d));
216     assertEquals(new Double JavaDoc(1.5d).hashCode(), gene.hashCode());
217   }
218
219   /**
220    * @throws Exception
221    *
222    * @author Klaus Meffert
223    * @since 2.4
224    */

225   public void testSetEnergy_0()
226       throws Exception JavaDoc {
227     BaseGeneImpl gene = new BaseGeneImpl(conf);
228     assertEquals(0.0, gene.getEnergy(), DELTA);
229     gene.setEnergy(2.3);
230     assertEquals(2.3, gene.getEnergy(), DELTA);
231   }
232
233   /**
234    * @throws Exception
235    *
236    * @author Klaus Meffert
237    * @since 2.4
238    */

239   public void testSetEnergy_1()
240       throws Exception JavaDoc {
241     BaseGeneImpl gene = new BaseGeneImpl(conf);
242     gene.setEnergy(2.3);
243     assertEquals(2.3, gene.getEnergy(), DELTA);
244     gene.setEnergy( -55.8);
245     assertEquals( -55.8, gene.getEnergy(), DELTA);
246     gene.setEnergy(0.5);
247     gene.setEnergy(0.8);
248     assertEquals(0.8, gene.getEnergy(), DELTA);
249   }
250
251   /**
252    * @throws Exception
253    *
254    * @author Klaus Meffert
255    * @since 2.4
256    */

257   public void testSetApplicationData_0()
258       throws Exception JavaDoc {
259     BaseGeneImpl gene = new BaseGeneImpl(conf);
260     assertNull(gene.getApplicationData());
261     Integer JavaDoc i = new Integer JavaDoc(23);
262     gene.setApplicationData(i);
263     assertSame(i, gene.getApplicationData());
264     String JavaDoc s = "Hallo";
265     gene.setApplicationData(s);
266     assertSame(s, gene.getApplicationData());
267   }
268
269   /**
270    * @throws Exception
271    *
272    * @author Klaus Meffert
273    * @since 2.4
274    */

275   public void testIsCompareApplicationData_0()
276       throws Exception JavaDoc {
277     BaseGeneImpl gene = new BaseGeneImpl(conf);
278     assertFalse(gene.isCompareApplicationData());
279     gene.setCompareApplicationData(false);
280     assertFalse(gene.isCompareApplicationData());
281     gene.setCompareApplicationData(true);
282     assertTrue(gene.isCompareApplicationData());
283   }
284
285   /**
286    * Test implementation of Gene interface extending abstract BaseGene class.
287    *
288    * @author Klaus Meffert
289    * @since 2.3
290    */

291   class BaseGeneImpl
292       extends BaseGene {
293     private Object JavaDoc m_allele;
294
295     private int m_compareTo_result;
296
297     public int compareTo(Object JavaDoc a_o) {
298       return m_compareTo_result;
299     }
300
301     public BaseGeneImpl(final Configuration a_config)
302         throws InvalidConfigurationException {
303       super(a_config);
304     }
305
306     protected Gene newGeneInternal() {
307       return null;
308     }
309
310     public void setAllele(Object JavaDoc a_newValue) {
311       m_allele = a_newValue;
312     }
313
314     public String JavaDoc getPersistentRepresentation() {
315       return null;
316     }
317
318     public void setValueFromPersistentRepresentation(String JavaDoc a_representation) {
319     }
320
321     public void setToRandomValue(RandomGenerator a_numberGenerator) {
322     }
323
324     public void applyMutation(int a_index, double a_percentage) {
325     }
326
327     protected Object JavaDoc getInternalValue() {
328       return m_allele;
329     }
330   }
331   class AppDataForTest
332       implements IApplicationData {
333     public int compareTo(Object JavaDoc o2) {
334       return 0;
335     }
336
337     public Object JavaDoc clone()
338         throws CloneNotSupportedException JavaDoc {
339       return null;
340     }
341   }
342
343 }
344
Popular Tags