| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.apache.qpid.proton.engine; |
| 22 |
|
| 23 |
import java.io.IOException; |
| 24 |
|
| 25 |
import junit.framework.TestCase; |
| 26 |
|
| 27 |
import org.apache.qpid.proton.engine.Event.Type; |
| 28 |
import org.apache.qpid.proton.engine.EventExtensibilityTest.ExtraEvent.ExtraTypes; |
| 29 |
import org.apache.qpid.proton.reactor.Reactor; |
| 30 |
import org.apache.qpid.proton.reactor.Selectable; |
| 31 |
import org.apache.qpid.proton.reactor.Task; |
| 32 |
import org.junit.Test; |
| 33 |
|
| 34 |
public class EventExtensibilityTest extends TestCase { |
| 35 |
|
| 36 |
// ////////////// |
| 37 |
// / Extra package public API classes |
| 38 |
// ////////////// |
| 39 |
|
| 40 |
/** |
| 41 |
* Sample additional information that gets generated and passed around with |
| 42 |
* the extension events. The information is stored inside |
| 43 |
* {@link Event#attachments()} see {@link ExtraEventImpl#getExtraInfo()} |
| 44 |
*/ |
| 45 |
public static class ExtraInfo { |
| 46 |
public int foo; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Additional events generated by this extension. |
| 51 |
* |
| 52 |
*/ |
| 53 |
public interface ExtraEvent extends Event { |
| 54 |
/** |
| 55 |
* Enum is a convenient mechanism for defining new event types |
| 56 |
*/ |
| 57 |
public enum ExtraTypes implements EventType { |
| 58 |
FOO, |
| 59 |
BAR, |
| 60 |
NOT_A_EXTRA_EVENT { |
| 61 |
@Override public boolean isValid() { return false; } |
| 62 |
}; |
| 63 |
|
| 64 |
@Override |
| 65 |
public boolean isValid() { |
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* typesafe access to event type generated by this extension useful for |
| 72 |
* handling in switch statements |
| 73 |
* |
| 74 |
* @return one of enum values. When invoked on an Event that is not an |
| 75 |
* ExtraEvent the return value shall be |
| 76 |
*/ |
| 77 |
ExtraTypes getExtraEventType(); |
| 78 |
|
| 79 |
/** |
| 80 |
* typesafe access to extra information attached to additional events |
| 81 |
* |
| 82 |
* @return ExtraInfo stored in the event attachment |
| 83 |
*/ |
| 84 |
ExtraInfo getExtraInfo(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* New handler methods for handling the extended event types. These methods |
| 89 |
* can take {@link ExtraEvent} as a parameter to get typesafe access to |
| 90 |
* {@link ExtraInfo} |
| 91 |
* |
| 92 |
* The interface needs to extend the {@link Handler} interface. |
| 93 |
*/ |
| 94 |
public interface ExtraHandler extends Handler { |
| 95 |
void onFoo(ExtraEvent e); |
| 96 |
|
| 97 |
void onBar(ExtraEvent e); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Implementation of the default base class for ExtraHandler. All events |
| 102 |
* should be forwarded to the {@link Handler#onUnhandled(Event)} method |
| 103 |
*/ |
| 104 |
public static class ExtraBaseHandler extends BaseHandler implements ExtraHandler { |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onFoo(ExtraEvent e) { |
| 108 |
this.onUnhandled(e); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void onBar(ExtraEvent e) { |
| 113 |
this.onUnhandled(e); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void handle(Event e) { |
| 118 |
EventType type = e.getEventType(); |
| 119 |
if (type instanceof ExtraEvent.ExtraTypes) { |
| 120 |
final ExtraEvent event; |
| 121 |
if (e instanceof ExtraEvent) { |
| 122 |
event = (ExtraEvent)e; |
| 123 |
} else { |
| 124 |
event = new ExtraEventImpl(e); |
| 125 |
} |
| 126 |
switch((ExtraEvent.ExtraTypes)type) { |
| 127 |
case BAR: |
| 128 |
onBar(event); |
| 129 |
break; |
| 130 |
case FOO: |
| 131 |
onFoo(event); |
| 132 |
break; |
| 133 |
case NOT_A_EXTRA_EVENT: |
| 134 |
super.handle(e); |
| 135 |
break; |
| 136 |
} |
| 137 |
} else { |
| 138 |
super.handle(e); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
// ////////////// |
| 145 |
// / Extra package implementation classes |
| 146 |
// ////////////// |
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* Typesafe access to ExtraInfo attached to event |
| 151 |
* |
| 152 |
*/ |
| 153 |
public static class ExtraEventImpl implements ExtraEvent { |
| 154 |
/** |
| 155 |
* making this accessor public allows for easy binding to the scripting language |
| 156 |
*/ |
| 157 |
public static final ExtendableAccessor<Event, ExtraInfo> extraInfoAccessor = new ExtendableAccessor<>(ExtraInfo.class); |
| 158 |
|
| 159 |
private Event impl; |
| 160 |
|
| 161 |
public ExtraEventImpl(Event impl) { |
| 162 |
this.impl = impl; |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public ExtraTypes getExtraEventType() { |
| 167 |
EventType type = impl.getEventType(); |
| 168 |
if (type instanceof ExtraTypes) |
| 169 |
return (ExtraTypes) type; |
| 170 |
else |
| 171 |
return ExtraTypes.NOT_A_EXTRA_EVENT; |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public ExtraInfo getExtraInfo() { |
| 176 |
return extraInfoAccessor.get(impl); |
| 177 |
} |
| 178 |
|
| 179 |
// ---- delegate methods for the Event |
| 180 |
|
| 181 |
@Override |
| 182 |
public Record attachments() { |
| 183 |
return impl.attachments(); |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public EventType getEventType() { |
| 188 |
return impl.getEventType(); |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public Type getType() { |
| 193 |
return impl.getType(); |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public Object getContext() { |
| 198 |
return impl.getContext(); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public Handler getRootHandler() { |
| 203 |
return impl.getRootHandler(); |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public void dispatch(Handler handler) throws HandlerException { |
| 208 |
impl.dispatch(handler); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public void redispatch(EventType as_type, Handler handler) throws HandlerException { |
| 213 |
impl.redispatch(as_type, handler); |
| 214 |
} |
| 215 |
@Override |
| 216 |
public Connection getConnection() { |
| 217 |
return impl.getConnection(); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public Session getSession() { |
| 222 |
return impl.getSession(); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public Link getLink() { |
| 227 |
return impl.getLink(); |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public Sender getSender() { |
| 232 |
return impl.getSender(); |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public Receiver getReceiver() { |
| 237 |
return impl.getReceiver(); |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
public Delivery getDelivery() { |
| 242 |
return impl.getDelivery(); |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public Transport getTransport() { |
| 247 |
return impl.getTransport(); |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public Reactor getReactor() { |
| 252 |
return impl.getReactor(); |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public Selectable getSelectable() { |
| 257 |
return impl.getSelectable(); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public Task getTask() { |
| 262 |
return impl.getTask(); |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
public Event copy() { |
| 267 |
return new ExtraEventImpl(impl.copy()); |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
public void delegate() throws HandlerException { |
| 272 |
impl.delegate(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
public class ExtendedTestEventGenerator extends BaseHandler { |
| 277 |
@Override |
| 278 |
public void onReactorInit(Event e) { |
| 279 |
ExtraInfo extra = new ExtraInfo(); |
| 280 |
extra.foo = 1234; |
| 281 |
ExtraEventImpl.extraInfoAccessor.set(e, extra); |
| 282 |
e.redispatch(ExtraEvent.ExtraTypes.FOO, this); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
public class ExtendedTestHandler extends ExtraBaseHandler { |
| 287 |
public boolean didFoo = false; |
| 288 |
@Override |
| 289 |
public void onFoo(ExtraEvent e) { |
| 290 |
assertEquals(ExtraEvent.ExtraTypes.FOO, e.getEventType()); |
| 291 |
assertEquals(ExtraEvent.ExtraTypes.FOO, e.getExtraEventType()); |
| 292 |
assertEquals(Event.Type.NON_CORE_EVENT, e.getType()); |
| 293 |
assertNotNull(e.getExtraInfo()); |
| 294 |
assertEquals(1234, e.getExtraInfo().foo); |
| 295 |
didFoo = true; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
public class FooUnawareTestHandler extends BaseHandler { |
| 300 |
public boolean seenFoo = false; |
| 301 |
@Override |
| 302 |
public void onUnhandled(Event e) { |
| 303 |
if (e.getEventType() == ExtraTypes.FOO) { |
| 304 |
seenFoo = true; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
@Test |
| 310 |
public void testExtendedType() throws IOException { |
| 311 |
Reactor r = Reactor.Factory.create(); |
| 312 |
ExtendedTestEventGenerator gen = new ExtendedTestEventGenerator(); |
| 313 |
BaseHandler empty = new BaseHandler(); |
| 314 |
ExtendedTestHandler extra = new ExtendedTestHandler(); |
| 315 |
FooUnawareTestHandler unaware = new FooUnawareTestHandler(); |
| 316 |
gen.add(empty); |
| 317 |
empty.add(extra); |
| 318 |
extra.add(unaware); |
| 319 |
r.setGlobalHandler(gen); |
| 320 |
r.run(); |
| 321 |
assertTrue(extra.didFoo); |
| 322 |
assertTrue(unaware.seenFoo); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void test() { |
| 327 |
Event.Type t = Type.NON_CORE_EVENT; |
| 328 |
switch (extracted(t)) { |
| 329 |
case CONNECTION_BOUND: |
| 330 |
fail(); |
| 331 |
case CONNECTION_FINAL: |
| 332 |
fail(); |
| 333 |
case CONNECTION_INIT: |
| 334 |
fail(); |
| 335 |
case CONNECTION_LOCAL_CLOSE: |
| 336 |
fail(); |
| 337 |
case CONNECTION_LOCAL_OPEN: |
| 338 |
fail(); |
| 339 |
case CONNECTION_REMOTE_CLOSE: |
| 340 |
fail(); |
| 341 |
case CONNECTION_REMOTE_OPEN: |
| 342 |
fail(); |
| 343 |
case CONNECTION_UNBOUND: |
| 344 |
fail(); |
| 345 |
case DELIVERY: |
| 346 |
fail(); |
| 347 |
case LINK_FINAL: |
| 348 |
fail(); |
| 349 |
case LINK_FLOW: |
| 350 |
fail(); |
| 351 |
case LINK_INIT: |
| 352 |
fail(); |
| 353 |
case LINK_LOCAL_CLOSE: |
| 354 |
fail(); |
| 355 |
case LINK_LOCAL_DETACH: |
| 356 |
fail(); |
| 357 |
case LINK_LOCAL_OPEN: |
| 358 |
fail(); |
| 359 |
case LINK_REMOTE_CLOSE: |
| 360 |
fail(); |
| 361 |
case LINK_REMOTE_DETACH: |
| 362 |
fail(); |
| 363 |
case LINK_REMOTE_OPEN: |
| 364 |
fail(); |
| 365 |
case REACTOR_FINAL: |
| 366 |
fail(); |
| 367 |
case REACTOR_INIT: |
| 368 |
fail(); |
| 369 |
case REACTOR_QUIESCED: |
| 370 |
fail(); |
| 371 |
case SELECTABLE_ERROR: |
| 372 |
fail(); |
| 373 |
case SELECTABLE_EXPIRED: |
| 374 |
fail(); |
| 375 |
case SELECTABLE_FINAL: |
| 376 |
fail(); |
| 377 |
case SELECTABLE_INIT: |
| 378 |
fail(); |
| 379 |
case SELECTABLE_READABLE: |
| 380 |
fail(); |
| 381 |
case SELECTABLE_UPDATED: |
| 382 |
fail(); |
| 383 |
case SELECTABLE_WRITABLE: |
| 384 |
fail(); |
| 385 |
case SESSION_FINAL: |
| 386 |
fail(); |
| 387 |
case SESSION_INIT: |
| 388 |
fail(); |
| 389 |
case SESSION_LOCAL_CLOSE: |
| 390 |
fail(); |
| 391 |
case SESSION_LOCAL_OPEN: |
| 392 |
fail(); |
| 393 |
case SESSION_REMOTE_CLOSE: |
| 394 |
fail(); |
| 395 |
case SESSION_REMOTE_OPEN: |
| 396 |
fail(); |
| 397 |
case TIMER_TASK: |
| 398 |
fail(); |
| 399 |
case TRANSPORT: |
| 400 |
fail(); |
| 401 |
case TRANSPORT_CLOSED: |
| 402 |
fail(); |
| 403 |
case TRANSPORT_ERROR: |
| 404 |
fail(); |
| 405 |
case TRANSPORT_HEAD_CLOSED: |
| 406 |
fail(); |
| 407 |
case TRANSPORT_TAIL_CLOSED: |
| 408 |
fail(); |
| 409 |
case NON_CORE_EVENT: |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
private Type extracted(Event.Type t) { |
| 415 |
return t; |
| 416 |
} |
| 417 |
|
| 418 |
} |