1 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 26 public class XMLManagerTest 27 extends JGAPTestCase { 28 29 private final static String CVS_REVISION = "$Revision: 1.17 $"; 30 31 private final static String 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 m_chromosome_tag; 49 50 private String m_genes_tag; 51 52 private String 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 (54)); 62 m_genes[1] = new IntegerGene(conf, 22, 44); 63 m_genes[1].setAllele(new Integer (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 ) privateAccessor.getField(XMLManager.class, 74 "CHROMOSOME_TAG"); 75 m_genes_tag = (String ) privateAccessor.getField(XMLManager.class, 76 "GENES_TAG"); 77 m_genotype_tag = (String ) privateAccessor.getField(XMLManager.class, 78 "GENOTYPE_TAG"); 79 } 80 catch (Exception ex) { 81 throw new RuntimeException ("Error in setUp: " + ex.getMessage()); 82 } 83 } 84 85 public void testGetChromosomeFromDocument_0() 86 throws Exception { 87 try { 88 XMLManager.getChromosomeFromDocument(m_conf, null); 89 fail(); 90 } 91 catch (NullPointerException nex) { 92 ; } 94 } 95 96 public void testGetChromosomeFromDocument_1() 97 throws Exception { 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 { 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 ; } 114 } 115 116 public void testGetChromosomeFromElement_1() 117 throws Exception { 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 { 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 144 public void testGetGenesFromElement_2() 145 throws Exception { 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 163 public void testGetGenesFromElement_1() 164 throws Exception { 165 try { 166 XMLManager.getGenesFromElement(m_conf, null); 167 fail(); 168 } 169 catch (ImproperXMLException iex) { 170 ; } 172 } 173 174 public void testGetGenotypeFromDocument_0() 175 throws Exception { 176 Document doc = XMLManager.representChromosomeAsDocument(m_chrom); 177 try { 178 XMLManager.getGenotypeFromDocument(m_conf, doc); 179 fail(); 180 } 181 catch (ImproperXMLException iex) { 182 ; } 184 } 185 186 public void testGetGenotypeFromDocument_1() 187 throws Exception { 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 { 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 208 public void testGetGenotypeFromElement_1() 209 throws Exception { 210 try { 211 XMLManager.getGenotypeFromElement(m_conf, null); 212 fail(); 213 } 214 catch (ImproperXMLException iex) { 215 ; } 217 } 218 219 public void testRepresentChromosomeAsDocument_0() 220 throws Exception { 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 { 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 { 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 { 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 { 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 { 260 try { 261 XMLManager.readFile(File.createTempFile("FILENAME_WRITE", "tmp")); 262 fail(); 263 } 264 catch (Exception ex) { 265 ; } 267 } 268 269 public void testReadFile_1() 270 throws Exception { 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 { 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 292 public void testWriteFile_1() 293 throws Exception { 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 307 public void testChromosome_0() 308 throws Exception { 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 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 331 public void testGenotype_0() 332 throws Exception { 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 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 |