KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gui > ConfigWriterTest


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.gui;
11
12 import java.io.*;
13 import org.jgap.data.config.*;
14 import java.util.*;
15 import org.jgap.*;
16 import org.jgap.event.*;
17 import org.jgap.impl.*;
18 import junit.framework.*;
19
20 /**
21  * Tests the ConfigWriter class.
22  *
23  * @author Siddhartha Azad
24  * @since 2.3
25  */

26 public class ConfigWriterTest
27     extends JGAPTestCase {
28   /** String containing the CVS revision. Read out via reflection!*/
29   private final static String JavaDoc CVS_REVISION = "$Revision: 1.17 $";
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(ConfigWriterTest.class);
33     return suite;
34   }
35
36   /**
37    * Get mock config data, use the ConfigWriter to write a property file
38    * retrieve the written file, load it and check if the contents got
39    * written correctly.
40    *
41    * @throws Exception
42    *
43    * @author Siddhartha Azad
44    * @since 2.3
45    * */

46   public void testConfigData_0()
47       throws Exception JavaDoc {
48     IConfigInfo ici = new MockConfigInfoForTest();
49     ConfigWriter.getInstance().write(ici); /**@todo write to stream*/
50     // read the file and check the values
51
int totalProps = 0;
52     Properties props = new Properties();
53     try {
54       props.load(new FileInputStream("jgapTmp.con"));
55           /**@todo read from stream*/
56       ConfigData cd = ici.getConfigData();
57       String JavaDoc nsPrefix = cd.getNS() + ".";
58       String JavaDoc name;
59       List values;
60       for (int i = 0; i < cd.getNumLists(); i++) {
61         name = cd.getListNameAt(i);
62         values = cd.getListValuesAt(i);
63         int idx = 0;
64         for (Iterator iter = values.iterator(); iter.hasNext(); idx++) {
65           // append an index for same key elements
66
String JavaDoc tmpName = nsPrefix + name + "[" + idx + "]";
67           try {
68             assertEquals(props.getProperty(tmpName), (String JavaDoc) iter.next());
69           } catch (Exception JavaDoc ex) {
70             ex.printStackTrace();
71             fail();
72           }
73           totalProps++;
74         }
75       }
76       // test the TextField properties
77
String JavaDoc value = "", tmpName = "";
78       for (int i = 0; i < cd.getNumTexts(); i++) {
79         name = cd.getTextNameAt(i);
80         value = cd.getTextValueAt(i);
81         tmpName = nsPrefix + name;
82         try {
83           assertEquals(props.getProperty(tmpName), value);
84         } catch (Exception JavaDoc ex) {
85           ex.printStackTrace();
86           fail();
87         }
88         totalProps++;
89       }
90       assertEquals(totalProps, props.size());
91     } catch (IOException ioEx) {
92       ioEx.printStackTrace();
93       fail();
94     }
95   }
96
97   /**
98    * Test the reading of a config file and loading of the m_populatioSize
99    * variable of the Configuration Configurable.
100    *
101    * @throws Exception
102    *
103    * @author Siddhartha Azad
104    * @since 2.3
105    */

106   public void testConfigReader_0()
107       throws Exception JavaDoc {
108     Configuration.reset();
109     Configuration conf = new Configuration("jgapTest.con", false);
110     assertEquals(conf.getPopulationSize(), 35);
111     // Some setup so that the Configuration doesn't throw an Exception. We do
112
// not set the population size of the Configuration since this is supposed
113
// to be read directly from the config file.
114
// set up a sample chromosome
115
Gene[] sampleGenes = new Gene[3];
116     sampleGenes[0] = new IntegerGene(conf, 60, 100);
117     sampleGenes[1] = new IntegerGene(conf, 1, 50);
118     sampleGenes[2] = new IntegerGene(conf, 100, 150);
119     Chromosome sampleChromosome = new Chromosome(conf, sampleGenes);
120     FitnessFunction fitFunc = new StaticFitnessFunction(100.0d);
121     conf.setFitnessFunction(fitFunc);
122     // The higher the value, the better
123
conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
124     conf.setSampleChromosome(sampleChromosome);
125     BestChromosomesSelector bestChromsSelector = new
126         BestChromosomesSelector(conf, 1.0d);
127     bestChromsSelector.setDoubletteChromosomesAllowed(false);
128     conf.addNaturalSelector(bestChromsSelector, true);
129     conf.setRandomGenerator(new StockRandomGenerator());
130     conf.setEventManager(new EventManager());
131     conf.addGeneticOperator(new CrossoverOperator(conf));
132     conf.addGeneticOperator(new MutationOperator(conf, 15));
133     Genotype.randomInitialGenotype(conf);
134   }
135
136   /**
137    *
138    * @throws Exception
139    *
140    * @author Klaus Meffert
141    * @since 3.1
142    */

143   public void testGetInstance_0()
144       throws Exception JavaDoc {
145     ConfigWriter inst = ConfigWriter.getInstance();
146     assertSame(inst, ConfigWriter.getInstance());
147   }
148 }
149
Popular Tags