Search in sources :

Example 11 with XMLProducer

use of beast.util.XMLProducer in project beast2 by CompEvol.

the class XMLProducerTest method testObjectWithoutID.

// test_XmlExamples
@Test
public void testObjectWithoutID() {
    BEASTInterface o = new Nucleotide();
    XMLProducer producer = new XMLProducer();
    System.out.println(producer.toXML(o));
}
Also used : XMLProducer(beast.util.XMLProducer) Nucleotide(beast.evolution.datatype.Nucleotide) BEASTInterface(beast.core.BEASTInterface) Test(org.junit.Test)

Example 12 with XMLProducer

use of beast.util.XMLProducer in project beast2 by CompEvol.

the class XMLProducerTest method test_ThatXmlExamplesProduces.

/**
 * parse all XML files in the given directory, then produce XML from it, and see if the produced XML still parses *
 */
public void test_ThatXmlExamplesProduces(String dir, List<String> exceptions) {
    try {
        Randomizer.setSeed(127);
        Logger.FILE_MODE = Logger.LogFileMode.overwrite;
        System.out.println("Test XML Examples in " + dir);
        File exampleDir = new File(dir);
        String[] exampleFiles = exampleDir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".xml");
            }
        });
        List<String> failedFiles = new ArrayList<String>();
        for (String fileName : exampleFiles) {
            if (exceptions.contains(fileName)) {
                System.out.println("Skipping exception " + fileName);
            } else {
                System.out.println("Processing " + fileName);
                XMLProducer producer = new XMLProducer();
                BEASTInterface o = null;
                try {
                    o = producer.parseFile(new File(dir + "/" + fileName));
                } catch (Exception e) {
                    o = null;
                }
                if (o != null) {
                    String xml = producer.toXML(o);
                    // FileWriter outfile = new FileWriter(new File("/tmp/XMLProducerTest.xml"));
                    // outfile.write(xml);
                    // outfile.close();
                    XMLParser parser2 = new XMLParser();
                    try {
                        parser2.parseFragment(xml, false);
                    } catch (Exception e) {
                        System.out.println("test_ThatXmlExamplesProduces::Failed for " + fileName + ": " + e.getMessage());
                        failedFiles.add(fileName);
                    }
                }
                System.out.println("Done " + fileName);
            }
        }
        if (failedFiles.size() > 0) {
            System.out.println("\ntest_ThatXmlExamplesProduces::Failed for : " + failedFiles.toString());
        } else {
            System.out.println("\ntest_ThatXmlExamplesProduces::Success");
        }
        assertTrue(failedFiles.toString(), failedFiles.size() == 0);
    } catch (Exception e) {
        System.out.println("exception thrown ");
        System.out.println(e.getMessage());
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) XMLProducer(beast.util.XMLProducer) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser) File(java.io.File)

Example 13 with XMLProducer

use of beast.util.XMLProducer in project beast2 by CompEvol.

the class XMLTest method testAnnotatedConstructor2.

@Test
public void testAnnotatedConstructor2() throws Exception {
    List<Taxon> taxa = new ArrayList<>();
    taxa.add(new Taxon("first one"));
    taxa.add(new Taxon("second one"));
    AnnotatedRunnableTestClass t = new AnnotatedRunnableTestClass(3, taxa);
    XMLProducer producer = new XMLProducer();
    String xml = producer.toXML(t);
    assertEquals(3, (int) t.getParam1());
    FileWriter outfile = new FileWriter(new File("/tmp/XMLTest.xml"));
    outfile.write(xml);
    outfile.close();
    XMLParser parser = new XMLParser();
    BEASTInterface b = parser.parseFile(new File("/tmp/XMLTest.xml"));
    assertEquals(3, (int) ((AnnotatedRunnableTestClass) b).getParam1());
    assertEquals(2, ((AnnotatedRunnableTestClass) b).getTaxon().size());
}
Also used : XMLProducer(beast.util.XMLProducer) Taxon(beast.evolution.alignment.Taxon) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser) File(java.io.File) Test(org.junit.Test)

Aggregations

XMLProducer (beast.util.XMLProducer)13 BEASTInterface (beast.core.BEASTInterface)9 File (java.io.File)6 XMLParser (beast.util.XMLParser)5 PrintStream (java.io.PrintStream)4 Alignment (beast.evolution.alignment.Alignment)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 MCMC (beast.core.MCMC)2 FileNotFoundException (java.io.FileNotFoundException)2 FileWriter (java.io.FileWriter)2 FilenameFilter (java.io.FilenameFilter)2 HashSet (java.util.HashSet)2 Scanner (java.util.Scanner)2 Input (beast.core.Input)1 Sequence (beast.evolution.alignment.Sequence)1 Taxon (beast.evolution.alignment.Taxon)1 BranchRateModel (beast.evolution.branchratemodel.BranchRateModel)1 Nucleotide (beast.evolution.datatype.Nucleotide)1 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)1