KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > test > DistributedStateTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cluster.test;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.rmi.server.UnicastRemoteObject JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Vector JavaDoc;
28 import javax.management.MBeanServerInvocationHandler JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.Notification JavaDoc;
31
32 import junit.framework.Test;
33
34 import org.jboss.test.JBossClusteredTestCase;
35 import org.jboss.test.cluster.ds.IDistributedState;
36 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
37 import org.jboss.jmx.adaptor.rmi.RMIAdaptorExt;
38 import org.jboss.jmx.adaptor.rmi.RMINotificationListener;
39
40 /** Tests of http session replication
41  *
42  * @author Scott.Stark@jboss.org
43  * @version $Revision: 57898 $
44  */

45 public class DistributedStateTestCase extends JBossClusteredTestCase
46 {
47    static class TestListener extends UnicastRemoteObject JavaDoc
48       implements RMINotificationListener
49    {
50       TestListener() throws RemoteException JavaDoc
51       {
52       }
53       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
54          throws RemoteException JavaDoc
55       {
56          System.out.println(notification);
57       }
58    }
59
60    public static Test suite() throws Exception JavaDoc
61    {
62       Test t1 = getDeploySetup(DistributedStateTestCase.class, "ds-tests.sar");
63       return t1;
64    }
65
66    public DistributedStateTestCase(String JavaDoc name)
67    {
68       super(name);
69    }
70
71    public void testStateReplication()
72       throws Exception JavaDoc
73    {
74       log.debug("+++ testStateReplication");
75       
76       RMIAdaptor[] adaptors = getAdaptors();
77       RMIAdaptorExt server0 = (RMIAdaptorExt) adaptors[0];
78       log.info("server0: "+server0);
79       ObjectName JavaDoc clusterService = new ObjectName JavaDoc("jboss:service=DefaultPartition");
80       Vector JavaDoc view0 = (Vector JavaDoc) server0.getAttribute(clusterService, "CurrentView");
81       log.info("server0: CurrentView, "+view0);
82       ObjectName JavaDoc dsService = new ObjectName JavaDoc("jboss.test:service=DistributedStateTestCase");
83       IDistributedState ds0 = (IDistributedState)
84          MBeanServerInvocationHandler.newProxyInstance(server0, dsService,
85          IDistributedState.class, true);
86       TestListener listener = new TestListener();
87       server0.addNotificationListener(dsService, listener, null, null);
88       ds0.put("key0", "value0");
89       String JavaDoc value = (String JavaDoc) ds0.get("key0");
90       log.info("server0: get(key0): "+value);
91       assertTrue("server0: value == value0", value.equals("value0"));
92
93       RMIAdaptorExt server1 = (RMIAdaptorExt) adaptors[1];
94       log.info("server1: "+server1);
95       Vector JavaDoc view1 = (Vector JavaDoc) server1.getAttribute(clusterService, "CurrentView");
96       log.info("server1: CurrentView, "+view1);
97       IDistributedState ds1 = (IDistributedState)
98          MBeanServerInvocationHandler.newProxyInstance(server1, dsService,
99          IDistributedState.class, true);
100       server1.addNotificationListener(dsService, listener, null, null);
101       value = (String JavaDoc) ds1.get("key0");
102       log.info("server1: get(key0): "+value);
103       assertTrue("server1: value == value0", value.equals("value0"));
104       ds1.put("key0", "value1");
105       value = (String JavaDoc) ds1.get("key0");
106       assertTrue("server1: value == value1("+value+")", value.equals("value1"));
107       value = (String JavaDoc) ds0.get("key0");
108       assertTrue("server0: value == value1("+value+")", value.equals("value1"));
109       
110       ds1.put("key1", "value11");
111       Collection JavaDoc categories = ds0.listAllCategories();
112       assertEquals("server0 categories size", 1, categories.size());
113       categories = ds1.listAllCategories();
114       assertEquals("server1 categories size", 1, categories.size());
115       
116       String JavaDoc category = (String JavaDoc)categories.iterator().next();
117       Collection JavaDoc keys = ds0.listAllKeys(category);
118       assertEquals("server0 keys size", 2, keys.size());
119       keys = ds1.listAllKeys(category);
120       assertEquals("server1 keys size", 2, keys.size());
121       Collection JavaDoc vals = ds0.listAllValues(category);
122       assertEquals("server0 values size", 2, vals.size());
123       vals = ds1.listAllValues(category);
124       assertEquals("server1 values size", 2, vals.size());
125
126       ds0.remove("key0");
127       value = (String JavaDoc) ds1.get("key0");
128       assertTrue("server1: value == null("+value+")", value == null);
129       value = (String JavaDoc) ds0.get("key0");
130       assertTrue("server0: value == null("+value+")", value == null);
131    }
132
133 }
134
Popular Tags