KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > ConfigurationTest


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

22 public class ConfigurationTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.36 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(ConfigurationTest.class);
29     return suite;
30   }
31
32   public void setUp() {
33     super.setUp();
34     // Important: Reset the configurational parameters set.
35
// ----------------------------------------------------
36
Configuration.reset();
37   }
38
39   /**
40    * @author Klaus Meffert
41    */

42   public void testAddGeneticOperator_0() {
43     Configuration conf = new Configuration();
44     try {
45       conf.addGeneticOperator(null);
46       fail();
47     } catch (InvalidConfigurationException invex) {
48       ; //this is OK
49
}
50   }
51
52   /**
53    * @author Klaus Meffert
54    */

55   public void testAddGeneticOperator_1() {
56     Configuration conf = new Configuration();
57     try {
58       conf.lockSettings();
59       conf.addGeneticOperator(new MutationOperator());
60       fail();
61     } catch (InvalidConfigurationException invex) {
62       ; //this is OK
63
}
64   }
65
66   /**
67    * @throws Exception
68    *
69    * @author Klaus Meffert
70    */

71   public void testAddGeneticOperator_2()
72       throws Exception JavaDoc {
73     Configuration conf = new Configuration();
74     conf.addGeneticOperator(new MutationOperator(conf));
75     assertEquals(1, conf.getGeneticOperators().size());
76     conf.addGeneticOperator(new MutationOperator(conf));
77     assertEquals(2, conf.getGeneticOperators().size());
78   }
79
80   /**
81    * @throws Exception
82    * @author Klaus Meffert
83    */

84   public void testVerifyStateIsValid_0()
85       throws Exception JavaDoc {
86     Configuration conf = new Configuration();
87     assertEquals(false, conf.isLocked());
88     conf.setFitnessFunction(new StaticFitnessFunction(2));
89     try {
90       conf.verifyStateIsValid();
91       fail();
92     } catch (InvalidConfigurationException invex) {
93       ; //this is OK
94
}
95   }
96
97   /**
98    * @throws Exception
99    * @author Klaus Meffert
100    */

101   public void testVerifyStateIsValid_1()
102       throws Exception JavaDoc {
103     Configuration conf = new Configuration();
104     assertEquals(false, conf.isLocked());
105     conf.setFitnessFunction(new StaticFitnessFunction(2));
106     Gene gene = new BooleanGene(conf);
107     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
108     try {
109       conf.verifyStateIsValid();
110       fail();
111     } catch (InvalidConfigurationException invex) {
112       ; //this is OK
113
}
114   }
115
116   /**
117    * @throws Exception
118    * @author Klaus Meffert
119    */

120   public void testVerifyStateIsValid_2()
121       throws Exception JavaDoc {
122     Configuration conf = new Configuration();
123     assertEquals(false, conf.isLocked());
124     conf.setFitnessFunction(new StaticFitnessFunction(2));
125     Gene gene = new BooleanGene(conf);
126     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
127     conf.addNaturalSelector(new WeightedRouletteSelector(conf), false);
128     try {
129       conf.verifyStateIsValid();
130       fail();
131     } catch (InvalidConfigurationException invex) {
132       ; //this is OK
133
}
134   }
135
136   /**
137    * @throws Exception
138    * @author Klaus Meffert
139    */

140   public void testVerifyStateIsValid_3()
141       throws Exception JavaDoc {
142     Configuration conf = new Configuration();
143     assertEquals(false, conf.isLocked());
144     conf.setFitnessFunction(new StaticFitnessFunction(2));
145     Gene gene = new BooleanGene(conf);
146     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
147     conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
148     conf.setRandomGenerator(new StockRandomGenerator());
149     try {
150       conf.verifyStateIsValid();
151       fail();
152     } catch (InvalidConfigurationException invex) {
153       ; //this is OK
154
}
155   }
156
157   /**
158    * @throws Exception
159    * @author Klaus Meffert
160    */

161   public void testVerifyStateIsValid_4()
162       throws Exception JavaDoc {
163     Configuration conf = new Configuration();
164     assertEquals(false, conf.isLocked());
165     conf.setFitnessFunction(new StaticFitnessFunction(2));
166     Gene gene = new BooleanGene(conf);
167     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
168     conf.addNaturalSelector(new WeightedRouletteSelector(conf), false);
169     conf.setRandomGenerator(new StockRandomGenerator());
170     conf.setEventManager(new EventManager());
171     try {
172       conf.verifyStateIsValid();
173       fail();
174     } catch (InvalidConfigurationException invex) {
175       ; //this is OK
176
}
177   }
178
179   /**
180    * @throws Exception
181    * @author Klaus Meffert
182    */

183   public void testVerifyStateIsValid_5()
184       throws Exception JavaDoc {
185     Configuration conf = new Configuration();
186     assertEquals(false, conf.isLocked());
187     conf.setFitnessFunction(new StaticFitnessFunction(2));
188     Gene gene = new BooleanGene(conf);
189     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
190     conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
191     conf.setRandomGenerator(new StockRandomGenerator());
192     conf.setEventManager(new EventManager());
193     conf.addGeneticOperator(new MutationOperator(conf));
194     try {
195       conf.verifyStateIsValid();
196       fail();
197     } catch (InvalidConfigurationException invex) {
198       ; //this is OK
199
}
200   }
201
202   /**
203    * @throws Exception
204    *
205    * @author Klaus Meffert
206    */

207   public void testVerifyStateIsValid_6()
208       throws Exception JavaDoc {
209     Configuration conf = new Configuration();
210     assertEquals(false, conf.isLocked());
211     conf.setFitnessFunction(new StaticFitnessFunction(2));
212     Gene gene = new BooleanGene(conf);
213     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
214     conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
215     conf.setRandomGenerator(new StockRandomGenerator());
216     conf.setEventManager(new EventManager());
217     conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
218     conf.addGeneticOperator(new MutationOperator(conf));
219     conf.setPopulationSize(1);
220     conf.verifyStateIsValid();
221   }
222
223   /**
224    * @throws Exception
225    *
226    * @author Klaus Meffert
227    */

228   public void testVerifyStateIsValid_7()
229       throws Exception JavaDoc {
230     Configuration conf = new Configuration();
231     assertEquals(false, conf.isLocked());
232     conf.setFitnessFunction(new StaticFitnessFunction(2));
233     Gene gene = new BooleanGene(conf);
234     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
235     conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
236     conf.setRandomGenerator(new StockRandomGenerator());
237     conf.setEventManager(new EventManager());
238     conf.addGeneticOperator(new MutationOperator(conf));
239     conf.setPopulationSize(1);
240     try {
241       conf.verifyStateIsValid();
242       fail();
243     } catch (IllegalArgumentException JavaDoc illex) {
244       ; //this is OK
245
}
246   }
247
248   /**
249    * Configuration with sample chromosome that contains no genes.
250    *
251    * @throws Exception
252    *
253    * @author Klaus Meffert
254    * @since 2.6
255    */

256   public void testVerifyStateIsValid_8()
257       throws Exception JavaDoc {
258     Configuration conf = new DefaultConfiguration();
259     conf.setFitnessFunction(new TestFitnessFunction());
260     Chromosome sample = new Chromosome(conf);
261     conf.setSampleChromosome(sample);
262     conf.setPopulationSize(5);
263     try {
264       conf.verifyStateIsValid();
265       fail();
266     } catch (InvalidConfigurationException illex) {
267       ; //this is OK
268
}
269   }
270
271   /**
272    * Configuration with sample chromosome that has a gene with allele value
273    * null.
274    *
275    * @throws Exception
276    *
277    * @author Klaus Meffert
278    * @since 2.6
279    */

280   public void testVerifyStateIsValid_9()
281       throws Exception JavaDoc {
282     Configuration conf = new DefaultConfiguration();
283     conf.setFitnessFunction(new TestFitnessFunction());
284     BooleanGene gene = new BooleanGene(conf);
285     gene.setAllele(null);
286     Chromosome sample = new Chromosome(conf, gene, 55);
287     conf.setSampleChromosome(sample);
288     conf.setPopulationSize(5);
289     conf.verifyStateIsValid();
290   }
291
292   /**
293    * @throws Exception
294    *
295    * @author Klaus Meffert
296    */

297   public void testIsLocked_0()
298       throws Exception JavaDoc {
299     Configuration conf = new Configuration();
300     assertEquals(false, conf.isLocked());
301     conf.setFitnessFunction(new StaticFitnessFunction(2));
302     conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
303     Gene gene = new BooleanGene(conf);
304     conf.setSampleChromosome(new Chromosome(conf, gene, 5));
305     conf.addNaturalSelector(new WeightedRouletteSelector(conf), false);
306     conf.setRandomGenerator(new StockRandomGenerator());
307     conf.setEventManager(new EventManager());
308     conf.addGeneticOperator(new MutationOperator(conf));
309     conf.setPopulationSize(1);
310     assertEquals(false, conf.isLocked());
311     conf.lockSettings();
312     assertEquals(true, conf.isLocked());
313   }
314
315   /**
316    * @throws Exception
317    *
318    * @author Klaus Meffert
319    */

320   public void testGetter_0()
321       throws Exception JavaDoc {
322     Configuration conf = new Configuration();
323     assertEquals(false, conf.isLocked());
324     FitnessFunction fitFunc = new StaticFitnessFunction(2);
325     conf.setFitnessFunction(fitFunc);
326     Gene gene = new BooleanGene(conf);
327     Chromosome sample = new Chromosome(conf, gene, 55);
328     conf.setSampleChromosome(sample);
329     NaturalSelector natSel = new WeightedRouletteSelector();
330     conf.addNaturalSelector(natSel, false);
331     RandomGenerator randGen = new StockRandomGenerator();
332     conf.setRandomGenerator(randGen);
333     IEventManager evMan = new EventManager();
334     conf.setEventManager(evMan);
335     GeneticOperator mutOp = new MutationOperator(conf);
336     conf.addGeneticOperator(mutOp);
337     GeneticOperator croOp = new CrossoverOperator(conf);
338     conf.addGeneticOperator(croOp);
339     conf.setPopulationSize(7);
340     assertEquals(fitFunc, conf.getFitnessFunction());
341     assertEquals(natSel, conf.getNaturalSelectors(false).get(0));
342     assertEquals(randGen, conf.getRandomGenerator());
343     assertEquals(sample, conf.getSampleChromosome());
344     assertEquals(evMan, conf.getEventManager());
345     assertEquals(7, conf.getPopulationSize());
346     assertEquals(2, conf.getGeneticOperators().size());
347     assertEquals(mutOp, conf.getGeneticOperators().get(0));
348     assertEquals(croOp, conf.getGeneticOperators().get(1));
349   }
350
351   /**
352    * Tests a deprecated function!
353    * @throws Exception
354    */

355   public void testSetNaturalSelector_0()
356       throws Exception JavaDoc {
357     Configuration conf = new Configuration();
358     NaturalSelector selector = new WeightedRouletteSelector();
359     conf.setNaturalSelector(selector);
360     assertEquals(selector, conf.getNaturalSelectors(false).get(0));
361   }
362
363   /**
364    * Tests a deprecated function!
365    */

366   public void testGetNaturalSelector_0() {
367     Configuration conf = new Configuration();
368     NaturalSelector selector = conf.getNaturalSelector();
369     assertEquals(null, selector);
370   }
371
372   /**
373    * @throws Exception
374    *
375    * @author Klaus Meffert
376    * @since 2.2
377    */

378   public void testGetNaturalSelector_1()
379       throws Exception JavaDoc {
380     Configuration conf = new Configuration();
381     NaturalSelector selector = new BestChromosomesSelector(conf);
382     conf.addNaturalSelector(selector, false);
383     assertEquals(selector, conf.getNaturalSelector());
384   }
385
386   /**
387    * @throws Exception
388    * @author Klaus Meffert
389    */

390   public void testGetNaturalSelector_2()
391       throws Exception JavaDoc {
392     Configuration conf = new Configuration();
393     try {
394       conf.getNaturalSelector(true, 0);
395       fail();
396     } catch (IllegalArgumentException JavaDoc iex) {
397       ; //this is OK
398
}
399   }
400
401   /**
402    * @throws Exception
403    * @author Klaus Meffert
404    */

405   public void testGetNaturalSelector_3()
406       throws Exception JavaDoc {
407     Configuration conf = new Configuration();
408     try {
409       conf.getNaturalSelector(false, 0);
410       fail();
411     } catch (IllegalArgumentException JavaDoc iex) {
412       ; //this is OK
413
}
414   }
415
416   /**
417    * @throws Exception
418    *
419    * @author Klaus Meffert
420    * @since 2.2
421    */

422   public void testGetNaturalSelector_4()
423       throws Exception JavaDoc {
424     Configuration conf = new Configuration();
425     NaturalSelector selector = new BestChromosomesSelector(conf);
426     conf.addNaturalSelector(selector, false);
427     assertEquals(selector, conf.getNaturalSelector(false, 0));
428   }
429
430   /**
431    * @throws Exception
432    * @author Klaus Meffert
433    */

434   public void testAddNaturalSelector_0()
435       throws Exception JavaDoc {
436     Configuration conf = new Configuration();
437     NaturalSelector selector = new WeightedRouletteSelector();
438     conf.addNaturalSelector(selector, true);
439     assertEquals(selector, conf.getNaturalSelectors(true).get(0));
440   }
441
442   /**
443    * @throws Exception
444    * @author Klaus Meffert
445    */

446   public void testAddNaturalSelector_1()
447       throws Exception JavaDoc {
448     Configuration conf = new Configuration();
449     NaturalSelector selector = new WeightedRouletteSelector();
450     conf.addNaturalSelector(selector, false);
451     assertEquals(selector, conf.getNaturalSelectors(false).get(0));
452   }
453
454   /**
455    * @throws Exception
456    * @author Klaus Meffert
457    */

458   public void testAddNaturalSelector_2()
459       throws Exception JavaDoc {
460     Configuration conf = new Configuration();
461     NaturalSelector selector1 = new WeightedRouletteSelector();
462     NaturalSelector selector2 = new WeightedRouletteSelector();
463     conf.addNaturalSelector(selector1, false);
464     assertEquals(selector1, conf.getNaturalSelectors(false).get(0));
465     conf.addNaturalSelector(selector2, true);
466     assertEquals(selector2, conf.getNaturalSelectors(true).get(0));
467     assertEquals(selector1, conf.getNaturalSelectors(false).get(0));
468     try {
469       assertEquals(null, conf.getNaturalSelectors(false).get(1));
470       fail();
471     } catch (Exception JavaDoc ex) {
472       ; //this is OK
473
}
474   }
475
476   /**
477    * @throws Exception
478    * @author Klaus Meffert
479    */

480   public void testAddNaturalSelector_3()
481       throws Exception JavaDoc {
482     Configuration conf = new Configuration();
483     NaturalSelector selector = new WeightedRouletteSelector();
484     conf.addNaturalSelector(selector, false);
485     conf.getNaturalSelectors(false).clear();
486     conf.addNaturalSelector(selector, false);
487     Integer JavaDoc i = (Integer JavaDoc) privateAccessor.getField(conf,
488         "m_sizeNaturalSelectorsPost");
489     assertEquals(1, i.intValue());
490   }
491
492   /**
493    * @throws Exception
494    * @author Klaus Meffert
495    */

496   public void testAddNaturalSelector_4()
497       throws Exception JavaDoc {
498     Configuration conf = new Configuration();
499     NaturalSelector selector = new WeightedRouletteSelector();
500     conf.addNaturalSelector(selector, true);
501     conf.getNaturalSelectors(true).clear();
502     conf.addNaturalSelector(selector, true);
503     Integer JavaDoc i = (Integer JavaDoc) privateAccessor.getField(conf,
504         "m_sizeNaturalSelectorsPre");
505     assertEquals(1, i.intValue());
506   }
507
508   /**
509    * @author Klaus Meffert
510    */

511   public void testSetFitnessFunction_0() {
512     Configuration conf = new Configuration();
513     try {
514       conf.setFitnessFunction(null);
515       fail();
516     } catch (InvalidConfigurationException invex) {
517       ; //this is OK
518
}
519   }
520
521   /**
522    * @throws Exception
523    *
524    * @author Klaus Meffert
525    * @since 3.0
526    */

527   public void testSetFitnessFunction_1()
528       throws Exception JavaDoc {
529     Configuration conf = new Configuration();
530     conf.setFitnessFunction(new TestFitnessFunction());
531     try {
532       conf.setFitnessFunction(new StaticFitnessFunction(2.3d));
533       fail();
534     } catch (RuntimeException JavaDoc invex) {
535       ; //this is OK
536
}
537   }
538
539   /**
540    * @throws Exception
541    *
542    * @author Klaus Meffert
543    * @since 3.0
544    */

545   public void testSetFitnessFunction_2()
546       throws Exception JavaDoc {
547     Configuration conf = new Configuration();
548     conf.setFitnessFunction(new TestFitnessFunction());
549     Configuration conf2 = new Configuration();
550     try {
551       conf2.setFitnessFunction(new StaticFitnessFunction(2.3d));
552       fail();
553     } catch (RuntimeException JavaDoc invex) {
554       ; //this is OK
555
}
556   }
557
558   /**
559    * @throws Exception
560    *
561    * @author Klaus Meffert
562    * @since 3.0
563    */

564   public void testSetFitnessFunction_3()
565       throws Exception JavaDoc {
566     Configuration conf = new Configuration();
567     conf.setFitnessFunction(new StaticFitnessFunction(2.3d));
568     Configuration conf2 = new Configuration();
569     try {
570       conf2.setFitnessFunction(new StaticFitnessFunction(4.5d));
571       fail();
572     } catch (RuntimeException JavaDoc invex) {
573       ; //this is OK
574
}
575   }
576
577   /**
578    * @throws Exception
579    *
580    * @author Klaus Meffert
581    * @since 3.0
582    */

583   public void testSetFitnessFunction_4()
584       throws Exception JavaDoc {
585     new Thread JavaDoc(new MyThread()).start();
586     new Thread JavaDoc(new MyThread()).start();
587   }
588
589   /**
590    * @author Klaus Meffert
591    */

592   public void testSetFitnessEvaluator_0() {
593     Configuration conf = new Configuration();
594     try {
595       conf.setFitnessEvaluator(null);
596       fail();
597     } catch (IllegalStateException JavaDoc istex) {
598       ; //this is OK
599
}
600   }
601
602   /**
603    * @author Klaus Meffert
604    */

605   public void testSetPreserveFittestIndividual_0() {
606     Configuration conf = new Configuration();
607     assertFalse(conf.isPreserveFittestIndividual());
608     conf.setPreservFittestIndividual(true);
609     assertTrue(conf.isPreserveFittestIndividual());
610     conf.setPreservFittestIndividual(false);
611     assertFalse(conf.isPreserveFittestIndividual());
612   }
613
614   /**
615    * @author Klaus Meffert
616    */

617   public void testGenerationNr_0() {
618     Configuration conf = new Configuration();
619     assertEquals(0, conf.getGenerationNr());
620     conf.incrementGenerationNr();
621     assertEquals(1, conf.getGenerationNr());
622     conf.incrementGenerationNr();
623     conf.incrementGenerationNr();
624     assertEquals(3, conf.getGenerationNr());
625   }
626
627   /**
628    * @throws Exception
629    * @author Klaus Meffert
630    */

631   public void testSetBulkFitnessFunction_0() {
632     Configuration conf = new Configuration();
633     try {
634       conf.setBulkFitnessFunction(null);
635       fail();
636     } catch (InvalidConfigurationException invex) {
637       ; //this is OK
638
}
639   }
640
641   /**
642    * @throws Exception
643    * @author Klaus Meffert
644    */

645   public void testSetBulkFitnessFunction_1()
646       throws Exception JavaDoc {
647     Configuration conf = new Configuration();
648     conf.setFitnessFunction(new TestFitnessFunction());
649     try {
650       conf.setBulkFitnessFunction(new TestBulkFitnessFunction());
651       fail();
652     } catch (InvalidConfigurationException invex) {
653       ; //this is OK
654
}
655   }
656
657   /**
658    * @throws Exception
659    * @author Klaus Meffert
660    */

661   public void testSetBulkFitnessFunction_2()
662       throws Exception JavaDoc {
663     Configuration conf = new Configuration();
664     conf.setBulkFitnessFunction(new TestBulkFitnessFunction());
665     try {
666       conf.setFitnessFunction(new TestFitnessFunction());
667       fail();
668     } catch (InvalidConfigurationException invex) {
669       ; //this is OK
670
}
671   }
672
673   /**
674    * @throws Exception
675    * @author Klaus Meffert
676    */

677   public void testSetBulkFitnessFunction_3()
678       throws Exception JavaDoc {
679     Configuration conf = new Configuration();
680     conf.setBulkFitnessFunction(new TestBulkFitnessFunction());
681   }
682
683   /**
684    * @throws Exception
685    *
686    * @author Klaus Meffert
687    * @since 3.0
688    */

689   public void testSetBulkFitnessFunction_4()
690       throws Exception JavaDoc {
691     Configuration conf = new Configuration();
692     conf.setBulkFitnessFunction(new TestBulkFitnessFunction());
693     try {
694       conf.setBulkFitnessFunction(new TestBulkFitnessFunction2());
695       fail();
696     } catch (RuntimeException JavaDoc rex) {
697       ; //this is OK
698
}
699   }
700
701   /**
702    * Setting the same bulk fitness function should be possible without error.
703    * @throws Exception
704    *
705    * @author Klaus Meffert
706    * @since 3.0
707    */

708   public void testSetBulkFitnessFunction_5()
709       throws Exception JavaDoc {
710     Configuration conf = new Configuration();
711     conf.setBulkFitnessFunction(new TestBulkFitnessFunction());
712     conf.setBulkFitnessFunction(new TestBulkFitnessFunction());
713   }
714
715   /**
716    * @throws Exception
717    *
718    * @author Klaus Meffert
719    * @since 3.0
720    */

721   public void testSetBulkFitnessFunction_6()
722       throws Exception JavaDoc {
723     new Thread JavaDoc(new MyThreadBulk()).start();
724     new Thread JavaDoc(new MyThreadBulk()).start();
725   }
726
727   /**
728    * @throws Exception
729    * @author Klaus Meffert
730    */

731   public void testGetPopulationSize_0()
732       throws Exception JavaDoc {
733     Configuration conf = new Configuration();
734     assertEquals(0, conf.getPopulationSize());
735     final int SIZE = 22;
736     conf.setPopulationSize(SIZE);
737     assertEquals(SIZE, conf.getPopulationSize());
738   }
739
740   /**
741    * @throws Exception
742    * @author Klaus Meffert
743    */

744   public void testSetPopulationSize_1()
745       throws Exception JavaDoc {
746     Configuration conf = new Configuration();
747     try {
748       conf.setPopulationSize(0);
749       fail();
750     } catch (InvalidConfigurationException invex) {
751       ; //this is OK
752
}
753   }
754
755   /**
756    * @author Klaus Meffert
757    */

758   public void testSetRandomGenerator_0() {
759     Configuration conf = new Configuration();
760     try {
761       conf.setRandomGenerator(null);
762       fail();
763     } catch (InvalidConfigurationException invex) {
764       ; //this is OK
765
}
766   }
767
768   /**
769    * @author Klaus Meffert
770    */

771   public void testSetEventManager_0() {
772     Configuration conf = new Configuration();
773     try {
774       conf.setEventManager(null);
775       fail();
776     } catch (InvalidConfigurationException invex) {
777       ; //this is OK
778
}
779   }
780
781   /**
782    * @throws Exception
783    *
784    * @author Klaus Meffert
785    * @since 3.0
786    */

787   public void testSetEventManager_1()
788       throws Exception JavaDoc {
789     Configuration conf = new Configuration();
790     conf.setEventManager(new EventManager());
791     conf.setEventManager(new EventManager());
792   }
793
794   /**
795    * @throws Exception
796    *
797    * @author Klaus Meffert
798    * @since 3.0
799    */

800   public void testSetEventManager_2()
801       throws Exception JavaDoc {
802     Configuration conf = new Configuration();
803     conf.setEventManager(new EventManager());
804     try {
805       conf.setEventManager(new TestEventManager());
806       fail();
807     } catch (RuntimeException JavaDoc rex) {
808       ; //this is OK
809
}
810   }
811
812   /**
813    * @throws Exception
814    *
815    * @author Klaus Meffert
816    * @since 3.0
817    */

818   public void testSetEventManager_3()
819       throws Exception JavaDoc {
820     new Thread JavaDoc(new MyThreadEvent()).start();
821     new Thread JavaDoc(new MyThreadEvent()).start();
822   }
823
824   /**
825    * @throws Exception
826    * @author Klaus Meffert
827    */

828   public void testLock_0()
829       throws Exception JavaDoc {
830     Configuration conf = new Configuration();
831     try {
832       conf.lockSettings();
833       fail();
834     } catch (InvalidConfigurationException invex) {
835       ; //this is OK
836
}
837   }
838
839   /**
840    * @throws Exception
841    * @author Klaus Meffert
842    */

843   public void testLock_1()
844       throws Exception JavaDoc {
845     Configuration conf = new Configuration();
846     conf.setFitnessFunction(new TestFitnessFunction());
847     Gene gene = new BooleanGene(conf);
848     Chromosome sample = new Chromosome(conf, gene, 55);
849     conf.setSampleChromosome(sample);
850     conf.addNaturalSelector(new WeightedRouletteSelector(conf), false);
851     conf.setRandomGenerator(new GaussianRandomGenerator());
852     conf.setEventManager(new EventManager());
853     conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
854     conf.addGeneticOperator(new MutationOperator(conf));
855     conf.setPopulationSize(1);
856     conf.lockSettings();
857     assertEquals(true, conf.isLocked());
858     try {
859       conf.verifyChangesAllowed();
860       fail();
861     } catch (InvalidConfigurationException invex) {
862       ; //this is OK
863
}
864   }
865
866   /**
867    * @throws Exception
868    * @author Klaus Meffert
869    */

870   public void testSetSampleChromosome_0()
871       throws Exception JavaDoc {
872     Configuration conf = new Configuration();
873     Gene gene = new BooleanGene(conf);
874     new Chromosome(conf, gene, 55);
875     try {
876       conf.setSampleChromosome(null);
877       fail();
878     } catch (InvalidConfigurationException invex) {
879       ; //this is OK
880
}
881   }
882
883   /**
884    * @throws Exception
885    * @author Klaus Meffert
886    */

887   public void testGetChromosomeSize_0()
888       throws Exception JavaDoc {
889     Configuration conf = new Configuration();
890     Gene gene = new BooleanGene(conf);
891     final int SIZE = 55;
892     Chromosome sample = new Chromosome(conf, gene, SIZE);
893     conf.setSampleChromosome(sample);
894     assertEquals(SIZE, conf.getChromosomeSize());
895   }
896
897   /**
898    *
899    * @throws Exception
900    *
901    * @author Klaus Meffert
902    * @since 2.4
903    */

904   public void testToString_0()
905       throws Exception JavaDoc {
906     Configuration conf = new ConfigurationForTest();
907     conf.addGeneticOperator(new MutationOperator(conf));
908     conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
909     conf.addNaturalSelector(new WeightedRouletteSelector(conf), false);
910     conf.addNaturalSelector(new BestChromosomesSelector(conf), false);
911     String JavaDoc s = trimString(conf.toString());
912     String JavaDoc eventmgr = conf.getEventManager() != null ?
913         conf.getEventManager().getClass().getName() : conf.S_NONE;
914     String JavaDoc genops = "";
915     if (conf.getGeneticOperators().size() < 1) {
916       genops = conf.S_NONE;
917     }
918     else {
919       for (int i = 0; i < conf.getGeneticOperators().size(); i++) {
920         if (i > 0) {
921           genops += "; ";
922         }
923         genops += conf.getGeneticOperators().get(i).getClass().getName(); ;
924       }
925     }
926     // natural selectors (pre)
927
String JavaDoc natselsPre = "";
928     int natsize = conf.getNaturalSelectors(true).size();
929     if (natsize < 1) {
930       natselsPre = conf.S_NONE;
931     }
932     else {
933       for (int i = 0; i < natsize; i++) {
934         if (i > 0) {
935           natselsPre += "; ";
936         }
937         natselsPre += " " +
938             conf.getNaturalSelectors(true).get(i).getClass().getName();
939       }
940     }
941     // natural selectors (post)
942
String JavaDoc natselsPost = "";
943     natsize = conf.getNaturalSelectors(false).size();
944     if (natsize < 1) {
945       natselsPost = conf.S_NONE;
946     }
947     else {
948       for (int i = 0; i < natsize; i++) {
949         if (i > 0) {
950           natselsPost += "; ";
951         }
952         natselsPost += " " +
953             conf.getNaturalSelectors(false).get(i).getClass().getName();
954       }
955     }
956     assertEquals(trimString(conf.S_CONFIGURATION + ":"
957                             + conf.S_CONFIGURATION_NAME + ":"
958                             + conf.getName() + " "
959                             + conf.S_POPULATION_SIZE + ":"
960                             + conf.getPopulationSize() + " "
961                             + conf.S_MINPOPSIZE + ":"
962                             + conf.getMinimumPopSizePercent() + " "
963                             + conf.S_CHROMOSOME_SIZE + ":"
964                             + conf.getChromosomeSize() + " "
965                             + conf.S_SAMPLE_CHROM + ":"
966                             + conf.S_SIZE + ":"
967                             + conf.getSampleChromosome().size() + " "
968                             + conf.S_TOSTRING + ":"
969                             + conf.getSampleChromosome().toString() + " "
970                             + conf.S_RANDOM_GENERATOR + ":"
971                             + conf.getRandomGenerator().getClass().getName() +
972                             " "
973                             + conf.S_EVENT_MANAGER + ":"
974                             + eventmgr + " "
975                             + conf.S_CONFIGURATION_HANDLER + ":"
976                             + conf.getConfigurationHandler().getName() + " "
977                             + conf.S_FITNESS_FUNCTION + ":"
978                             + conf.getFitnessFunction().getClass().getName() +
979                             " "
980                             + conf.S_FITNESS_EVALUATOR + ":"
981                             + conf.getFitnessEvaluator().getClass().getName() +
982                             " "
983                             + conf.S_GENETIC_OPERATORS + ":"
984                             + genops + " "
985                             + conf.S_NATURAL_SELECTORS + "(" + conf.S_PRE + ")" +
986                             ":"
987                             + natselsPre + " "
988                             + conf.S_NATURAL_SELECTORS + "(" + conf.S_POST +
989                             ")" + ":"
990                             + natselsPost + " "
991                  ), s);
992   }
993
994   /**
995    * @throws Exception
996    *
997    * @author Klaus Meffert
998    * @since 2.6
999    */

1000  public void testToString_1()
1001      throws Exception JavaDoc {
1002    Configuration conf = new ConfigurationForTest();
1003    conf.getGeneticOperators().clear();
1004    conf.getNaturalSelectors(false).clear();
1005    conf.getNaturalSelectors(true).clear();
1006    privateAccessor.setField(conf, "m_eventManager", null);
1007    String JavaDoc s = trimString(conf.toString());
1008    String JavaDoc eventmgr = conf.S_NONE;
1009    String JavaDoc genops = conf.S_NONE;
1010    // natural selectors (pre)
1011
String JavaDoc natselsPre = conf.S_NONE;
1012    // natural selectors (post)
1013
String JavaDoc natselsPost = conf.S_NONE;
1014    assertEquals(trimString(conf.S_CONFIGURATION + ":"
1015                            + conf.S_CONFIGURATION_NAME + ":"
1016                            + conf.getName() + " "
1017                            + conf.S_POPULATION_SIZE + ":"
1018                            + conf.getPopulationSize() + " "
1019                            + conf.S_MINPOPSIZE + ":"
1020                            + conf.getMinimumPopSizePercent() + " "
1021                            + conf.S_CHROMOSOME_SIZE + ":"
1022                            + conf.getChromosomeSize() + " "
1023                            + conf.S_SAMPLE_CHROM + ":"
1024                            + conf.S_SIZE + ":"
1025                            + conf.getSampleChromosome().size() + " "
1026                            + conf.S_TOSTRING + ":"
1027                            + conf.getSampleChromosome().toString() + " "
1028                            + conf.S_RANDOM_GENERATOR + ":"
1029                            + conf.getRandomGenerator().getClass().getName() +
1030                            " "
1031                            + conf.S_EVENT_MANAGER + ":"
1032                            + eventmgr + " "
1033                            + conf.S_CONFIGURATION_HANDLER + ":"
1034                            + conf.getConfigurationHandler().getName() + " "
1035                            + conf.S_FITNESS_FUNCTION + ":"
1036                            + conf.getFitnessFunction().getClass().getName() +
1037                            " "
1038                            + conf.S_FITNESS_EVALUATOR + ":"
1039                            + conf.getFitnessEvaluator().getClass().getName() +
1040                            " "
1041                            + conf.S_GENETIC_OPERATORS + ":"
1042                            + genops + " "
1043                            + conf.S_NATURAL_SELECTORS + "(" + conf.S_PRE + ")" +
1044                            ":"
1045                            + natselsPre + " "
1046                            + conf.S_NATURAL_SELECTORS + "(" + conf.S_POST +
1047                            ")" + ":"
1048                            + natselsPost + " "
1049                 ), s);
1050  }
1051
1052  /**
1053   *
1054   * @param s1 String
1055   * @return String
1056   *
1057   * @author Klaus Meffert
1058   * @since 2.4
1059   */

1060  private String JavaDoc trimString(String JavaDoc s1) {
1061    String JavaDoc result = s1;
1062    int index = 0;
1063    while (index < result.length()) {
1064      if (result.charAt(index) == '\n') {
1065        result = result.substring(0, index) + result.substring(index + 1);
1066      }
1067      else {
1068        if (result.charAt(index) == ':') {
1069          if (index < result.length() - 1) {
1070            if (result.charAt(index + 1) == ' ' ||
1071                result.charAt(index + 1) == '\n') {
1072              result = result.substring(0, index + 1) +
1073                  result.substring(index + 2);
1074            }
1075            else {
1076              index++;
1077            }
1078          }
1079          else {
1080            index++;
1081          }
1082        }
1083        else if (result.charAt(index) == ' ') {
1084          if (index == 0) {
1085            result = result.substring(1);
1086          }
1087          else if (index == result.length() - 1) {
1088            result = result.substring(0, result.length() - 1);
1089          }
1090          else
1091          if (result.charAt(index + 1) == ' ' ||
1092              result.charAt(index + 1) == '\n') {
1093            result = result.substring(0, index) + result.substring(index + 1);
1094          }
1095          else {
1096            index++;
1097          }
1098        }
1099        else {
1100          index++;
1101        }
1102      }
1103    }
1104    return result;
1105  }
1106
1107  /**
1108   * @author Klaus Meffert
1109   */

1110  public void testGetName_0() {
1111    Configuration conf = new Configuration("tEstName");
1112    assertEquals("tEstName", conf.getName());
1113  }
1114
1115  /**
1116   * @author Klaus Meffert
1117   */

1118  public void testGetName_1() {
1119    Configuration conf = new Configuration("tEstName");
1120    conf.setName("HallI");
1121    assertEquals("HallI", conf.getName());
1122    conf.setName("HallA");
1123    assertEquals("HallA", conf.getName());
1124  }
1125
1126  /**
1127   * @author Klaus Meffert
1128   */

1129  public void testGetName_2() {
1130    Configuration conf = new Configuration();
1131    assertEquals(null, conf.getName());
1132  }
1133
1134  /**
1135   * @author Klaus Meffert
1136   * @since 2.4
1137   */

1138  public void testKeepPopSizeConstant_0() {
1139    Configuration conf = new Configuration();
1140    assertEquals(true, conf.isKeepPopulationSizeConstant());
1141    conf.setKeepPopulationSizeConstant(false);
1142    assertEquals(false, conf.isKeepPopulationSizeConstant());
1143    conf.setKeepPopulationSizeConstant(true);
1144    assertEquals(true, conf.isKeepPopulationSizeConstant());
1145    conf.setKeepPopulationSizeConstant(false);
1146    assertEquals(false, conf.isKeepPopulationSizeConstant());
1147    assertEquals(JGAPFactory.class, conf.getJGAPFactory().getClass());
1148  }
1149
1150  /**
1151   * Construct with valid System property for JGAPFactory set.
1152   *
1153   * @author Klaus Meffert
1154   * @since 2.6
1155   */

1156  public void testConstruct_0() {
1157    System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS,
1158                       "org.jgap.MyFactoryTest");
1159    Configuration conf = new Configuration();
1160    assertEquals(MyFactoryTest.class, conf.getJGAPFactory().getClass());
1161  }
1162
1163  /**
1164   * Construct with invalid System property for JGAPFactory set.
1165   *
1166   * @author Klaus Meffert
1167   * @since 2.6
1168   */

1169  public void testConstruct_1() {
1170    System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS,
1171                       "org.jgap.myFactoryTest");
1172    try {
1173      new Configuration();
1174      fail();
1175    } catch (RuntimeException JavaDoc rex) {
1176      ; //this is OK
1177
}
1178  }
1179
1180  /**
1181   * Construct with no System property for JGAPFactory set.
1182   *
1183   * @author Klaus Meffert
1184   * @since 3.0
1185   */

1186  public void testConstruct_2() {
1187    System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS, "");
1188    Configuration conf = new Configuration();
1189    assertEquals(JGAPFactory.class, conf.getJGAPFactory().getClass());
1190  }
1191
1192  /**
1193   * @throws Exception
1194   *
1195   * @author Klaus Meffert
1196   * @since 3.2
1197   */

1198  public void testClone_0()
1199      throws Exception JavaDoc {
1200    Configuration conf = new Configuration();
1201    conf.setFitnessFunction(new StaticFitnessFunction(2));
1202    Gene gene = new BooleanGene(conf);
1203    conf.setSampleChromosome(new Chromosome(conf, gene, 5));
1204    conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
1205    conf.setRandomGenerator(new StockRandomGenerator());
1206    conf.setEventManager(new EventManager());
1207    conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
1208    conf.addGeneticOperator(new MutationOperator(conf));
1209    conf.setPopulationSize(1);
1210    assertEquals(conf, conf.clone());
1211  }
1212
1213  /**
1214   * Cloning multiple times in a row must be possible.
1215   *
1216   * @throws Exception
1217   *
1218   * @author Klaus Meffert
1219   * @since 3.2
1220   */

1221  public void testClone_1()
1222      throws Exception JavaDoc {
1223    Configuration conf = new Configuration();
1224    conf.setFitnessFunction(new StaticFitnessFunction(2));
1225    Gene gene = new BooleanGene(conf);
1226    conf.setSampleChromosome(new Chromosome(conf, gene, 5));
1227    conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
1228    conf.setRandomGenerator(new StockRandomGenerator());
1229    conf.setEventManager(new EventManager());
1230    conf.setFitnessEvaluator(new DefaultFitnessEvaluator());
1231    conf.addGeneticOperator(new MutationOperator(conf));
1232    conf.setPopulationSize(1);
1233    conf.clone();
1234    conf.clone();
1235  }
1236
1237  /**
1238   * Exposes bug 1642378.
1239   *
1240   * @throws Exception
1241   *
1242   * @author Klaus Meffert
1243   * @since 3.2
1244   */

1245  public void testRemoveNaturalSelector_0()
1246      throws Exception JavaDoc {
1247    Configuration conf = new Configuration();
1248    NaturalSelector selector = new BestChromosomesSelector(conf);
1249    conf.addNaturalSelector(selector, false);
1250    conf.removeNaturalSelectors(false);
1251    assertEquals(0, conf.getNaturalSelectors(false).size());
1252    assertEquals(0, conf.getNaturalSelectorsSize(false));
1253  }
1254
1255  /**
1256   * Exposes bug 1642378.
1257   *
1258   * @throws Exception
1259   *
1260   * @author Klaus Meffert
1261   * @since 3.2
1262   */

1263  public void testRemoveNaturalSelector_1()
1264      throws Exception JavaDoc {
1265    Configuration conf = new Configuration();
1266    NaturalSelector selector = new BestChromosomesSelector(conf);
1267    conf.addNaturalSelector(selector, !false);
1268    conf.removeNaturalSelectors(!false);
1269    assertEquals(0, conf.getNaturalSelectors(!false).size());
1270    assertEquals(0, conf.getNaturalSelectorsSize(!false));
1271  }
1272
1273  /**
1274   * @throws Exception
1275   *
1276   * @author Klaus Meffert
1277   * @since 3.2
1278   */

1279  public void testRemoveNaturalSelector_2()
1280      throws Exception JavaDoc {
1281    Configuration conf = new Configuration();
1282    NaturalSelector selector = new BestChromosomesSelector(conf);
1283    conf.addNaturalSelector(selector, true);
1284    conf.removeNaturalSelectors(false);
1285    assertEquals(0, conf.getNaturalSelectors(false).size());
1286    assertEquals(0, conf.getNaturalSelectorsSize(false));
1287    assertEquals(1, conf.getNaturalSelectors(true).size());
1288    assertEquals(1, conf.getNaturalSelectorsSize(true));
1289  }
1290
1291  /**
1292   * @throws Exception
1293   *
1294   * @author Klaus Meffert
1295   * @since 3.2
1296   */

1297  public void testRemoveNaturalSelector_3()
1298      throws Exception JavaDoc {
1299    Configuration conf = new Configuration();
1300    NaturalSelector selector = new BestChromosomesSelector(conf);
1301    conf.addNaturalSelector(selector, false);
1302    conf.removeNaturalSelectors(true);
1303    assertEquals(0, conf.getNaturalSelectors(true).size());
1304    assertEquals(0, conf.getNaturalSelectorsSize(true));
1305    assertEquals(1, conf.getNaturalSelectors(false).size());
1306    assertEquals(1, conf.getNaturalSelectorsSize(false));
1307  }
1308
1309}
1310class MyFactoryTest
1311    extends JGAPFactory {
1312  public MyFactoryTest() {
1313    super(false);
1314  }
1315}
1316class TestBulkFitnessFunction
1317    extends BulkFitnessFunction {
1318  public void evaluate(Population a_subjects) {
1319  }
1320
1321  public int hashCode() {
1322    return -199;
1323  }
1324}
1325class TestBulkFitnessFunction2
1326    extends BulkFitnessFunction {
1327  public void evaluate(Population a_subjects) {
1328  }
1329
1330  public int hashCode() {
1331    return -297;
1332  }
1333}
1334class MyThread
1335    implements Runnable JavaDoc {
1336  public void run() {
1337    Configuration conf = new Configuration();
1338    try {
1339      conf.setFitnessFunction(new StaticFitnessFunction(2.3));
1340      Thread.sleep(100);
1341    } catch (Exception JavaDoc ex) {
1342      ex.printStackTrace();
1343    }
1344  }
1345}
1346class MyThreadBulk
1347    implements Runnable JavaDoc {
1348  public void run() {
1349    Configuration conf = new Configuration();
1350    try {
1351      conf.setBulkFitnessFunction(new TestBulkFitnessFunction());
1352      Thread.sleep(100);
1353    } catch (Exception JavaDoc ex) {
1354      ex.printStackTrace();
1355    }
1356  }
1357}
1358class MyThreadEvent
1359    implements Runnable JavaDoc {
1360  public void run() {
1361    Configuration conf = new Configuration();
1362    try {
1363      conf.setEventManager(new EventManager());
1364      Thread.sleep(100);
1365    } catch (Exception JavaDoc ex) {
1366      ex.printStackTrace();
1367    }
1368  }
1369}
1370class TestEventManager
1371    extends EventManager {
1372}
1373
Popular Tags