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.as.quickstarts.mbeanhelloworld.service.HelloService;
29  import org.jboss.shrinkwrap.api.Archive;
30  import org.jboss.shrinkwrap.api.ShrinkWrap;
31  import org.jboss.shrinkwrap.api.asset.EmptyAsset;
32  import org.jboss.shrinkwrap.api.spec.JavaArchive;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.junit.runner.RunWith;
36  
37  /**
38   * Testing annotated component mbean.
39   * 
40   * @author Jeremie Lagarde
41   * 
42   */
43  @RunWith(Arquillian.class)
44  public class AnnotatedComponentHelloWorldTest {
45  
46      /**
47       * Constructs a deployment archive
48       * 
49       * @return the deployment archive
50       */
51      @Deployment
52      public static Archive<?> createTestArchive() {
53          return ShrinkWrap.create(JavaArchive.class,"helloworld.jar")
54                  .addClasses(AnnotatedComponentHelloWorld.class).addClasses(AbstractComponentMBean.class)
55                  .addClass(IAnnotatedHelloWorldMBean.class)
56                  .addClass(HelloService.class)
57                  .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
58      }
59  
60      @Test
61      public void testHello() throws Exception {
62          MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
63          ObjectName objectName = new ObjectName("quickstarts", "type", AnnotatedComponentHelloWorld.class.getSimpleName());
64          MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName);
65          Assert.assertNotNull(mbeanInfo);
66          Assert.assertEquals(0L,mbeanServer.getAttribute(objectName, "Count"));
67          Assert.assertEquals("Hello",mbeanServer.getAttribute(objectName, "WelcomeMessage"));
68          Assert.assertEquals("Hello jer!",mbeanServer.invoke(objectName, "sayHello", new Object[] {"jer"}, new String[] {"java.lang.String"}));
69          Assert.assertEquals(1L,mbeanServer.getAttribute(objectName, "Count"));
70          mbeanServer.setAttribute(objectName, new Attribute("WelcomeMessage","Hi"));
71          Assert.assertEquals("Hi jer!",mbeanServer.invoke(objectName, "sayHello", new Object[] {"jer"}, new String[] {"java.lang.String"}));
72          Assert.assertEquals(2L,mbeanServer.getAttribute(objectName, "Count"));
73      }
74  }