1 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 26 public class ConfigWriterTest 27 extends JGAPTestCase { 28 29 private final static String CVS_REVISION = "$Revision: 1.17 $"; 30 31 public static Test suite() { 32 TestSuite suite = new TestSuite(ConfigWriterTest.class); 33 return suite; 34 } 35 36 46 public void testConfigData_0() 47 throws Exception { 48 IConfigInfo ici = new MockConfigInfoForTest(); 49 ConfigWriter.getInstance().write(ici); 50 int totalProps = 0; 52 Properties props = new Properties(); 53 try { 54 props.load(new FileInputStream("jgapTmp.con")); 55 56 ConfigData cd = ici.getConfigData(); 57 String nsPrefix = cd.getNS() + "."; 58 String 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 String tmpName = nsPrefix + name + "[" + idx + "]"; 67 try { 68 assertEquals(props.getProperty(tmpName), (String ) iter.next()); 69 } catch (Exception ex) { 70 ex.printStackTrace(); 71 fail(); 72 } 73 totalProps++; 74 } 75 } 76 String 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 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 106 public void testConfigReader_0() 107 throws Exception { 108 Configuration.reset(); 109 Configuration conf = new Configuration("jgapTest.con", false); 110 assertEquals(conf.getPopulationSize(), 35); 111 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 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 143 public void testGetInstance_0() 144 throws Exception { 145 ConfigWriter inst = ConfigWriter.getInstance(); 146 assertSame(inst, ConfigWriter.getInstance()); 147 } 148 } 149 | Popular Tags |