KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > demo > TestDemoMediator


1 package org.bsf.smartValueObject.demo;
2
3 import junit.framework.TestCase;
4 import org.bsf.smartValueObject.mediator.ConcurrencyException;
5 import org.bsf.smartValueObject.mediator.MediatorException;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 /**
12  * Testcase DemoMediator.
13  *
14  */

15 public class TestDemoMediator extends TestCase {
16     private DemoMediator mediator;
17
18     private CompanyVO company1;
19     private CompanyVO company2;
20
21     public void setUp() {
22         mediator = new DemoMediator();
23         company1 = new CompanyVO();
24         company2 = new CompanyVO();
25         company1.setName("company1");
26         company2.setName("company2");
27         company1.setCreationDate(new Date JavaDoc());
28         company2.setCreationDate(new Date JavaDoc());
29
30         SubsidiaryVO sub1 = new SubsidiaryVO();
31         sub1.setWorkforce(1000);
32         sub1.setName("sub1");
33
34         SubsidiaryVO sub2 = new SubsidiaryVO();
35         sub2.setWorkforce(2000);
36         sub2.setName("sub2");
37
38         SubsidiaryVO sub3 = new SubsidiaryVO();
39         sub3.setWorkforce(3000);
40         sub3.setName("sub3");
41
42         SubsidiaryVO sub4 = new SubsidiaryVO();
43         sub4.setWorkforce(4000);
44         sub4.setName("sub4");
45
46         company1.addSubsidiary(sub1);
47         company1.addSubsidiary(sub2);
48         company2.addSubsidiary(sub3);
49         company2.addSubsidiary(sub4);
50     }
51
52     public void testUpdateAndGetGraph() throws MediatorException {
53         CompanyVO result = updateCompany1();
54
55         Collection JavaDoc c = result.getSubsidiaries();
56         assertEquals(2, c.size());
57         for (Iterator JavaDoc iterator = c.iterator(); iterator.hasNext();) {
58             SubsidiaryVO vo = (SubsidiaryVO) iterator.next();
59             assertTrue(
60                     vo.getName().equals("sub1") ||
61                     vo.getName().equals("sub2"));
62             assertTrue(
63                     vo.getWorkforce() == 1000 ||
64                     vo.getWorkforce() == 2000);
65
66         }
67     }
68
69     public void testDeleted() throws MediatorException {
70         CompanyVO result = updateCompany1();
71
72         Collection JavaDoc c = result.getSubsidiaries();
73         assertEquals(2, c.size());
74
75         SubsidiaryVO vo = null;
76         for (Iterator JavaDoc iterator = c.iterator(); iterator.hasNext();) {
77             vo = (SubsidiaryVO) iterator.next();
78         }
79
80         c.remove(vo);
81
82         mediator.updateGraph(result);
83
84         // get new graph
85
result = (CompanyVO) mediator.getGraph(getPrototype(0));
86         c = result.getSubsidiaries();
87         assertEquals(1, c.size());
88
89         SubsidiaryVO vo2 = (SubsidiaryVO) c.iterator().next();
90         assertTrue(
91                 !vo.getId().equals(vo2.getId()) &&
92                 !vo.getName().equals(vo2.getName()));
93     }
94
95     public void testCreated() throws MediatorException {
96         CompanyVO result = updateCompany1();
97         Collection JavaDoc c = result.getSubsidiaries();
98         SubsidiaryVO sub = new SubsidiaryVO();
99         sub.setName("new subsidiary");
100         sub.setWorkforce(222);
101         assertTrue(c.add(sub));
102
103         mediator.updateGraph(result);
104
105         CompanyVO result2 = (CompanyVO) mediator.getGraph(getPrototype(0));
106         assertTrue(result != result2);
107
108         c = result2.getSubsidiaries();
109         assertEquals(3, c.size());
110         boolean verified = false;
111         for (Iterator JavaDoc iterator = c.iterator(); iterator.hasNext();) {
112             SubsidiaryVO vo = (SubsidiaryVO) iterator.next();
113             if (vo.getId().equals(new Long JavaDoc(2))) {
114                 assertEquals("new subsidiary", vo.getName());
115                 assertEquals(222, vo.getWorkforce());
116                 verified = true;
117             }
118         }
119         assertTrue(verified);
120     }
121
122     public void testUpdate() throws MediatorException {
123         CompanyVO company = updateCompany1();
124         company.setName("new name");
125         mediator.updateGraph(company);
126
127         CompanyVO result = (CompanyVO) mediator.getGraph(getPrototype(0));
128         assertEquals("new name", result.getName());
129     }
130
131
132     public void testModified() throws MediatorException {
133         CompanyVO result = updateCompany1();
134
135         Collection JavaDoc c = result.getSubsidiaries();
136         assertEquals(2, c.size());
137
138         SubsidiaryVO vo = null;
139         for (Iterator JavaDoc iterator = c.iterator(); iterator.hasNext();) {
140             vo = (SubsidiaryVO) iterator.next();
141         }
142
143         vo.setName("modifiedSub");
144         Long JavaDoc voKey = vo.getId();
145
146         mediator.updateGraph(result);
147
148         result = (CompanyVO) mediator.getGraph(getPrototype(0));
149         boolean verified = false;
150         for (Iterator JavaDoc iterator = result.subsidiaries(); iterator.hasNext();) {
151             vo = (SubsidiaryVO) iterator.next();
152             if (vo.getId().equals(voKey)) {
153                 assertEquals("modifiedSub", vo.getName());
154                 verified = true;
155             }
156         }
157         assertTrue(verified);
158     }
159
160     public void testModified2() throws MediatorException {
161         CompanyVO result = updateCompany1();
162         SubsidiaryVO vo = null;
163         for (Iterator JavaDoc iterator = result.subsidiaries(); iterator.hasNext();) {
164             vo = (SubsidiaryVO) iterator.next();
165             result.removeSubsidiary(vo);
166         }
167
168         System.out.println(mediator.updateGraph(result).getReport());
169     }
170
171     public void testDeleteGraph() throws MediatorException {
172         CompanyVO result = updateCompany1();
173         mediator.deleteGraph(result);
174
175         try {
176             result = (CompanyVO) mediator.getGraph(getPrototype(0));
177         } catch (MediatorException e) {
178             // expected
179
return;
180         }
181         fail("Got no exception while trying to retrieve deleted graph");
182     }
183
184     public void testTwoCompanies() throws MediatorException {
185         updateCompany1();
186         updateCompany2();
187     }
188
189     public void testConcurrency() throws MediatorException {
190         CompanyVO result = updateCompany1();
191
192         result.setName("new");
193         mediator.updateGraph(result);
194         result.setName("new2");
195         try {
196             mediator.updateGraph(result);
197         } catch (ConcurrencyException e) {
198             // expected
199
return;
200         }
201         fail("Got no concurrency exception while trying to update graph");
202     }
203
204     //- private helper methods.
205
private CompanyVO updateCompany1() throws MediatorException {
206         mediator.updateGraph(company1);
207         CompanyVO companyVO = getPrototype(0);
208         CompanyVO result = (CompanyVO) mediator.getGraph(companyVO);
209         assertEquals(company1.getName(), result.getName());
210
211         return result;
212     }
213
214     private CompanyVO updateCompany2() throws MediatorException {
215         mediator.updateGraph(company2);
216         CompanyVO companyVO = getPrototype(1);
217         CompanyVO result = (CompanyVO) mediator.getGraph(companyVO);
218         assertEquals(company2.getName(), result.getName());
219
220         return result;
221     }
222
223     private CompanyVO getPrototype(long pk) {
224         CompanyVO companyVO = new CompanyVO();
225         companyVO.setId(new Long JavaDoc(pk));
226         return companyVO;
227     }
228 }
229
Popular Tags