aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src/test/java
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-09-11 15:22:03 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-09-19 10:24:01 +0200
commitd8c9b99b1aec0aca512bc68ae26b8ce2e09b24cf (patch)
treeccbacf6304a4e2021737aa15b0b395746570352e /server/sonar-process/src/test/java
parent29206c1d0c22b77881da32ae38aeac504e5b2d4c (diff)
downloadsonarqube-d8c9b99b1aec0aca512bc68ae26b8ce2e09b24cf.tar.gz
sonarqube-d8c9b99b1aec0aca512bc68ae26b8ce2e09b24cf.zip
SONAR-9773 ignore JAVA_TOOL_OPTIONS is defined and log warning
Diffstat (limited to 'server/sonar-process/src/test/java')
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java81
1 files changed, 80 insertions, 1 deletions
diff --git a/server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java b/server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java
index f2bc87961d9..5c87e00c6b4 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/command/CommandFactoryImplTest.java
@@ -19,23 +19,35 @@
*/
package org.sonar.process.command;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.AppenderBase;
import java.io.File;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
+import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
+import org.mockito.Mockito;
import org.sonar.process.ProcessId;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
+import org.sonar.process.System2;
+import org.sonar.process.logging.LogbackHelper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.when;
public class CommandFactoryImplTest {
+
+ private static final String MEMORY_APPENDER_NAME = "MEMORY_APPENDER";
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
@@ -44,6 +56,7 @@ public class CommandFactoryImplTest {
private File homeDir;
private File tempDir;
private File logsDir;
+ private ListAppender listAppender;
@Before
public void setUp() throws Exception {
@@ -52,6 +65,41 @@ public class CommandFactoryImplTest {
logsDir = temp.newFolder();
}
+ @After
+ public void tearDown() throws Exception {
+ if (listAppender != null) {
+ listAppender.stop();
+ new LogbackHelper().getRootContext().getLogger(CommandFactoryImpl.class)
+ .detachAppender(listAppender);
+ }
+ }
+
+ @Test
+ public void constructor_logs_no_warning_if_env_variable_JAVA_TOOL_OPTIONS_is_not_set() {
+ System2 system2 = Mockito.mock(System2.class);
+ when(system2.getenv(anyString())).thenReturn(null);
+ attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);
+
+ new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);
+
+ assertThat(listAppender.getLogs()).isEmpty();
+ }
+
+ @Test
+ public void constructor_logs_warning_if_env_variable_JAVA_TOOL_OPTIONS_is_set() {
+ System2 system2 = Mockito.mock(System2.class);
+ when(system2.getenv("JAVA_TOOL_OPTIONS")).thenReturn("sds");
+ attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);
+
+ new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);
+
+ assertThat(listAppender.getLogs())
+ .extracting(ILoggingEvent::getMessage)
+ .containsOnly(
+ "JAVA_TOOL_OPTIONS is defined but will be ignored. " +
+ "Use properties sonar.*.javaOpts and/or sonar.*.javaAdditionalOpts in sonar.properties to change SQ JVM processes options");
+ }
+
@Test
public void createEsCommand_throws_ISE_if_es_binary_is_not_found() throws Exception {
expectedException.expect(IllegalStateException.class);
@@ -86,6 +134,8 @@ public class CommandFactoryImplTest {
assertThat(esCommand.getLog4j2Properties())
.contains(entry("appender.file_es.fileName", new File(logsDir, "es.log").getAbsolutePath()));
+
+ assertThat(esCommand.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
}
@Test
@@ -132,6 +182,8 @@ public class CommandFactoryImplTest {
// default settings
.contains(entry("sonar.web.javaOpts", "-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError"))
.contains(entry("sonar.cluster.enabled", "false"));
+
+ assertThat(command.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
}
@Test
@@ -153,6 +205,8 @@ public class CommandFactoryImplTest {
// default settings
.contains(entry("sonar.web.javaOpts", "-Xmx10G"))
.contains(entry("sonar.cluster.enabled", "false"));
+
+ assertThat(command.getSuppressedEnvVariables()).containsOnly("JAVA_TOOL_OPTIONS");
}
@Test
@@ -181,6 +235,31 @@ public class CommandFactoryImplTest {
Props props = new Props(p);
ProcessProperties.completeDefaults(props);
- return new CommandFactoryImpl(props, tempDir);
+ return new CommandFactoryImpl(props, tempDir, System2.INSTANCE);
+ }
+
+ private <T> void attachMemoryAppenderToLoggerOf(Class<T> loggerClass) {
+ this.listAppender = new ListAppender();
+ new LogbackHelper().getRootContext().getLogger(loggerClass)
+ .addAppender(listAppender);
+ listAppender.start();
+ }
+
+ private static final class ListAppender extends AppenderBase<ILoggingEvent> {
+ private final List<ILoggingEvent> logs = new ArrayList<>();
+
+ @Override
+ public String getName() {
+ return MEMORY_APPENDER_NAME;
+ }
+
+ @Override
+ protected void append(ILoggingEvent eventObject) {
+ logs.add(eventObject);
+ }
+
+ public List<ILoggingEvent> getLogs() {
+ return logs;
+ }
}
}