@Override public void stop() { Server server = this.serverProvider.getServer(); if (server == null) throw new IllegalStateException(); server.removeLifecycleListener(this); if (this.init.get() && this.start.compareAndSet(true, false)) { this.eventHandler.stop(this.factory.createServer(server)); } if (this.init.compareAndSet(true, false)) { this.destroy(server); } }
@Override public void start() { Server server = this.serverProvider.getServer(); if (server == null) throw new IllegalStateException(); if (!this.containsListener(server)) { server.addLifecycleListener(this); } if (this.init.compareAndSet(false, true)) { this.init(server); } if (this.start.compareAndSet(false, true)) { this.eventHandler.start(this.factory.createServer(server)); } }
@Test public void test() throws Exception { MBeanServer mbeanServer = mock(MBeanServer.class); ObjectName name = ObjectName.getInstance("Catalina:type=Server"); Server expected = mock(Server.class); Service service = mock(Service.class); when(mbeanServer.invoke(same(name), eq("findServices"), (Object[]) isNull(), (String[]) isNull())).thenReturn(new Service[] { service }); when(service.getServer()).thenReturn(expected); ServerProvider provider = new JMXServerProvider(mbeanServer, name); Server result = provider.getServer(); assertSame(expected, result); }
@Test public void test() { Server server = mock(Server.class); ServerProvider provider = new SimpleServerProvider(server); assertSame(server, provider.getServer()); } }
@Test public void testNotFound() throws Exception { MBeanServer mbeanServer = mock(MBeanServer.class); ObjectName name = ObjectName.getInstance("Catalina:type=Server"); when(mbeanServer.invoke(same(name), eq("findServices"), (Object[]) isNull(), (String[]) isNull())).thenThrow(new InstanceNotFoundException()); ServerProvider provider = new JMXServerProvider(mbeanServer, name); RuntimeException exception = null; try { provider.getServer(); } catch (IllegalStateException e) { exception = e; } assertNotNull(exception); } }
@Test public void start() { Service service = mock(Service.class); LifecycleListener listener = mock(LifecycleListener.class); LifecycleEngine engine = mock(LifecycleEngine.class); Container container = mock(Container.class); LifecycleContainer childContainer = mock(LifecycleContainer.class); Server server = mock(Server.class); TomcatEventHandler handler = this.createEventHandler(this.eventHandler, this.provider, this.factory); when(this.provider.getServer()).thenReturn(this.server); when(this.server.findLifecycleListeners()).thenReturn(new LifecycleListener[] { listener }); when(this.server.findServices()).thenReturn(new Service[] { service }); when(service.getContainer()).thenReturn(engine); when(engine.findChildren()).thenReturn(new Container[] { container }); when(container.findChildren()).thenReturn(new Container[] { childContainer }); when(this.factory.createServer(same(this.server))).thenReturn(server); handler.start(); verify(this.server).addLifecycleListener(same(handler)); verify(this.eventHandler).init(same(server)); verify(engine).addContainerListener(handler); verify(engine).addLifecycleListener(handler); verify(container).addContainerListener(handler); verify(childContainer).addLifecycleListener(handler); verify(childContainer).addPropertyChangeListener(handler); verify(this.eventHandler).start(same(server)); }
@Test public void stop() throws Exception { Server server = mock(Server.class); Service service = mock(Service.class); LifecycleEngine engine = mock(LifecycleEngine.class); Container container = mock(Container.class); LifecycleContainer childContainer = mock(LifecycleContainer.class); TomcatEventHandler handler = this.createEventHandler(this.eventHandler, this.provider, this.factory); this.initServer(handler, this.server); this.startServer(handler, this.server); when(this.provider.getServer()).thenReturn(this.server); when(this.server.findServices()).thenReturn(new Service[] { service }); when(service.getContainer()).thenReturn(engine); when(engine.findChildren()).thenReturn(new Container[] { container }); when(container.findChildren()).thenReturn(new Container[] { childContainer }); when(this.factory.createServer(this.server)).thenReturn(server); handler.stop(); verify(this.server).removeLifecycleListener(same(handler)); verify(this.eventHandler).stop(same(server)); verify(engine).removeContainerListener(handler); verify(engine).removeLifecycleListener(handler); verify(container).removeContainerListener(handler); verify(childContainer).removeLifecycleListener(handler); verify(childContainer).removePropertyChangeListener(handler); verify(this.eventHandler).shutdown(); }