]> source.dussan.org Git - sonarqube.git/commitdiff
Fix test failing on Eclipse
authorDavid Gageot <david@gageot.net>
Mon, 2 Jul 2012 14:32:46 +0000 (16:32 +0200)
committerDavid Gageot <david@gageot.net>
Mon, 2 Jul 2012 14:43:37 +0000 (16:43 +0200)
sonar-server/infinitest.filters [deleted file]
sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java
sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java

diff --git a/sonar-server/infinitest.filters b/sonar-server/infinitest.filters
deleted file mode 100644 (file)
index 61f1f0b..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-# These tests fail in Eclipse. Until they are fixed, let's ignore them
-
-.*ServerImplTest
index 9a9735761f22d47b77984ab5cd970276718b19e0..dfec05e9002ab6acd56868a844aaa57c2b4defcf 100644 (file)
@@ -19,6 +19,7 @@
  */
 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;
@@ -37,20 +38,23 @@ public final class ServerImpl extends Server {
   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");
       }
@@ -80,7 +84,7 @@ public final class ServerImpl extends Server {
     return startedAt;
   }
 
-  String loadVersionFromManifest(String pomFilename) throws IOException {
+  private String loadVersionFromManifest(String pomFilename) throws IOException {
     InputStream pomFileStream = getClass().getResourceAsStream(pomFilename);
     try {
       return readVersion(pomFileStream);
index f4ad55d47ea4782689bf576fff46f708f44e3c8d..51a6df8c7fbf11de3f22d246403c14d1401a1ed2 100644 (file)
 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
@@ -71,7 +88,7 @@ public class ServerImplTest {
     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"));
   }