rapodaca / mx forked from metamolecular/mx

Essential Cheminformatics

This URL has Read+Write access

rapodaca (author)
Mon Jun 29 20:25:18 -0700 2009
commit  36d044b6ca2f5ec8cc71db25545fa2a3ca368458
tree    4379ea24065ed71b998aab9f9e899df5b7139672
parent  265e25aff96152296538b04f63a9afebac8bedd0
mx / src / com / metamolecular / mx / test / DefaultWalkerTest.java
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 1 /*
2 * MX Cheminformatics Tools for Java
3 *
4 * Copyright (c) 2007-2009 Metamolecular, LLC
5 *
6 * http://metamolecular.com
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26 package com.metamolecular.mx.test;
27
28 import com.metamolecular.mx.model.Atom;
29 import com.metamolecular.mx.model.Bond;
30 import com.metamolecular.mx.walk.Step;
31 import com.metamolecular.mx.walk.DefaultWalker;
32 import com.metamolecular.mx.walk.Reporter;
33 import java.util.List;
34 import junit.framework.TestCase;
35 import static org.mockito.Mockito.*;
36
37 /**
38 * @author Richard L. Apodaca <rapodaca at metamolecular.com>
39 */
40 public class DefaultWalkerTest extends TestCase
41 {
42 private DefaultWalker walker;
43 private Reporter reporter;
44 private Step step;
45 private List path;
46 private Atom atom;
47 private Bond bond;
48
49 @Override
50 protected void setUp() throws Exception
51 {
52 walker = new DefaultWalker();
53 reporter = mock(Reporter.class);
54 step = mock(Step.class);
55 path = mock(List.class);
56 atom = mock(Atom.class);
57 bond = mock(Bond.class);
58
59 when(step.getPath()).thenReturn(path);
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 60 when(step.getAtom()).thenReturn(atom);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 61 }
62
63 public void testItSetsMaximumDepth()
64 {
65 walker.setMaximumDepth(5);
66
67 assertEquals(5, walker.getMaximumDepth());
68 }
69
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 70 public void testItDoesntRequestNextBondWhenMaximumDepthReached()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 71 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 72 walker.setMaximumDepth(2);
73 when(path.size()).thenReturn(2);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 74 doStep();
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 75 verify(step, never()).nextBond();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 76 }
77
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 78 public void testItRequestsNextBondWhenMaximumDepthNotReached()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 79 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 80 Step branch1 = mock(Step.class);
81
82 walker.setMaximumDepth(2);
83 when(path.size()).thenReturn(1);
84 when(step.hasNextBond()).thenReturn(true, false);
409af281 » rapodaca 2009-06-27 first failing test for new ... 85 when(step.nextBond()).thenReturn(bond);
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 86 when(step.nextStep(bond)).thenReturn(branch1);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 87 doStep();
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 88 verify(step, times(1)).nextBond();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 89 }
90
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 91 public void testItRequestsNextBondWhenMaximumDepthNotSet()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 92 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 93 Step branch1 = mock(Step.class);
94
95 walker.setMaximumDepth(0);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 96
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 97 when(path.size()).thenReturn(2);
98 when(step.hasNextBond()).thenReturn(true, false);
409af281 » rapodaca 2009-06-27 first failing test for new ... 99 when(step.nextBond()).thenReturn(bond);
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 100 when(branch1.getAtom()).thenReturn(atom);
101 when(step.nextStep(bond)).thenReturn(branch1);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 102
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 103 doStep();
104 verify(step, times(1)).nextBond();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 105 }
106
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 107 public void testItDoesntCheckPathDepthWhenNotSet()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 108 {
109 doStep();
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 110 verify(path, never()).size();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 111 }
112
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 113 public void testItReportsAtomWhenNextBondUnavailable()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 114 {
115 doStep();
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 116 verify(reporter, times(1)).atomFound(atom);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 117 }
118
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 119 public void testItReporstNoBranchStartWhenStepHasOneBond()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 120 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 121 Step branch1 = mock(Step.class);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 122
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 123 when(step.hasNextBond()).thenReturn(true, false);
124 when(step.nextBond()).thenReturn(bond);
125 when(branch1.getAtom()).thenReturn(atom);
126 when(step.nextStep(any(Bond.class))).thenReturn(branch1);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 127
128 doStep();
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 129 verify(reporter, times(0)).branchStart();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 130 }
131
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 132 public void testItReportsNoBranchEndWhenStepHasOneBond()
22907ddc » rapodaca 2009-06-27 walker reports ring closure 133 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 134 Step branch1 = mock(Step.class);
22907ddc » rapodaca 2009-06-27 walker reports ring closure 135
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 136 when(step.hasNextBond()).thenReturn(true, false);
137 when(step.nextBond()).thenReturn(bond);
138 when(branch1.getAtom()).thenReturn(atom);
139 when(step.nextStep(any(Bond.class))).thenReturn(branch1);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 140
141 doStep();
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 142 verify(reporter, times(0)).branchEnd();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 143 }
144
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 145 public void testItReportsTwoBranchStartsWhenStepHasThreeBonds()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 146 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 147 when(step.hasNextBond()).thenReturn(true, true, true, false);
409af281 » rapodaca 2009-06-27 first failing test for new ... 148 when(step.nextBond()).thenReturn(bond);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 149
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 150 Step branch1 = mock(Step.class);
151 Step branch2 = mock(Step.class);
152 Step branch3 = mock(Step.class);
153
154 when(branch1.getAtom()).thenReturn(atom);
155 when(branch2.getAtom()).thenReturn(atom);
156 when(branch3.getAtom()).thenReturn(atom);
157
158 when(step.nextStep(any(Bond.class))).thenReturn(branch1, branch2, branch3);
159
160 doStep();
161 verify(reporter, times(2)).branchStart();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 162 }
163
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 164 public void testItReportsTwoBranchEndsWhenStepHasThreeBonds()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 165 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 166 when(step.hasNextBond()).thenReturn(true, true, true, false);
167 when(step.nextBond()).thenReturn(bond);
168
169 Step branch1 = mock(Step.class);
170 Step branch2 = mock(Step.class);
171 Step branch3 = mock(Step.class);
172
173 when(branch1.getAtom()).thenReturn(atom);
174 when(branch2.getAtom()).thenReturn(atom);
175 when(branch3.getAtom()).thenReturn(atom);
176
177 when(step.nextStep(any(Bond.class))).thenReturn(branch1, branch2, branch3);
178
179 doStep();
180 verify(reporter, times(2)).branchEnd();
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 181 }
182
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 183 public void testItReportsRingClosureWhenStepNextBondClosesRing()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 184 {
409af281 » rapodaca 2009-06-27 first failing test for new ... 185 when(step.hasNextBond()).thenReturn(true, false);
186 when(step.nextBond()).thenReturn(bond);
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 187
188 Step branch1 = mock(Step.class);
189 when(branch1.getAtom()).thenReturn(atom);
190 when(step.nextStep(bond)).thenReturn(branch1);
191 when(step.closesRingWith(bond)).thenReturn(true);
192
193 doStep();
194 verify(reporter, times(1)).ringClosed(bond);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 195 }
196
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 197 public void testItDoesntReportRingClosureWhenStepNextBondDoesntCloseRing()
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 198 {
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 199 when(step.hasNextBond()).thenReturn(true, false);
409af281 » rapodaca 2009-06-27 first failing test for new ... 200 when(step.nextBond()).thenReturn(bond);
36d044b6 » rapodaca 2009-06-29 rewrote tests to use no mor... 201
202 Step branch1 = mock(Step.class);
203 when(branch1.getAtom()).thenReturn(atom);
204 when(step.nextStep(bond)).thenReturn(branch1);
205 when(step.closesRingWith(bond)).thenReturn(false);
206
207 doStep();
208 verify(reporter, never()).ringClosed(bond);
69832817 » rapodaca 2009-06-27 first defaultwalker code/tests 209 }
210
211 private void doStep()
212 {
213 walker.step(step, reporter);
214 }
215 }