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