Skip to content

Commit

Permalink
Resolved NoNotify fails on AtomParity. Error was due to subclassing o…
Browse files Browse the repository at this point in the history
…f AtomParity. Also the assertEquals params were swapped as the assertion was the wrong way.

Change-Id: Idebf543ef01e1f81f9bdb1264a1dea0088c8d752
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Nov 4, 2012
1 parent 938306a commit 46ffbb4
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/main/org/openscience/cdk/nonotify/NNAtomParity.java
Expand Up @@ -29,7 +29,12 @@
package org.openscience.cdk.nonotify;

import org.openscience.cdk.AtomParity;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomParity;
import org.openscience.cdk.interfaces.IBond;

import java.util.Map;

/**
* @cdk.module nonotify
Expand All @@ -49,6 +54,31 @@ public NNAtomParity(
int parity) {
super(centralAtom, first, second, third, fourth, parity);
}

/**
* @inheritDoc
*/
@TestMethod("testMap_Map_Map,testMap_Null_Map,testMap_Map_Map_NullElement,testMap_Map_Map_EmptyMapping")
@Override
public IAtomParity map(Map<IAtom, IAtom> atoms, Map<IBond, IBond> bonds) {

if(atoms == null) // not using bond mapping
throw new IllegalArgumentException("null atom mapping provided");

IAtom[] neighbors = getSurroundingAtoms();

// could map neighbours with a for loop but we need to pull individuals
// atoms for the constructor
return new NNAtomParity(
getAtom() != null ? atoms.get(getAtom()) : null,
neighbors[0] != null ? atoms.get(neighbors[0]) : null,
neighbors[1] != null ? atoms.get(neighbors[1]) : null,
neighbors[2] != null ? atoms.get(neighbors[2]) : null,
neighbors[3] != null ? atoms.get(neighbors[3]) : null,
getParity()
);

}

}

Expand Down
Expand Up @@ -404,7 +404,7 @@ public abstract class AbstractAtomContainerTest extends AbstractChemObjectTest {

IStereoElement element = elements.next();

Assert.assertEquals("cloned element was incorrect class", element.getClass(), chirality.getClass());
Assert.assertEquals("cloned element was incorrect class", chirality.getClass(), element.getClass());
assertThat("too many stereo elements", elements.hasNext(), is(not(true)));

// we've tested the class already - cast is okay
Expand Down Expand Up @@ -472,7 +472,7 @@ public abstract class AbstractAtomContainerTest extends AbstractChemObjectTest {

IStereoElement element = elements.next();

Assert.assertEquals("cloned element was incorrect class", element.getClass(), dbStereo.getClass());
Assert.assertEquals("cloned element was incorrect class", dbStereo.getClass(), element.getClass());
assertThat("too many stereo elements", elements.hasNext(), is(not(true)));

// we've tested the class already - cast is okay
Expand Down Expand Up @@ -546,7 +546,7 @@ public abstract class AbstractAtomContainerTest extends AbstractChemObjectTest {

IStereoElement element = elements.next();

Assert.assertEquals("cloned element was incorrect class", element.getClass(), chirality.getClass());
Assert.assertEquals("cloned element was incorrect class", chirality.getClass(), element.getClass());
assertThat("too many stereo elements", elements.hasNext(), is(not(true)));

// we've tested the class already - cast is okay
Expand Down
144 changes: 144 additions & 0 deletions src/test/org/openscience/cdk/nonotify/NNAtomParityTest.java
Expand Up @@ -31,6 +31,15 @@
import org.openscience.cdk.interfaces.AbstractAtomParityTest;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomParity;
import org.openscience.cdk.interfaces.IChemObjectBuilder;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;

/**
* Checks the functionality of the {@link NNAtomParity}.
Expand Down Expand Up @@ -74,4 +83,139 @@ public class NNAtomParityTest extends AbstractAtomParityTest {
Assert.assertNotNull(parity);
}


@Test public void testMap_Map_Map() throws CloneNotSupportedException {

IChemObjectBuilder builder = NoNotificationChemObjectBuilder.getInstance();

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");

// new stereo element
IAtomParity original = new AtomParity(c1,
o2,n3,c4,h5,
2);

// clone the atoms and place in a map
Map<IAtom,IAtom> mapping = new HashMap<IAtom,IAtom>();
IAtom c1clone = (IAtom) c1.clone(); mapping.put(c1, c1clone);
IAtom o2clone = (IAtom) o2.clone(); mapping.put(o2, o2clone);
IAtom n3clone = (IAtom) n3.clone(); mapping.put(n3, n3clone);
IAtom c4clone = (IAtom) c4.clone(); mapping.put(c4, c4clone);
IAtom h5clone = (IAtom) h5.clone(); mapping.put(h5, h5clone);

// map the existing element a new element
IAtomParity mapped = original.map(mapping, Collections.EMPTY_MAP);

Assert.assertThat("mapped chiral atom was the same as the original",
mapped.getAtom(), is(not(sameInstance(original.getAtom()))));
Assert.assertThat("mapped chiral atom was not the clone",
mapped.getAtom(), is(sameInstance(c1clone)));

IAtom[] originalLigands = original.getSurroundingAtoms();
IAtom[] mappedLigands = mapped.getSurroundingAtoms();

Assert.assertThat("first ligand was te same as the original",
mappedLigands[0], is(not(sameInstance(originalLigands[0]))));
Assert.assertThat("first mapped ligand was not the clone",
mappedLigands[0], is(sameInstance(o2clone)));
Assert.assertThat("second ligand was te same as the original",
mappedLigands[1], is(not(sameInstance(originalLigands[1]))));
Assert.assertThat("second mapped ligand was not the clone",
mappedLigands[1], is(sameInstance(n3clone)));
Assert.assertThat("third ligand was te same as the original",
mappedLigands[2], is(not(sameInstance(originalLigands[2]))));
Assert.assertThat("third mapped ligand was not the clone",
mappedLigands[2], is(sameInstance(c4clone)));
Assert.assertThat("forth ligand was te same as the original",
mappedLigands[3], is(not(sameInstance(originalLigands[3]))));
Assert.assertThat("forth mapped ligand was not the clone",
mappedLigands[3], is(sameInstance(h5clone)));

Assert.assertThat("stereo was not mapped",
mapped.getParity(), is(original.getParity()));

}

@Test(expected = IllegalArgumentException.class)
public void testMap_Null_Map() throws CloneNotSupportedException {

IChemObjectBuilder builder = NoNotificationChemObjectBuilder.getInstance();

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");

// new stereo element
IAtomParity original = new AtomParity(c1,
o2,n3,c4,h5,
2);


// map the existing element a new element - should through an IllegalArgumentException
IAtomParity mapped = original.map(null, Collections.EMPTY_MAP);

}

@Test
public void testMap_Map_Map_NullElement() throws CloneNotSupportedException {

IChemObjectBuilder builder = NoNotificationChemObjectBuilder.getInstance();

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");

// new stereo element
IAtomParity original = new NNAtomParity(null,
null, null, null, null,
0);


// map the existing element a new element
IAtomParity mapped = original.map(Collections.EMPTY_MAP, Collections.EMPTY_MAP);

Assert.assertNull(mapped.getAtom());
Assert.assertNull(mapped.getSurroundingAtoms()[0]);
Assert.assertNull(mapped.getSurroundingAtoms()[1]);
Assert.assertNull(mapped.getSurroundingAtoms()[2]);
Assert.assertNull(mapped.getSurroundingAtoms()[3]);

}

@Test
public void testMap_Map_Map_EmptyMapping() throws CloneNotSupportedException {

IChemObjectBuilder builder = NoNotificationChemObjectBuilder.getInstance();

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");

// new stereo element
IAtomParity original = new NNAtomParity(c1,
o2,n3,c4,h5,
2);


// map the existing element a new element - should through an IllegalArgumentException
IAtomParity mapped = original.map(Collections.EMPTY_MAP, Collections.EMPTY_MAP);

Assert.assertNull(mapped.getAtom());
Assert.assertNull(mapped.getSurroundingAtoms()[0]);
Assert.assertNull(mapped.getSurroundingAtoms()[1]);
Assert.assertNull(mapped.getSurroundingAtoms()[2]);
Assert.assertNull(mapped.getSurroundingAtoms()[3]);
Assert.assertNotNull(mapped.getParity());

}
}

0 comments on commit 46ffbb4

Please sign in to comment.