import java.util.HashMap;
import java.util.Map;
-public class MinimumViableSystem {
-
- private final Map<String, String> 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<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")) {
// 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));
}
}
*/
package org.sonar.process;
+import com.google.common.collect.ImmutableMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@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.");
}
@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());
public SearchServer(Props props) {
this.settings = new SearchSettings(props);
- new MinimumViableSystem().check();
+ new MinimumViableSystem()
+ .checkJavaVersion()
+ .checkWritableTempDir();
}
@Override
*/
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;
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);
}
}
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();