Skip to content

Commit

Permalink
Added dedicated unit testing for the InChIGeneratorFactory
Browse files Browse the repository at this point in the history
Change-Id: I6f77b41e950474857e953181b0f03af5fa92a4ea
  • Loading branch information
egonw committed Mar 15, 2012
1 parent 516edbb commit a80b6d5
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/org/openscience/cdk/inchi/InChIGeneratorFactory.java
Expand Up @@ -26,6 +26,8 @@
import net.sf.jniinchi.JniInchiWrapper;
import net.sf.jniinchi.LoadNativeLibraryException;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
Expand Down Expand Up @@ -64,6 +66,7 @@
* @cdk.module inchi
* @cdk.githash
*/
@TestClass("org.openscience.cdk.inchi.InChIGeneratorFactoryTest")
public class InChIGeneratorFactory {

private static InChIGeneratorFactory INSTANCE;
Expand Down Expand Up @@ -92,6 +95,7 @@ private InChIGeneratorFactory() throws CDKException {
* @throws CDKException if unable to load native code when attempting
* to create the factory
*/
@TestMethod("testGetInstance")
public static InChIGeneratorFactory getInstance() throws CDKException {
synchronized ( InChIGeneratorFactory.class ) {
if ( INSTANCE == null ) {
Expand All @@ -108,6 +112,7 @@ public static InChIGeneratorFactory getInstance() throws CDKException {
* @return the InChI generator object
* @throws CDKException if the generator cannot be instantiated
*/
@TestMethod("testGetInChIGenerator_IAtomContainer")
public InChIGenerator getInChIGenerator(IAtomContainer container)
throws CDKException {
return(new InChIGenerator(container));
Expand All @@ -121,6 +126,7 @@ public InChIGenerator getInChIGenerator(IAtomContainer container)
* @return the InChI generator object
* @throws CDKException if the generator cannot be instantiated
*/
@TestMethod("testGetInChIGenerator_IAtomContainer_String")
public InChIGenerator getInChIGenerator(IAtomContainer container,
String options)
throws CDKException {
Expand All @@ -135,6 +141,7 @@ public InChIGenerator getInChIGenerator(IAtomContainer container,
* @return the InChI generator object
* @throws CDKException if the generator cannot be instantiated
*/
@TestMethod("testGetInChIGenerator_IAtomContainer_List")
public InChIGenerator getInChIGenerator(IAtomContainer container,
List<INCHI_OPTION> options) throws CDKException {
return(new InChIGenerator(container, options));
Expand All @@ -148,6 +155,7 @@ public InChIGenerator getInChIGenerator(IAtomContainer container,
* @return the InChI structure generator object
* @throws CDKException if the generator cannot be instantiated
*/
@TestMethod("testGetInChIToStructure_String_IChemObjectBuilder")
public InChIToStructure getInChIToStructure(String inchi,
IChemObjectBuilder builder)
throws CDKException {
Expand All @@ -163,6 +171,7 @@ public InChIToStructure getInChIToStructure(String inchi,
* @return the InChI structure generator object
* @throws CDKException if the generator cannot be instantiated
*/
@TestMethod("testGetInChIToStructure_String_IChemObjectBuilder_NullString")
public InChIToStructure getInChIToStructure(String inchi,
IChemObjectBuilder builder,
String options)
Expand All @@ -179,6 +188,7 @@ public InChIToStructure getInChIToStructure(String inchi,
* @return the InChI structure generator object
* @throws CDKException if the generator cannot be instantiated
*/
@TestMethod("testGetInChIToStructure_String_IChemObjectBuilder_List")
public InChIToStructure getInChIToStructure(String inchi,
IChemObjectBuilder builder,
List<String> options)
Expand Down
146 changes: 146 additions & 0 deletions src/test/org/openscience/cdk/inchi/InChIGeneratorFactoryTest.java
@@ -0,0 +1,146 @@
/* Copyright (C) 2006 Sam Adams <sea36@users.sf.net>
* 2007 Rajarshi Guha <rajarshi.guha@gmail.com>
* 2010,2012 Egon Willighagen <egonw@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.
*
* 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.inchi;

import java.util.ArrayList;
import java.util.List;

import net.sf.jniinchi.INCHI_OPTION;
import net.sf.jniinchi.INCHI_RET;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.Atom;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;

/**
* @cdk.module test-inchi
*/
public class InChIGeneratorFactoryTest {

@Test
public void testGetInstance() throws CDKException {
InChIGeneratorFactory factory = InChIGeneratorFactory.getInstance();
Assert.assertNotNull(factory);
}

/**
* Because we are not setting any options, we get an Standard InChI.
*/
@Test public void testGetInChIGenerator_IAtomContainer() throws Exception {
IAtomContainer ac = new AtomContainer();
IAtom a = new Atom("Cl");
a.setImplicitHydrogenCount(1);
ac.addAtom(a);
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(ac);
Assert.assertEquals(gen.getReturnStatus(), INCHI_RET.OKAY);
Assert.assertEquals("InChI=1S/ClH/h1H", gen.getInchi());
}

/**
* Because we are setting an options, we get a non-standard InChI.
*/
@Test public void testGetInChIGenerator_IAtomContainer_String() throws Exception {
IAtomContainer ac = new AtomContainer();
IAtom a = new Atom("Cl");
a.setImplicitHydrogenCount(1);
ac.addAtom(a);
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(ac, "FixedH");
Assert.assertEquals(gen.getReturnStatus(), INCHI_RET.OKAY);
Assert.assertEquals("InChI=1/ClH/h1H", gen.getInchi());
}

/**
* Because we are setting no option, we get a Standard InChI.
*/
@Test public void testGetInChIGenerator_IAtomContainer_NullString() throws Exception {
IAtomContainer ac = new AtomContainer();
IAtom a = new Atom("Cl");
a.setImplicitHydrogenCount(1);
ac.addAtom(a);
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(ac, (String)null);
Assert.assertEquals(gen.getReturnStatus(), INCHI_RET.OKAY);
Assert.assertEquals("InChI=1S/ClH/h1H", gen.getInchi());
}

/**
* Because we are setting an options, we get a non-standard InChI.
*/
@Test public void testGetInChIGenerator_IAtomContainer_List() throws Exception {
IAtomContainer ac = new AtomContainer();
IAtom a = new Atom("Cl");
a.setImplicitHydrogenCount(1);
ac.addAtom(a);
List<INCHI_OPTION> options = new ArrayList<INCHI_OPTION>();
options.add(INCHI_OPTION.FixedH);
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(ac, options);
Assert.assertEquals(gen.getReturnStatus(), INCHI_RET.OKAY);
Assert.assertEquals("InChI=1/ClH/h1H", gen.getInchi());
}

/**
* Because we are setting an options, we get a non-standard InChI.
*/
@Test(expected=IllegalArgumentException.class)
public void testGetInChIGenerator_IAtomContainer_NullList() throws Exception {
IAtomContainer ac = new AtomContainer();
IAtom a = new Atom("Cl");
a.setImplicitHydrogenCount(1);
ac.addAtom(a);
InChIGeneratorFactory.getInstance().getInChIGenerator(ac, (List<INCHI_OPTION>)null);
}

@Test
public void testGetInChIToStructure_String_IChemObjectBuilder() throws CDKException {
InChIToStructure parser = InChIGeneratorFactory.getInstance().getInChIToStructure(
"InChI=1/ClH/h1H", DefaultChemObjectBuilder.getInstance()
);
Assert.assertNotNull(parser);
}

@Test(expected=IllegalArgumentException.class)
public void testGetInChIToStructure_String_IChemObjectBuilder_NullString() throws CDKException {
InChIGeneratorFactory.getInstance().getInChIToStructure(
"InChI=1/ClH/h1H", DefaultChemObjectBuilder.getInstance(), (String)null
);
}

@Test(expected=IllegalArgumentException.class)
public void testGetInChIToStructure_String_IChemObjectBuilder_NullList() throws CDKException {
InChIGeneratorFactory.getInstance().getInChIToStructure(
"InChI=1/ClH/h1H", DefaultChemObjectBuilder.getInstance(), (List<String>)null
);
}

/**
* No options set.
*/
@Test public void testGetInChIToStructure_String_IChemObjectBuilder_List() throws CDKException {
InChIToStructure parser = InChIGeneratorFactory.getInstance().getInChIToStructure(
"InChI=1/ClH/h1H", DefaultChemObjectBuilder.getInstance(), new ArrayList<String>()
);
Assert.assertNotNull(parser);
}
}
4 changes: 3 additions & 1 deletion src/test/org/openscience/cdk/modulesuites/MinchiTests.java
Expand Up @@ -24,6 +24,7 @@
import org.junit.runners.Suite.SuiteClasses;
import org.openscience.cdk.coverage.InchiCoverageTest;
import org.openscience.cdk.graph.invariant.InChINumbersToolsTest;
import org.openscience.cdk.inchi.InChIGeneratorFactoryTest;
import org.openscience.cdk.inchi.InChIGeneratorTest;
import org.openscience.cdk.inchi.InChIToStructureTest;

Expand All @@ -37,6 +38,7 @@
InchiCoverageTest.class,
InChIGeneratorTest.class,
InChIToStructureTest.class,
InChINumbersToolsTest.class
InChINumbersToolsTest.class,
InChIGeneratorFactoryTest.class
})
public class MinchiTests {}

0 comments on commit a80b6d5

Please sign in to comment.