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.updaters;
25  
26  import java.util.Collection;
27  
28  import org.jmock.Mock;
29  import org.jmock.MockObjectTestCase;
30  
31  import com.enspire.gemini.updaters.CollectionPropertyRelationshipUpdater;
32  import com.enspire.reflection.PropertyReflection;
33  
34  /***
35   * TODO To change the template for this generated type comment go to
36   * Window - Preferences - Java - Code Style - Code Templates
37   *
38   * @author Dragan Djuric <dragand@dev.java.net>
39   **/
40  public class CollectionPropertyRelationshipUpdaterTest extends MockObjectTestCase {
41  
42      private CollectionPropertyRelationshipUpdater testCPAUpdater;
43  
44      String propertyName = "name";
45      private Mock mockPropertyReflection;
46      private Mock mockCollection;
47      
48      /***
49       * Creates the object that is going to be under test.
50       * 
51       * @return the object that is going to be under test
52       */
53      private CollectionPropertyRelationshipUpdater createTestObject() {
54          return new CollectionPropertyRelationshipUpdater();
55      }
56      
57      /***
58       * @see junit.framework.TestCase#setUp()
59       */
60      protected void setUp() throws Exception {
61          super.setUp();
62          testCPAUpdater = createTestObject();
63          mockPropertyReflection = new Mock(PropertyReflection.class);
64          mockCollection = new Mock(Collection.class);
65          PropertyReflection worker = (PropertyReflection)mockPropertyReflection.proxy();
66          testCPAUpdater.setPropertyReflection(worker);
67      }
68      
69      public void testSetDoesNotAlreadyContain() {
70          Object owner = new Object();
71          Object value = new Object();
72          Collection collection = (Collection)mockCollection.proxy();
73          
74          mockPropertyReflection.expects(once()).method("getSimpleProperty").
75                  with(same(owner), same(propertyName)).
76                  will(returnValue(collection));
77          mockCollection.expects(once()).method("contains").with(same(value)).
78                  will(returnValue(false));
79          mockCollection.expects(once()).method("add").with(same(value)).
80                  will(returnValue(true));
81          testCPAUpdater.set(owner, propertyName, value);
82          assertTrue("Object should be added to the collection.", true);
83      }
84  
85      public void testSetAlreadyContains() {
86          Object owner = new Object();
87          Object value = new Object();
88          Collection collection = (Collection)mockCollection.proxy();
89          
90          mockPropertyReflection.expects(once()).method("getSimpleProperty").
91                  with(same(owner), same(propertyName)).
92                  will(returnValue(collection));
93          mockCollection.expects(once()).method("contains").with(same(value)).
94                  will(returnValue(true));
95          testCPAUpdater.set(owner, propertyName, value);
96          assertTrue("Object should NOT be added to the collection.", true);
97      }
98      
99      public void testSetOwnerIsNull() {
100         Object owner = null;
101         Object value = new Object();
102         Collection collection = (Collection)mockCollection.proxy();
103         testCPAUpdater.set(owner, propertyName, value);
104         assertTrue("PropertyReflection should NOT be called to set the property.",
105                 true);
106     }
107 
108     public void testSetValueIsNull() {
109         Object owner = new Object();
110         Object value = null;
111         Collection collection = (Collection)mockCollection.proxy();
112         testCPAUpdater.set(owner, propertyName, value);
113         assertTrue("PropertyReflection should NOT be called to set the property.",
114                 true);
115     }
116     
117     public void testSetCollectionIsNull() {
118         Object owner = new Object();
119         Object value = new Object();
120         Collection collection = null;
121         mockPropertyReflection.expects(once()).method("getSimpleProperty").
122                 with(same(owner), same(propertyName)).
123                 will(returnValue(collection));
124         testCPAUpdater.set(owner, propertyName, value);
125         assertTrue("Object should bot be added to the collection.", true);
126     }
127     
128     public void testUnset() {
129         Object owner = new Object();
130         Object value = new Object();
131         Collection collection = (Collection)mockCollection.proxy();
132         mockPropertyReflection.expects(once()).method("getSimpleProperty").
133                 with(same(owner), same(propertyName)).
134                 will(returnValue(collection));
135         mockCollection.expects(once()).method("remove").with(same(value)).
136                 will(returnValue(true));
137         testCPAUpdater.unset(owner, propertyName, value);
138         assertTrue("Object should be removed from the collection.", true);
139     }
140     
141     public void testUnsetOwnerIsNull() {
142         Object owner = null;
143         Object value = new Object();
144         Collection collection = (Collection)mockCollection.proxy();
145         testCPAUpdater.unset(owner, propertyName, value);
146         assertTrue("PropertyReflection should NOT be called to set the property.",
147                 true);
148     }
149     
150     public void testUnsetCollectionIsNull() {
151         Object owner = new Object();
152         Object value = new Object();
153         Collection collection = null;
154         mockPropertyReflection.expects(once()).method("getSimpleProperty").
155                 with(same(owner), same(propertyName)).
156                 will(returnValue(collection));
157         testCPAUpdater.unset(owner, propertyName, value);
158         assertTrue("Object should NOT be removed from the collection.", true);
159     }
160 }