KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > binding > BindingServiceUnitTestCase


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.binding;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.management.AttributeNotFoundException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34
35 import org.jboss.jmx.adaptor.rmi.RMIAdaptorExt;
36 import org.jboss.management.j2ee.StateManageable;
37 import org.jboss.system.BarrierController;
38 import org.jboss.system.ServiceMBean;
39 import org.jboss.test.JBossTestCase;
40
41 /** Tests of the effect of the binding manager service on a two jboss instances.
42  * This needs the configurations created by the test-example-binding-manager
43  * target running to pass.
44  *
45  * @author Scott.Stark@jboss.org
46  * @version $Revision: 40682 $
47  */

48 public class BindingServiceUnitTestCase
49    extends JBossTestCase
50 {
51    static final String JavaDoc SERVER0_JNDI_URL = "jnp://" + System.getProperty("jbosstest.server.host", "localhost") + ":1199";
52    static final String JavaDoc SERVER1_JNDI_URL = "jnp://" + System.getProperty("jbosstest.server.host", "localhost") + ":1299";
53    static HashSet JavaDoc VALID_STATES = new HashSet JavaDoc();
54
55    static
56    {
57       // JSR-77 running state
58
VALID_STATES.add("j2ee.state.running");
59       // JBoss mbean service started state
60
VALID_STATES.add("Started");
61    }
62    public BindingServiceUnitTestCase(String JavaDoc name)
63    {
64       super(name);
65    }
66
67    /** Query for all mbeans in the jnp://localhost:1199 and jnp://localhost:1299
68     * servers and assert that every mbean with a State attribute has reached
69     * the ServiceMBean.STARTED state.
70     *
71     * @throws Exception
72     */

73    public void testAvailableServicesServer0()
74       throws Exception JavaDoc
75    {
76       int count = testAvailableServices(SERVER0_JNDI_URL);
77       log.info("server0 service count:"+count);
78    }
79    public void testAvailableServicesServer1()
80       throws Exception JavaDoc
81    {
82       int count = testAvailableServices(SERVER1_JNDI_URL);
83       log.info("server1 service count:"+count);
84    }
85
86    private int testAvailableServices(String JavaDoc jndiURL)
87       throws Exception JavaDoc
88    {
89       log.info("+++ testAvailableServices, jndiURL="+jndiURL);
90
91       Properties JavaDoc env = new Properties JavaDoc();
92       env.setProperty(Context.PROVIDER_URL, jndiURL);
93       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
94       RMIAdaptorExt server = (RMIAdaptorExt) ctx.lookup("jmx/invoker/RMIAdaptor");
95       ObjectName JavaDoc all = new ObjectName JavaDoc("*:*");
96       Set JavaDoc allNames = server.queryNames(all, null);
97       ArrayList JavaDoc serverErrors = new ArrayList JavaDoc();
98       Iterator JavaDoc names = allNames.iterator();
99       int serviceCount = 0;
100       while( names.hasNext() )
101       {
102          ObjectName JavaDoc name = (ObjectName JavaDoc) names.next();
103          try
104          {
105             // BarrierController Barriers can be in CREATED or STOPPED state
106
// e.g. if the controller's startup notification hasn't been received,
107
// so log a message and exclude them from the search.
108
boolean isBarrier = server.isInstanceOf(name, BarrierController.Barrier.class.getName());
109             if( isBarrier )
110             {
111                log.debug("Skipping BarrierController.Barrier service: '" + name
112                      + "', in state: " + (String JavaDoc) server.getAttribute(name, "StateString"));
113                continue;
114             }
115             /* If this is a JSR-77 mbean, only the StateManageable types
116              have a meaningful state string
117             */

118             boolean jsr77State = server.isInstanceOf(name, StateManageable.class.getName());
119             if( jsr77State )
120             {
121                // the stateManageable also needs to be true
122
Boolean JavaDoc flag = (Boolean JavaDoc) server.getAttribute(name, "stateManageable");
123                jsr77State = flag.booleanValue();
124             }
125             boolean mbeanService = server.isInstanceOf(name, ServiceMBean.class.getName());
126             if( jsr77State == true || mbeanService == true )
127             {
128                serviceCount ++;
129                String JavaDoc state = (String JavaDoc) server.getAttribute(name, "StateString");
130                if( VALID_STATES.contains(state) == false )
131                {
132                      String JavaDoc msg = name+" is not Started, state="+state;
133                      log.error(msg);
134                      serverErrors.add(msg);
135                }
136             }
137          }
138          catch(AttributeNotFoundException JavaDoc e)
139          {
140             // Ignore as a non-service
141
}
142       }
143       assertTrue("All services are started, errors="
144          +serverErrors.size(), serverErrors.size() == 0);
145       return serviceCount;
146    }
147
148    /**
149     * Override to ignore
150     */

151    public void testServerFound()
152    {
153    }
154 }
155
Popular Tags