KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > event > MessageCountEventTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package javax.mail.event;
19 import javax.mail.Folder;
20 import javax.mail.TestData;
21 import junit.framework.TestCase;
22 /**
23  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
24  */

25 public class MessageCountEventTest extends TestCase {
26     public MessageCountEventTest(String name) {
27         super(name);
28     }
29     public void testEvent() {
30         doEventTests(MessageCountEvent.ADDED);
31         doEventTests(MessageCountEvent.REMOVED);
32         try {
33             doEventTests(-12345);
34             fail("Expected exception due to invalid type -12345");
35         } catch (IllegalArgumentException e) {
36         }
37     }
38     private void doEventTests(int type) {
39         Folder folder = TestData.getTestFolder();
40         MessageCountEvent event =
41             new MessageCountEvent(folder, type, false, null);
42         assertEquals(folder, event.getSource());
43         assertEquals(type, event.getType());
44         MessageCountListenerTest listener = new MessageCountListenerTest();
45         event.dispatch(listener);
46         assertEquals("Unexpcted method dispatched", type, listener.getState());
47     }
48     public static class MessageCountListenerTest
49         implements MessageCountListener {
50         private int state = 0;
51         public void messagesAdded(MessageCountEvent event) {
52             if (state != 0) {
53                 fail("Recycled Listener");
54             }
55             state = MessageCountEvent.ADDED;
56         }
57         public void messagesRemoved(MessageCountEvent event) {
58             if (state != 0) {
59                 fail("Recycled Listener");
60             }
61             state = MessageCountEvent.REMOVED;
62         }
63         public int getState() {
64             return state;
65         }
66     }
67 }
68
Popular Tags