1   package jgroup.relacs.gmi;
2   
3   import static jgroup.relacs.gmi.MethodSemantics.ANYCAST;
4   import static jgroup.relacs.gmi.MethodSemantics.ATOMIC;
5   import static jgroup.relacs.gmi.MethodSemantics.LEADERCAST;
6   import static jgroup.relacs.gmi.MethodSemantics.MULTICAST;
7   
8   import java.lang.reflect.Method;
9   import java.rmi.Remote;
10  
11  import jgroup.core.protocols.Anycast;
12  import jgroup.core.protocols.Atomic;
13  import jgroup.core.protocols.Leadercast;
14  import jgroup.core.protocols.Multicast;
15  
16  import junit.framework.TestCase;
17  
18  public class MethodDetailsTest extends TestCase
19  {
20  
21    interface MethodTest1 extends Remote { void details(); }
22  
23    interface MethodTest2_1 extends Remote { @Anycast void details(); }
24    interface MethodTest2_2 extends Remote { @Leadercast void details(); }
25    interface MethodTest2_3 extends Remote { @Multicast void details(); }
26    interface MethodTest2_4 extends Remote { @Atomic void details(); }
27  
28    private class MethodTest1_Impl1 implements MethodTest1 { public void details() {} }
29    private class MethodTest1_Impl2 implements MethodTest1 { @Anycast public void details() {} }
30    private class MethodTest1_Impl3 implements MethodTest1 { @Leadercast public void details() {} }
31    private class MethodTest1_Impl4 implements MethodTest1 { @Multicast public void details() {} }
32    private class MethodTest1_Impl5 implements MethodTest1 { @Atomic public void details() {} }
33  
34    private class MethodTest2_Impl1 implements MethodTest2_1 { public void details() { } }
35    private class MethodTest2_Impl2 implements MethodTest2_2 { public void details() { } }
36    private class MethodTest2_Impl3 implements MethodTest2_3 { public void details() { } }
37    private class MethodTest2_Impl4 implements MethodTest2_4 { public void details() { } }
38  
39    private class MethodTest3_Impl1 implements MethodTest2_1 { @Atomic public void details() { } }
40    private class MethodTest3_Impl2 implements MethodTest2_2 { @Anycast public void details() { } }
41    private class MethodTest3_Impl3 implements MethodTest2_3 { @Leadercast public void details() { } }
42    private class MethodTest3_Impl4 implements MethodTest2_4 { @Multicast public void details() { } }
43  
44    // Test that we cannot declare multiple invocation semantics for a single method
45    private class MethodTest4_Impl1 implements MethodTest1 { @Anycast @Multicast public void details() { } }
46  
47    // Test extending classes
48    private class MethodTest5_Impl1 extends MethodTest1_Impl1 { @Anycast public void details() {} }
49    private class MethodTest5_Impl2 extends MethodTest1_Impl1 { @Leadercast public void details() {} }
50    private class MethodTest5_Impl3 extends MethodTest1_Impl1 { @Multicast public void details() {} }
51    private class MethodTest5_Impl4 extends MethodTest1_Impl1 { @Atomic public void details() {} }
52  
53    private class MethodTest6_Impl1 extends MethodTest1_Impl2 { public void details() {} }
54    private class MethodTest6_Impl2 extends MethodTest1_Impl2 { @Leadercast public void details() {} }
55    private class MethodTest6_Impl3 extends MethodTest1_Impl2 { @Multicast public void details() {} }
56    private class MethodTest6_Impl4 extends MethodTest1_Impl2 { @Atomic public void details() {} }
57  
58    private MethodTest1_Impl1 mt1impl1;
59    private MethodTest1_Impl2 mt1impl2;
60    private MethodTest1_Impl3 mt1impl3;
61    private MethodTest1_Impl4 mt1impl4;
62    private MethodTest1_Impl5 mt1impl5;
63    private MethodDetails mt1details1, mt1details2, mt1details3, mt1details4, mt1details5; 
64    private Method mt1method1, mt1method2, mt1method3, mt1method4, mt1method5;
65  
66    private MethodTest2_Impl1 mt2impl1;
67    private MethodTest2_Impl2 mt2impl2;
68    private MethodTest2_Impl3 mt2impl3;
69    private MethodTest2_Impl4 mt2impl4;
70    private Method mt2method1, mt2method2, mt2method3, mt2method4;
71    private MethodDetails mt2details1, mt2details2, mt2details3, mt2details4;
72    private MethodDetails mt3details1, mt3details2, mt3details3, mt3details4;
73    private MethodDetails mt5details1, mt5details2, mt5details3, mt5details4;
74    private MethodDetails mt6details1, mt6details2, mt6details3, mt6details4;
75  
76    protected void setUp() throws Exception
77    {
78      super.setUp();
79      mt1impl1 = new MethodTest1_Impl1();
80      mt1impl2 = new MethodTest1_Impl2();
81      mt1impl3 = new MethodTest1_Impl3();
82      mt1impl4 = new MethodTest1_Impl4();
83      mt1impl5 = new MethodTest1_Impl5();
84      mt1method1 = mt1impl1.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
85      mt1method2 = mt1impl2.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
86      mt1method3 = mt1impl3.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
87      mt1method4 = mt1impl4.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
88      mt1method5 = mt1impl5.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
89      mt1details1 = new MethodDetails(mt1method1, mt1impl1);
90      mt1details2 = new MethodDetails(mt1method2, mt1impl2);
91      mt1details3 = new MethodDetails(mt1method3, mt1impl3);
92      mt1details4 = new MethodDetails(mt1method4, mt1impl4);
93      mt1details5 = new MethodDetails(mt1method5, mt1impl5);
94  
95      mt2impl1 = new MethodTest2_Impl1();
96      mt2impl2 = new MethodTest2_Impl2();
97      mt2impl3 = new MethodTest2_Impl3();
98      mt2impl4 = new MethodTest2_Impl4();
99      mt2method1 = mt2impl1.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
100     mt2method2 = mt2impl2.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
101     mt2method3 = mt2impl3.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
102     mt2method4 = mt2impl4.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
103     mt2details1 = new MethodDetails(mt2method1, mt2impl1);
104     mt2details2 = new MethodDetails(mt2method2, mt2impl2);
105     mt2details3 = new MethodDetails(mt2method3, mt2impl3);
106     mt2details4 = new MethodDetails(mt2method4, mt2impl4);
107 
108     MethodTest3_Impl1 mt3impl1 = new MethodTest3_Impl1();
109     MethodTest3_Impl2 mt3impl2 = new MethodTest3_Impl2();
110     MethodTest3_Impl3 mt3impl3 = new MethodTest3_Impl3();
111     MethodTest3_Impl4 mt3impl4 = new MethodTest3_Impl4();
112     Method mt3method1 = mt3impl1.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
113     Method mt3method2 = mt3impl2.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
114     Method mt3method3 = mt3impl3.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
115     Method mt3method4 = mt3impl4.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
116     mt3details1 = new MethodDetails(mt3method1, mt3impl1);
117     mt3details2 = new MethodDetails(mt3method2, mt3impl2);
118     mt3details3 = new MethodDetails(mt3method3, mt3impl3);
119     mt3details4 = new MethodDetails(mt3method4, mt3impl4);
120 
121     MethodTest5_Impl1 mt5impl1 = new MethodTest5_Impl1();
122     MethodTest5_Impl2 mt5impl2 = new MethodTest5_Impl2();
123     MethodTest5_Impl3 mt5impl3 = new MethodTest5_Impl3();
124     MethodTest5_Impl4 mt5impl4 = new MethodTest5_Impl4();
125     Method mt5method1 = mt5impl1.getClass().getMethod("details", new Class[0]);
126     Method mt5method2 = mt5impl2.getClass().getMethod("details", new Class[0]);
127     Method mt5method3 = mt5impl3.getClass().getMethod("details", new Class[0]);
128     Method mt5method4 = mt5impl4.getClass().getMethod("details", new Class[0]);
129     mt5details1 = new MethodDetails(mt5method1, mt5impl1);
130     mt5details2 = new MethodDetails(mt5method2, mt5impl2);
131     mt5details3 = new MethodDetails(mt5method3, mt5impl3);
132     mt5details4 = new MethodDetails(mt5method4, mt5impl4);
133 
134     MethodTest5_Impl1 mt6impl1 = new MethodTest5_Impl1();
135     MethodTest5_Impl2 mt6impl2 = new MethodTest5_Impl2();
136     MethodTest5_Impl3 mt6impl3 = new MethodTest5_Impl3();
137     MethodTest5_Impl4 mt6impl4 = new MethodTest5_Impl4();
138     Method mt6method1 = mt6impl1.getClass().getMethod("details", new Class[0]);
139     Method mt6method2 = mt6impl2.getClass().getMethod("details", new Class[0]);
140     Method mt6method3 = mt6impl3.getClass().getMethod("details", new Class[0]);
141     Method mt6method4 = mt6impl4.getClass().getMethod("details", new Class[0]);
142     mt6details1 = new MethodDetails(mt6method1, mt6impl1);
143     mt6details2 = new MethodDetails(mt6method2, mt6impl2);
144     mt6details3 = new MethodDetails(mt6method3, mt6impl3);
145     mt6details4 = new MethodDetails(mt6method4, mt6impl4);
146   }
147 
148   /*
149    * Test method for 'jgroup.relacs.gmi.MethodDetails.getServer()'
150    */
151   public final void testGetServer()
152   {
153     assertTrue(mt1details1.getServer() == mt1impl1);
154     assertTrue(mt1details2.getServer() == mt1impl2);
155     assertTrue(mt1details3.getServer() == mt1impl3);
156     assertTrue(mt1details4.getServer() == mt1impl4);
157     assertTrue(mt1details5.getServer() == mt1impl5);
158     assertFalse(mt1details1.getServer() == mt1impl2);
159     assertFalse(mt1details1.getServer() == mt1impl3);
160     assertFalse(mt1details1.getServer() == mt1impl4);
161     assertFalse(mt1details1.getServer() == mt1impl5);
162     assertFalse(mt1details2.getServer() == mt1impl1);
163 
164     assertTrue(mt2details1.getServer() == mt2impl1);
165     assertTrue(mt2details2.getServer() == mt2impl2);
166     assertTrue(mt2details3.getServer() == mt2impl3);
167     assertTrue(mt2details4.getServer() == mt2impl4);
168   }
169 
170   /*
171    * Test method for 'jgroup.relacs.gmi.MethodDetails.getMethod()'
172    */
173   public final void testGetMethod()
174   {
175     assertTrue(mt1details1.getMethod() == mt1method1);
176     assertTrue(mt1details2.getMethod() == mt1method2);
177     assertTrue(mt1details3.getMethod() == mt1method3);
178     assertTrue(mt1details4.getMethod() == mt1method4);
179     assertTrue(mt1details5.getMethod() == mt1method5);
180 
181     assertTrue(mt2details1.getMethod() == mt2method1);
182     assertTrue(mt2details2.getMethod() == mt2method2);
183     assertTrue(mt2details3.getMethod() == mt2method3);
184     assertTrue(mt2details4.getMethod() == mt2method4);
185   }
186 
187   /*
188    * Test method for 'jgroup.relacs.gmi.MethodDetails.getInvocationSemantics()'
189    */
190   public final void testGetInvocationSemantics() throws Exception
191   {
192     // Test default annotation
193     assertTrue(mt1details1.getInvocationSemantics() == ANYCAST);
194 
195     // Test that we can declare annotations in the server only
196     assertTrue(mt1details2.getInvocationSemantics() == ANYCAST);
197     assertTrue(mt1details3.getInvocationSemantics() == LEADERCAST);
198     assertTrue(mt1details4.getInvocationSemantics() == MULTICAST);
199     assertTrue(mt1details5.getInvocationSemantics() == ATOMIC);
200 
201     // Test that we can declare annotations in an interface only
202     assertTrue(mt2details1.getInvocationSemantics() == ANYCAST);
203     assertTrue(mt2details2.getInvocationSemantics() == LEADERCAST);
204     assertTrue(mt2details3.getInvocationSemantics() == MULTICAST);
205     assertTrue(mt2details4.getInvocationSemantics() == ATOMIC);
206 
207     // Test that the class annotation overrides the interface declared one.
208     assertTrue(mt3details1.getInvocationSemantics() == ATOMIC);
209     assertTrue(mt3details2.getInvocationSemantics() == ANYCAST);
210     assertTrue(mt3details3.getInvocationSemantics() == LEADERCAST);
211     assertTrue(mt3details4.getInvocationSemantics() == MULTICAST);
212     
213     // Test that the class annotation overrides the super class declared one.
214     assertTrue(mt5details1.getInvocationSemantics() == ANYCAST);
215     assertTrue(mt5details2.getInvocationSemantics() == LEADERCAST);
216     assertTrue(mt5details3.getInvocationSemantics() == MULTICAST);
217     assertTrue(mt5details4.getInvocationSemantics() == ATOMIC);
218     
219     assertTrue(mt6details1.getInvocationSemantics() == ANYCAST);
220     assertTrue(mt6details2.getInvocationSemantics() == LEADERCAST);
221     assertTrue(mt6details3.getInvocationSemantics() == MULTICAST);
222     assertTrue(mt6details4.getInvocationSemantics() == ATOMIC);
223     
224     // Test that we cannot declare multiple invocation semantics for a single method
225     MethodTest4_Impl1 mt4impl1 = new MethodTest4_Impl1();
226     Method mt4method1 = mt4impl1.getClass().getInterfaces()[0].getMethod("details", new Class[0]);
227     try {
228       MethodDetails mt4details1 = new MethodDetails(mt4method1, mt4impl1);
229       fail("Expected Error since we declared multiple invocation semantics on a single method");
230     } catch (Error e) {
231       // Ignored, should happen
232     }
233   }
234 
235 }