View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.mbeanhelloworld.mbean;
18  
19  import java.lang.management.ManagementFactory;
20  
21  import javax.management.Attribute;
22  import javax.management.MBeanInfo;
23  import javax.management.MBeanServer;
24  import javax.management.ObjectName;
25  
26  import org.jboss.arquillian.container.test.api.Deployment;
27  import org.jboss.arquillian.junit.Arquillian;
28  import org.jboss.shrinkwrap.api.Archive;
29  import org.jboss.shrinkwrap.api.ShrinkWrap;
30  import org.jboss.shrinkwrap.api.spec.JavaArchive;
31  import org.junit.Assert;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  
35  /**
36   * Testing pojo mbean with mxbean interface.
37   * 
38   * @author Jeremie Lagarde
39   * 
40   */
41  @RunWith(Arquillian.class)
42  public class MXPojoHelloWorldTest {
43  
44      /**
45       * Constructs a deployment archive
46       * 
47       * @return the deployment archive
48       */
49      @Deployment
50      public static Archive<?> createTestArchive() {
51          return ShrinkWrap.create(JavaArchive.class, "myservice.sar")
52              .addClasses(MXPojoHelloWorld.class).addClasses(IHelloWorldMXBean.class)
53              .addAsManifestResource("META-INF/jboss-service.xml", "jboss-service.xml");
54      }
55  
56      @Test
57      public void testHello() throws Exception {
58          MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
59          ObjectName objectName = new ObjectName("quickstarts", "type", "SarMXPojoHelloWorld");
60          MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName);
61          Assert.assertNotNull(mbeanInfo);
62          Assert.assertEquals(0L, mbeanServer.getAttribute(objectName, "Count"));
63          Assert.assertEquals("Welcome", mbeanServer.getAttribute(objectName, "WelcomeMessage"));
64          Assert.assertEquals("Welcome jer!",
65              mbeanServer.invoke(objectName, "sayHello", new Object[] { "jer" }, new String[] { "java.lang.String" }));
66          Assert.assertEquals(1L, mbeanServer.getAttribute(objectName, "Count"));
67          mbeanServer.setAttribute(objectName, new Attribute("WelcomeMessage", "Hi"));
68          Assert.assertEquals("Hi jer!",
69              mbeanServer.invoke(objectName, "sayHello", new Object[] { "jer" }, new String[] { "java.lang.String" }));
70          Assert.assertEquals(2L, mbeanServer.getAttribute(objectName, "Count"));
71      }
72  
73  }