1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 }