Permalink
Browse files

Added unit tests for DoubleBond and AtomParity cloning

  • Loading branch information...
1 parent 4704944 commit 88a2bb249de6ee787c9b174e8c7a9f35868e8dc0 @johnmay committed Oct 31, 2012
Showing with 143 additions and 1 deletion.
  1. +143 −1 src/test/org/openscience/cdk/interfaces/AbstractAtomContainerTest.java
@@ -30,6 +30,7 @@
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
+import org.openscience.cdk.stereo.DoubleBondStereochemistry;
import org.openscience.cdk.stereo.TetrahedralChirality;
import static org.hamcrest.CoreMatchers.is;
@@ -406,7 +407,7 @@
Assert.assertEquals("cloned element was incorrect class", element.getClass(), chirality.getClass());
assertThat("too many stereo elements", elements.hasNext(), is(not(true)));
- // we've tested the class already
+ // we've tested the class already - cast is okay
ITetrahedralChirality cloneChirality = (ITetrahedralChirality) element;
IAtom[] ligands = cloneChirality.getLigands();
@@ -425,6 +426,147 @@
}
+ /**
+ * Unit test to ensure that the stereo elements remain intact on cloning a
+ * container. This test ensures DoubleBondStereochemistry is preserved
+ *
+ * @cdk.bug 1###
@egonw
egonw Nov 2, 2012

Missing bug number

+ * @throws Exception
+ */
+ @Test public void testClone_IStereoElement_DoubleBond() throws Exception {
+
+ IAtomContainer container = (IAtomContainer) newChemObject();
+
+ IChemObjectBuilder builder = container.getBuilder();
+
+ IAtom c1 = builder.newInstance(IAtom.class, "C");
+ IAtom c2 = builder.newInstance(IAtom.class, "C");
+ IAtom c3 = builder.newInstance(IAtom.class, "C");
+ IAtom c4 = builder.newInstance(IAtom.class, "C");
+
+ container.addAtom(c1);
+ container.addAtom(c2);
+ container.addAtom(c3);
+ container.addAtom(c4);
+
+ IBond c1c2 = builder.newInstance(IBond.class, c1, c2, IBond.Order.DOUBLE);
+ IBond c2c3 = builder.newInstance(IBond.class, c2, c3);
+ IBond c1c4 = builder.newInstance(IBond.class, c1, c4);
+
+ container.addBond(c1c2);
+ container.addBond(c2c3);
+ container.addBond(c1c4);
+
+ IDoubleBondStereochemistry dbStereo = new DoubleBondStereochemistry(c1c2,
+ new IBond[] {c2c3, c1c4},
+ IDoubleBondStereochemistry.Conformation.OPPOSITE);
+
+ container.addStereoElement(dbStereo);
+
+ // clone the container
+ IAtomContainer clone = (IAtomContainer) container.clone();
+
+ Iterator<IStereoElement> elements = clone.stereoElements().iterator();
+
+ assertThat("no stereo elements cloned", elements.hasNext(), is(true));
+
+ IStereoElement element = elements.next();
+
+ Assert.assertEquals("cloned element was incorrect class", element.getClass(), dbStereo.getClass());
+ assertThat("too many stereo elements", elements.hasNext(), is(not(true)));
+
+ // we've tested the class already - cast is okay
+ IDoubleBondStereochemistry clonedDBStereo = (IDoubleBondStereochemistry) element;
+ IBond[] ligands = clonedDBStereo.getBonds();
+
+ assertThat("not enough ligands", ligands.length, is(2));
+
+ // test same instance - reference equality '=='
+ assertThat("expected same c2-c3 instance", ligands[0], sameInstance(clone.getBond(1)));
+ assertThat("expected same c1-c4 instance", ligands[1], sameInstance(clone.getBond(2)));
+
+ assertThat("incorrect stereo", clonedDBStereo.getStereo(), sameInstance(IDoubleBondStereochemistry.Conformation.OPPOSITE));
+
+ assertThat("incorrect chiral atom", clonedDBStereo.getStereoBond(), sameInstance(clone.getBond(0)));
+
+ }
+
+
+ /**
+ * Unit test to ensure that the stereo elements remain intact on cloning a
+ * container. This test ensures AtomParity is preserved
+ *
+ * @cdk.bug 1###
+ * @throws Exception
+ */
+ @Test public void testClone_IStereoElement_AtomParity() throws Exception {
+
+ IAtomContainer container = (IAtomContainer) newChemObject();
+
+ IChemObjectBuilder builder = container.getBuilder();
+
+ IAtom c1 = builder.newInstance(IAtom.class, "C");
+ IAtom o2 = builder.newInstance(IAtom.class, "O");
+ IAtom n3 = builder.newInstance(IAtom.class, "N");
+ IAtom c4 = builder.newInstance(IAtom.class, "C");
+ IAtom h5 = builder.newInstance(IAtom.class, "H");
+
+ container.addAtom(c1);
+ container.addAtom(o2);
+ container.addAtom(n3);
+ container.addAtom(c4);
+ container.addAtom(h5);
+
+ IBond c1o2 = builder.newInstance(IBond.class, c1, o2);
+ IBond c1n3 = builder.newInstance(IBond.class, c1, n3);
+ IBond c1c4 = builder.newInstance(IBond.class, c1, c4);
+ IBond c1h5 = builder.newInstance(IBond.class, c1, h5);
+
+ c1o2.setStereo(IBond.Stereo.UP);
+
+ container.addBond(c1o2);
+ container.addBond(c1n3);
+ container.addBond(c1c4);
+ container.addBond(c1h5);
+
+ IAtomParity chirality = builder.newInstance(IAtomParity.class,
+ c1,
+ o2, n3, c4, h5,
+ 1);
+
+ container.addStereoElement(chirality);
+
+
+ // clone the container
+ IAtomContainer clone = (IAtomContainer) container.clone();
+
+ Iterator<IStereoElement> elements = clone.stereoElements().iterator();
+
+ assertThat("no stereo elements cloned", elements.hasNext(), is(true));
+
+ IStereoElement element = elements.next();
+
+ Assert.assertEquals("cloned element was incorrect class", element.getClass(), chirality.getClass());
+ assertThat("too many stereo elements", elements.hasNext(), is(not(true)));
+
+ // we've tested the class already - cast is okay
+ IAtomParity cloneChirality = (IAtomParity) element;
+ IAtom[] ligands = cloneChirality.getSurroundingAtoms();
+
+ assertThat("not enough ligands", ligands.length, is(4));
+
+ // test same instance - reference equality '=='
+ assertThat("expected same oxygen instance", ligands[0], sameInstance(clone.getAtom(1)));
+ assertThat("expected same nitrogen instance", ligands[1], sameInstance(clone.getAtom(2)));
+ assertThat("expected same carbon instance", ligands[2], sameInstance(clone.getAtom(3)));
+ assertThat("expected same hydrogen instance", ligands[3], sameInstance(clone.getAtom(4)));
+
+ assertThat("incorrect stereo", cloneChirality.getParity(), is(1));
+
+ assertThat("incorrect chiral atom", cloneChirality.getAtom(), sameInstance(clone.getAtom(0)));
+
+ }
+
@Test public void testSetStereoElements_List() {

0 comments on commit 88a2bb2

Please sign in to comment.