*/
package org.sonar.server.platform;
+import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
private String version;
private final Date startedAt;
private Settings settings;
+ private final String manifest;
public ServerImpl(Settings settings) {
- this(settings, new Date());
+ this(settings, "/META-INF/maven/org.codehaus.sonar/sonar-plugin-api/pom.properties");
}
- ServerImpl(Settings settings, Date startedAt) {
+ @VisibleForTesting
+ ServerImpl(Settings settings, String manifest) {
this.settings = settings;
- this.startedAt = startedAt;
+ this.startedAt = new Date();
+ this.manifest = manifest;
}
public void start() {
try {
id = new SimpleDateFormat("yyyyMMddHHmmss").format(startedAt);
- version = loadVersionFromManifest("/META-INF/maven/org.codehaus.sonar/sonar-plugin-api/pom.properties");
+ version = loadVersionFromManifest(manifest);
if (StringUtils.isBlank(version)) {
throw new ServerStartException("Unknown Sonar version");
}
return startedAt;
}
- String loadVersionFromManifest(String pomFilename) throws IOException {
+ private String loadVersionFromManifest(String pomFilename) throws IOException {
InputStream pomFileStream = getClass().getResourceAsStream(pomFilename);
try {
return readVersion(pomFileStream);
package org.sonar.server.platform;
import org.hamcrest.core.Is;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
-import java.io.IOException;
-import java.util.Date;
-
-import static org.junit.Assert.*;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assert.assertThat;
public class ServerImplTest {
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
@Test
public void alwaysReturnTheSameValues() {
- ServerImpl server = new ServerImpl(new Settings());
+ ServerImpl server = new ServerImpl(new Settings(), "/org/sonar/server/platform/ServerImplTest/pom-with-version.properties");
server.start();
- assertNotNull(server.getId());
- assertEquals(server.getId(), server.getId());
+ assertThat(server.getId()).isNotNull();
+ assertThat(server.getId()).isEqualTo(server.getId());
- assertNotNull(server.getVersion());
- assertEquals(server.getVersion(), server.getVersion());
+ assertThat(server.getVersion()).isNotNull();
+ assertThat(server.getVersion()).isEqualTo(server.getVersion());
- assertNotNull(server.getStartedAt());
- assertEquals(server.getStartedAt(), server.getStartedAt());
+ assertThat(server.getStartedAt()).isNotNull();
+ assertThat(server.getStartedAt()).isEqualTo(server.getStartedAt());
}
@Test
- public void getVersionFromFile() throws IOException {
- assertEquals("1.0", new ServerImpl(new Settings()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-with-version.properties"));
+ public void getVersionFromFile() {
+ ServerImpl server = new ServerImpl(new Settings(), "/org/sonar/server/platform/ServerImplTest/pom-with-version.properties");
+ server.start();
+
+ assertThat(server.getVersion()).isEqualTo("1.0");
}
@Test
- public void testFileWithNoVersion() throws IOException {
- assertEquals("", new ServerImpl(new Settings()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-without-version.properties"));
+ public void testFileWithNoVersion() {
+ exception.expect(ServerStartException.class);
+ exception.expectMessage("Unknown Sonar version");
+
+ ServerImpl server = new ServerImpl(new Settings(), "/org/sonar/server/platform/ServerImplTest/pom-without-version.properties");
+ server.start();
}
@Test
- public void testFileWithEmptyVersionParameter() throws IOException {
- assertEquals("", new ServerImpl(new Settings()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-with-empty-version.properties"));
+ public void testFileWithEmptyVersionParameter() {
+ exception.expect(ServerStartException.class);
+ exception.expectMessage("Unknown Sonar version");
+
+ ServerImpl server = new ServerImpl(new Settings(), "/org/sonar/server/platform/ServerImplTest/pom-with-empty-version.properties");
+ server.start();
}
@Test
- public void shouldNotFailIfFileNotFound() throws IOException {
- assertEquals("", new ServerImpl(new Settings()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/unknown-file.properties"));
+ public void shouldFailIfFileNotFound() {
+ exception.expect(ServerStartException.class);
+ exception.expectMessage("Unknown Sonar version");
+
+ ServerImpl server = new ServerImpl(new Settings(), "/org/sonar/server/platform/ServerImplTest/unknown-file.properties");
+ server.start();
}
@Test
Settings settings = new Settings();
settings.setProperty(CoreProperties.PERMANENT_SERVER_ID, "abcde");
- ServerImpl server = new ServerImpl(settings, new Date());
+ ServerImpl server = new ServerImpl(settings);
assertThat(server.getPermanentServerId(), Is.is("abcde"));
}