|
5 | 5 | import static org.junit.Assert.fail; |
6 | 6 | import static org.mockito.Mockito.mock; |
7 | 7 | import static org.mockito.Mockito.when; |
| 8 | +import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.setField; |
8 | 9 |
|
9 | 10 | import uk.gov.justice.services.jmx.api.mbean.SystemCommanderMBean; |
10 | 11 | import uk.gov.justice.services.jmx.api.name.CommandMBeanNameProvider; |
| 12 | +import uk.gov.justice.services.jmx.api.name.ObjectNameFactory; |
11 | 13 | import uk.gov.justice.services.jmx.system.command.client.MBeanClientException; |
| 14 | +import uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil; |
12 | 15 |
|
13 | 16 | import java.io.IOException; |
14 | 17 |
|
@@ -47,6 +50,7 @@ public void shouldConnectToARemoteInstanceOfTheJmxBean() throws Exception { |
47 | 50 |
|
48 | 51 | when(commandMBeanNameProvider.create(contextName)).thenReturn(objectName); |
49 | 52 | when(jmxConnector.getMBeanServerConnection()).thenReturn(connection); |
| 53 | + when(connection.isRegistered(objectName)).thenReturn(true); |
50 | 54 | when(remoteMBeanFactory.createRemote(connection, objectName, mBeanInterface)).thenReturn(systemCommanderMBean); |
51 | 55 |
|
52 | 56 | assertThat(mBeanConnector.connect(contextName, mBeanInterface, jmxConnector), is(systemCommanderMBean)); |
@@ -74,4 +78,34 @@ public void shouldThrowExceptionIfConnectingToTheMBeanFails() throws Exception { |
74 | 78 | assertThat(expected.getMessage(), is("Failed to get remote connection to MBean 'SystemCommanderMBean'")); |
75 | 79 | } |
76 | 80 | } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void shouldThrowExceptionIfMBeanNameNotCorrect() throws Exception { |
| 84 | + |
| 85 | + final String contextName = "people"; |
| 86 | + final ObjectName realObjectName = createARealObjectName(contextName); |
| 87 | + |
| 88 | + final Class<SystemCommanderMBean> mBeanInterface = SystemCommanderMBean.class; |
| 89 | + final JMXConnector jmxConnector = mock(JMXConnector.class); |
| 90 | + final MBeanServerConnection connection = mock(MBeanServerConnection.class); |
| 91 | + |
| 92 | + when(commandMBeanNameProvider.create(contextName)).thenReturn(realObjectName); |
| 93 | + when(jmxConnector.getMBeanServerConnection()).thenReturn(connection); |
| 94 | + when(connection.isRegistered(realObjectName)).thenReturn(false); |
| 95 | + |
| 96 | + |
| 97 | + try { |
| 98 | + mBeanConnector.connect(contextName, mBeanInterface, jmxConnector); |
| 99 | + fail(); |
| 100 | + } catch (final MBeanClientException expected) { |
| 101 | + assertThat(expected.getMessage(), is("No JMX bean found with name 'people-system-command-handler-mbean'. Is your context name of 'people' correct?")); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private ObjectName createARealObjectName(final String contextName) { |
| 106 | + final CommandMBeanNameProvider commandMBeanNameProvider = new CommandMBeanNameProvider(); |
| 107 | + setField(commandMBeanNameProvider, "objectNameFactory", new ObjectNameFactory()); |
| 108 | + |
| 109 | + return commandMBeanNameProvider.create(contextName); |
| 110 | + } |
77 | 111 | } |
0 commit comments