KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > OfferManagerTest


1 package org.jacorb.test.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2003 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.jacorb.notification.OfferManager;
31 import org.omg.CosNotification.EventType;
32 import org.omg.CosNotifyComm.NotifyPublishOperations;
33
34 /**
35  * @author Alphonse Bendt
36  */

37
38 public class OfferManagerTest extends TestCase {
39
40     public static final EventType[] EMPTY_EVENT_TYPE_ARRAY = new EventType[0];
41
42     OfferManager offerManager_;
43     List JavaDoc added_;
44     List JavaDoc removed_;
45     NotifyPublishOperations listener_;
46
47     ////////////////////////////////////////
48

49     public OfferManagerTest (String JavaDoc name) {
50         super(name);
51     }
52
53     ////////////////////////////////////////
54

55     public void setUp() throws Exception JavaDoc {
56         offerManager_ = new OfferManager();
57
58         added_ = new ArrayList JavaDoc();
59         removed_ = new ArrayList JavaDoc();
60         listener_ = new NotifyPublishOperations() {
61                 public void offer_change(EventType[] added, EventType[] removed) {
62                     for (int x=0; x<added.length; ++x) {
63                         added_.add(added[x]);
64                     }
65
66                     for (int x=0; x<removed.length; ++x) {
67                         removed_.add(removed[x]);
68                     }
69                 }
70             };
71     }
72
73     /**
74      * @todo should this fail?
75      */

76     public void _testRemoveNonExistent() throws Exception JavaDoc {
77         offerManager_.addListener(listener_);
78
79         offerManager_.offer_change(EMPTY_EVENT_TYPE_ARRAY,
80                                    new EventType[] { new EventType("domain1", "type1") });
81
82         assertEquals(0, added_.size());
83         assertEquals(1, removed_.size());
84         assertEquals("domain1", ((EventType)removed_.get(0)).domain_name);
85         assertEquals("type1", ((EventType)removed_.get(0)).type_name);
86     }
87
88
89     public void testRemoveNotifies() throws Exception JavaDoc {
90         EventType[] _toBeAdded = new EventType[] {new EventType("domain1", "type1")};
91
92         offerManager_.offer_change(_toBeAdded, EMPTY_EVENT_TYPE_ARRAY);
93
94         offerManager_.addListener(listener_);
95
96         offerManager_.offer_change(EMPTY_EVENT_TYPE_ARRAY,
97                                    new EventType[] { new EventType("domain1", "type1") });
98
99         assertEquals(0, added_.size());
100         assertEquals(1, removed_.size());
101         assertEquals("domain1", ((EventType)removed_.get(0)).domain_name);
102         assertEquals("type1", ((EventType)removed_.get(0)).type_name);
103
104         // shouldn't cause a problem
105
offerManager_.offer_change(EMPTY_EVENT_TYPE_ARRAY,
106                                    new EventType[] { new EventType("domain1", "type1") });
107     }
108
109
110     public void testAddNotifies() throws Exception JavaDoc {
111         EventType[] _toBeAdded = new EventType[] {new EventType("domain1", "type1")};
112
113         offerManager_.offer_change(_toBeAdded, EMPTY_EVENT_TYPE_ARRAY);
114
115         offerManager_.addListener(listener_);
116
117         _toBeAdded = new EventType[] {new EventType("domain2", "type2")};
118
119         offerManager_.offer_change(_toBeAdded, EMPTY_EVENT_TYPE_ARRAY);
120
121         assertEquals(1, added_.size());
122
123         assertEquals("domain2", ((EventType)added_.get(0)).domain_name);
124         assertEquals("type2", ((EventType)added_.get(0)).type_name);
125
126         assertEquals(0, removed_.size());
127
128         // another offer with known event types should cause a notification
129
offerManager_.offer_change(_toBeAdded, EMPTY_EVENT_TYPE_ARRAY);
130
131         assertEquals(1, added_.size());
132     }
133
134
135     public static Test suite() throws Exception JavaDoc {
136         return new TestSuite(OfferManagerTest.class);
137     }
138 }
139
Popular Tags