]> source.dussan.org Git - sonarqube.git/commitdiff
Refactor org.sonar.batch.bootstrapper.Batch
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 16 Jan 2012 11:14:50 +0000 (12:14 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 16 Jan 2012 11:15:13 +0000 (12:15 +0100)
sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java
sonar-batch/src/test/java/org/sonar/batch/bootstrapper/BatchTest.java

index 3aaeb3f8993b25f9c7a0f3a6acf8cdb676b1b335..7c05398b27d1488f004ebb43fca13e30398d54ec 100644 (file)
@@ -44,16 +44,9 @@ public final class Batch {
     components.addAll(builder.components);
     components.add(builder.environment);
     projectReactor = builder.projectReactor;
-    logging = LoggingConfiguration.create().setProperties((Map) projectReactor.getRoot().getProperties());
-  }
-
-  /**
-   * Disable the internal configuration of Logback. In this case the batch bootstrapper must provide its
-   * own implementation of SLF4J.
-   */
-  public Batch disableLoggingConfiguration() {
-    this.logging = null;
-    return this;
+    if (builder.isEnableLoggingConfiguration()) {
+      logging = LoggingConfiguration.create().setProperties((Map) projectReactor.getRoot().getProperties());
+    }
   }
 
   public LoggingConfiguration getLoggingConfiguration() {
@@ -94,32 +87,21 @@ public final class Batch {
     private ProjectReactor projectReactor;
     private EnvironmentInformation environment;
     private List components = Lists.newArrayList();
+    private boolean enableLoggingConfiguration = true;
 
     private Builder() {
     }
 
-    public ProjectReactor getProjectReactor() {
-      return projectReactor;
-    }
-
     public Builder setProjectReactor(ProjectReactor projectReactor) {
       this.projectReactor = projectReactor;
       return this;
     }
 
-    public EnvironmentInformation getEnvironment() {
-      return environment;
-    }
-
     public Builder setEnvironment(EnvironmentInformation env) {
       this.environment = env;
       return this;
     }
 
-    public List getComponents() {
-      return components;
-    }
-
     public Builder setComponents(List l) {
       this.components = l;
       return this;
@@ -135,15 +117,28 @@ public final class Batch {
       return this;
     }
 
+    public boolean isEnableLoggingConfiguration() {
+      return enableLoggingConfiguration;
+    }
+
+    /**
+     * Logback is configured by default. It can be disabled, but n this case the batch bootstrapper must provide its
+     * own implementation of SLF4J.
+     */
+    public Builder setEnableLoggingConfiguration(boolean b) {
+      this.enableLoggingConfiguration = b;
+      return this;
+    }
+
     public Batch build() {
       if (projectReactor == null) {
-        throw new IllegalArgumentException("ProjectReactor is not set");
+        throw new IllegalStateException("ProjectReactor is not set");
       }
       if (environment == null) {
-        throw new IllegalArgumentException("EnvironmentInfo is not set");
+        throw new IllegalStateException("EnvironmentInfo is not set");
       }
       if (components == null) {
-        throw new IllegalArgumentException("Batch components are not set");
+        throw new IllegalStateException("Batch components are not set");
       }
       return new Batch(this);
     }
index c1718c1e56cb9d59b851c203d1c1292fa080cc2d..eeb52de29187b2a26ca54fc966b84c8c21027845 100644 (file)
@@ -20,7 +20,7 @@
 package org.sonar.batch.bootstrapper;
 
 import org.junit.Test;
-import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.batch.bootstrap.*;
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -31,6 +31,7 @@ public class BatchTest {
   public void testBuilder() {
     Batch batch = newBatch();
     assertNotNull(batch);
+
   }
 
   private Batch newBatch() {
@@ -41,7 +42,7 @@ public class BatchTest {
       .build();
   }
 
-  @Test(expected = IllegalArgumentException.class)
+  @Test(expected = IllegalStateException.class)
   public void shouldFailIfNoEnvironment() {
     Batch.builder()
       .setProjectReactor(new ProjectReactor(org.sonar.api.batch.bootstrap.ProjectDefinition.create()))
@@ -49,7 +50,7 @@ public class BatchTest {
       .build();
   }
 
-  @Test(expected = IllegalArgumentException.class)
+  @Test(expected = IllegalStateException.class)
   public void shouldFailIfNoProjectReactor() {
     Batch.builder()
       .setEnvironment(new EnvironmentInformation("Gradle", "1.0"))
@@ -57,7 +58,7 @@ public class BatchTest {
       .build();
   }
 
-  @Test(expected = IllegalArgumentException.class)
+  @Test(expected = IllegalStateException.class)
   public void shouldFailIfNullComponents() {
     Batch.builder()
       .setProjectReactor(new ProjectReactor(org.sonar.api.batch.bootstrap.ProjectDefinition.create()))
@@ -68,8 +69,12 @@ public class BatchTest {
 
   @Test
   public void shouldDisableLoggingConfiguration() {
-    Batch batch = newBatch();
-    batch.disableLoggingConfiguration();
+    Batch batch = Batch.builder()
+      .setEnvironment(new EnvironmentInformation("Gradle", "1.0"))
+      .setProjectReactor(new ProjectReactor(org.sonar.api.batch.bootstrap.ProjectDefinition.create()))
+      .addComponent("fake")
+      .setEnableLoggingConfiguration(false)
+      .build();
     assertNull(batch.getLoggingConfiguration());
   }