3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.application.process;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.concurrent.TimeUnit;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.sonar.application.process.ProcessMonitorImpl;
28 import org.sonar.process.ProcessCommands;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
32 import static org.mockito.Mockito.doThrow;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
38 public class ProcessMonitorImplTest {
41 public void ProcessMonitorImpl_is_a_proxy_of_Process() throws Exception {
42 Process process = mock(Process.class, RETURNS_DEEP_STUBS);
43 ProcessCommands commands = mock(ProcessCommands.class, RETURNS_DEEP_STUBS);
45 ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, commands);
48 verify(process).waitFor();
50 underTest.closeStreams();
51 verify(process.getErrorStream()).close();
52 verify(process.getInputStream()).close();
53 verify(process.getOutputStream()).close();
55 underTest.destroyForcibly();
56 verify(process).destroyForcibly();
58 assertThat(underTest.getInputStream()).isNotNull();
61 verify(process).isAlive();
63 underTest.waitFor(123, TimeUnit.MILLISECONDS);
64 verify(process).waitFor(123, TimeUnit.MILLISECONDS);
68 public void ProcessMonitorImpl_is_a_proxy_of_Commands() throws Exception {
69 Process process = mock(Process.class, RETURNS_DEEP_STUBS);
70 ProcessCommands commands = mock(ProcessCommands.class, RETURNS_DEEP_STUBS);
72 ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, commands);
74 underTest.askForStop();
75 verify(commands).askForStop();
77 underTest.acknowledgeAskForRestart();
78 verify(commands).acknowledgeAskForRestart();
80 underTest.askedForRestart();
81 verify(commands).askedForRestart();
83 underTest.isOperational();
84 verify(commands).isOperational();
88 public void closeStreams_ignores_null_stream() {
89 ProcessCommands commands = mock(ProcessCommands.class);
90 Process process = mock(Process.class);
91 when(process.getInputStream()).thenReturn(null);
93 ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, commands);
96 underTest.closeStreams();
100 public void closeStreams_ignores_failure_if_stream_fails_to_be_closed() throws Exception {
101 InputStream stream = mock(InputStream.class);
102 doThrow(new IOException("error")).when(stream).close();
103 Process process = mock(Process.class);
104 when(process.getInputStream()).thenReturn(stream);
106 ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, mock(ProcessCommands.class, Mockito.RETURNS_MOCKS));
109 underTest.closeStreams();