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
18
19
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
48
49
50
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
61
62
63
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
76
77
78
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
89
90
91
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
105
106
107
108
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
118
119
120
121
122 @Test
123 public void testSettingWithDomain() throws IOException, JMException {
124 setBeanAndVerify("type=x", "something", "something:type=x");
125 }
126
127
128
129
130
131
132
133 @Test
134 public void testSettingWithoutDomain() throws IOException, JMException {
135 setBeanAndVerify("something:type=x", null, "something:type=x");
136 }
137
138
139
140
141
142
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 }