]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 Remove Action builder and replace it by Actions
authorJulien Lancelot <julien.lancelot@gmail.com>
Mon, 3 Jun 2013 14:32:53 +0000 (16:32 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Mon, 3 Jun 2013 14:32:53 +0000 (16:32 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java
sonar-server/src/main/java/org/sonar/server/issue/ActionService.java
sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java

index 6da2f1ecbc4824838321bba0a2bb9e3604bfda01..a797d20106c76db554af4b0e45e0ca8609ff2a3c 100644 (file)
@@ -21,34 +21,46 @@ package org.sonar.api.issue.action;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
-import com.google.common.collect.Lists;
-import org.sonar.api.ServerExtension;
+import com.google.common.collect.ImmutableList;
 import org.sonar.api.issue.Issue;
 import org.sonar.api.issue.condition.Condition;
 
-import java.util.Arrays;
 import java.util.List;
 
-public class Action implements ServerExtension {
+import static com.google.common.collect.Lists.newArrayList;
+
+public class Action {
 
   private final String key;
   private final List<Condition> conditions;
   private final List<Function> functions;
 
-  private Action(ActionBuilder builder) {
-    key = builder.key;
-    conditions = builder.conditions;
-    functions = builder.functions;
+  public Action(String key) {
+    this.key = key;
+    this.conditions = newArrayList();
+    this.functions = newArrayList();
+
+    Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Action key must be set");
   }
 
   public String key() {
     return key;
   }
 
+  public Action setConditions(Condition... conditions) {
+    this.conditions.addAll(ImmutableList.copyOf(conditions));
+    return this;
+  }
+
   public List<Condition> conditions() {
     return conditions;
   }
 
+  public Action setFunctions(Function... functions) {
+    this.functions.addAll(ImmutableList.copyOf(functions));
+    return this;
+  }
+
   public List<Function> functions() {
     return functions;
   }
@@ -87,36 +99,4 @@ public class Action implements ServerExtension {
     return key;
   }
 
-  public static Action create(String key) {
-    return builder(key).build();
-  }
-
-  public static ActionBuilder builder(String key) {
-    return new ActionBuilder(key);
-  }
-
-  public static class ActionBuilder {
-    private final String key;
-    private List<Condition> conditions = Lists.newArrayList();
-    private List<Function> functions = Lists.newArrayList();
-
-    private ActionBuilder(String key) {
-      this.key = key;
-    }
-
-    public ActionBuilder conditions(Condition... c) {
-      this.conditions.addAll(Arrays.asList(c));
-      return this;
-    }
-
-    public ActionBuilder functions(Function... f) {
-      this.functions.addAll(Arrays.asList(f));
-      return this;
-    }
-
-    public Action build() {
-      Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Action key must be set");
-      return new Action(this);
-    }
-  }
 }
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java
new file mode 100644 (file)
index 0000000..ae6ccb0
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.api.issue.action;
+
+import org.sonar.api.ServerExtension;
+
+import java.util.List;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+public class Actions implements ServerExtension {
+
+  private final List<Action> actions;
+
+  public Actions() {
+    actions = newArrayList();
+  }
+
+  public Action add(String actionKey) {
+    Action action = new Action(actionKey);
+    this.actions.add(action);
+    return action;
+  }
+
+  public List<Action> list() {
+    return actions;
+  }
+}
index d961d134e210f08f8c52c9df5a68457ed68b4035..d5a01d3ce6db6440817b540bcc99fba5469d0e42 100644 (file)
@@ -37,21 +37,20 @@ public class ActionTest {
   Function function2 = mock(Function.class);
 
   @Test
-  public void test_builder() throws Exception {
-    Action transition = Action.builder("link-to-jira")
-      .conditions(condition1, condition2)
-      .functions(function1, function2)
-      .build();
-    assertThat(transition.key()).isEqualTo("link-to-jira");
-    assertThat(transition.conditions()).containsOnly(condition1, condition2);
-    assertThat(transition.functions()).containsOnly(function1, function2);
+  public void test_action() throws Exception {
+    Action action = new Action("link-to-jira")
+      .setConditions(condition1, condition2)
+      .setFunctions(function1, function2);
+
+    assertThat(action.key()).isEqualTo("link-to-jira");
+    assertThat(action.conditions()).containsOnly(condition1, condition2);
+    assertThat(action.functions()).containsOnly(function1, function2);
   }
 
-
   @Test
   public void key_should_be_set() throws Exception {
     try {
-      Action.builder("").build();
+      new Action("");
       fail();
     } catch (Exception e) {
       assertThat(e).hasMessage("Action key must be set");
@@ -61,24 +60,23 @@ public class ActionTest {
   @Test
   public void should_verify_conditions() throws Exception {
     DefaultIssue issue = new DefaultIssue();
-    Action transition = Action.builder("link-to-jira")
-      .conditions(condition1, condition2)
-      .build();
+    Action action = new Action("link-to-jira")
+      .setConditions(condition1, condition2);
 
     when(condition1.matches(issue)).thenReturn(true);
     when(condition2.matches(issue)).thenReturn(false);
-    assertThat(transition.supports(issue)).isFalse();
+    assertThat(action.supports(issue)).isFalse();
 
     when(condition1.matches(issue)).thenReturn(true);
     when(condition2.matches(issue)).thenReturn(true);
-    assertThat(transition.supports(issue)).isTrue();
+    assertThat(action.supports(issue)).isTrue();
   }
 
   @Test
   public void test_equals_and_hashCode() throws Exception {
-    Action t1 = Action.create("link-to-jira");
-    Action t2 = Action.create("link-to-jira");
-    Action t3 = Action.create("comment");
+    Action t1 = new Action("link-to-jira");
+    Action t2 = new Action("link-to-jira");
+    Action t3 = new Action("comment");
 
     assertThat(t1).isEqualTo(t1);
     assertThat(t1).isEqualTo(t2);
@@ -89,7 +87,7 @@ public class ActionTest {
 
   @Test
   public void test_toString() throws Exception {
-    Action t1 = Action.create("link-to-jira");
+    Action t1 = new Action("link-to-jira");
     assertThat(t1.toString()).isEqualTo("link-to-jira");
   }
 
index b8a4ac85fbe9e6cb78c18c84a30813cb4dfccb31..a847cbaf7b2087d93ed6e08a8a9667d13e2f127b 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.issue.Issue;
 import org.sonar.api.issue.IssueQuery;
 import org.sonar.api.issue.IssueQueryResult;
 import org.sonar.api.issue.action.Action;
+import org.sonar.api.issue.action.Actions;
 import org.sonar.api.issue.action.Function;
 import org.sonar.api.issue.internal.DefaultIssue;
 import org.sonar.api.issue.internal.IssueChangeContext;
@@ -44,7 +45,6 @@ import org.sonar.server.user.UserSession;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
-import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
@@ -57,9 +57,9 @@ public class ActionService implements ServerComponent {
   private final IssueUpdater updater;
   private final Settings settings;
   private final PropertiesDao propertiesDao;
-  private final List<Action> actions;
+  private final Actions actions;
 
-  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao, List<Action> actions) {
+  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao, Actions actions) {
     this.finder = finder;
     this.issueStorage = issueStorage;
     this.updater = updater;
@@ -68,12 +68,8 @@ public class ActionService implements ServerComponent {
     this.actions = actions;
   }
 
-  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao) {
-    this(finder, issueStorage, updater, settings, propertiesDao, Collections.<Action>emptyList());
-  }
-
   public List<Action> listAvailableActions(final Issue issue) {
-    return newArrayList(Iterables.filter(actions, new Predicate<Action>() {
+    return newArrayList(Iterables.filter(actions.list(), new Predicate<Action>() {
       @Override
       public boolean apply(Action action) {
         return action.supports(issue);
@@ -124,7 +120,7 @@ public class ActionService implements ServerComponent {
 
   @CheckForNull
   private Action getAction(final String actionKey) {
-    return Iterables.find(actions, new Predicate<Action>() {
+    return Iterables.find(actions.list(), new Predicate<Action>() {
       @Override
       public boolean apply(Action action) {
         return action.key().equals(actionKey);
index c2d8eaa0400e14a88ca0fccb5aec93f7bfae2208..8bbd125bcfe125b920a2eb5aaffac61ccb3ee710 100644 (file)
@@ -27,7 +27,7 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.issue.Issue;
 import org.sonar.api.issue.IssueQuery;
 import org.sonar.api.issue.IssueQueryResult;
-import org.sonar.api.issue.action.Action;
+import org.sonar.api.issue.action.Actions;
 import org.sonar.api.issue.action.Function;
 import org.sonar.api.issue.condition.Condition;
 import org.sonar.api.issue.internal.DefaultIssue;
@@ -39,7 +39,6 @@ import org.sonar.core.properties.PropertiesDao;
 import org.sonar.server.user.UserSession;
 
 import java.util.Collections;
-import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
@@ -56,7 +55,7 @@ public class ActionServiceTest {
   private PropertiesDao propertiesDao;
   private Settings settings;
 
-  private List<Action> actions;
+  private Actions actions;
   private ActionService actionService;
 
   @Before
@@ -66,7 +65,7 @@ public class ActionServiceTest {
     updater = mock(IssueUpdater.class);
     propertiesDao = mock(PropertiesDao.class);
     settings = new Settings();
-    actions = newArrayList();
+    actions = new Actions();
     actionService = new ActionService(finder, issueStorage, updater, settings, propertiesDao, actions);
   }
 
@@ -83,7 +82,7 @@ public class ActionServiceTest {
     when(issueQueryResult.component(issue)).thenReturn(project);
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function1, function2).build());
+    actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function1, function2);
 
     assertThat(actionService.execute("ABCD", "link-to-jira", mock(UserSession.class))).isNotNull();
 
@@ -107,7 +106,7 @@ public class ActionServiceTest {
 
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build());
+    actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function);
     assertThat(actionService.execute("ABCD", "link-to-jira", userSession)).isNotNull();
 
     verify(updater).addComment(eq(issue), eq("New tweet on issue ABCD"), any(IssueChangeContext.class));
@@ -121,7 +120,7 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.<Issue>emptyList());
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build());
+    actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function);
     try {
     actionService.execute("ABCD", "link-to-jira", mock(UserSession.class));
     } catch (Exception e){
@@ -138,7 +137,7 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build());
+    actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function);
     try {
       actionService.execute("ABCD", "tweet", mock(UserSession.class));
     } catch (Exception e){
@@ -155,7 +154,7 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actions.add(Action.builder("link-to-jira").conditions(new NeverMatch()).functions(function).build());
+    actions.add("link-to-jira").setConditions(new NeverMatch()).setFunctions(function);
     try {
       actionService.execute("ABCD", "link-to-jira", mock(UserSession.class));
     } catch (Exception e){
@@ -170,11 +169,9 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    Action action1 = Action.builder("link-to-jira").conditions(new AlwaysMatch()).build();
-    actions.add(action1);
-    Action action2 = Action.builder("tweet").conditions(new NeverMatch()).build();
-    actions.add(action2);
-    assertThat(actionService.listAvailableActions("ABCD")).containsOnly(action1);
+    actions.add("link-to-jira").setConditions(new AlwaysMatch());
+    actions.add("tweet").setConditions(new NeverMatch());
+    assertThat(actionService.listAvailableActions("ABCD")).hasSize(1);
   }
 
   @Test
@@ -182,8 +179,8 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.<Issue>emptyList());
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).build());
-    actions.add(Action.builder("tweet").conditions(new NeverMatch()).build());
+    actions.add("link-to-jira").setConditions(new AlwaysMatch());
+    actions.add("tweet").setConditions(new NeverMatch());
     try {
       actionService.listAvailableActions("ABCD");
       fail();
@@ -198,7 +195,6 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actionService = new ActionService(finder, issueStorage, updater, settings, propertiesDao);
     assertThat(actionService.listAvailableActions("ABCD")).isEmpty();
   }