From cf62a834433c8629932d678fb97a65bc9004a57e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 14 Dec 2015 13:37:59 +0100 Subject: SONAR-7140 Do not check java.io.tmpdir in bootstrap process --- .../org/sonar/process/MinimumViableSystem.java | 35 ++++++++------------ .../org/sonar/process/MinimumViableSystemTest.java | 38 ++++++---------------- 2 files changed, 23 insertions(+), 50 deletions(-) (limited to 'server/sonar-process') diff --git a/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java b/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java index fdbc7217611..9a166fdaed7 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java +++ b/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java @@ -27,59 +27,50 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -public class MinimumViableSystem { - - private final Map requiredJavaOptions = new HashMap<>(); - - public MinimumViableSystem setRequiredJavaOption(String propertyKey, String expectedValue) { - requiredJavaOptions.put(propertyKey, expectedValue); - return this; - } +import static java.lang.String.format; - /** - * Entry point for all checks - */ - public void check() { - checkJavaVersion(); - checkJavaOptions(); - checkWritableTempDir(); - } +public class MinimumViableSystem { /** * 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 requiredJavaOptions) { for (Map.Entry 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")) { // 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.6, 1.7 and 1.8. Got %s.", javaVersion)); } } diff --git a/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java b/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java index 88e813f4fbb..a3b3f582ee7 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java @@ -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,32 +34,16 @@ 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.6"); try { - mve.checkJavaVersion("1.9"); + underTest.checkJavaVersion("1.9"); fail(); } catch (MessageException e) { assertThat(e).hasMessage("Supported versions of Java are 1.6, 1.7 and 1.8. Got 1.9."); @@ -66,34 +51,31 @@ public class MinimumViableSystemTest { } @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()); -- cgit v1.2.3 From 6a13e5f6ed2da5101d33b84849594814351c379a Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 14 Dec 2015 14:25:14 +0100 Subject: SONAR-7141 Fail fast if server started with Java 1.6 --- .../src/main/java/org/sonar/process/MinimumViableSystem.java | 4 ++-- .../src/test/java/org/sonar/process/MinimumViableSystemTest.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'server/sonar-process') diff --git a/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java b/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java index 9a166fdaed7..4b182468fb3 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java +++ b/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java @@ -68,9 +68,9 @@ public class MinimumViableSystem { // 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(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)); } } diff --git a/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java b/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java index a3b3f582ee7..291862930be 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java @@ -40,13 +40,13 @@ public class MinimumViableSystemTest { public void checkJavaVersion() { // yes, sources are compiled with a supported Java version! underTest.checkJavaVersion(); - underTest.checkJavaVersion("1.6"); + underTest.checkJavaVersion("1.7"); try { - underTest.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."); } } -- cgit v1.2.3