KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > xml > XMLManagerTest


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.xml;
11
12 import java.io.*;
13 import javax.xml.parsers.*;
14 import org.jgap.*;
15 import org.jgap.supergenes.*;
16 import org.jgap.impl.*;
17 import org.w3c.dom.*;
18 import junit.framework.*;
19
20 /**
21  * Tests the XMLManager class.
22  *
23  * @author Klaus Meffert
24  * @since 1.0
25  */

26 public class XMLManagerTest
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   private final static String JavaDoc FILENAME_WRITE = "GAtestWrite.xml";
32
33   public static Test suite() {
34     TestSuite suite = new TestSuite(XMLManagerTest.class);
35     return suite;
36   }
37
38   private Configuration m_conf;
39
40   private Chromosome m_chrom;
41   private Chromosome m_supergenechrom;
42
43   private Gene[] m_genes;
44   private Gene[] m_supergenes;
45
46   private Genotype m_genotype;
47
48   private String JavaDoc m_chromosome_tag;
49
50   private String JavaDoc m_genes_tag;
51
52   private String JavaDoc m_genotype_tag;
53
54   public void setUp() {
55     super.setUp();
56     try {
57       Configuration.reset();
58       m_conf = new DefaultConfiguration();
59       m_genes = new IntegerGene[2];
60       m_genes[0] = new IntegerGene(conf, 0, 100);
61       m_genes[0].setAllele(new Integer JavaDoc(54));
62       m_genes[1] = new IntegerGene(conf, 22, 44);
63       m_genes[1].setAllele(new Integer JavaDoc(37));
64       m_conf.setFitnessFunction(new RandomFitnessFunction());
65       m_conf.setPopulationSize(8);
66       m_chrom = new Chromosome(conf, m_genes);
67       InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf, new Gene[]{});
68       m_supergenes = new Supergene[]{gene};
69       m_supergenechrom = new Chromosome(conf, m_supergenes);
70
71       m_conf.setSampleChromosome(m_chrom);
72       m_genotype = new Genotype(m_conf, new Chromosome[] {m_chrom});
73       m_chromosome_tag = (String JavaDoc) privateAccessor.getField(XMLManager.class,
74           "CHROMOSOME_TAG");
75       m_genes_tag = (String JavaDoc) privateAccessor.getField(XMLManager.class,
76           "GENES_TAG");
77       m_genotype_tag = (String JavaDoc) privateAccessor.getField(XMLManager.class,
78           "GENOTYPE_TAG");
79     }
80     catch (Exception JavaDoc ex) {
81       throw new RuntimeException JavaDoc("Error in setUp: " + ex.getMessage());
82     }
83   }
84
85   public void testGetChromosomeFromDocument_0()
86       throws Exception JavaDoc {
87     try {
88       XMLManager.getChromosomeFromDocument(m_conf, null);
89       fail();
90     }
91     catch (NullPointerException JavaDoc nex) {
92       ; //this is OK
93
}
94   }
95
96   public void testGetChromosomeFromDocument_1()
97       throws Exception JavaDoc {
98     Document doc = XMLManager.representChromosomeAsDocument(m_chrom);
99     Chromosome chrom2 = XMLManager.getChromosomeFromDocument(m_conf, doc);
100     assertTrue(m_chrom.equals(chrom2));
101   }
102
103   public void testGetChromosomeFromElement_0()
104       throws Exception JavaDoc {
105     XMLManager.representChromosomeAsDocument(m_chrom);
106     Element elem = null;
107     try {
108       XMLManager.getChromosomeFromElement(m_conf, elem);
109       fail();
110     }
111     catch (ImproperXMLException iex) {
112       ; //this is OK
113
}
114   }
115
116   public void testGetChromosomeFromElement_1()
117       throws Exception JavaDoc {
118     Document doc = XMLManager.representChromosomeAsDocument(m_chrom);
119     Element elem = doc.getDocumentElement();
120     Chromosome chrom2 = XMLManager.getChromosomeFromElement(m_conf, elem);
121     assertEquals(m_chrom, chrom2);
122   }
123
124   public void testGetGenesFromElement_0()
125       throws Exception JavaDoc {
126     Document doc = XMLManager.representChromosomeAsDocument(m_chrom);
127     Element elem = doc.getDocumentElement();
128     NodeList chromElems = elem.getElementsByTagName(m_genes_tag);
129     Gene[] genes2 = XMLManager.getGenesFromElement(m_conf,
130         (Element) chromElems.item(0));
131     assertEquals(m_genes.length, genes2.length);
132     for (int i = 0; i < m_genes.length; i++) {
133       assertEquals(m_genes[i], genes2[i]);
134     }
135   }
136
137   /**
138    *
139    * @throws Exception
140    *
141    * @author Klaus Meffert
142    * @since 3.0
143    */

144   public void testGetGenesFromElement_2()
145       throws Exception JavaDoc {
146     Document doc = XMLManager.representChromosomeAsDocument(m_supergenechrom);
147     Element elem = doc.getDocumentElement();
148     NodeList chromElems = elem.getElementsByTagName(m_genes_tag);
149     Gene[] genes2 = XMLManager.getGenesFromElement(m_conf,
150         (Element) chromElems.item(0));
151     assertEquals(m_supergenes.length, genes2.length);
152     for (int i = 0; i < m_supergenes.length; i++) {
153       assertEquals(m_supergenes[i], genes2[i]);
154     }
155   }
156
157   /**
158    * @throws Exception
159    *
160    * @author Klaus Meffert
161    * @since 2.6
162    */

163   public void testGetGenesFromElement_1()
164       throws Exception JavaDoc {
165     try {
166       XMLManager.getGenesFromElement(m_conf, null);
167       fail();
168     }
169     catch (ImproperXMLException iex) {
170       ; //this is OK
171
}
172   }
173
174   public void testGetGenotypeFromDocument_0()
175       throws Exception JavaDoc {
176     Document doc = XMLManager.representChromosomeAsDocument(m_chrom);
177     try {
178       XMLManager.getGenotypeFromDocument(m_conf, doc);
179       fail();
180     }
181     catch (ImproperXMLException iex) {
182       ; //this is OK
183
}
184   }
185
186   public void testGetGenotypeFromDocument_1()
187       throws Exception JavaDoc {
188     Document doc = XMLManager.representGenotypeAsDocument(m_genotype);
189     Genotype genotype2 = XMLManager.getGenotypeFromDocument(m_conf, doc);
190     assertTrue(m_genotype.equals(genotype2));
191     assertEquals(m_genotype, genotype2);
192   }
193
194   public void testGetGenotypeFromElement_0()
195       throws Exception JavaDoc {
196     Document doc = XMLManager.representGenotypeAsDocument(m_genotype);
197     Element elem = doc.getDocumentElement();
198     Genotype genotype2 = XMLManager.getGenotypeFromElement(m_conf, elem);
199     assertEquals(m_genotype, genotype2);
200   }
201
202   /**
203    * @throws Exception
204    *
205    * @author Klaus Meffert
206    * @since 2.6
207    */

208   public void testGetGenotypeFromElement_1()
209       throws Exception JavaDoc {
210     try {
211       XMLManager.getGenotypeFromElement(m_conf, null);
212       fail();
213     }
214     catch (ImproperXMLException iex) {
215       ; //this is OK
216
}
217   }
218
219   public void testRepresentChromosomeAsDocument_0()
220       throws Exception JavaDoc {
221     Document doc = XMLManager.representChromosomeAsDocument(m_chrom);
222     Element elem = doc.getDocumentElement();
223     assertEquals(m_chromosome_tag, elem.getTagName());
224   }
225
226   public void testRepresentChromosomeAsElement_0()
227       throws Exception JavaDoc {
228     DocumentBuilder docCreator =
229         DocumentBuilderFactory.newInstance().newDocumentBuilder();
230     Document doc = docCreator.newDocument();
231     Element elem = XMLManager.representChromosomeAsElement(m_chrom, doc);
232     assertEquals(m_chromosome_tag, elem.getTagName());
233   }
234
235   public void testRepresentGenesAsElement_0()
236       throws Exception JavaDoc {
237     Document doc = XMLManager.representChromosomeAsDocument(m_chrom);
238     Element elem = XMLManager.representGenesAsElement(m_genes, doc);
239     assertEquals(m_genes_tag, elem.getTagName());
240   }
241
242   public void testRepresentGenotypeAsDocument_0()
243       throws Exception JavaDoc {
244     Document doc = XMLManager.representGenotypeAsDocument(m_genotype);
245     Element elem = doc.getDocumentElement();
246     assertEquals(m_genotype_tag, elem.getTagName());
247   }
248
249   public void testRepresentGenotypeAsElement_0()
250       throws Exception JavaDoc {
251     DocumentBuilder docCreator =
252         DocumentBuilderFactory.newInstance().newDocumentBuilder();
253     Document doc = docCreator.newDocument();
254     Element elem = XMLManager.representGenotypeAsElement(m_genotype, doc);
255     assertEquals(m_genotype_tag, elem.getTagName());
256   }
257
258   public void testReadFile_0()
259       throws Exception JavaDoc {
260     try {
261       XMLManager.readFile(File.createTempFile("FILENAME_WRITE", "tmp"));
262       fail();
263     }
264     catch (Exception JavaDoc ex) {
265       ; //this is OK
266
}
267   }
268
269   public void testReadFile_1()
270       throws Exception JavaDoc {
271     Document doc = XMLManager.representGenotypeAsDocument(m_genotype);
272     File f = File.createTempFile("FILENAME_WRITE", "tmp");
273     XMLManager.writeFile(XMLManager.representGenotypeAsDocument(m_genotype),
274                          f);
275     XMLManager.readFile(f);
276     Genotype population = XMLManager.getGenotypeFromDocument(m_conf, doc);
277     assertEquals(m_genotype, population);
278   }
279
280   public void testWriteFile_0()
281       throws Exception JavaDoc {
282     XMLManager.representGenotypeAsDocument(m_genotype);
283     File f = File.createTempFile("FILENAME_WRITE", "tmp");
284     XMLManager.writeFile(XMLManager.representGenotypeAsDocument(m_genotype),
285                          f);
286   }
287
288   /**
289    * Do the same as above test to verify that overriding existing file works.
290    * @throws Exception
291    */

292   public void testWriteFile_1()
293       throws Exception JavaDoc {
294     XMLManager.representGenotypeAsDocument(m_genotype);
295     File f = File.createTempFile("FILENAME_WRITE", "tmp");
296     XMLManager.writeFile(XMLManager.representGenotypeAsDocument(m_genotype),
297                          f);
298   }
299
300   /**
301    * Tests XML capabilities of JGAP. Moved from examples.simpleBoolean.TestXML.
302    * @throws Exception
303    *
304    * @author Klaus Meffert
305    * @since 3.0
306    */

307   public void testChromosome_0()
308       throws Exception JavaDoc {
309     Configuration.resetProperty(Configuration.PROPERTY_SAMPLE_CHROM_INST);
310     conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 8));
311     conf.setPopulationSize(10);
312     conf.reset();
313     conf.setFitnessFunction(new TestFitnessFunction());
314     // Test Chromsome manipulation methods.
315
// ------------------------------------
316
IChromosome chromosome = Chromosome.randomInitialChromosome(conf);
317     Document chromosomeDoc =
318         XMLManager.representChromosomeAsDocument(chromosome);
319     IChromosome chromosomeFromXML =
320         XMLManager.getChromosomeFromDocument(conf, chromosomeDoc);
321     assertEquals(chromosomeFromXML, chromosome);
322   }
323
324   /**
325    * Tests XML capabilities of JGAP. Moved from examples.simpleBoolean.TestXML.
326    * @throws Exception
327    *
328    * @author Klaus Meffert
329    * @since 3.0
330    */

331   public void testGenotype_0()
332       throws Exception JavaDoc {
333     Configuration.resetProperty(Configuration.PROPERTY_SAMPLE_CHROM_INST);
334     conf.setSampleChromosome(new Chromosome(conf, new BooleanGene(conf), 8));
335     conf.setPopulationSize(10);
336     conf.reset();
337     conf.setFitnessFunction(new TestFitnessFunction());
338     // Test Genotype manipulation methods.
339
// -----------------------------------
340
Genotype genotype = Genotype.randomInitialGenotype(conf);
341     Document genotypeDoc = XMLManager.representGenotypeAsDocument(genotype);
342     Genotype genotypeFromXML =
343         XMLManager.getGenotypeFromDocument(conf, genotypeDoc);
344     assertEquals(genotypeFromXML, genotype);
345   }
346
347 }
348
Popular Tags