]> source.dussan.org Git - sonarqube.git/blob
f1644b9c808336f03f215a9f246dda20b38314b8
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.application.process;
21
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;
29
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;
36
37
38 public class ProcessMonitorImplTest {
39
40   @Test
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);
44
45     ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, commands);
46
47     underTest.waitFor();
48     verify(process).waitFor();
49
50     underTest.closeStreams();
51     verify(process.getErrorStream()).close();
52     verify(process.getInputStream()).close();
53     verify(process.getOutputStream()).close();
54
55     underTest.destroyForcibly();
56     verify(process).destroyForcibly();
57
58     assertThat(underTest.getInputStream()).isNotNull();
59
60     underTest.isAlive();
61     verify(process).isAlive();
62
63     underTest.waitFor(123, TimeUnit.MILLISECONDS);
64     verify(process).waitFor(123, TimeUnit.MILLISECONDS);
65   }
66
67   @Test
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);
71
72     ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, commands);
73
74     underTest.askForStop();
75     verify(commands).askForStop();
76
77     underTest.acknowledgeAskForRestart();
78     verify(commands).acknowledgeAskForRestart();
79
80     underTest.askedForRestart();
81     verify(commands).askedForRestart();
82
83     underTest.isOperational();
84     verify(commands).isOperational();
85   }
86
87   @Test
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);
92
93     ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, commands);
94
95     // no failures
96     underTest.closeStreams();
97   }
98
99   @Test
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);
105
106     ProcessMonitorImpl underTest = new ProcessMonitorImpl(process, mock(ProcessCommands.class, Mockito.RETURNS_MOCKS));
107
108     // no failures
109     underTest.closeStreams();
110   }
111 }