KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jgap.*;
13 import org.jgap.impl.*;
14 import org.jgap.data.*;
15 import junit.framework.*;
16 import org.w3c.dom.*;
17
18 /**
19  * Tests for XMLDocumentBuilder class
20  *
21  * @author Klaus Meffert
22  * @since 1.0
23  */

24 public class XMLDocumentBuilderTest
25     extends JGAPTestCase {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
28
29   private final static String JavaDoc FILENAME_WRITE = "GAtestWrite.xml";
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(XMLDocumentBuilderTest.class);
33     return suite;
34   }
35
36   /**
37    * @throws Exception
38    * @author Klaus Meffert
39    * @since 2.6
40    */

41   public void testBuildDocument_0()
42       throws Exception JavaDoc {
43     XMLDocumentBuilder doc = new XMLDocumentBuilder();
44     DataTreeBuilder builder = DataTreeBuilder.getInstance();
45     Chromosome chrom = new Chromosome(conf, new Gene[] {
46                                       new IntegerGene(conf, 1, 5),
47                                       new IntegerGene(conf, 1, 10)});
48     chrom.getGene(0).setAllele(new Integer JavaDoc(1));
49     chrom.getGene(1).setAllele(new Integer JavaDoc( -3));
50     IDataCreators doc2 = builder.representChromosomeAsDocument(chrom);
51     Document result = (Document)doc.buildDocument(doc2);
52     assertEquals(null, result.getParentNode());
53     assertEquals(1, result.getChildNodes().getLength());
54     Node child = result.getChildNodes().item(0);
55     assertEquals("chromosome", child.getNodeName());
56     assertEquals(1, child.getChildNodes().getLength());
57     Node genes = child.getChildNodes().item(0);
58     assertEquals(2, genes.getChildNodes().getLength());
59   }
60
61   /**
62    * Artifically create a null node
63    * @throws Exception
64    * @author Klaus Meffert
65    * @since 2.6
66    */

67   public void testBuildDocument_1()
68       throws Exception JavaDoc {
69     XMLDocumentBuilder doc = new XMLDocumentBuilder();
70     DataTreeBuilder builder = DataTreeBuilder.getInstance();
71     Chromosome chrom = new Chromosome(conf, new Gene[] {
72                                       new IntegerGene(conf, 1, 5),
73                                       new IntegerGene(conf, 1, 10)});
74     chrom.getGene(0).setAllele(new Integer JavaDoc(1));
75     chrom.getGene(1).setAllele(new Integer JavaDoc( -3));
76     IDataCreators doc2 = builder.representChromosomeAsDocument(chrom);
77     IDataElement elem = doc2.getTree().item(0);
78     privateAccessor.setField(elem, "m_elements", null);
79     Document result = (Document)doc.buildDocument(doc2);
80     assertEquals(null, result.getParentNode());
81     assertEquals(1, result.getChildNodes().getLength());
82     Node child = result.getChildNodes().item(0);
83     assertEquals("chromosome", child.getNodeName());
84     assertEquals(0, child.getChildNodes().getLength());
85   }
86 }
87
Popular Tags