KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > JGAPFactoryTest


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.impl;
11
12 import java.util.*;
13 import org.jgap.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the JGAPFactory class
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class JGAPFactoryTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(JGAPFactoryTest.class);
29     return suite;
30   }
31
32   /**
33    * @author Klaus Meffert
34    * @since 3.0
35    */

36   public void testConstruct_0() {
37     JGAPFactory factory = new JGAPFactory(false);
38     assertFalse(factory.isUseCaching());
39   }
40
41   /**
42    * @author Klaus Meffert
43    * @since 3.0
44    */

45   public void testConstruct_1() {
46     JGAPFactory factory = new JGAPFactory(true);
47     assertTrue(factory.isUseCaching());
48   }
49
50   /**
51    * @author Klaus Meffert
52    * @since 3.0
53    */

54   public void testSetParameters_0() {
55     JGAPFactory factory = new JGAPFactory(false);
56     List params = new Vector();
57     params.add("Param1");
58     params.add("Param2");
59     factory.setParameters(params);
60     assertEquals(params, factory.getParameters());
61   }
62
63   /**
64    * @author Klaus Meffert
65    * @since 3.0
66    */

67   public void testRegisterCloneHandler_0() {
68     JGAPFactory factory = new JGAPFactory(false);
69     ICloneHandler cloneHandler = new DefaultCloneHandler();
70     int index = factory.registerCloneHandler(cloneHandler);
71     assertEquals(0, index);
72     assertSame(cloneHandler, factory.removeCloneHandler(index));
73     try {
74       assertNull(factory.removeCloneHandler(0));
75       fail();
76     } catch (IndexOutOfBoundsException JavaDoc iex) {
77       ; //this is OK
78
}
79   }
80
81   /**
82    * @author Klaus Meffert
83    * @since 3.0
84    */

85   public void testRegisterCompareToHandler_0() {
86     JGAPFactory factory = new JGAPFactory(false);
87     ICompareToHandler cHandler = new DefaultCompareToHandler();
88     int index = factory.registerCompareToHandler(cHandler);
89     assertEquals(0, index);
90     assertSame(cHandler, factory.removeCompareToHandler(index));
91     try {
92       assertNull(factory.removeCompareToHandler(0));
93       fail();
94     } catch (IndexOutOfBoundsException JavaDoc iex) {
95       ; //this is OK
96
}
97   }
98
99   /**
100    * @author Klaus Meffert
101    * @since 3.0
102    */

103   public void testRegisterInitializer_0() {
104     JGAPFactory factory = new JGAPFactory(false);
105     IInitializer initer = new DefaultInitializer();
106     int index = factory.registerInitializer(initer);
107     assertEquals(0, index);
108     assertSame(initer, factory.removeInitializer(index));
109     try {
110       assertNull(factory.removeInitializer(0));
111       fail();
112     } catch (IndexOutOfBoundsException JavaDoc iex) {
113       ; //this is OK
114
}
115   }
116
117   /**
118    * @author Klaus Meffert
119    * @since 3.1
120    */

121   public void testCreateRandomGenerator_0() {
122     JGAPFactory factory = new JGAPFactory(true);
123     assertEquals(StockRandomGenerator.class,
124                  factory.createRandomGenerator().getClass());
125   }
126
127   /**
128    * Without caching.
129    *
130    * @author Klaus Meffert
131    * @since 3.1
132    */

133   public void testGetCloneHandlerFor_0() {
134     JGAPFactory factory = new JGAPFactory(false);
135     ICloneHandler cloneHandler = new DefaultCloneHandler();
136     factory.registerCloneHandler(cloneHandler);
137     assertSame(cloneHandler, factory.getCloneHandlerFor(null, IApplicationData.class));
138   }
139
140   /**
141    * With caching.
142    *
143    * @author Klaus Meffert
144    * @since 3.1
145    */

146   public void testGetCloneHandlerFor_1() {
147     JGAPFactory factory = new JGAPFactory(true);
148     ICloneHandler cloneHandler = new DefaultCloneHandler();
149     factory.registerCloneHandler(cloneHandler);
150     assertSame(cloneHandler, factory.getCloneHandlerFor(null, IApplicationData.class));
151     // Second request to verify caching returns correct results.
152
// ---------------------------------------------------------
153
assertSame(cloneHandler, factory.getCloneHandlerFor(null, IApplicationData.class));
154   }
155
156   /**
157    * Type with no handler registered, caching active.
158    *
159    * @author Klaus Meffert
160    * @since 3.1
161    */

162   public void testGetCloneHandlerFor_2() {
163     JGAPFactory factory = new JGAPFactory(true);
164     ICloneHandler cloneHandler = new DefaultCloneHandler();
165     factory.registerCloneHandler(cloneHandler);
166     assertNull(factory.getCloneHandlerFor(null, IntegerGene.class));
167     assertNull(factory.getCloneHandlerFor(null, IntegerGene.class));
168   }
169
170   /**
171    * Type with no handler registered, no caching
172    *
173    * @author Klaus Meffert
174    * @since 3.1
175    */

176   public void testGetCloneHandlerFor_3() {
177     JGAPFactory factory = new JGAPFactory(false);
178     ICloneHandler cloneHandler = new DefaultCloneHandler();
179     factory.registerCloneHandler(cloneHandler);
180     assertNull(factory.getCloneHandlerFor(null, IntegerGene.class));
181     assertNull(factory.getCloneHandlerFor(null, IntegerGene.class));
182   }
183
184   /**
185    * Null type, no caching.
186    *
187    * @author Klaus Meffert
188    * @since 3.1
189    */

190   public void testGetCloneHandlerFor_4() {
191     JGAPFactory factory = new JGAPFactory(false);
192     ICloneHandler cloneHandler = new DefaultCloneHandler();
193     factory.registerCloneHandler(cloneHandler);
194     assertNull(factory.getCloneHandlerFor(null, null));
195   }
196
197   /**
198    * Null type, with caching.
199    *
200    * @author Klaus Meffert
201    * @since 3.1
202    */

203   public void testGetCloneHandlerFor_5() {
204     JGAPFactory factory = new JGAPFactory(true);
205     ICloneHandler cloneHandler = new DefaultCloneHandler();
206     factory.registerCloneHandler(cloneHandler);
207     assertNull(factory.getCloneHandlerFor(null, null));
208   }
209
210   /**
211    * Request for an unsupported object instance.
212    *
213    * @throws Exception
214    *
215    * @author Klaus Meffert
216    * @since 3.1
217    */

218   public void testGetCloneHandlerFor_6()
219       throws Exception JavaDoc {
220     JGAPFactory factory = new JGAPFactory(false);
221     ICloneHandler cloneHandler = new DefaultCloneHandler();
222     factory.registerCloneHandler(cloneHandler);
223     Object JavaDoc inst = new IntegerGene(conf);
224     assertNull(factory.getCloneHandlerFor(inst, null));
225   }
226
227   /**
228    * Request for a supported object instance.
229    *
230    * @throws Exception
231    *
232    * @author Klaus Meffert
233    * @since 3.1
234    */

235   public void testGetCloneHandlerFor_7()
236       throws Exception JavaDoc {
237     JGAPFactory factory = new JGAPFactory(false);
238     ICloneHandler cloneHandler = new DefaultCloneHandler();
239     factory.registerCloneHandler(cloneHandler);
240     Object JavaDoc inst = new Chromosome(conf);
241     assertSame(cloneHandler, factory.getCloneHandlerFor(inst, null));
242   }
243
244   /**
245    * Ensures JGAPFactory is implementing Serializable.
246    *
247    * @throws Exception
248    *
249    * @author Klaus Meffert
250    * @since 3.2
251    */

252   public void testIsSerializable_0()
253       throws Exception JavaDoc {
254     JGAPFactory inst = new JGAPFactory(false);
255     assertTrue(isSerializable(inst));
256   }
257
258   /**
259    * Ensures that JGAPFactory and all objects contained implement Serializable
260    * correctly.
261    *
262    * @throws Exception
263    *
264    * @author Klaus Meffert
265    * @since 3.2
266    */

267   public void testDoSerialize_0()
268       throws Exception JavaDoc {
269     JGAPFactory inst = new JGAPFactory(false);
270     Object JavaDoc o = doSerialize(inst);
271     assertEquals(o, inst);
272   }
273
274 }
275
Popular Tags