Skip to content

Commit

Permalink
Separating out reader factory tests
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Jul 31, 2013
1 parent 17b02f0 commit 5cc687d
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 93 deletions.
165 changes: 165 additions & 0 deletions src/test/org/openscience/cdk/io/AbstractReaderFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2013. John May <jwmay@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 U
*/
package org.openscience.cdk.io;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.ChemFile;
import org.openscience.cdk.ChemModel;
import org.openscience.cdk.Reaction;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IReaction;
import org.openscience.cdk.io.formats.CMLFormat;
import org.openscience.cdk.io.formats.CTXFormat;
import org.openscience.cdk.io.formats.GamessFormat;
import org.openscience.cdk.io.formats.Gaussian98Format;
import org.openscience.cdk.io.formats.GhemicalSPMFormat;
import org.openscience.cdk.io.formats.IChemFormat;
import org.openscience.cdk.io.formats.IChemFormatMatcher;
import org.openscience.cdk.io.formats.INChIFormat;
import org.openscience.cdk.io.formats.INChIPlainTextFormat;
import org.openscience.cdk.io.formats.IResourceFormat;
import org.openscience.cdk.io.formats.MDLFormat;
import org.openscience.cdk.io.formats.MDLV2000Format;
import org.openscience.cdk.io.formats.MDLV3000Format;
import org.openscience.cdk.io.formats.Mol2Format;
import org.openscience.cdk.io.formats.PDBFormat;
import org.openscience.cdk.io.formats.PubChemASNFormat;
import org.openscience.cdk.io.formats.PubChemCompoundXMLFormat;
import org.openscience.cdk.io.formats.PubChemSubstanceXMLFormat;
import org.openscience.cdk.io.formats.ShelXFormat;
import org.openscience.cdk.io.formats.VASPFormat;
import org.openscience.cdk.io.formats.XYZFormat;
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
import org.openscience.cdk.tools.manipulator.ChemModelManipulator;
import org.openscience.cdk.tools.manipulator.ReactionManipulator;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

/**
* TestCase for the instantiation and functionality of the {@link org.openscience.cdk.io.ReaderFactory}.
*
* @cdk.module test-io
*/
public class AbstractReaderFactoryTest {

private ReaderFactory factory = new ReaderFactory();

void expectReader(String filename, IResourceFormat expectedFormat, int expectedAtomCount, int expectedBondCount) throws Exception {
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
Assert.assertNotNull("Cannot find file: " + filename, ins);
if (expectedFormat instanceof IChemFormatMatcher) {
factory.registerFormat((IChemFormatMatcher)expectedFormat);
}
ISimpleChemObjectReader reader = factory.createReader(ins);
Assert.assertNotNull(reader);
Assert.assertEquals(
((IChemFormat)expectedFormat).getReaderClassName(),
reader.getClass().getName()
);
// now try reading something from it
IChemObject[] objects = {
new ChemFile(), new ChemModel(), new AtomContainer(),
new Reaction()
};
boolean read = false;
for (int i=0; (i<objects.length && !read); i++) {
if (reader.accepts(objects[i].getClass())) {
IChemObject chemObject = reader.read(objects[i]);
Assert.assertNotNull("Reader accepted a " +
objects[i].getClass().getName() + " but failed to read it",
chemObject
);
assertAtomCount(expectedAtomCount, chemObject);
assertBondCount(expectedBondCount, chemObject);
read = true;
}
}
if (read) {
// ok, reseting worked
} else {
Assert.fail("Reading an IChemObject from the Reader did not work properly.");
}
}

void assertBondCount(int expectedBondCount, IChemObject chemObject) {
if (expectedBondCount != -1) {
if (chemObject instanceof IChemFile) {
Assert.assertEquals(
expectedBondCount,
ChemFileManipulator.getBondCount((IChemFile)chemObject)
);
} else if (chemObject instanceof IChemModel) {
Assert.assertEquals(
expectedBondCount,
ChemModelManipulator.getBondCount((IChemModel)chemObject)
);
} else if (chemObject instanceof IAtomContainer) {
Assert.assertEquals(
expectedBondCount,
((IAtomContainer)chemObject).getBondCount()
);
} else if (chemObject instanceof IReaction) {
Assert.assertEquals(
expectedBondCount,
ReactionManipulator.getBondCount((IReaction)chemObject)
);
}
}
}

void assertAtomCount(int expectedAtomCount, IChemObject chemObject) {
if (expectedAtomCount != -1) {
if (chemObject instanceof IChemFile) {
Assert.assertEquals(
expectedAtomCount,
ChemFileManipulator.getAtomCount((IChemFile)chemObject)
);
} else if (chemObject instanceof IChemModel) {
Assert.assertEquals(
expectedAtomCount,
ChemModelManipulator.getAtomCount((IChemModel)chemObject)
);
} else if (chemObject instanceof IAtomContainer) {
Assert.assertEquals(
expectedAtomCount,
((IAtomContainer)chemObject).getAtomCount()
);
} else if (chemObject instanceof IReaction) {
Assert.assertEquals(
expectedAtomCount,
ReactionManipulator.getAtomCount((IReaction)chemObject)
);
}
}
}

}
74 changes: 74 additions & 0 deletions src/test/org/openscience/cdk/io/PDBReaderFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* $RCSfile$
* $Author$
* $Date$
* $Revision$
*
* Copyright (C) 2003-2007 The Chemistry Development Kit (CDK) project
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
* */
package org.openscience.cdk.io;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.ChemFile;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.io.formats.CMLFormat;
import org.openscience.cdk.io.formats.CTXFormat;
import org.openscience.cdk.io.formats.GamessFormat;
import org.openscience.cdk.io.formats.Gaussian98Format;
import org.openscience.cdk.io.formats.GhemicalSPMFormat;
import org.openscience.cdk.io.formats.IChemFormat;
import org.openscience.cdk.io.formats.IChemFormatMatcher;
import org.openscience.cdk.io.formats.INChIFormat;
import org.openscience.cdk.io.formats.INChIPlainTextFormat;
import org.openscience.cdk.io.formats.MDLFormat;
import org.openscience.cdk.io.formats.MDLV2000Format;
import org.openscience.cdk.io.formats.MDLV3000Format;
import org.openscience.cdk.io.formats.Mol2Format;
import org.openscience.cdk.io.formats.PDBFormat;
import org.openscience.cdk.io.formats.PubChemASNFormat;
import org.openscience.cdk.io.formats.PubChemCompoundXMLFormat;
import org.openscience.cdk.io.formats.PubChemSubstanceXMLFormat;
import org.openscience.cdk.io.formats.ShelXFormat;
import org.openscience.cdk.io.formats.VASPFormat;
import org.openscience.cdk.io.formats.XYZFormat;
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

/**
* TestCase for the instantiation and functionality of the {@link org.openscience.cdk.io.ReaderFactory}.
*
* @cdk.module test-pdb
*/
public class PDBReaderFactoryTest extends AbstractReaderFactoryTest {

private ReaderFactory factory = new ReaderFactory();

@Test public void testPDB() throws Exception {
expectReader("data/pdb/coffeine.pdb", PDBFormat.getInstance(), -1, -1);
}
}
98 changes: 6 additions & 92 deletions src/test/org/openscience/cdk/io/ReaderFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.zip.GZIPInputStream;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.CDKTestCase;
Expand Down Expand Up @@ -73,7 +74,7 @@
*
* @cdk.module test-io
*/
public class ReaderFactoryTest extends CDKTestCase {
public class ReaderFactoryTest extends AbstractReaderFactoryTest {

private ReaderFactory factory = new ReaderFactory();

Expand All @@ -83,6 +84,7 @@ public class ReaderFactoryTest extends CDKTestCase {
Assert.assertNotNull(reader);
Assert.assertEquals(format.getFormatName(), reader.getFormat().getFormatName());
}

@Test public void testGaussian98() throws Exception {
expectReader("data/gaussian/g98.out", Gaussian98Format.getInstance(), -1, -1);
}
Expand Down Expand Up @@ -135,10 +137,11 @@ public class ReaderFactoryTest extends CDKTestCase {
expectReader("data/mdl/molV3000.mol", MDLV3000Format.getInstance(), -1, -1);
}

@Test public void testPDB() throws Exception {
@Ignore("test moved to cdk-test-pdb/PDBReaderFactoryTest")
public void testPDB() throws Exception {
expectReader("data/pdb/coffeine.pdb", PDBFormat.getInstance(), -1, -1);
}

@Test public void testMol2() throws Exception {
expectReader("data/mol2/fromWebsite.mol2", Mol2Format.getInstance(), -1, -1);
}
Expand All @@ -165,95 +168,6 @@ public class ReaderFactoryTest extends CDKTestCase {
Assert.assertNull(reader);
}

private void expectReader(String filename, IResourceFormat expectedFormat, int expectedAtomCount, int expectedBondCount) throws Exception {
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
Assert.assertNotNull("Cannot find file: " + filename, ins);
if (expectedFormat instanceof IChemFormatMatcher) {
factory.registerFormat((IChemFormatMatcher)expectedFormat);
}
ISimpleChemObjectReader reader = factory.createReader(ins);
Assert.assertNotNull(reader);
Assert.assertEquals(
((IChemFormat)expectedFormat).getReaderClassName(),
reader.getClass().getName()
);
// now try reading something from it
IChemObject[] objects = {
new ChemFile(), new ChemModel(), new AtomContainer(),
new Reaction()
};
boolean read = false;
for (int i=0; (i<objects.length && !read); i++) {
if (reader.accepts(objects[i].getClass())) {
IChemObject chemObject = reader.read(objects[i]);
Assert.assertNotNull("Reader accepted a " +
objects[i].getClass().getName() + " but failed to read it",
chemObject
);
assertAtomCount(expectedAtomCount, chemObject);
assertBondCount(expectedBondCount, chemObject);
read = true;
}
}
if (read) {
// ok, reseting worked
} else {
Assert.fail("Reading an IChemObject from the Reader did not work properly.");
}
}

private void assertBondCount(int expectedBondCount, IChemObject chemObject) {
if (expectedBondCount != -1) {
if (chemObject instanceof IChemFile) {
Assert.assertEquals(
expectedBondCount,
ChemFileManipulator.getBondCount((IChemFile)chemObject)
);
} else if (chemObject instanceof IChemModel) {
Assert.assertEquals(
expectedBondCount,
ChemModelManipulator.getBondCount((IChemModel)chemObject)
);
} else if (chemObject instanceof IAtomContainer) {
Assert.assertEquals(
expectedBondCount,
((IAtomContainer)chemObject).getBondCount()
);
} else if (chemObject instanceof IReaction) {
Assert.assertEquals(
expectedBondCount,
ReactionManipulator.getBondCount((IReaction)chemObject)
);
}
}
}

private void assertAtomCount(int expectedAtomCount, IChemObject chemObject) {
if (expectedAtomCount != -1) {
if (chemObject instanceof IChemFile) {
Assert.assertEquals(
expectedAtomCount,
ChemFileManipulator.getAtomCount((IChemFile)chemObject)
);
} else if (chemObject instanceof IChemModel) {
Assert.assertEquals(
expectedAtomCount,
ChemModelManipulator.getAtomCount((IChemModel)chemObject)
);
} else if (chemObject instanceof IAtomContainer) {
Assert.assertEquals(
expectedAtomCount,
((IAtomContainer)chemObject).getAtomCount()
);
} else if (chemObject instanceof IReaction) {
Assert.assertEquals(
expectedAtomCount,
ReactionManipulator.getAtomCount((IReaction)chemObject)
);
}
}
}

/**
* @cdk.bug 2153298
*/
Expand Down
4 changes: 3 additions & 1 deletion src/test/org/openscience/cdk/modulesuites/MpdbTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.openscience.cdk.coverage.PdbCoverageTest;
import org.openscience.cdk.io.PDBReaderFactoryTest;
import org.openscience.cdk.io.PDBReaderTest;
import org.openscience.cdk.io.PDBWriterTest;
import org.openscience.cdk.templates.AminoAcidsTest;
Expand All @@ -43,6 +44,7 @@
AminoAcidsTest.class,
PDBReaderTest.class,
PDBWriterTest.class,
ProteinBuilderToolTest.class
ProteinBuilderToolTest.class,
PDBReaderFactoryTest.class
})
public class MpdbTests {}

0 comments on commit 5cc687d

Please sign in to comment.