KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jgap.*;
13 import org.jgap.event.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the DefaultConfiguration class.
18  *
19  * @author Klaus Meffert
20  * @since 1.1
21  */

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

36   public void testConstruct_0() {
37     assertEquals(EventManager.class, conf.getEventManager().getClass());
38     assertEquals(DefaultFitnessEvaluator.class,
39                  conf.getFitnessEvaluator().getClass());
40     assertEquals(BestChromosomesSelector.class,
41                  conf.getNaturalSelectors(true).get(0).getClass());
42     assertEquals(StockRandomGenerator.class,
43                  conf.getRandomGenerator().getClass());
44     assertEquals(ChromosomePool.class, conf.getChromosomePool().getClass());
45     assertEquals(2, conf.getGeneticOperators().size());
46     // Test if all 2 slots are occupied by the 2 default GeneticOperator's
47
int code = 0;
48     GeneticOperator op;
49     for (int i = 0; i < 2; i++) {
50       op = (GeneticOperator) conf.getGeneticOperators().get(i);
51       if (op instanceof MutationOperator) {
52         code = code ^ 1;
53       }
54 // else if (op instanceof ReproductionOperator) {
55
// code = code ^ 2;
56
// }
57
else if (op instanceof CrossoverOperator) {
58         code = code ^ 4;
59       }
60     }
61     assertEquals(5, code);
62   }
63
64   /**
65    * Provoke error during initialization.
66    *
67    * @author Klaus Meffert
68    * @since 2.6
69    */

70   public void testConstruct_1() {
71     try {
72       Configuration conf = new DefaultConfigForTest();
73       fail();
74     }
75     catch (RuntimeException JavaDoc rex) {
76       ; //this is OK
77
}
78   }
79
80   /**
81    * @throws Exception
82    *
83    * @author Klaus Meffert
84    * @since 3.1
85    */

86   public void testConstruct_2()
87       throws Exception JavaDoc {
88     DefaultConfiguration.reset();
89     DefaultConfiguration conf = new DefaultConfiguration();
90     assertEquals("", conf.getId());
91     assertEquals("", conf.getName());
92   }
93
94   /**
95    * @throws Exception
96    *
97    * @author Klaus Meffert
98    * @since 3.1
99    */

100   public void testConstruct_3()
101       throws Exception JavaDoc {
102     DefaultConfiguration.reset();
103     DefaultConfiguration conf = new DefaultConfiguration("xxX1","3a");
104     assertEquals("xxX1", conf.getId());
105     assertEquals("3a", conf.getName());
106   }
107
108   class DefaultConfigForTest
109       extends DefaultConfiguration {
110     public void setRandomGenerator(RandomGenerator a_generatorToSet)
111         throws InvalidConfigurationException {
112       throw new InvalidConfigurationException("For Test only");
113     }
114   }
115 }
116
Popular Tags