001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.binding;
023:
024: import java.util.ArrayList;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.Properties;
028: import java.util.Set;
029:
030: import javax.management.AttributeNotFoundException;
031: import javax.management.ObjectName;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034:
035: import org.jboss.jmx.adaptor.rmi.RMIAdaptorExt;
036: import org.jboss.management.j2ee.StateManageable;
037: import org.jboss.system.BarrierController;
038: import org.jboss.system.ServiceMBean;
039: import org.jboss.test.JBossTestCase;
040:
041: /** Tests of the effect of the binding manager service on a two jboss instances.
042: * This needs the configurations created by the test-example-binding-manager
043: * target running to pass.
044: *
045: * @author Scott.Stark@jboss.org
046: * @version $Revision: 57211 $
047: */
048: public class BindingServiceUnitTestCase extends JBossTestCase {
049: static final String SERVER0_JNDI_URL = "jnp://"
050: + System.getProperty("jbosstest.server.host", "localhost")
051: + ":1199";
052: static final String SERVER1_JNDI_URL = "jnp://"
053: + System.getProperty("jbosstest.server.host", "localhost")
054: + ":1299";
055: static HashSet VALID_STATES = new HashSet();
056:
057: static {
058: // JSR-77 running state
059: VALID_STATES.add("j2ee.state.running");
060: // JBoss mbean service started state
061: VALID_STATES.add("Started");
062: }
063:
064: public BindingServiceUnitTestCase(String name) {
065: super (name);
066: }
067:
068: /** Query for all mbeans in the jnp://localhost:1199 and jnp://localhost:1299
069: * servers and assert that every mbean with a State attribute has reached
070: * the ServiceMBean.STARTED state.
071: *
072: * @throws Exception
073: */
074: public void testAvailableServicesServer0() throws Exception {
075: int count = testAvailableServices(SERVER0_JNDI_URL);
076: log.info("server0 service count:" + count);
077: }
078:
079: public void testAvailableServicesServer1() throws Exception {
080: int count = testAvailableServices(SERVER1_JNDI_URL);
081: log.info("server1 service count:" + count);
082: }
083:
084: private int testAvailableServices(String jndiURL) throws Exception {
085: log.info("+++ testAvailableServices, jndiURL=" + jndiURL);
086:
087: Properties env = new Properties();
088: env.setProperty(Context.PROVIDER_URL, jndiURL);
089: InitialContext ctx = new InitialContext(env);
090: RMIAdaptorExt server = (RMIAdaptorExt) ctx
091: .lookup("jmx/invoker/RMIAdaptor");
092: ObjectName all = new ObjectName("*:*");
093: Set allNames = server.queryNames(all, null);
094: ArrayList serverErrors = new ArrayList();
095: Iterator names = allNames.iterator();
096: int serviceCount = 0;
097: while (names.hasNext()) {
098: ObjectName name = (ObjectName) names.next();
099: try {
100: // BarrierController Barriers can be in CREATED or STOPPED state
101: // e.g. if the controller's startup notification hasn't been received,
102: // so log a message and exclude them from the search.
103: boolean isBarrier = server.isInstanceOf(name,
104: BarrierController.Barrier.class.getName());
105: if (isBarrier) {
106: log
107: .debug("Skipping BarrierController.Barrier service: '"
108: + name
109: + "', in state: "
110: + (String) server.getAttribute(
111: name, "StateString"));
112: continue;
113: }
114: /* If this is a JSR-77 mbean, only the StateManageable types
115: have a meaningful state string
116: */
117: boolean jsr77State = server.isInstanceOf(name,
118: StateManageable.class.getName());
119: if (jsr77State) {
120: // the stateManageable also needs to be true
121: Boolean flag = (Boolean) server.getAttribute(name,
122: "stateManageable");
123: jsr77State = flag.booleanValue();
124: }
125: boolean mbeanService = server.isInstanceOf(name,
126: ServiceMBean.class.getName());
127: if (jsr77State == true || mbeanService == true) {
128: serviceCount++;
129: String state = (String) server.getAttribute(name,
130: "StateString");
131: if (VALID_STATES.contains(state) == false) {
132: String msg = name + " is not Started, state="
133: + state;
134: log.error(msg);
135: serverErrors.add(msg);
136: }
137: }
138: } catch (AttributeNotFoundException e) {
139: // Ignore as a non-service
140: }
141: }
142: assertTrue("All services are started, errors="
143: + serverErrors.size(), serverErrors.size() == 0);
144: return serviceCount;
145: }
146:
147: /**
148: * Override to ignore
149: */
150: public void testServerFound() {
151: }
152: }
|