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.Address;
20 import javax.mail.Folder;
21 import javax.mail.Message;
22 import javax.mail.TestData;
23 import javax.mail.Transport;
24 import javax.mail.internet.AddressException;
25 import javax.mail.internet.InternetAddress;
26 import junit.framework.TestCase;
27 /**
28 * @version $Rev: 125583 $ $Date: 2005-01-18 19:44:27 -0800 (Tue, 18 Jan 2005) $
29 */
30 public class TransportEventTest extends TestCase {
31 public TransportEventTest(String name) {
32 super(name);
33 }
34 public void testEvent() throws AddressException {
35 doEventTests(TransportEvent.MESSAGE_DELIVERED);
36 doEventTests(TransportEvent.MESSAGE_PARTIALLY_DELIVERED);
37 doEventTests(TransportEvent.MESSAGE_NOT_DELIVERED);
38 }
39 private void doEventTests(int type) throws AddressException {
40 Folder folder = TestData.getTestFolder();
41 Message message = TestData.getMessage();
42 Transport transport = TestData.getTestTransport();
43 Address[] sent = new Address[] { new InternetAddress("alex@here.com")};
44 Address[] empty = new Address[0];
45 TransportEvent event =
46 new TransportEvent(transport, type, sent, empty, empty, message);
47 assertEquals(transport, event.getSource());
48 assertEquals(type, event.getType());
49 TransportListenerTest listener = new TransportListenerTest();
50 event.dispatch(listener);
51 assertEquals("Unexpcted method dispatched", type, listener.getState());
52 }
53 public static class TransportListenerTest implements TransportListener {
54 private int state = 0;
55 public void messageDelivered(TransportEvent event) {
56 if (state != 0) {
57 fail("Recycled Listener");
58 }
59 state = TransportEvent.MESSAGE_DELIVERED;
60 }
61 public void messagePartiallyDelivered(TransportEvent event) {
62 if (state != 0) {
63 fail("Recycled Listener");
64 }
65 state = TransportEvent.MESSAGE_PARTIALLY_DELIVERED;
66 }
67 public void messageNotDelivered(TransportEvent event) {
68 if (state != 0) {
69 fail("Recycled Listener");
70 }
71 state = TransportEvent.MESSAGE_NOT_DELIVERED;
72 }
73 public int getState() {
74 return state;
75 }
76 }
77 }