View Javadoc
1   package org.cyclopsgroup.jmxterm.cmd;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNull;
5   
6   import java.io.IOException;
7   import java.io.StringWriter;
8   import javax.management.JMException;
9   import javax.management.MBeanServerConnection;
10  import javax.management.ObjectName;
11  import org.cyclopsgroup.jmxterm.MockSession;
12  import org.jmock.Expectations;
13  import org.jmock.Mockery;
14  import org.junit.Test;
15  
16  /**
17   * Test case for {@link BeanCommand}
18   *
19   * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo</a>
20   */
21  public class BeanCommandTest {
22    private BeanCommand command = new BeanCommand();
23  
24    private StringWriter output = new StringWriter();
25  
26    private void setBeanAndVerify(String beanName, String domainName, final String expectedBean)
27        throws IOException, JMException {
28      Mockery context = new Mockery();
29      final MBeanServerConnection con = context.mock(MBeanServerConnection.class);
30      command.setBean(beanName);
31      MockSession s = new MockSession(output, con);
32      if (domainName != null) {
33        s.setDomain(domainName);
34      }
35      context.checking(
36          new Expectations() {
37            {
38              atLeast(1).of(con).getMBeanInfo(new ObjectName(expectedBean));
39            }
40          });
41      command.setSession(s);
42      command.execute();
43      assertEquals(expectedBean, s.getBean());
44    }
45  
46    /**
47     * Test execution with NULL result
48     *
49     * @throws IOException Allows network IO errors
50     * @throws JMException Allows JMX exceptions
51     */
52    @Test
53    public void testExecuteWithGettingNull() throws IOException, JMException {
54      command.setSession(new MockSession(output, null));
55      command.execute();
56      assertEquals("null", output.toString().trim());
57    }
58  
59    /**
60     * Test execution with some result
61     *
62     * @throws IOException Allows network IO errors
63     * @throws JMException Allows JMX exceptions
64     */
65    @Test
66    public void testExecuteWithGettingSomething() throws IOException, JMException {
67      MockSession s = new MockSession(output, null);
68      s.setBean("something");
69      command.setSession(s);
70      command.execute();
71      assertEquals("something", output.toString().trim());
72    }
73  
74    /**
75     * Test the case where an illegal bean is requested
76     *
77     * @throws IOException Allows network IO errors
78     * @throws JMException Allows JMX exceptions
79     */
80    @Test(expected = IllegalArgumentException.class)
81    public void testExecuteWithInvalidBean() throws IOException, JMException {
82      command.setBean("blablabla");
83      command.setSession(new MockSession(output, null));
84      command.execute();
85    }
86  
87    /**
88     * Test the case where NULL is get
89     *
90     * @throws IOException Allows network IO errors
91     * @throws JMException Allows JMX exceptions
92     */
93    @Test
94    public void testExecuteWithSettingNull() throws IOException, JMException {
95      command.setBean("null");
96      MockSession s = new MockSession(output, null);
97      s.setBean("something");
98      command.setSession(s);
99      command.execute();
100     assertNull(s.getBean());
101   }
102 
103   /**
104    * Test setting names with special character such as dot, dash and underline, without setting a
105    * domain first
106    *
107    * @throws IOException Allows network IO errors
108    * @throws JMException Allows JMX exceptions
109    */
110   @Test
111   public void testSettingSpecialCharactersWithoutDomain() throws IOException, JMException {
112     setBeanAndVerify(
113         "domain_name.with-dash:attr.name_1-1=a.b", null, "domain_name.with-dash:attr.name_1-1=a.b");
114   }
115 
116   /**
117    * Test the case where a domain is set
118    *
119    * @throws IOException Allows network IO errors
120    * @throws JMException Allows JMX exceptions
121    */
122   @Test
123   public void testSettingWithDomain() throws IOException, JMException {
124     setBeanAndVerify("type=x", "something", "something:type=x");
125   }
126 
127   /**
128    * Test the case where domain is set
129    *
130    * @throws IOException Allows network IO errors
131    * @throws JMException Allows JMX exceptions
132    */
133   @Test
134   public void testSettingWithoutDomain() throws IOException, JMException {
135     setBeanAndVerify("something:type=x", null, "something:type=x");
136   }
137 
138   /**
139    * Test setting names with special character such as dot, dash and underline
140    *
141    * @throws IOException Allows network IO errors
142    * @throws JMException Allows JMX exceptions
143    */
144   @Test
145   public void testSettingWithSpecialCharacters() throws IOException, JMException {
146     setBeanAndVerify(
147         "attr.name_1-1=a.b", "domain_name.with-dash", "domain_name.with-dash:attr.name_1-1=a.b");
148   }
149 }