import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
+import java.net.URL;
class Env {
+ static final String ERROR_MESSAGE = "Do not copy-paste the configuration files (conf directory) from the old version. Update the content of the new files instead.";
private final File confFile;
- Env(File confFile) {
- this.confFile = confFile;
+ // visible for testing
+ Env(URL confUrl) throws URISyntaxException {
+ if (confUrl == null) {
+ throw new IllegalStateException(ERROR_MESSAGE);
+ }
+ this.confFile = new File(confUrl.toURI());
}
Env() throws URISyntaxException {
- this(new File(Env.class.getResource("/sonar.properties").toURI()));
+ this(Env.class.getResource("/sonar.properties"));
}
File rootDir() {
import java.io.File;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
public class EnvTest {
FileUtils.touch(confFile);
FileUtils.touch(logFile);
- Env env = new Env(confFile);
+ Env env = new Env(confFile.toURL());
assertThat(env.rootDir()).isDirectory().exists().isEqualTo(home);
assertThat(env.file("conf/sonar.properties")).isFile().exists().isEqualTo(confFile);
FileUtils.touch(confFile);
FileUtils.touch(logFile);
- Env env = new Env(confFile);
+ Env env = new Env(confFile.toURL());
File data = env.freshDir("data/h2");
assertThat(data).isDirectory().exists();
@Test
public void temp_dir_should_be_writable() throws Exception {
- new Env(temp.newFile()).verifyWritableTempDir();
+ new Env(temp.newFile().toURL()).verifyWritableTempDir();
// do not fail
}
+
+ @Test
+ public void fail_if_conf_file_not_found() throws Exception {
+ try {
+ // note that "new Env(null)" would be exact, but let's
+ // keep "new Env()" for increasing code coverage :-)
+ new Env();
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage(Env.ERROR_MESSAGE);
+ }
+ }
}
public TemporaryFolder temp = new TemporaryFolder();
@Before
- public void prepare_app() throws IOException {
+ public void prepare_app() throws Exception {
File confFile = new File("src/test/fake-app/conf/sonar.properties");
if (!confFile.exists()) {
confFile = new File("sonar-application/src/test/fake-app/conf/sonar.properties");
File rootDir = temp.newFolder();
FileUtils.copyDirectory(confFile.getParentFile().getParentFile(), rootDir);
- env = new Env(new File(rootDir, "conf/sonar.properties"));
+ env = new Env(new File(rootDir, "conf/sonar.properties").toURL());
starter = new StartServer(env);
}