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 org.jmock.Mock;
27  import org.jmock.MockObjectTestCase;
28  
29  import com.enspire.gemini.updaters.SimplePropertyRelationshipUpdater;
30  import com.enspire.reflection.PropertyReflection;
31  
32  /***
33   * @author Dragan Djuric <dragand@dev.java.net>
34   **/
35  public class SimplePropertyRelationshipUpdaterTest extends MockObjectTestCase {
36  
37      private SimplePropertyRelationshipUpdater testSPAUpdater;
38      
39      String propertyName = "name";
40      private Mock mockPropertyReflection;
41      
42      /***
43       * @return
44       */
45      private SimplePropertyRelationshipUpdater createTestObject() {
46          return new SimplePropertyRelationshipUpdater();
47      }
48      
49      /***
50       * @see junit.framework.TestCase#setUp()
51       */
52      protected void setUp() throws Exception {
53          super.setUp();
54          testSPAUpdater = createTestObject();
55          mockPropertyReflection = new Mock(PropertyReflection.class);
56          PropertyReflection worker = (PropertyReflection)mockPropertyReflection.proxy();
57          testSPAUpdater.setPropertyReflection(worker);
58      }
59      
60      public void testSetDifferent() {
61          Object owner = new Object();
62          Object value = new Object();
63          Object oldValue = new Object();
64          mockPropertyReflection.expects(once()).method("getSimpleProperty").
65                  with(same(owner), same(propertyName)).
66                  will(returnValue(oldValue));
67          mockPropertyReflection.expects(once()).method("setSimpleProperty").
68                  with(same(owner), same(propertyName), same(value));
69          testSPAUpdater.set(owner, propertyName, value);
70          assertTrue("PropertyReflection should be called to set the property.",
71                  true);
72      }
73      
74      public void testSetTheSame() {
75          Object owner = new Object();
76          Object value = new Object();
77          Object oldValue = value;
78          mockPropertyReflection.expects(once()).method("getSimpleProperty").
79                  with(same(owner), same(propertyName)).
80                  will(returnValue(oldValue));
81          testSPAUpdater.set(owner, propertyName, value);
82          assertTrue("PropertyReflection should NOT be called to set the property.",
83                  true);
84      }
85      
86      public void testSetOwnerIsNull() {
87          Object owner = null;
88          Object value = new Object();
89          Object oldValue = value;
90          testSPAUpdater.set(owner, propertyName, value);
91          assertTrue("PropertyReflection should NOT be called to set the property.",
92                  true);
93      }
94  
95      public void testSetValueIsNull() {
96          Object owner = new Object();
97          Object value = null;
98          Object oldValue = new Object();
99          mockPropertyReflection.expects(once()).method("getSimpleProperty").
100                 with(same(owner), same(propertyName)).
101                 will(returnValue(oldValue));
102         mockPropertyReflection.expects(once()).method("setSimpleProperty").
103                 with(same(owner), same(propertyName), same(value));
104         testSPAUpdater.set(owner, propertyName, value);
105         assertTrue("PropertyReflection should be called to set the property.",
106                 true);
107     }
108     
109     public void testUnset() {
110         Object owner = new Object();
111         Object value = new Object();
112         Object oldValue = new Object();
113         mockPropertyReflection.expects(once()).method("getSimpleProperty").
114                 with(same(owner), same(propertyName)).
115                 will(returnValue(oldValue));
116         mockPropertyReflection.expects(once()).method("setSimpleProperty").
117                 with(same(owner), same(propertyName), same(null));
118         testSPAUpdater.unset(owner, propertyName, value);
119         assertTrue("PropertyReflection should be called to set the property.",
120                 true);
121     }
122 }