| 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 static org.junit.Assert.*; |
| 24 |
|
| 25 |
import java.io.IOException; |
| 26 |
import java.util.ArrayList; |
| 27 |
|
| 28 |
import org.apache.qpid.proton.reactor.Reactor; |
| 29 |
import org.junit.Test; |
| 30 |
|
| 31 |
public class EventDelegationTest { |
| 32 |
|
| 33 |
private ArrayList<String> trace = new ArrayList<String>(); |
| 34 |
|
| 35 |
class ExecutionFlowTracer extends BaseHandler { |
| 36 |
protected String name; |
| 37 |
|
| 38 |
ExecutionFlowTracer(String name) { |
| 39 |
this.name = name; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void onReactorInit(Event e) { |
| 44 |
trace.add(name); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
class DelegatingFlowTracer extends ExecutionFlowTracer { |
| 49 |
public DelegatingFlowTracer(String name) { |
| 50 |
super(name); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void onReactorInit(Event e) { |
| 55 |
trace.add("(" + name); |
| 56 |
e.delegate(); |
| 57 |
trace.add(name + ")"); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
Handler assemble(Handler outer, Handler...inner) { |
| 62 |
for(Handler h : inner) { |
| 63 |
outer.add(h); |
| 64 |
} |
| 65 |
return outer; |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void testImplicitDelegate() throws IOException { |
| 70 |
Handler h = |
| 71 |
assemble( |
| 72 |
new ExecutionFlowTracer("A"), |
| 73 |
assemble( |
| 74 |
new ExecutionFlowTracer("A.A"), |
| 75 |
new ExecutionFlowTracer("A.A.A"), |
| 76 |
new ExecutionFlowTracer("A.A.B") |
| 77 |
), |
| 78 |
assemble( |
| 79 |
new ExecutionFlowTracer("A.B") |
| 80 |
) |
| 81 |
); |
| 82 |
Reactor r = Reactor.Factory.create(); |
| 83 |
r.getHandler().add(h); |
| 84 |
r.run(); |
| 85 |
assertArrayEquals(new String[]{"A", "A.A", "A.A.A", "A.A.B", "A.B"}, trace.toArray()); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void testExplicitDelegate() throws IOException { |
| 90 |
Handler h = |
| 91 |
assemble( |
| 92 |
new ExecutionFlowTracer("A"), |
| 93 |
assemble( |
| 94 |
new DelegatingFlowTracer("A.A"), |
| 95 |
new ExecutionFlowTracer("A.A.A"), |
| 96 |
new ExecutionFlowTracer("A.A.B") |
| 97 |
), |
| 98 |
assemble( |
| 99 |
new ExecutionFlowTracer("A.B") |
| 100 |
) |
| 101 |
); |
| 102 |
Reactor r = Reactor.Factory.create(); |
| 103 |
r.getHandler().add(h); |
| 104 |
r.run(); |
| 105 |
assertArrayEquals(new String[]{"A", "(A.A", "A.A.A", "A.A.B", "A.A)", "A.B"}, trace.toArray()); |
| 106 |
} |
| 107 |
|
| 108 |
} |