KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > samples > ExampleTest


1 /*
2  * Copyright (c) 2001-2005 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.samples;
6
7 import junit.framework.TestCase;
8
9 import org.easymock.MockControl;
10
11 public class ExampleTest extends TestCase {
12
13     private ClassUnderTest classUnderTest;
14
15     private MockControl control;
16
17     private Collaborator mock;
18
19     protected void setUp() {
20         control = MockControl.createControl(Collaborator.class);
21         mock = (Collaborator) control.getMock();
22         classUnderTest = new ClassUnderTest();
23         classUnderTest.addListener(mock);
24     }
25
26     public void testRemoveNonExistingDocument() {
27         control.replay();
28         classUnderTest.removeDocument("Does not exist");
29     }
30
31     public void testAddDocument() {
32         mock.documentAdded("New Document");
33         control.replay();
34         classUnderTest.addDocument("New Document", new byte[0]);
35         control.verify();
36     }
37
38     public void testAddAndChangeDocument() {
39         mock.documentAdded("Document");
40         mock.documentChanged("Document");
41         control.setVoidCallable(3);
42         control.replay();
43         classUnderTest.addDocument("Document", new byte[0]);
44         classUnderTest.addDocument("Document", new byte[0]);
45         classUnderTest.addDocument("Document", new byte[0]);
46         classUnderTest.addDocument("Document", new byte[0]);
47         control.verify();
48     }
49
50     public void testVoteForRemoval() {
51         // expect document addition
52
mock.documentAdded("Document");
53         // expect to be asked to vote, and vote for it
54
control.expectAndReturn(mock.voteForRemoval("Document"), 42);
55         // expect document removal
56
mock.documentRemoved("Document");
57
58         control.replay();
59         classUnderTest.addDocument("Document", new byte[0]);
60         assertTrue(classUnderTest.removeDocument("Document"));
61         control.verify();
62     }
63
64     public void testVoteAgainstRemoval() {
65         // expect document addition
66
mock.documentAdded("Document");
67         // expect to be asked to vote, and vote against it
68
control.expectAndReturn(mock.voteForRemoval("Document"), -42); //
69
// document removal is *not* expected
70

71         control.replay();
72         classUnderTest.addDocument("Document", new byte[0]);
73         assertFalse(classUnderTest.removeDocument("Document"));
74         control.verify();
75     }
76
77     public void testVoteForRemovals() {
78         mock.documentAdded("Document 1");
79         mock.documentAdded("Document 2");
80         mock.voteForRemovals(new String JavaDoc[] { "Document 1", "Document 2" });
81         control.setMatcher(MockControl.ARRAY_MATCHER);
82         control.setReturnValue(42);
83         mock.documentRemoved("Document 1");
84         mock.documentRemoved("Document 2");
85         control.replay();
86         classUnderTest.addDocument("Document 1", new byte[0]);
87         classUnderTest.addDocument("Document 2", new byte[0]);
88         assertTrue(classUnderTest.removeDocuments(new String JavaDoc[] { "Document 1",
89                 "Document 2" }));
90         control.verify();
91     }
92
93     public void testVoteAgainstRemovals() {
94         mock.documentAdded("Document 1");
95         mock.documentAdded("Document 2");
96         mock.voteForRemovals(new String JavaDoc[] { "Document 1", "Document 2" });
97         control.setMatcher(MockControl.ARRAY_MATCHER);
98         control.setReturnValue(-42);
99         control.replay();
100         classUnderTest.addDocument("Document 1", new byte[0]);
101         classUnderTest.addDocument("Document 2", new byte[0]);
102         assertFalse(classUnderTest.removeDocuments(new String JavaDoc[] { "Document 1",
103                 "Document 2" }));
104         control.verify();
105     }
106 }
107
Popular Tags