1
|
|
/*
|
2
|
|
* Angkor Web Framework
|
3
|
|
*
|
4
|
|
* Distributable under LGPL license.
|
5
|
|
* See terms of license at gnu.org.
|
6
|
|
*/
|
7
|
|
|
8
|
|
package com.tirsen.angkor.test.unit;
|
9
|
|
|
10
|
|
import junit.framework.TestCase;
|
11
|
|
import com.tirsen.angkor.Multicaster;
|
12
|
|
|
13
|
|
/**
|
14
|
|
* <!-- $Id: MulticasterTest.java,v 1.2 2002/10/13 13:37:26 tirsen Exp $ -->
|
15
|
|
*
|
16
|
|
* @author $Author: tirsen $
|
17
|
|
* @version $Revision: 1.2 $
|
18
|
|
*/
|
19
|
|
public class MulticasterTest extends TestCase
|
20
|
|
{
|
21
|
4
|
public MulticasterTest(String s)
|
22
|
|
{
|
23
|
4
|
super(s);
|
24
|
|
}
|
25
|
|
|
26
|
|
public static interface MulticastedInterface
|
27
|
|
{
|
28
|
|
Object method() throws Exception;
|
29
|
|
}
|
30
|
|
|
31
|
|
public static class MulticastedInterfaceImpl implements MulticastedInterface
|
32
|
|
{
|
33
|
|
private boolean called;
|
34
|
|
|
35
|
6
|
public Object method()
|
36
|
|
{
|
37
|
6
|
called = true;
|
38
|
6
|
System.out.println(this + " received a method call!");
|
39
|
6
|
return new Object();
|
40
|
|
}
|
41
|
|
|
42
|
3
|
public void assertCalled()
|
43
|
|
{
|
44
|
3
|
assertTrue(called);
|
45
|
|
}
|
46
|
|
|
47
|
1
|
public void assertNotCalled()
|
48
|
|
{
|
49
|
1
|
assertFalse(called);
|
50
|
|
}
|
51
|
|
|
52
|
1
|
public void reset()
|
53
|
|
{
|
54
|
1
|
called = false;
|
55
|
|
}
|
56
|
|
}
|
57
|
|
|
58
|
1
|
public void testMulticaster() throws Exception
|
59
|
|
{
|
60
|
1
|
MulticastedInterface multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
|
61
|
1
|
assertTrue("multicaster was not empty at creation", Multicaster.isEmpty(multicast));
|
62
|
|
|
63
|
1
|
MulticastedInterfaceImpl impl1 = new MulticastedInterfaceImpl();
|
64
|
1
|
Multicaster.add(multicast, impl1);
|
65
|
1
|
assertNull("multicaster returned something from method-call", multicast.method());
|
66
|
1
|
impl1.assertCalled();
|
67
|
|
|
68
|
1
|
MulticastedInterfaceImpl impl2 = new MulticastedInterfaceImpl();
|
69
|
1
|
MulticastedInterfaceImpl impl3 = new MulticastedInterfaceImpl();
|
70
|
|
|
71
|
1
|
Multicaster.add(multicast, impl2);
|
72
|
1
|
Multicaster.add(multicast, impl3);
|
73
|
|
|
74
|
1
|
assertNull("multicaster returned something from method-call", multicast.method());
|
75
|
1
|
impl2.assertCalled();
|
76
|
1
|
impl3.assertCalled();
|
77
|
|
|
78
|
1
|
impl3.reset();
|
79
|
1
|
Multicaster.remove(multicast, impl3);
|
80
|
1
|
multicast.method();
|
81
|
1
|
impl3.assertNotCalled();
|
82
|
|
}
|
83
|
|
|
84
|
|
/**
|
85
|
|
* Tests thread-safety, one implementation modifies the multicaster while being multicasted.
|
86
|
|
*/
|
87
|
1
|
public void testThreadSafety() throws Exception
|
88
|
|
{
|
89
|
1
|
final MulticastedInterface multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
|
90
|
|
|
91
|
1
|
MulticastedInterface modifyingImpl = new MulticastedInterface()
|
92
|
|
{
|
93
|
1
|
public Object method()
|
94
|
|
{
|
95
|
1
|
return Multicaster.remove(multicast, this);
|
96
|
|
}
|
97
|
|
};
|
98
|
|
|
99
|
1
|
Multicaster.add(multicast, modifyingImpl);
|
100
|
1
|
Multicaster.add(multicast, new MulticastedInterfaceImpl());
|
101
|
|
|
102
|
1
|
multicast.method();
|
103
|
|
}
|
104
|
|
|
105
|
1
|
public void testExceptions()
|
106
|
|
{
|
107
|
1
|
MulticastedInterface errorImpl = new MulticastedInterface()
|
108
|
|
{
|
109
|
1
|
public Object method()
|
110
|
|
{
|
111
|
1
|
throw new Error();
|
112
|
|
}
|
113
|
|
};
|
114
|
|
|
115
|
1
|
MulticastedInterface multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
|
116
|
1
|
Multicaster.add(multicast, errorImpl);
|
117
|
1
|
try
|
118
|
|
{
|
119
|
1
|
multicast.method();
|
120
|
|
}
|
121
|
|
catch (Throwable e)
|
122
|
|
{
|
123
|
1
|
assertTrue(e instanceof Error);
|
124
|
|
}
|
125
|
|
|
126
|
1
|
MulticastedInterface runtimeExceptionImpl = new MulticastedInterface()
|
127
|
|
{
|
128
|
1
|
public Object method()
|
129
|
|
{
|
130
|
1
|
throw new RuntimeException();
|
131
|
|
}
|
132
|
|
};
|
133
|
|
|
134
|
1
|
multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
|
135
|
1
|
Multicaster.add(multicast, runtimeExceptionImpl);
|
136
|
1
|
try
|
137
|
|
{
|
138
|
1
|
multicast.method();
|
139
|
|
}
|
140
|
|
catch (Throwable e)
|
141
|
|
{
|
142
|
1
|
assertTrue(e instanceof RuntimeException);
|
143
|
|
}
|
144
|
|
|
145
|
1
|
MulticastedInterface exceptionImpl = new MulticastedInterface()
|
146
|
|
{
|
147
|
1
|
public Object method() throws Exception
|
148
|
|
{
|
149
|
1
|
throw new Exception();
|
150
|
|
}
|
151
|
|
};
|
152
|
|
|
153
|
1
|
multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
|
154
|
1
|
Multicaster.add(multicast, exceptionImpl);
|
155
|
1
|
try
|
156
|
|
{
|
157
|
1
|
multicast.method();
|
158
|
|
}
|
159
|
|
catch (Throwable e)
|
160
|
|
{
|
161
|
1
|
assertTrue(e instanceof Exception);
|
162
|
|
}
|
163
|
|
}
|
164
|
|
|
165
|
1
|
public void testNull()
|
166
|
|
{
|
167
|
1
|
assertTrue("isEmpty does not handle null", Multicaster.isEmpty(null));
|
168
|
|
}
|
169
|
|
}
|
170
|
|
|