Automatic merge from branch-5.3

* origin/branch-5.3:
  SONAR-7141 Fail fast if server started with Java 1.6
  SONAR-7140 Do not check java.io.tmpdir in bootstrap process
  SONAR-6905 Batch should dump analysis-related URL into a properties file
This commit is contained in:
Jenkins CI 2015-12-14 22:20:07 +01:00
commit f63a3ab73d
7 changed files with 36 additions and 59 deletions

View File

@ -27,59 +27,50 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static java.lang.String.format;
public class MinimumViableSystem {
private final Map<String, String> requiredJavaOptions = new HashMap<>();
public MinimumViableSystem setRequiredJavaOption(String propertyKey, String expectedValue) {
requiredJavaOptions.put(propertyKey, expectedValue);
return this;
}
/**
* Entry point for all checks
*/
public void check() {
checkJavaVersion();
checkJavaOptions();
checkWritableTempDir();
}
/**
* Verify that temp directory is writable
*/
private void checkWritableTempDir() {
public MinimumViableSystem checkWritableTempDir() {
checkWritableDir(System.getProperty("java.io.tmpdir"));
return this;
}
// Visible for testing
void checkWritableDir(String tempPath) {
try {
File tempFile = File.createTempFile("check", "tmp", new File(tempPath));
FileUtils.deleteQuietly(tempFile);
} catch (IOException e) {
throw new IllegalStateException(String.format("Temp directory is not writable: %s", tempPath), e);
throw new IllegalStateException(format("Temp directory is not writable: %s", tempPath), e);
}
}
void checkJavaOptions() {
public MinimumViableSystem checkRequiredJavaOptions(Map<String, String> requiredJavaOptions) {
for (Map.Entry<String, String> entry : requiredJavaOptions.entrySet()) {
String value = System.getProperty(entry.getKey());
if (!StringUtils.equals(value, entry.getValue())) {
throw new MessageException(String.format(
throw new MessageException(format(
"JVM option '%s' must be set to '%s'. Got '%s'", entry.getKey(), entry.getValue(), StringUtils.defaultString(value)));
}
}
return this;
}
void checkJavaVersion() {
public MinimumViableSystem checkJavaVersion() {
String javaVersion = System.getProperty("java.specification.version");
checkJavaVersion(javaVersion);
return this;
}
// Visible for testing
void checkJavaVersion(String javaVersion) {
if (!javaVersion.startsWith("1.6") && !javaVersion.startsWith("1.7") && !javaVersion.startsWith("1.8")) {
if (!javaVersion.startsWith("1.7") && !javaVersion.startsWith("1.8")) {
// still better than "java.lang.UnsupportedClassVersionError: Unsupported major.minor version 49.0
throw new MessageException(String.format("Supported versions of Java are 1.6, 1.7 and 1.8. Got %s.", javaVersion));
throw new MessageException(format("Supported versions of Java are 1.7 and 1.8. Got %s.", javaVersion));
}
}

View File

@ -19,6 +19,7 @@
*/
package org.sonar.process;
import com.google.common.collect.ImmutableMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@ -33,67 +34,48 @@ public class MinimumViableSystemTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
/**
* Verifies that all checks can be verified without error.
* Test environment does not necessarily follows all checks.
*/
@Test
public void check() {
MinimumViableSystem mve = new MinimumViableSystem();
try {
mve.check();
// ok
} catch (MessageException e) {
// also ok. All other exceptions are errors.
}
}
MinimumViableSystem underTest = new MinimumViableSystem();
@Test
public void checkJavaVersion() {
MinimumViableSystem mve = new MinimumViableSystem();
// yes, sources are compiled with a supported Java version!
mve.checkJavaVersion();
mve.checkJavaVersion("1.6");
underTest.checkJavaVersion();
underTest.checkJavaVersion("1.7");
try {
mve.checkJavaVersion("1.9");
underTest.checkJavaVersion("1.6");
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("Supported versions of Java are 1.6, 1.7 and 1.8. Got 1.9.");
assertThat(e).hasMessage("Supported versions of Java are 1.7 and 1.8. Got 1.6.");
}
}
@Test
public void checkJavaOption() {
public void checkRequiredJavaOptions() {
String key = "MinimumViableEnvironmentTest.test.prop";
MinimumViableSystem mve = new MinimumViableSystem()
.setRequiredJavaOption(key, "true");
try {
System.setProperty(key, "false");
mve.checkJavaOptions();
underTest.checkRequiredJavaOptions(ImmutableMap.of(key, "true"));
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("JVM option '" + key + "' must be set to 'true'. Got 'false'");
}
System.setProperty(key, "true");
mve.checkJavaOptions();
// do not fail
underTest.checkRequiredJavaOptions(ImmutableMap.of(key, "true"));
}
@Test
public void checkWritableTempDir() throws Exception {
File dir = temp.newFolder();
MinimumViableSystem mve = new MinimumViableSystem();
mve.checkWritableDir(dir.getAbsolutePath());
underTest.checkWritableDir(dir.getAbsolutePath());
dir.delete();
try {
mve.checkWritableDir(dir.getAbsolutePath());
underTest.checkWritableDir(dir.getAbsolutePath());
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Temp directory is not writable: " + dir.getAbsolutePath());

View File

@ -34,7 +34,9 @@ public class SearchServer implements Monitored {
public SearchServer(Props props) {
this.settings = new SearchSettings(props);
new MinimumViableSystem().check();
new MinimumViableSystem()
.checkJavaVersion()
.checkWritableTempDir();
}
@Override

View File

@ -19,6 +19,7 @@
*/
package org.sonar.server.app;
import com.google.common.collect.ImmutableMap;
import org.sonar.process.MinimumViableSystem;
import org.sonar.process.Monitored;
import org.sonar.process.ProcessEntryPoint;
@ -30,8 +31,9 @@ public class WebServer implements Monitored {
WebServer(Props props) throws Exception {
new MinimumViableSystem()
.setRequiredJavaOption("file.encoding", "UTF-8")
.check();
.checkJavaVersion()
.checkWritableTempDir()
.checkRequiredJavaOptions(ImmutableMap.of("file.encoding", "UTF-8"));
this.tomcat = new EmbeddedTomcat(props);
}

View File

@ -109,7 +109,7 @@ public class App implements Stoppable {
}
public static void main(String[] args) throws Exception {
new MinimumViableSystem().check();
new MinimumViableSystem().checkJavaVersion();
CommandLineParser cli = new CommandLineParser();
Properties rawProperties = cli.parseArguments(args);
Props props = new PropsBuilder(rawProperties, new JdbcSettings()).build();

View File

@ -224,6 +224,6 @@ public class ReportPublisher implements Startable {
// If server base URL was not configured in Sonar server then is is better to take URL configured on batch side
baseUrl = wsClient.baseUrl();
}
return baseUrl.replaceAll("(/)+$", "") + "/";
return baseUrl.replaceAll("(/)+$", "");
}
}

View File

@ -81,7 +81,7 @@ public class ReportPublisherTest {
File detailsFile = new File(temp.getRoot(), "report-task.txt");
assertThat(readFileToString(detailsFile)).isEqualTo(
"projectKey=struts\n" +
"serverUrl=https://localhost/\n" +
"serverUrl=https://localhost\n" +
"dashboardUrl=https://localhost/dashboard/index/struts\n" +
"ceTaskId=TASK-123\n" +
"ceTaskUrl=https://localhost/api/ce/task?id=TASK-123\n"
@ -102,7 +102,7 @@ public class ReportPublisherTest {
File detailsFile = new File(temp.getRoot(), "report-task.txt");
assertThat(readFileToString(detailsFile)).isEqualTo(
"projectKey=struts\n" +
"serverUrl=https://publicserver/sonarqube/\n" +
"serverUrl=https://publicserver/sonarqube\n" +
"dashboardUrl=https://publicserver/sonarqube/dashboard/index/struts\n" +
"ceTaskId=TASK-123\n" +
"ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n"