1 package org.jacorb.test.notification; 2 3 23 24 import java.lang.reflect.Constructor ; 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 ; 39 import org.omg.CORBA.IntHolder ; 40 import org.omg.CORBA.ORB ; 41 import org.omg.CORBA.Repository ; 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 ; 50 import org.omg.PortableServer.POA ; 51 import org.picocontainer.MutablePicoContainer; 52 53 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 69 public NotificationTestCase(String name, NotificationTestCaseSetup setup) 70 { 71 super(name); 72 73 setup_ = setup; 74 } 75 76 78 public final void setUp() throws Exception 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 90 { 91 } 93 94 public final void tearDown() throws Exception 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 109 { 110 } 112 113 public MutablePicoContainer getPicoContainer() 114 { 115 return container_; 116 } 117 118 public Filter createFilter() throws Exception 119 { 120 return getDefaultChannel().default_filter_factory().create_filter("EXTENDED_TCL"); 121 } 122 123 public ORB 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 ()); 136 137 assertTrue(defaultChannel_.MyFactory()._is_equivalent(getFactory())); 138 } 139 140 return defaultChannel_; 141 } catch (Exception e) 142 { 143 e.printStackTrace(); 144 throw new RuntimeException (); 145 } 146 } 147 148 public EventChannel resolveChannel() throws Exception 149 { 150 org.omg.CORBA.Object 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 ()); 154 } 155 156 public ORB getORB() 157 { 158 return setup_.getORB(); 159 } 160 161 public POA getPOA() 162 { 163 return setup_.getPOA(); 164 } 165 166 public DynAnyFactory getDynAnyFactory() throws Exception 167 { 168 return (DynAnyFactory ) getPicoContainer().getComponentInstance(DynAnyFactory .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 e) 207 { 208 e.printStackTrace(); 209 210 throw new RuntimeException (e.getMessage()); 211 } 212 } 213 214 private NotificationTestCaseSetup getSetup() 215 { 216 return setup_; 217 } 218 219 public static Test suite(Class clazz) throws Exception 220 { 221 return suite(clazz, "test"); 222 } 223 224 public static Test suite(Class clazz, String testPrefix) throws Exception 225 { 226 return suite("TestSuite defined in Class: " + clazz.getName(), clazz, testPrefix); 227 } 228 229 public static Test suite(String suiteName, Class clazz) throws Exception 230 { 231 return suite(suiteName, clazz, "test"); 232 } 233 234 private static Test suite(String suiteName, Class clazz, String testMethodPrefix) 235 throws Exception 236 { 237 TestSuite _suite = new TestSuite(suiteName); 238 239 NotificationTestCaseSetup _setup = new NotificationTestCaseSetup(_suite); 240 241 String [] _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 clazz, 249 String [] testMethods) throws Exception 250 { 251 Constructor _ctor = clazz.getConstructor(new Class [] { String .class, 252 NotificationTestCaseSetup.class }); 253 254 for (int x = 0; x < testMethods.length; ++x) 255 { 256 suite.addTest((Test) _ctor.newInstance(new Object [] { testMethods[x], setup })); 257 } 258 } 259 260 public Any toAny(String s) 261 { 262 Any a = getORB().create_any(); 263 264 a.insert_string(s); 265 266 return a; 267 } 268 269 public Any toAny(int i) 270 { 271 Any a = getORB().create_any(); 272 273 a.insert_long(i); 274 275 return a; 276 } 277 278 public Any toAny(Property[] props) throws Exception 279 { 280 Any _any = getORB().create_any(); 281 282 PropertySeqHelper.insert(_any, props); 283 284 return _any; 285 } 286 287 public Repository getRepository() throws Exception 288 { 289 return RepositoryHelper.narrow(getORB().resolve_initial_references("InterfaceRepository")); 290 } 291 } | Popular Tags |