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.Collection;
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 BidirectionalCollectionAddTest extends MockObjectTestCase {
39  
40      private BidirectionalCollectionAdd testCommand;
41      
42      private Mock mockBidirectionalProperty;
43      private Mock mockCollection;
44      private Mock mockRelationshipUpdater;
45      
46      private BidirectionalProperty bidirectionalProperty;
47      private Collection unidirectional;
48      private RelationshipUpdater relationshipUpdater;
49      private Object addValue;
50      private String oppositeName = "oppositeName";
51      private Object owner;
52      
53      /***
54       * @see junit.framework.TestCase#setUp()
55       */
56      
57      protected void setUp() throws Exception {
58          super.setUp();
59          mockBidirectionalProperty = new Mock(BidirectionalProperty.class);
60          mockCollection = new Mock(Collection.class);
61          mockRelationshipUpdater = new Mock(RelationshipUpdater.class);
62          bidirectionalProperty = 
63                  (BidirectionalProperty)mockBidirectionalProperty.proxy();
64          unidirectional = (Collection)mockCollection.proxy();
65          relationshipUpdater = 
66                  (RelationshipUpdater)mockRelationshipUpdater.proxy();
67          addValue = new Object();
68          testCommand = new BidirectionalCollectionAdd(
69                  bidirectionalProperty, unidirectional, addValue);
70          owner = new Object();
71      }
72      
73      public void testExecute() {
74          Object oldOwner = new Object();
75          mockCollection.expects(once()).method("add").
76                  with(same(addValue)).will(returnValue(true));
77          mockBidirectionalProperty.expects(once()).method(
78                  "getRelationshipUpdater").withNoArguments().will(
79                  returnValue(relationshipUpdater));
80          mockBidirectionalProperty.expects(once()).method("getOwner").
81                  withNoArguments().will(returnValue(owner));
82          mockBidirectionalProperty.expects(once()).method("getOppositeName").
83                  withNoArguments().will(returnValue(oppositeName));
84          mockRelationshipUpdater.expects(once()).method("set").with(
85                  same(addValue), same(oppositeName), same(owner)).
86                  will(returnValue(oldOwner));
87          testCommand.execute();
88      }
89      
90      public void testExecuteExisting() {
91          Object oldOwner = new Object();
92          mockCollection.expects(once()).method("add").
93                  with(same(addValue)).will(returnValue(false));
94          testCommand.execute();
95      }
96      
97      public void testExecutethrowsException() {
98          mockCollection.expects(once()).method("add").
99                  with(same(addValue)).will(returnValue(true));
100         mockBidirectionalProperty.expects(once()).method(
101                 "getRelationshipUpdater").withNoArguments().will(
102                 returnValue(relationshipUpdater));
103         mockBidirectionalProperty.expects(once()).method("getOwner").
104                 withNoArguments().will(returnValue(owner));
105         mockBidirectionalProperty.expects(once()).method("getOppositeName").
106                 withNoArguments().will(returnValue(oppositeName));
107         mockRelationshipUpdater.expects(once()).method("set").with(
108                 same(addValue), same(oppositeName), same(owner)).
109                 will(throwException(new RuntimeException()));
110         mockCollection.expects(once()).method("remove").
111                 with(same(addValue)).will(returnValue(true));
112         try {
113             testCommand.execute();
114             fail("RuntimeException should be thrown");
115         }catch(RuntimeException e) {
116         }
117     }
118     
119     public void testUndo() {
120         Object oldOwner = new Object();
121         mockCollection.expects(once()).method("add").
122                 with(same(addValue)).will(returnValue(true));
123         mockBidirectionalProperty.expects(atLeastOnce()).method(
124                 "getRelationshipUpdater").withNoArguments().will(
125                 returnValue(relationshipUpdater));
126         mockBidirectionalProperty.expects(atLeastOnce()).method("getOwner").
127                 withNoArguments().will(returnValue(owner));
128         mockBidirectionalProperty.expects(atLeastOnce()).method("getOppositeName").
129                 withNoArguments().will(returnValue(oppositeName));
130         mockRelationshipUpdater.expects(once()).method("set").with(
131                 same(addValue), same(oppositeName), same(owner)).
132                 will(returnValue(oldOwner));
133         testCommand.execute();
134         
135         mockCollection.expects(once()).method("remove").
136                 with(same(addValue)).will(returnValue(true));
137         mockRelationshipUpdater.expects(atLeastOnce()).method("unset").with(
138                 same(addValue), same(oppositeName), same(owner)).
139                 will(returnValue(null));
140         mockRelationshipUpdater.expects(once()).method("set").with(
141                 same(addValue), same(oppositeName), same(oldOwner));
142         testCommand.undo();
143     }
144 }