KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.test.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-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
24 import java.lang.reflect.Constructor JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.apache.avalon.framework.configuration.Configuration;
31 import org.apache.avalon.framework.logger.Logger;
32 import org.jacorb.notification.MessageFactory;
33 import org.jacorb.notification.container.PicoContainerFactory;
34 import org.jacorb.notification.engine.TaskProcessor;
35 import org.jacorb.notification.filter.ETCLEvaluator;
36 import org.jacorb.notification.queue.EventQueueFactory;
37 import org.jacorb.test.common.TestUtils;
38 import org.omg.CORBA.Any JavaDoc;
39 import org.omg.CORBA.IntHolder JavaDoc;
40 import org.omg.CORBA.ORB JavaDoc;
41 import org.omg.CORBA.Repository JavaDoc;
42 import org.omg.CORBA.RepositoryHelper;
43 import org.omg.CosNotification.Property;
44 import org.omg.CosNotification.PropertySeqHelper;
45 import org.omg.CosNotifyChannelAdmin.EventChannel;
46 import org.omg.CosNotifyChannelAdmin.EventChannelFactory;
47 import org.omg.CosNotifyChannelAdmin.EventChannelFactoryHelper;
48 import org.omg.CosNotifyFilter.Filter;
49 import org.omg.DynamicAny.DynAnyFactory JavaDoc;
50 import org.omg.PortableServer.POA JavaDoc;
51 import org.picocontainer.MutablePicoContainer;
52
53 /**
54  * @author Alphonse Bendt
55  */

56
57 public abstract class NotificationTestCase extends TestCase
58 {
59     private NotificationTestCaseSetup setup_;
60
61     private EventChannel defaultChannel_;
62
63     protected Logger logger_;
64     
65     private MutablePicoContainer container_;
66     
67     ///////////////////////////////
68

69     public NotificationTestCase(String JavaDoc name, NotificationTestCaseSetup setup)
70     {
71         super(name);
72
73         setup_ = setup;
74     }
75
76     ////////////////////////////////////////
77

78     public final void setUp() throws Exception JavaDoc
79     {
80         container_ = PicoContainerFactory.createChildContainer(setup_.getPicoContainer());
81         
82         logger_ = ((org.jacorb.config.Configuration) getConfiguration()).getNamedLogger(getClass()
83                 .getName()
84                 + "." + getName());
85
86         setUpTest();
87     }
88
89     protected void setUpTest() throws Exception JavaDoc
90     {
91         // empty on purpose.
92
}
93
94     public final void tearDown() throws Exception JavaDoc
95     {
96         setup_.getPicoContainer().removeChildContainer(container_);
97         
98         tearDownTest();
99
100         if (defaultChannel_ != null)
101         {
102             defaultChannel_.destroy();
103         }
104         
105         super.tearDown();
106     }
107     
108     protected void tearDownTest() throws Exception JavaDoc
109     {
110         // empty to be overridden.
111
}
112
113     public MutablePicoContainer getPicoContainer()
114     {
115         return container_;
116     }
117
118     public Filter createFilter() throws Exception JavaDoc
119     {
120         return getDefaultChannel().default_filter_factory().create_filter("EXTENDED_TCL");
121     }
122
123     public ORB JavaDoc getClientORB()
124     {
125         return getSetup().getClientORB();
126     }
127
128     public EventChannel getDefaultChannel()
129     {
130         try
131         {
132             if (defaultChannel_ == null)
133             {
134                 defaultChannel_ = getFactory().create_channel(new Property[0], new Property[0],
135                         new IntHolder JavaDoc());
136
137                 assertTrue(defaultChannel_.MyFactory()._is_equivalent(getFactory()));
138             }
139
140             return defaultChannel_;
141         } catch (Exception JavaDoc e)
142         {
143             e.printStackTrace();
144             throw new RuntimeException JavaDoc();
145         }
146     }
147     
148     public EventChannel resolveChannel() throws Exception JavaDoc
149     {
150         org.omg.CORBA.Object JavaDoc object = getORB().resolve_initial_references("NotificationService");
151         EventChannelFactory factory = EventChannelFactoryHelper.narrow(object);
152         
153         return factory.create_channel(new Property[0], new Property[0], new IntHolder JavaDoc());
154     }
155
156     public ORB JavaDoc getORB()
157     {
158         return setup_.getORB();
159     }
160
161     public POA JavaDoc getPOA()
162     {
163         return setup_.getPOA();
164     }
165
166     public DynAnyFactory JavaDoc getDynAnyFactory() throws Exception JavaDoc
167     {
168         return (DynAnyFactory JavaDoc) getPicoContainer().getComponentInstance(DynAnyFactory JavaDoc.class);
169     }
170
171     public Configuration getConfiguration()
172     {
173         return (Configuration) getPicoContainer().getComponentInstance(Configuration.class);
174     }
175
176     public MessageFactory getMessageFactory()
177     {
178         return (MessageFactory) getPicoContainer().getComponentInstance(MessageFactory.class);
179     }
180
181     public ETCLEvaluator getEvaluator()
182     {
183         return (ETCLEvaluator) getPicoContainer().getComponentInstance(ETCLEvaluator.class);
184     }
185
186     public TaskProcessor getTaskProcessor()
187     {
188         return (TaskProcessor) getPicoContainer().getComponentInstance(TaskProcessor.class);
189     }
190
191     public EventQueueFactory getEventQueueFactory()
192     {
193         return (EventQueueFactory) getPicoContainer().getComponentInstance(EventQueueFactory.class);
194     }
195
196     public NotificationTestUtils getTestUtils()
197     {
198         return setup_.getTestUtils();
199     }
200
201     public EventChannelFactory getFactory()
202     {
203         try
204         {
205             return EventChannelFactoryHelper.narrow(setup_.getFactoryServant().activate());
206         } catch (Exception JavaDoc e)
207         {
208             e.printStackTrace();
209             
210             throw new RuntimeException JavaDoc(e.getMessage());
211         }
212     }
213
214     private NotificationTestCaseSetup getSetup()
215     {
216         return setup_;
217     }
218
219     public static Test suite(Class JavaDoc clazz) throws Exception JavaDoc
220     {
221         return suite(clazz, "test");
222     }
223
224     public static Test suite(Class JavaDoc clazz, String JavaDoc testPrefix) throws Exception JavaDoc
225     {
226         return suite("TestSuite defined in Class: " + clazz.getName(), clazz, testPrefix);
227     }
228     
229     public static Test suite(String JavaDoc suiteName, Class JavaDoc clazz) throws Exception JavaDoc
230     {
231         return suite(suiteName, clazz, "test");
232     }
233
234     private static Test suite(String JavaDoc suiteName, Class JavaDoc clazz, String JavaDoc testMethodPrefix)
235             throws Exception JavaDoc
236     {
237         TestSuite _suite = new TestSuite(suiteName);
238
239         NotificationTestCaseSetup _setup = new NotificationTestCaseSetup(_suite);
240
241         String JavaDoc[] _methodNames = TestUtils.getTestMethods(clazz, testMethodPrefix);
242
243         addToSuite(_suite, _setup, clazz, _methodNames);
244
245         return _setup;
246     }
247
248     private static void addToSuite(TestSuite suite, NotificationTestCaseSetup setup, Class JavaDoc clazz,
249             String JavaDoc[] testMethods) throws Exception JavaDoc
250     {
251         Constructor JavaDoc _ctor = clazz.getConstructor(new Class JavaDoc[] { String JavaDoc.class,
252                 NotificationTestCaseSetup.class });
253
254         for (int x = 0; x < testMethods.length; ++x)
255         {
256             suite.addTest((Test) _ctor.newInstance(new Object JavaDoc[] { testMethods[x], setup }));
257         }
258     }
259
260     public Any JavaDoc toAny(String JavaDoc s)
261     {
262         Any JavaDoc a = getORB().create_any();
263
264         a.insert_string(s);
265
266         return a;
267     }
268
269     public Any JavaDoc toAny(int i)
270     {
271         Any JavaDoc a = getORB().create_any();
272
273         a.insert_long(i);
274
275         return a;
276     }
277
278     public Any JavaDoc toAny(Property[] props) throws Exception JavaDoc
279     {
280         Any JavaDoc _any = getORB().create_any();
281
282         PropertySeqHelper.insert(_any, props);
283
284         return _any;
285     }
286
287     public Repository JavaDoc getRepository() throws Exception JavaDoc
288     {
289         return RepositoryHelper.narrow(getORB().resolve_initial_references("InterfaceRepository"));
290     }
291 }
Popular Tags