1   /*
2   * E-nspire Gemini.
3   * A Java and AspectJ based framework that enables transparent 
4   * bidirectional relationships between Plain Old Java Objects.
5   * 
6   * Copyright (C) 2005 Dragan Djuric
7   * 
8   * This program is free software; you can redistribute it and/or
9   * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  * 
22  * Contact the author at dragand@dev.java.net
23  */
24  package com.enspire.gemini.commands;
25  
26  import java.util.List;
27  
28  import org.jmock.Mock;
29  import org.jmock.MockObjectTestCase;
30  
31  import com.enspire.gemini.BidirectionalProperty;
32  import com.enspire.gemini.RelationshipUpdater;
33  
34  /***
35   * @author Dragan Djuric <dragand@dev.java.net>
36   *
37   */
38  public class BidirectionalListAddByIndexTest extends MockObjectTestCase {
39  
40      private BidirectionalListAddByIndex testCommand;
41      
42      private Mock mockBidirectionalProperty;
43      private Mock mockList;
44      private Mock mockRelationshipUpdater;
45      
46      private BidirectionalProperty bidirectionalProperty;
47      private List unidirectional;
48      private RelationshipUpdater relationshipUpdater;
49      private Object addValue;
50      private int addIndex = 99;
51      private String oppositeName = "oppositeName";
52      private Object owner;
53      
54      /***
55       * @see junit.framework.TestCase#setUp()
56       */
57      
58      protected void setUp() throws Exception {
59          super.setUp();
60          mockBidirectionalProperty = new Mock(BidirectionalProperty.class);
61          mockList = new Mock(List.class);
62          mockRelationshipUpdater = new Mock(RelationshipUpdater.class);
63          bidirectionalProperty = 
64                  (BidirectionalProperty)mockBidirectionalProperty.proxy();
65          unidirectional = (List)mockList.proxy();
66          relationshipUpdater = 
67                  (RelationshipUpdater)mockRelationshipUpdater.proxy();
68          addValue = new Object();
69          testCommand = new BidirectionalListAddByIndex(
70                  bidirectionalProperty, unidirectional, addIndex, addValue);
71          owner = new Object();
72      }
73      
74      public void testExecute() {
75          Object oldOwner = new Object();
76          int size = 999;
77          mockList.expects(atLeastOnce()).method("size").withNoArguments().
78                  will(onConsecutiveCalls(returnValue(size), returnValue(size + 1)));
79          mockList.expects(once()).method("add").
80                  with(same(addIndex), same(addValue));
81          mockBidirectionalProperty.expects(once()).method(
82                  "getRelationshipUpdater").withNoArguments().will(
83                  returnValue(relationshipUpdater));
84          mockBidirectionalProperty.expects(once()).method("getOwner").
85                  withNoArguments().will(returnValue(owner));
86          mockBidirectionalProperty.expects(once()).method("getOppositeName").
87                  withNoArguments().will(returnValue(oppositeName));
88          mockRelationshipUpdater.expects(once()).method("set").with(
89                  same(addValue), same(oppositeName), same(owner)).
90                  will(returnValue(oldOwner));
91          testCommand.execute();
92      }
93      
94      public void testExecuteExisting() {
95          Object oldOwner = new Object();
96          int size = 999;
97          mockList.expects(atLeastOnce()).method("size").withNoArguments().
98                  will(onConsecutiveCalls(returnValue(size), returnValue(size)));
99          mockList.expects(once()).method("add").
100                 with(same(addIndex), same(addValue));
101         testCommand.execute();
102     }
103     
104     public void testExecutethrowsException() {
105         int size = 999;
106         mockList.expects(atLeastOnce()).method("size").withNoArguments().
107                 will(onConsecutiveCalls(returnValue(size), returnValue(size + 1)));
108         mockList.expects(once()).method("add").
109                 with(same(addIndex), same(addValue));
110         mockBidirectionalProperty.expects(once()).method(
111                 "getRelationshipUpdater").withNoArguments().will(
112                 returnValue(relationshipUpdater));
113         mockBidirectionalProperty.expects(once()).method("getOwner").
114                 withNoArguments().will(returnValue(owner));
115         mockBidirectionalProperty.expects(once()).method("getOppositeName").
116                 withNoArguments().will(returnValue(oppositeName));
117         mockRelationshipUpdater.expects(once()).method("set").with(
118                 same(addValue), same(oppositeName), same(owner)).
119                 will(throwException(new RuntimeException()));
120         mockList.expects(once()).method("remove").
121                 with(same(addIndex)).will(returnValue(true));
122         try {
123             testCommand.execute();
124             fail("RuntimeException should be thrown");
125         }catch(RuntimeException e) {
126         }
127     }
128     
129     public void testUndo() {
130         Object oldOwner = new Object();
131         int size = 999;
132         mockList.expects(atLeastOnce()).method("size").withNoArguments().
133                 will(onConsecutiveCalls(returnValue(size), returnValue(size + 1)));
134         mockList.expects(once()).method("add").
135                 with(same(addIndex), same(addValue));
136         mockBidirectionalProperty.expects(atLeastOnce()).method(
137                 "getRelationshipUpdater").withNoArguments().will(
138                 returnValue(relationshipUpdater));
139         mockBidirectionalProperty.expects(atLeastOnce()).method("getOwner").
140                 withNoArguments().will(returnValue(owner));
141         mockBidirectionalProperty.expects(atLeastOnce()).method("getOppositeName").
142                 withNoArguments().will(returnValue(oppositeName));
143         mockRelationshipUpdater.expects(once()).method("set").with(
144                 same(addValue), same(oppositeName), same(owner)).
145                 will(returnValue(oldOwner));
146         testCommand.execute();
147         
148         mockList.expects(once()).method("remove").
149                 with(same(addIndex)).will(returnValue(true));
150         mockRelationshipUpdater.expects(atLeastOnce()).method("unset").with(
151                 same(addValue), same(oppositeName), same(owner)).
152                 will(returnValue(null));
153         mockRelationshipUpdater.expects(once()).method("set").with(
154                 same(addValue), same(oppositeName), same(oldOwner));
155         testCommand.undo();
156     }
157 }