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 public MulticasterTest(String s)
22 {
23 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 public Object method()
36 {
37 called = true;
38 System.out.println(this + " received a method call!");
39 return new Object();
40 }
41
42 public void assertCalled()
43 {
44 assertTrue(called);
45 }
46
47 public void assertNotCalled()
48 {
49 assertFalse(called);
50 }
51
52 public void reset()
53 {
54 called = false;
55 }
56 }
57
58 public void testMulticaster() throws Exception
59 {
60 MulticastedInterface multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
61 assertTrue("multicaster was not empty at creation", Multicaster.isEmpty(multicast));
62
63 MulticastedInterfaceImpl impl1 = new MulticastedInterfaceImpl();
64 Multicaster.add(multicast, impl1);
65 assertNull("multicaster returned something from method-call", multicast.method());
66 impl1.assertCalled();
67
68 MulticastedInterfaceImpl impl2 = new MulticastedInterfaceImpl();
69 MulticastedInterfaceImpl impl3 = new MulticastedInterfaceImpl();
70
71 Multicaster.add(multicast, impl2);
72 Multicaster.add(multicast, impl3);
73
74 assertNull("multicaster returned something from method-call", multicast.method());
75 impl2.assertCalled();
76 impl3.assertCalled();
77
78 impl3.reset();
79 Multicaster.remove(multicast, impl3);
80 multicast.method();
81 impl3.assertNotCalled();
82 }
83
84 /***
85 * Tests thread-safety, one implementation modifies the multicaster while being multicasted.
86 */
87 public void testThreadSafety() throws Exception
88 {
89 final MulticastedInterface multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
90
91 MulticastedInterface modifyingImpl = new MulticastedInterface()
92 {
93 public Object method()
94 {
95 return Multicaster.remove(multicast, this);
96 }
97 };
98
99 Multicaster.add(multicast, modifyingImpl);
100 Multicaster.add(multicast, new MulticastedInterfaceImpl());
101
102 multicast.method();
103 }
104
105 public void testExceptions()
106 {
107 MulticastedInterface errorImpl = new MulticastedInterface()
108 {
109 public Object method()
110 {
111 throw new Error();
112 }
113 };
114
115 MulticastedInterface multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
116 Multicaster.add(multicast, errorImpl);
117 try
118 {
119 multicast.method();
120 }
121 catch (Throwable e)
122 {
123 assertTrue(e instanceof Error);
124 }
125
126 MulticastedInterface runtimeExceptionImpl = new MulticastedInterface()
127 {
128 public Object method()
129 {
130 throw new RuntimeException();
131 }
132 };
133
134 multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
135 Multicaster.add(multicast, runtimeExceptionImpl);
136 try
137 {
138 multicast.method();
139 }
140 catch (Throwable e)
141 {
142 assertTrue(e instanceof RuntimeException);
143 }
144
145 MulticastedInterface exceptionImpl = new MulticastedInterface()
146 {
147 public Object method() throws Exception
148 {
149 throw new Exception();
150 }
151 };
152
153 multicast = (MulticastedInterface) Multicaster.create(MulticastedInterface.class);
154 Multicaster.add(multicast, exceptionImpl);
155 try
156 {
157 multicast.method();
158 }
159 catch (Throwable e)
160 {
161 assertTrue(e instanceof Exception);
162 }
163 }
164
165 public void testNull()
166 {
167 assertTrue("isEmpty does not handle null", Multicaster.isEmpty(null));
168 }
169 }
This page was automatically generated by Maven