1 /* Copyright (C) 2003-2007  The Chemistry Development Kit (CDK) project
2  *
3  * Contact: cdk-devel@lists.sourceforge.net
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1
8  * of the License, or (at your option) any later version.
9  * All we ask is that proper credit is given for our work, which includes
10  * - but is not limited to - adding the above copyright notice to the beginning
11  * of your source code files, and to any copyright notice that you may distribute
12  * with programs based on this work.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 package org.openscience.cdk.interfaces;
24 
25 import org.junit.Assert;
26 import org.junit.Test;
27 
28 import java.util.Iterator;
29 
30 /**
31  * TestCase for {@link IReaction} implementations.
32  *
33  * @cdk.module test-interfaces
34  */
35 public abstract class AbstractReactionTest extends AbstractChemObjectTest {
36 
37     @Test
testGetReactantCount()38     public void testGetReactantCount() {
39         IReaction reaction = (IReaction) newChemObject();
40         Assert.assertEquals(0, reaction.getReactantCount());
41         reaction.addReactant(reaction.getBuilder().newInstance(IAtomContainer.class));
42         Assert.assertEquals(1, reaction.getReactantCount());
43     }
44 
45     @Test
testGetProductCount()46     public void testGetProductCount() {
47         IReaction reaction = (IReaction) newChemObject();
48         Assert.assertEquals(0, reaction.getProductCount());
49         reaction.addProduct(reaction.getBuilder().newInstance(IAtomContainer.class));
50         Assert.assertEquals(1, reaction.getProductCount());
51     }
52 
53     @Test
testAddReactant_IAtomContainer()54     public void testAddReactant_IAtomContainer() {
55         IReaction reaction = (IReaction) newChemObject();
56         IAtomContainer sodiumhydroxide = reaction.getBuilder().newInstance(IAtomContainer.class);
57         IAtomContainer aceticAcid = reaction.getBuilder().newInstance(IAtomContainer.class);
58         IAtomContainer water = reaction.getBuilder().newInstance(IAtomContainer.class);
59         IAtomContainer acetate = reaction.getBuilder().newInstance(IAtomContainer.class);
60         reaction.addReactant(sodiumhydroxide);
61         reaction.addReactant(aceticAcid);
62         reaction.addReactant(water);
63         Assert.assertEquals(3, reaction.getReactantCount());
64         // next one should trigger a growArray, if the grow
65         // size is still 3.
66         reaction.addReactant(acetate);
67         Assert.assertEquals(4, reaction.getReactantCount());
68 
69         Assert.assertEquals(1.0, reaction.getReactantCoefficient(aceticAcid), 0.00001);
70     }
71 
72     @Test
testSetReactants_IAtomContainerSet()73     public void testSetReactants_IAtomContainerSet() {
74         IReaction reaction = (IReaction) newChemObject();
75         IAtomContainer sodiumhydroxide = reaction.getBuilder().newInstance(IAtomContainer.class);
76         IAtomContainer aceticAcid = reaction.getBuilder().newInstance(IAtomContainer.class);
77         IAtomContainer water = reaction.getBuilder().newInstance(IAtomContainer.class);
78         IAtomContainerSet reactants = reaction.getBuilder().newInstance(IAtomContainerSet.class);
79         reactants.addAtomContainer(sodiumhydroxide);
80         reactants.addAtomContainer(aceticAcid);
81         reactants.addAtomContainer(water);
82         reaction.setReactants(reactants);
83         Assert.assertEquals(3, reaction.getReactantCount());
84 
85         Assert.assertEquals(1.0, reaction.getReactantCoefficient(aceticAcid), 0.00001);
86     }
87 
88     @Test
testAddReactant_IAtomContainer_Double()89     public void testAddReactant_IAtomContainer_Double() {
90         IReaction reaction = (IReaction) newChemObject();
91         IAtomContainer proton = reaction.getBuilder().newInstance(IAtomContainer.class);
92         IAtomContainer sulfate = reaction.getBuilder().newInstance(IAtomContainer.class);
93         reaction.addReactant(proton, 2d);
94         reaction.addReactant(sulfate, 1d);
95         Assert.assertEquals(2.0, reaction.getReactantCoefficient(proton), 0.00001);
96         Assert.assertEquals(1.0, reaction.getReactantCoefficient(sulfate), 0.00001);
97     }
98 
99     @Test
testAddProduct_IAtomContainer()100     public void testAddProduct_IAtomContainer() {
101         IReaction reaction = (IReaction) newChemObject();
102         IAtomContainer sodiumhydroxide = reaction.getBuilder().newInstance(IAtomContainer.class);
103         IAtomContainer aceticAcid = reaction.getBuilder().newInstance(IAtomContainer.class);
104         IAtomContainer water = reaction.getBuilder().newInstance(IAtomContainer.class);
105         IAtomContainer acetate = reaction.getBuilder().newInstance(IAtomContainer.class);
106         reaction.addProduct(sodiumhydroxide);
107         reaction.addProduct(aceticAcid);
108         reaction.addProduct(water);
109         Assert.assertEquals(3, reaction.getProductCount());
110         // next one should trigger a growArray, if the grow
111         // size is still 3.
112         reaction.addProduct(acetate);
113         Assert.assertEquals(4, reaction.getProductCount());
114 
115         Assert.assertEquals(1.0, reaction.getProductCoefficient(aceticAcid), 0.00001);
116     }
117 
118     @Test
testSetProducts_IAtomContainerSet()119     public void testSetProducts_IAtomContainerSet() {
120         IReaction reaction = (IReaction) newChemObject();
121         IAtomContainer sodiumhydroxide = reaction.getBuilder().newInstance(IAtomContainer.class);
122         IAtomContainer aceticAcid = reaction.getBuilder().newInstance(IAtomContainer.class);
123         IAtomContainer water = reaction.getBuilder().newInstance(IAtomContainer.class);
124         IAtomContainerSet products = reaction.getBuilder().newInstance(IAtomContainerSet.class);
125         products.addAtomContainer(sodiumhydroxide);
126         products.addAtomContainer(aceticAcid);
127         products.addAtomContainer(water);
128         reaction.setProducts(products);
129         Assert.assertEquals(3, reaction.getProductCount());
130 
131         Assert.assertEquals(1.0, reaction.getProductCoefficient(aceticAcid), 0.00001);
132     }
133 
134     @Test
testAddProduct_IAtomContainer_Double()135     public void testAddProduct_IAtomContainer_Double() {
136         IReaction reaction = (IReaction) newChemObject();
137         IAtomContainer proton = reaction.getBuilder().newInstance(IAtomContainer.class);
138         IAtomContainer sulfate = reaction.getBuilder().newInstance(IAtomContainer.class);
139         reaction.addProduct(proton, 2.0);
140         reaction.addProduct(sulfate, 1.0);
141         Assert.assertEquals(2.0, reaction.getProductCoefficient(proton), 0.00001);
142         Assert.assertEquals(1.0, reaction.getProductCoefficient(sulfate), 0.00001);
143     }
144 
145     @Test
testAddAgent_IAtomContainer()146     public void testAddAgent_IAtomContainer() {
147         IReaction reaction = (IReaction) newChemObject();
148         IAtomContainer proton = reaction.getBuilder().newInstance(IAtomContainer.class);
149         reaction.addAgent(proton);
150         Assert.assertEquals(1, reaction.getAgents().getAtomContainerCount());
151     }
152 
153     @Test
testGetReactantCoefficient_IAtomContainer()154     public void testGetReactantCoefficient_IAtomContainer() {
155         IReaction reaction = (IReaction) newChemObject();
156         IAtomContainer proton = reaction.getBuilder().newInstance(IAtomContainer.class);
157         reaction.addReactant(proton, 2.0);
158         Assert.assertEquals(2.0, reaction.getReactantCoefficient(proton), 0.00001);
159 
160         Assert.assertEquals(-1.0,
161                 reaction.getReactantCoefficient(reaction.getBuilder().newInstance(IAtomContainer.class)), 0.00001);
162     }
163 
164     @Test
testGetProductCoefficient_IAtomContainer()165     public void testGetProductCoefficient_IAtomContainer() {
166         IReaction reaction = (IReaction) newChemObject();
167         IAtomContainer proton = reaction.getBuilder().newInstance(IAtomContainer.class);
168         reaction.addProduct(proton, 2.0);
169         Assert.assertEquals(2.0, reaction.getProductCoefficient(proton), 0.00001);
170 
171         Assert.assertEquals(-1.0,
172                 reaction.getProductCoefficient(reaction.getBuilder().newInstance(IAtomContainer.class)), 0.00001);
173     }
174 
175     @Test
testSetReactantCoefficient_IAtomContainer_Double()176     public void testSetReactantCoefficient_IAtomContainer_Double() {
177         IReaction reaction = (IReaction) newChemObject();
178         IAtomContainer proton = reaction.getBuilder().newInstance(IAtomContainer.class);
179         reaction.addReactant(proton, 2.0);
180         reaction.setReactantCoefficient(proton, 3.0);
181         Assert.assertEquals(3.0, reaction.getReactantCoefficient(proton), 0.00001);
182     }
183 
184     @Test
testSetProductCoefficient_IAtomContainer_Double()185     public void testSetProductCoefficient_IAtomContainer_Double() {
186         IReaction reaction = (IReaction) newChemObject();
187         IAtomContainer proton = reaction.getBuilder().newInstance(IAtomContainer.class);
188         reaction.addProduct(proton, 2.0);
189         reaction.setProductCoefficient(proton, 1.0);
190         Assert.assertEquals(1.0, reaction.getProductCoefficient(proton), 0.00001);
191     }
192 
193     @Test
testGetReactantCoefficients()194     public void testGetReactantCoefficients() {
195         IReaction reaction = (IReaction) newChemObject();
196         IAtomContainer ed1 = reaction.getBuilder().newInstance(IAtomContainer.class);
197         IAtomContainer ed2 = reaction.getBuilder().newInstance(IAtomContainer.class);
198         reaction.addReactant(ed1, 2d);
199         reaction.addReactant(ed2, 3d);
200         Double[] ec = reaction.getReactantCoefficients();
201         Assert.assertEquals(2.0, ec.length, 0.00001);
202         Assert.assertEquals(reaction.getReactantCoefficient(ed1), ec[0], 0.00001);
203         Assert.assertEquals(3.0, ec[1], 0.00001);
204     }
205 
206     @Test
testGetProductCoefficients()207     public void testGetProductCoefficients() {
208         IReaction reaction = (IReaction) newChemObject();
209         IAtomContainer pr1 = reaction.getBuilder().newInstance(IAtomContainer.class);
210         IAtomContainer pr2 = reaction.getBuilder().newInstance(IAtomContainer.class);
211         reaction.addProduct(pr1, 1d);
212         reaction.addProduct(pr2, 2d);
213         Double[] pc = reaction.getProductCoefficients();
214         Assert.assertEquals(2.0, pc.length, 0.00001);
215         Assert.assertEquals(reaction.getProductCoefficient(pr1), pc[0], 0.00001);
216         Assert.assertEquals(2.0, pc[1], 0.00001);
217     }
218 
219     @Test
testSetReactantCoefficients_arrayDouble()220     public void testSetReactantCoefficients_arrayDouble() {
221         IReaction reaction = (IReaction) newChemObject();
222         IAtomContainer ed1 = reaction.getBuilder().newInstance(IAtomContainer.class);
223         IAtomContainer ed2 = reaction.getBuilder().newInstance(IAtomContainer.class);
224         reaction.addReactant(ed1, 2d);
225         reaction.addReactant(ed2, 3d);
226         Double[] ec = {1.0, 2.0};
227         boolean coeffSet = reaction.setReactantCoefficients(ec);
228         Assert.assertTrue(coeffSet);
229         Assert.assertEquals(1.0, reaction.getReactantCoefficient(ed1), 0.00001);
230         Assert.assertEquals(2.0, reaction.getReactantCoefficient(ed2), 0.00001);
231         Double[] ecFalse = {1.0};
232         Assert.assertFalse(reaction.setReactantCoefficients(ecFalse));
233     }
234 
235     @Test
testSetProductCoefficients_arrayDouble()236     public void testSetProductCoefficients_arrayDouble() {
237         IReaction reaction = (IReaction) newChemObject();
238         IAtomContainer pr1 = reaction.getBuilder().newInstance(IAtomContainer.class);
239         reaction.addProduct(pr1, 1d);
240         Double[] pc = {2.0};
241         boolean coeffSet = reaction.setProductCoefficients(pc);
242         Assert.assertTrue(coeffSet);
243         Assert.assertEquals(2.0, reaction.getProductCoefficient(pr1), 0.00001);
244         Double[] pcFalse = {1.0, 2.0};
245         Assert.assertFalse(reaction.setProductCoefficients(pcFalse));
246     }
247 
248     @Test
testGetReactants()249     public void testGetReactants() {
250         IReaction reaction = (IReaction) newChemObject();
251         IAtomContainer sodiumhydroxide = reaction.getBuilder().newInstance(IAtomContainer.class);
252         IAtomContainer aceticAcid = reaction.getBuilder().newInstance(IAtomContainer.class);
253         IAtomContainer water = reaction.getBuilder().newInstance(IAtomContainer.class);
254         reaction.addReactant(sodiumhydroxide);
255         reaction.addReactant(aceticAcid);
256         reaction.addReactant(water);
257         Assert.assertEquals(3, reaction.getReactants().getAtomContainerCount());
258     }
259 
260     @Test
testGetProducts()261     public void testGetProducts() {
262         IReaction reaction = (IReaction) newChemObject();
263         IAtomContainer sodiumhydroxide = reaction.getBuilder().newInstance(IAtomContainer.class);
264         IAtomContainer aceticAcid = reaction.getBuilder().newInstance(IAtomContainer.class);
265         IAtomContainer water = reaction.getBuilder().newInstance(IAtomContainer.class);
266         reaction.addProduct(sodiumhydroxide);
267         reaction.addProduct(aceticAcid);
268         reaction.addProduct(water);
269         Assert.assertEquals(3, reaction.getProducts().getAtomContainerCount());
270     }
271 
272     @Test
testGetAgents()273     public void testGetAgents() {
274         IReaction reaction = (IReaction) newChemObject();
275         IAtomContainer water = reaction.getBuilder().newInstance(IAtomContainer.class);
276         reaction.addAgent(water);
277         Assert.assertEquals(1, reaction.getAgents().getAtomContainerCount());
278     }
279 
280     @Test
testSetDirection_IReaction_Direction()281     public void testSetDirection_IReaction_Direction() {
282         IReaction reaction = (IReaction) newChemObject();
283         IReaction.Direction direction = IReaction.Direction.BIDIRECTIONAL;
284         reaction.setDirection(direction);
285         Assert.assertEquals(direction, reaction.getDirection());
286     }
287 
288     @Test
testGetDirection()289     public void testGetDirection() {
290         IReaction reaction = (IReaction) newChemObject();
291         Assert.assertEquals(IReaction.Direction.FORWARD, reaction.getDirection());
292     }
293 
294     /**
295      * Method to test whether the class complies with RFC #9.
296      */
297     @Test
testToString()298     public void testToString() {
299         IReaction reaction = (IReaction) newChemObject();
300         String description = reaction.toString();
301         for (int i = 0; i < description.length(); i++) {
302             Assert.assertTrue(description.charAt(i) != '\n');
303             Assert.assertTrue(description.charAt(i) != '\r');
304         }
305     }
306 
307     @Test
308     @Override
testClone()309     public void testClone() throws Exception {
310         IReaction reaction = (IReaction) newChemObject();
311         Object clone = reaction.clone();
312         Assert.assertNotNull(clone);
313         Assert.assertTrue(clone instanceof IReaction);
314     }
315 
316     @Test
testClone_Mapping()317     public void testClone_Mapping() throws Exception {
318         IReaction reaction = (IReaction) newChemObject();
319         IMapping mapping = reaction.getBuilder().newInstance(IMapping.class,
320                 reaction.getBuilder().newInstance(IAtom.class, "C"),
321                 reaction.getBuilder().newInstance(IAtom.class, "C"));
322         reaction.addMapping(mapping);
323         IReaction clonedReaction = (IReaction) reaction.clone();
324         Iterator<IMapping> mappings = reaction.mappings().iterator();
325         Iterator<IMapping> clonedMappings = clonedReaction.mappings().iterator();
326         Assert.assertNotNull(mappings);
327         Assert.assertTrue(mappings.hasNext());
328         Assert.assertNotNull(clonedMappings);
329         Assert.assertTrue(clonedMappings.hasNext());
330     }
331 
332     @Test
testAddMapping_IMapping()333     public void testAddMapping_IMapping() {
334         IReaction reaction = (IReaction) newChemObject();
335         IMapping mapping = reaction.getBuilder().newInstance(IMapping.class,
336                 reaction.getBuilder().newInstance(IAtom.class, "C"),
337                 reaction.getBuilder().newInstance(IAtom.class, "C"));
338         reaction.addMapping(mapping);
339         Iterator<IMapping> mappings = reaction.mappings().iterator();
340         Assert.assertNotNull(mappings);
341         Assert.assertTrue(mappings.hasNext());
342         Assert.assertEquals(mapping, (IMapping) mappings.next());
343     }
344 
345     @Test
testRemoveMapping_int()346     public void testRemoveMapping_int() {
347         IReaction reaction = (IReaction) newChemObject();
348         IMapping mapping = reaction.getBuilder().newInstance(IMapping.class,
349                 reaction.getBuilder().newInstance(IAtom.class, "C"),
350                 reaction.getBuilder().newInstance(IAtom.class, "C"));
351         reaction.addMapping(mapping);
352         Assert.assertEquals(1, reaction.getMappingCount());
353         reaction.removeMapping(0);
354         Assert.assertEquals(0, reaction.getMappingCount());
355     }
356 
357     @Test
testGetMapping_int()358     public void testGetMapping_int() {
359         IReaction reaction = (IReaction) newChemObject();
360         IMapping mapping = reaction.getBuilder().newInstance(IMapping.class,
361                 reaction.getBuilder().newInstance(IAtom.class, "C"),
362                 reaction.getBuilder().newInstance(IAtom.class, "C"));
363         reaction.addMapping(mapping);
364         IMapping gotIt = reaction.getMapping(0);
365         Assert.assertEquals(mapping, gotIt);
366     }
367 
368     @Test
testGetMappingCount()369     public void testGetMappingCount() {
370         IReaction reaction = (IReaction) newChemObject();
371         IMapping mapping = reaction.getBuilder().newInstance(IMapping.class,
372                 reaction.getBuilder().newInstance(IAtom.class, "C"),
373                 reaction.getBuilder().newInstance(IAtom.class, "C"));
374         reaction.addMapping(mapping);
375         Assert.assertEquals(1, reaction.getMappingCount());
376     }
377 
378     @Test
testMappings()379     public void testMappings() {
380         IReaction reaction = (IReaction) newChemObject();
381         IMapping mapping = reaction.getBuilder().newInstance(IMapping.class,
382                 reaction.getBuilder().newInstance(IAtom.class, "C"),
383                 reaction.getBuilder().newInstance(IAtom.class, "C"));
384         reaction.addMapping(mapping);
385         Assert.assertEquals(1, reaction.getMappingCount());
386     }
387 }
388