PageRenderTime 63ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jboss-5.1.0/testsuite/src/main/org/jboss/test/profileservice/test/ServerManagedObjectsTestCase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 293 lines | 201 code | 23 blank | 69 comment | 6 complexity | 2571460fdc87b9404f670a619d8a4d9f MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  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.profileservice.test;
  23. import java.net.InetAddress;
  24. import java.net.URL;
  25. import java.util.Collection;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import javax.naming.InitialContext;
  29. import org.jboss.deployers.spi.management.KnownComponentTypes;
  30. import org.jboss.deployers.spi.management.ManagementView;
  31. import org.jboss.managed.api.ComponentType;
  32. import org.jboss.managed.api.ManagedComponent;
  33. import org.jboss.managed.api.ManagedDeployment;
  34. import org.jboss.managed.api.ManagedOperation;
  35. import org.jboss.managed.api.ManagedProperty;
  36. import org.jboss.managed.api.RunState;
  37. import org.jboss.managed.plugins.ManagedOperationMatcher;
  38. import org.jboss.metatype.api.types.EnumMetaType;
  39. import org.jboss.metatype.api.types.MetaType;
  40. import org.jboss.metatype.api.types.SimpleMetaType;
  41. import org.jboss.metatype.api.values.EnumValue;
  42. import org.jboss.metatype.api.values.EnumValueSupport;
  43. import org.jboss.metatype.api.values.MetaValue;
  44. import org.jboss.metatype.api.values.SimpleValue;
  45. import org.jboss.metatype.api.values.SimpleValueSupport;
  46. import org.jboss.profileservice.management.matchers.AliasMatcher;
  47. import org.jboss.profileservice.spi.ProfileService;
  48. import org.jboss.test.JBossTestCase;
  49. import org.jboss.virtual.VFS;
  50. /**
  51. * Tests of key server bean managed object views
  52. *
  53. * @author Scott.Stark@jboss.org
  54. * @version $Revision: 88775 $
  55. */
  56. public class ServerManagedObjectsTestCase
  57. extends JBossTestCase
  58. {
  59. protected ManagementView activeView;
  60. public ServerManagedObjectsTestCase(String name)
  61. {
  62. super(name);
  63. }
  64. /**
  65. * Validate the ServerInfo component
  66. * @throws Exception
  67. */
  68. public void testServerInfo()
  69. throws Exception
  70. {
  71. ManagementView mgtView = getManagementView();
  72. mgtView.reload();
  73. ComponentType type = new ComponentType("MCBean", "ServerInfo");
  74. getLog().debug("MCBeans: "+mgtView.getComponentsForType(type));
  75. ManagedComponent mc = mgtView.getComponent("jboss.system:type=ServerInfo", type);
  76. assertNotNull(mc);
  77. // Serach by alias for the jmx name
  78. AliasMatcher matcher = new AliasMatcher();
  79. Set<ManagedComponent> mcs = mgtView.getMatchingComponents("jboss.system:type=ServerInfo", type, matcher);
  80. log.debug("jboss.system:type=ServerInfo components: "+mcs);
  81. assertEquals("Found one MC for alias", 1, mcs.size());
  82. Map<String, ManagedProperty> props = mc.getProperties();
  83. getLog().info(props);
  84. // , activeThreadGroupCount, hostAddress, OSVersion, javaVMName, totalMemory, activeThreadCount, alias, hostName, javaVMVendor, javaVendor, javaVMVersion, OSName, javaVersion
  85. // maxMemory
  86. ManagedProperty maxMemory = props.get("maxMemory");
  87. long maxMemoryValue = getLong(maxMemory);
  88. assertTrue("maxMemory > 1MB", maxMemoryValue > 1024*1024);
  89. // freeMemory
  90. ManagedProperty freeMemory = props.get("freeMemory");
  91. long freeMemoryValue = getLong(freeMemory);
  92. assertTrue("freeMemory > 1MB", freeMemoryValue > 1024*1024);
  93. // TotalMemory
  94. ManagedProperty totalMemory = props.get("totalMemory");
  95. long totalMemoryValue = getLong(totalMemory);
  96. assertTrue("totalMemory > 1MB", totalMemoryValue > 1024*1024);
  97. // availableProcessors
  98. ManagedProperty availableProcessors = props.get("availableProcessors");
  99. long availableProcessorsValue = getLong(availableProcessors);
  100. assertTrue("availableProcessors > 0", availableProcessorsValue > 0);
  101. // ActiveThreadCount
  102. ManagedProperty activeThreadCount = props.get("activeThreadCount");
  103. long activeThreadCountValue = getLong(activeThreadCount);
  104. assertTrue("activeThreadCount > 0", activeThreadCountValue > 0);
  105. // ActiveThreadGroupCount
  106. ManagedProperty activeThreadGroupCount = props.get("activeThreadGroupCount");
  107. long activeThreadGroupCountValue = getLong(activeThreadGroupCount);
  108. assertTrue("activeThreadGroupCount > 0", activeThreadGroupCountValue > 0);
  109. // Operations
  110. Set<ManagedOperation> ops = mc.getOperations();
  111. log.info("ServerInfo.ops: "+ ops);
  112. ManagedOperation listThreadCpuUtilization = ManagedOperationMatcher.findOperation(ops, "listThreadCpuUtilization");
  113. assertNotNull(listThreadCpuUtilization);
  114. MetaValue listThreadCpuUtilizationMV = listThreadCpuUtilization.invoke();
  115. // TODO
  116. assertNotNull(listThreadCpuUtilizationMV);
  117. assertEquals(SimpleMetaType.STRING, listThreadCpuUtilizationMV.getMetaType());
  118. SimpleValue listThreadCpuUtilizationSV = (SimpleValue) listThreadCpuUtilizationMV;
  119. String cpuUtilization = (String) listThreadCpuUtilizationSV.getValue();
  120. log.info(cpuUtilization);
  121. assertTrue(cpuUtilization.length() > 100);
  122. // Try invoking listThreadCpuUtilization and checking freeMemory until it changes
  123. long currentFreeMemoryValue = freeMemoryValue;
  124. for(int n = 0; n < 100; n ++)
  125. {
  126. listThreadCpuUtilization.invoke();
  127. currentFreeMemoryValue = getLong(freeMemory);
  128. if(currentFreeMemoryValue != freeMemoryValue)
  129. break;
  130. }
  131. assertTrue("currentFreeMemoryValue != original freeMemoryValue",
  132. currentFreeMemoryValue != freeMemoryValue);
  133. // The bean state
  134. ManagedProperty state = props.get("state");
  135. assertNotNull("state", state);
  136. EnumMetaType stateType = (EnumMetaType) state.getMetaType();
  137. EnumValue stateValue = (EnumValue) state.getValue();
  138. getLog().info("state: "+stateValue);
  139. EnumValue installed = new EnumValueSupport(stateType, "Installed");
  140. assertEquals(installed, stateValue);
  141. }
  142. /**
  143. * Test the jboss.system:type=MCServer component from the bootstrap
  144. * @throws Exception
  145. */
  146. public void testMCServer()
  147. throws Exception
  148. {
  149. ManagementView mgtView = getManagementView();
  150. ComponentType type = new ComponentType("MCBean", "MCServer");
  151. ManagedComponent mc = mgtView.getComponent("jboss.system:type=MCServer", type);
  152. assertNotNull(mc);
  153. // Validate we can obtain the bootstrap deployment by name
  154. ManagedDeployment md = mc.getDeployment();
  155. assertNotNull(md);
  156. getLog().info(md);
  157. ManagedDeployment bootstrapMD = mgtView.getDeployment(md.getName());
  158. assertNotNull(bootstrapMD);
  159. // Validate properties, [buildOS, buildID, config, buildNumber, startDate, buildDate, versionName, buildJVM, versionNumber, version]
  160. Map<String, ManagedProperty> props = mc.getProperties();
  161. getLog().info(props);
  162. ManagedProperty buildOS = mc.getProperty("buildOS");
  163. assertNotNull(buildOS);
  164. ManagedProperty buildID = mc.getProperty("buildID");
  165. assertNotNull(buildID);
  166. ManagedProperty buildNumber = mc.getProperty("buildNumber");
  167. assertNotNull(buildNumber);
  168. ManagedProperty buildDate = mc.getProperty("buildDate");
  169. assertNotNull(buildDate);
  170. ManagedProperty buildJVM = mc.getProperty("buildJVM");
  171. assertNotNull(buildJVM);
  172. ManagedProperty startDate = mc.getProperty("startDate");
  173. assertNotNull(startDate);
  174. ManagedProperty versionName = mc.getProperty("versionName");
  175. assertNotNull(versionName);
  176. ManagedProperty versionNumber = mc.getProperty("versionNumber");
  177. assertNotNull(versionNumber);
  178. ManagedProperty version = mc.getProperty("version");
  179. assertNotNull(version);
  180. // The config should be the ServerConfig ManagedObject
  181. ManagedProperty config = mc.getProperty("config");
  182. assertNotNull(config);
  183. // This should have a shutdown operation
  184. Set<ManagedOperation> ops = mc.getOperations();
  185. MetaType[] signature = {};
  186. ManagedOperation shutdown = ManagedOperationMatcher.findOperation(ops, "shutdown", signature);
  187. assertNotNull(shutdown);
  188. /* Invoke it
  189. MetaValue[] args = {};
  190. shutdown.invoke(args);
  191. */
  192. }
  193. /**
  194. *
  195. * @throws Exception
  196. */
  197. public void testTransactionManager()
  198. throws Exception
  199. {
  200. ManagementView mgtView = getManagementView();
  201. ComponentType type = new ComponentType("MCBean", "JTA");
  202. ManagedComponent mc = mgtView.getComponent("TransactionManager", type);
  203. assertNotNull(mc);
  204. Map<String, ManagedProperty> props = mc.getProperties();
  205. getLog().info(props);
  206. ManagedProperty transactionCount = props.get("transactionCount");
  207. assertNotNull(transactionCount);
  208. getLog().info("transactionCount, "+transactionCount.getValue());
  209. ManagedProperty commitCount = props.get("commitCount");
  210. assertNotNull(commitCount);
  211. getLog().info("commitCount, "+commitCount.getValue());
  212. ManagedProperty runningTransactionCount = props.get("runningTransactionCount");
  213. assertNotNull(runningTransactionCount);
  214. getLog().info("runningTransactionCount, "+runningTransactionCount.getValue());
  215. ManagedProperty rollbackCount = props.get("rollbackCount");
  216. assertNotNull(rollbackCount);
  217. getLog().info("rollbackCount, "+rollbackCount.getValue());
  218. ManagedProperty transactionTimeout = props.get("transactionTimeout");
  219. assertNotNull(transactionTimeout);
  220. getLog().info("transactionTimeout, "+transactionTimeout.getValue());
  221. ManagedProperty timedoutCount = props.get("timedoutCount");
  222. assertNotNull(timedoutCount);
  223. getLog().info("timedoutCount, "+timedoutCount.getValue());
  224. }
  225. /**
  226. *
  227. * @throws Exception
  228. */
  229. public void testServerConfig()
  230. throws Exception
  231. {
  232. ManagementView mgtView = getManagementView();
  233. ComponentType type = new ComponentType("MCBean", "ServerConfig");
  234. ManagedComponent mc = mgtView.getComponent("jboss.system:type=ServerConfig", type);
  235. assertNotNull(mc);
  236. Map<String, ManagedProperty> props = mc.getProperties();
  237. getLog().info(props);
  238. }
  239. public void testMBeanFactory() throws Exception
  240. {
  241. ManagementView mgtView = getManagementView();
  242. Collection<ManagedComponent> components = mgtView.getComponentsForType(new ComponentType("MBean", "WebApplicationManager"));
  243. assertNotNull(components);
  244. }
  245. /**
  246. * Obtain the ProfileService.ManagementView
  247. * @return
  248. * @throws Exception
  249. */
  250. protected ManagementView getManagementView()
  251. throws Exception
  252. {
  253. if( activeView == null )
  254. {
  255. InitialContext ctx = getInitialContext();
  256. ProfileService ps = (ProfileService) ctx.lookup("ProfileService");
  257. activeView = ps.getViewManager();
  258. // Init the VFS to setup the vfs* protocol handlers
  259. VFS.init();
  260. }
  261. activeView.load();
  262. return activeView;
  263. }
  264. private long getLong(ManagedProperty prop)
  265. {
  266. SimpleValue mv = (SimpleValue) prop.getValue();
  267. Number value = (Number) mv.getValue();
  268. return value.longValue();
  269. }
  270. }