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.net.InetAddress;
9   import java.util.ArrayList;
10  import java.util.List;
11  
12  import jgroup.core.EndPoint;
13  import jgroup.core.MemberId;
14  import jgroup.core.protocols.Anycast;
15  import jgroup.core.protocols.Atomic;
16  import jgroup.core.protocols.Leadercast;
17  import jgroup.core.protocols.Multicast;
18  import jgroup.relacs.types.EndPointImpl;
19  import jgroup.relacs.types.MemberIdImpl;
20  import junit.framework.TestCase;
21  
22  public class MethodSemanticsTest extends TestCase
23  {
24  
25    private List<MemberId> members1;
26    private List<MemberId> members2;
27  
28    @Override
29    protected void setUp() throws Exception
30    {
31      super.setUp();
32      EndPoint[] endpoints = new EndPointImpl[6];
33      endpoints[0] = new EndPointImpl(InetAddress.getByName("ba3.ux.uis.no"), 2121);
34      endpoints[1] = new EndPointImpl(InetAddress.getByName("ba4.ux.uis.no"), 2121);
35      endpoints[2] = new EndPointImpl(InetAddress.getByName("jo10.ux.uis.no"), 2121);
36      endpoints[3] = new EndPointImpl(InetAddress.getByName("wolfpack01.item.ntnu.no"), 2121);
37      endpoints[4] = new EndPointImpl(InetAddress.getByName("wolfpack02.item.ntnu.no"), 2121);
38      endpoints[5] = new EndPointImpl(InetAddress.getByName("wolfpack03.item.ntnu.no"), 2121);
39  
40      MemberId[] m1 = new MemberIdImpl[3];
41      m1[0] = new MemberIdImpl(endpoints[0], 0, endpoints[0], 1);
42      m1[1] = new MemberIdImpl(endpoints[1], 0, endpoints[1], 1);
43      m1[2] = new MemberIdImpl(endpoints[2], 0, endpoints[2], 1);
44      MemberId[] m2 = new MemberIdImpl[3];
45      m2[0] = new MemberIdImpl(endpoints[3], 0, endpoints[3], 1);
46      m2[1] = new MemberIdImpl(endpoints[4], 0, endpoints[4], 1);
47      m2[2] = new MemberIdImpl(endpoints[5], 0, endpoints[5], 1);
48  
49      members1 = new ArrayList<MemberId>(3);
50      members1.add(m1[0]);
51      members1.add(m1[1]);
52      members1.add(m1[2]);
53      
54      members2 = new ArrayList<MemberId>(6);
55      members2.add(m1[0]);
56      members2.add(m1[1]);
57      members2.add(m1[2]);
58      members2.add(m2[0]);
59      members2.add(m2[1]);
60      members2.add(m2[2]);
61    }
62  
63    /*
64     * Test method for 'jgroup.relacs.gmi.MethodSemantics.selectEndpoint(List)'
65     */
66    public final void testSelectEndpoint()
67    {
68      assertTrue(LEADERCAST.selectMember(members1) == members1.get(0));
69      assertTrue(LEADERCAST.selectMember(members2) == members2.get(0));
70    }
71  
72    /*
73     * Test method for 'jgroup.relacs.gmi.MethodSemantics.getInvocationSemantics(Class)'
74     */
75    public final void testGetInvocationSemantics()
76    {
77      assertTrue(MethodSemantics.getInvocationSemantics(Anycast.class) == ANYCAST);
78      assertTrue(MethodSemantics.getInvocationSemantics(Leadercast.class) == LEADERCAST);
79      assertTrue(MethodSemantics.getInvocationSemantics(Multicast.class) == MULTICAST);
80      assertTrue(MethodSemantics.getInvocationSemantics(Atomic.class) == ATOMIC);
81      assertFalse(MethodSemantics.getInvocationSemantics(Atomic.class) == ANYCAST);
82      assertFalse(MethodSemantics.getInvocationSemantics(Atomic.class) == LEADERCAST);
83      assertFalse(MethodSemantics.getInvocationSemantics(Atomic.class) == MULTICAST);
84    }
85  
86    /*
87     * Test method for 'jgroup.relacs.gmi.MethodSemantics.valueOf(int)'
88     */
89    public final void testValueOf()
90    {
91      int ordinal = ANYCAST.ordinal();
92      System.out.println(ANYCAST + ": ordinal=" + ordinal);
93      assertTrue("valueOf(ordinal) failed", ANYCAST == MethodSemantics.valueOf(ordinal));
94      assertFalse("valueOf(ordinal) failed", MULTICAST == MethodSemantics.valueOf(ordinal));
95      assertFalse("valueOf(ordinal) failed", LEADERCAST == MethodSemantics.valueOf(ordinal));
96      assertFalse("valueOf(ordinal) failed", ATOMIC == MethodSemantics.valueOf(ordinal));
97      try {
98        MethodSemantics.valueOf(ordinal+20);
99        fail("Expected ArrayIndexOutOfBoundsException");
100     } catch (ArrayIndexOutOfBoundsException e) {
101       // should happen
102     }
103   }
104 
105 }