2626import org .junit .jupiter .api .BeforeAll ;
2727import org .junit .jupiter .api .Test ;
2828import org .openscience .cdk .Atom ;
29- import org .openscience .cdk .test .CDKTestCase ;
3029import org .openscience .cdk .DefaultChemObjectBuilder ;
31- import org .openscience .cdk .interfaces .IAtom ;
32- import org .openscience .cdk .interfaces .IAtomContainer ;
33- import org .openscience .cdk .interfaces .IBond ;
30+ import org .openscience .cdk .exception .CDKException ;
31+ import org .openscience .cdk .interfaces .*;
3432import org .openscience .cdk .interfaces .IBond .Order ;
35- import org .openscience .cdk .interfaces .IChemObjectBuilder ;
36- import org .openscience .cdk .interfaces .IDoubleBondStereochemistry ;
3733import org .openscience .cdk .interfaces .IDoubleBondStereochemistry .Conformation ;
34+ import org .openscience .cdk .silent .SilentChemObjectBuilder ;
35+ import org .openscience .cdk .smiles .SmilesParser ;
36+ import org .openscience .cdk .test .CDKTestCase ;
3837
3938import java .util .Collections ;
4039import java .util .HashMap ;
4140import java .util .Map ;
4241
43- import static org .hamcrest .CoreMatchers .is ;
44- import static org .hamcrest .CoreMatchers .not ;
45- import static org .hamcrest .CoreMatchers .sameInstance ;
42+ import static org .hamcrest .CoreMatchers .*;
4643
47- /**
48- */
4944class DoubleBondStereochemistryTest extends CDKTestCase {
5045
5146 private static IAtomContainer molecule ;
@@ -55,7 +50,7 @@ class DoubleBondStereochemistryTest extends CDKTestCase {
5550 * This method creates <i>E</i>-but-2-ene.
5651 */
5752 @ BeforeAll
58- static void setup () throws Exception {
53+ static void setup () {
5954 molecule = DefaultChemObjectBuilder .getInstance ().newAtomContainer ();
6055 molecule .addAtom (new Atom ("C" ));
6156 molecule .addAtom (new Atom ("C" ));
@@ -129,7 +124,7 @@ void testGetBonds() {
129124 }
130125
131126 @ Test
132- void contains () throws Exception {
127+ void contains () {
133128 IChemObjectBuilder builder = DefaultChemObjectBuilder .getInstance ();
134129
135130 IAtom c1 = builder .newInstance (IAtom .class , "C" );
@@ -182,7 +177,7 @@ void testMap_Map_Map() throws CloneNotSupportedException {
182177 mapping .put (c2o4 , c2o4clone );
183178
184179 // map the existing element a new element
185- IDoubleBondStereochemistry mapped = original .map (Collections .EMPTY_MAP , mapping );
180+ IDoubleBondStereochemistry mapped = original .map (Collections .< IAtom , IAtom > emptyMap () , mapping );
186181
187182 org .hamcrest .MatcherAssert .assertThat ("mapped chiral atom was the same as the original" , mapped .getStereoBond (),
188183 is (not (sameInstance (original .getStereoBond ()))));
@@ -191,19 +186,19 @@ void testMap_Map_Map() throws CloneNotSupportedException {
191186 IBond [] originalBonds = original .getBonds ();
192187 IBond [] mappedBonds = mapped .getBonds ();
193188
194- org .hamcrest .MatcherAssert .assertThat ("first bond was te same as the original" , mappedBonds [0 ],
189+ org .hamcrest .MatcherAssert .assertThat ("first bond was the same as the original" , mappedBonds [0 ],
195190 is (not (sameInstance (originalBonds [0 ]))));
196- org .hamcrest .MatcherAssert .assertThat ("first mapped bond was not the clone" , mappedBonds [0 ], is (sameInstance (c1o3clone )));
197- org .hamcrest .MatcherAssert .assertThat ("second bond was te same as the original" , mappedBonds [1 ],
191+ org .hamcrest .MatcherAssert .assertThat ("first mapped bond was not the clone" , mappedBonds [0 ], is (sameInstance (c2o4clone )));
192+ org .hamcrest .MatcherAssert .assertThat ("second bond was the same as the original" , mappedBonds [1 ],
198193 is (not (sameInstance (originalBonds [1 ]))));
199- org .hamcrest .MatcherAssert .assertThat ("second mapped bond was not the clone" , mappedBonds [1 ], is (sameInstance (c2o4clone )));
194+ org .hamcrest .MatcherAssert .assertThat ("second mapped bond was not the clone" , mappedBonds [1 ], is (sameInstance (c1o3clone )));
200195
201196 org .hamcrest .MatcherAssert .assertThat ("stereo was not mapped" , mapped .getStereo (), is (original .getStereo ()));
202197
203198 }
204199
205200 @ Test
206- void testMap_Null_Map () throws CloneNotSupportedException {
201+ void testMap_Null_Map () {
207202 Assertions .assertThrows (IllegalArgumentException .class ,
208203 () -> {
209204 IChemObjectBuilder builder = DefaultChemObjectBuilder .getInstance ();
@@ -222,12 +217,12 @@ void testMap_Null_Map() throws CloneNotSupportedException {
222217 Conformation .OPPOSITE );
223218
224219 // map the existing element a new element - should through an IllegalArgumentException
225- IDoubleBondStereochemistry mapped = original .map (Collections .EMPTY_MAP , null );
220+ IDoubleBondStereochemistry mapped = original .map (Collections .< IAtom , IAtom > emptyMap () , null );
226221 });
227222 }
228223
229224 @ Test
230- void testMap_Map_Map_EmptyMapping () throws CloneNotSupportedException {
225+ void testMap_Map_Map_EmptyMapping () {
231226
232227 IChemObjectBuilder builder = DefaultChemObjectBuilder .getInstance ();
233228
@@ -245,7 +240,7 @@ void testMap_Map_Map_EmptyMapping() throws CloneNotSupportedException {
245240 Conformation .OPPOSITE );
246241
247242 // map the existing element a new element - should through an IllegalArgumentException
248- IDoubleBondStereochemistry mapped = original .map (Collections .EMPTY_MAP , Collections .EMPTY_MAP );
243+ IDoubleBondStereochemistry mapped = original .map (Collections .< IAtom , IAtom > emptyMap (), Collections .< IBond , IBond > emptyMap () );
249244
250245 org .hamcrest .MatcherAssert .assertThat (mapped , is (sameInstance (original )));
251246 }
@@ -258,4 +253,61 @@ void testToString() {
258253 Assertions .assertNotSame (0 , stringRepr .length ());
259254 Assertions .assertFalse (stringRepr .contains ("\n " ));
260255 }
256+
257+ @ Test
258+ void testMapStrictCarrierSwapNecessary () throws CDKException {
259+ // arrange
260+ SmilesParser smilesParser = new SmilesParser (SilentChemObjectBuilder .getInstance ());
261+ final IAtomContainer molecule1 = smilesParser .parseSmiles ("CCC\\ C(Cl)=C(/C)Cl" );
262+ final IAtomContainer molecule2 = smilesParser .parseSmiles ("ClC(C)=C(Cl)CCC" );
263+
264+ Map <IChemObject ,IChemObject > chemObjectMap = new HashMap <>();
265+ chemObjectMap .put (molecule1 .getBond (2 ), molecule2 .getBond (4 )); // carrier
266+ chemObjectMap .put (molecule1 .getBond (4 ), molecule2 .getBond (2 )); // focus
267+ chemObjectMap .put (molecule1 .getBond (5 ), molecule2 .getBond (1 )); // carrier
268+ IStereoElement <?,?> stereoElement = molecule1 .stereoElements ().iterator ().next ();
269+
270+ // act
271+ IStereoElement <?,?> newStereoElement = stereoElement .mapStrict (chemObjectMap );
272+
273+ // assert
274+ DoubleBondStereochemistry newDoubleBondStereochemistry = (DoubleBondStereochemistry ) newStereoElement ;
275+ Assertions .assertTrue (
276+ newDoubleBondStereochemistry .getFocus ().getBegin () == newDoubleBondStereochemistry .getCarriers ().get (0 ).getBegin () ||
277+ newDoubleBondStereochemistry .getFocus ().getBegin () == newDoubleBondStereochemistry .getCarriers ().get (0 ).getEnd ()
278+ );
279+ Assertions .assertTrue (
280+ newDoubleBondStereochemistry .getFocus ().getEnd () == newDoubleBondStereochemistry .getCarriers ().get (1 ).getBegin () ||
281+ newDoubleBondStereochemistry .getFocus ().getEnd () == newDoubleBondStereochemistry .getCarriers ().get (1 ).getEnd ()
282+ );
283+ }
284+
285+ @ Test
286+ void testMapStrictCarrierSwapNotNecessary () throws CDKException {
287+ // arrange
288+ SmilesParser smilesParser = new SmilesParser (SilentChemObjectBuilder .getInstance ());
289+ final IAtomContainer molecule1 = smilesParser .parseSmiles ("CCC\\ C(Cl)=C(/C)Cl" );
290+ final IAtomContainer molecule2 = smilesParser .parseSmiles ("CCCC(Cl)=C(/C)Cl" );
291+
292+ Map <IChemObject ,IChemObject > chemObjectMap = new HashMap <>();
293+ chemObjectMap .put (molecule1 .getBond (2 ), molecule2 .getBond (2 )); // carrier
294+ chemObjectMap .put (molecule1 .getBond (4 ), molecule2 .getBond (4 )); // focus
295+ chemObjectMap .put (molecule1 .getBond (5 ), molecule2 .getBond (5 )); // carrier
296+ IStereoElement <?,?> stereoElement = molecule1 .stereoElements ().iterator ().next ();
297+
298+ // act
299+ IStereoElement <?,?> newStereoElement = stereoElement .mapStrict (chemObjectMap );
300+
301+ // assert
302+ DoubleBondStereochemistry newDoubleBondStereochemistry = (DoubleBondStereochemistry ) newStereoElement ;
303+ Assertions .assertTrue (
304+ newDoubleBondStereochemistry .getFocus ().getBegin () == newDoubleBondStereochemistry .getCarriers ().get (0 ).getBegin () ||
305+ newDoubleBondStereochemistry .getFocus ().getBegin () == newDoubleBondStereochemistry .getCarriers ().get (0 ).getEnd ()
306+ );
307+ Assertions .assertTrue (
308+ newDoubleBondStereochemistry .getFocus ().getEnd () == newDoubleBondStereochemistry .getCarriers ().get (1 ).getBegin () ||
309+ newDoubleBondStereochemistry .getFocus ().getEnd () == newDoubleBondStereochemistry .getCarriers ().get (1 ).getEnd ()
310+ );
311+ }
312+
261313}
0 commit comments