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