1 /*
2  * Copyright (c) 2013 European Bioinformatics Institute (EMBL-EBI)
3  *                    John May <jwmay@users.sf.net>
4  *
5  * Contact: cdk-devel@lists.sourceforge.net
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or (at
10  * your option) any later version. All we ask is that proper credit is given
11  * for our work, which includes - but is not limited to - adding the above
12  * copyright notice to the beginning of your source code files, and to any
13  * copyright notice that you may distribute with programs based on this work.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 U
23  */
24 
25 package org.openscience.cdk.isomorphism;
26 
27 import org.junit.Test;
28 import org.openscience.cdk.interfaces.IAtom;
29 import org.openscience.cdk.interfaces.IPseudoAtom;
30 import org.openscience.cdk.isomorphism.matchers.IQueryAtom;
31 
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertTrue;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36 
37 /**
38  * @author John May
39  * @cdk.module test-isomorphism
40  */
41 public class AtomMatcherTest {
42 
43     @Test
anyMatch()44     public void anyMatch() throws Exception {
45         AtomMatcher matcher = AtomMatcher.forAny();
46         IAtom atom1 = mock(IAtom.class);
47         IAtom atom2 = mock(IAtom.class);
48         IAtom atom3 = mock(IAtom.class);
49         when(atom1.getAtomicNumber()).thenReturn(6);
50         when(atom2.getAtomicNumber()).thenReturn(7);
51         when(atom3.getAtomicNumber()).thenReturn(8);
52         assertTrue(matcher.matches(atom1, atom2));
53         assertTrue(matcher.matches(atom2, atom1));
54         assertTrue(matcher.matches(atom1, atom3));
55         assertTrue(matcher.matches(atom3, atom1));
56         assertTrue(matcher.matches(atom2, atom3));
57         assertTrue(matcher.matches(atom1, null));
58         assertTrue(matcher.matches(null, null));
59     }
60 
61     @Test
elementMatch()62     public void elementMatch() throws Exception {
63         AtomMatcher matcher = AtomMatcher.forElement();
64         IAtom atom1 = mock(IAtom.class);
65         IAtom atom2 = mock(IAtom.class);
66         when(atom1.getAtomicNumber()).thenReturn(6);
67         when(atom2.getAtomicNumber()).thenReturn(6);
68         assertTrue(matcher.matches(atom1, atom2));
69         assertTrue(matcher.matches(atom2, atom1));
70     }
71 
72     @Test
elementMismatch()73     public void elementMismatch() throws Exception {
74         AtomMatcher matcher = AtomMatcher.forElement();
75         IAtom atom1 = mock(IAtom.class);
76         IAtom atom2 = mock(IAtom.class);
77         when(atom1.getAtomicNumber()).thenReturn(6);
78         when(atom2.getAtomicNumber()).thenReturn(8);
79         assertFalse(matcher.matches(atom1, atom2));
80         assertFalse(matcher.matches(atom2, atom1));
81     }
82 
83     @Test
elementPseudo()84     public void elementPseudo() throws Exception {
85         AtomMatcher matcher = AtomMatcher.forElement();
86         IAtom atom1 = mock(IPseudoAtom.class);
87         IAtom atom2 = mock(IPseudoAtom.class);
88         assertTrue(matcher.matches(atom1, atom2));
89         assertTrue(matcher.matches(atom2, atom1));
90     }
91 
92     @Test(expected = NullPointerException.class)
elementError()93     public void elementError() throws Exception {
94         AtomMatcher matcher = AtomMatcher.forElement();
95         IAtom atom1 = mock(IAtom.class);
96         IAtom atom2 = mock(IAtom.class);
97         when(atom1.getAtomicNumber()).thenReturn(null);
98         when(atom2.getAtomicNumber()).thenReturn(null);
99         matcher.matches(atom1, atom2);
100     }
101 
102     @Test
queryMatch()103     public void queryMatch() throws Exception {
104         AtomMatcher matcher = AtomMatcher.forQuery();
105         IQueryAtom atom1 = mock(IQueryAtom.class);
106         IAtom atom2 = mock(IAtom.class);
107         IAtom atom3 = mock(IAtom.class);
108         when(atom1.matches(atom2)).thenReturn(true);
109         when(atom1.matches(atom3)).thenReturn(false);
110         assertTrue(matcher.matches(atom1, atom2));
111         assertFalse(matcher.matches(atom1, atom3));
112     }
113 }
114