From f39271bce5a3a175885d98ba3630932471b580c4 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 3 Jun 2013 16:32:53 +0200 Subject: [PATCH] SONAR-3755 Remove Action builder and replace it by Actions --- .../org/sonar/api/issue/action/Action.java | 60 +++++++------------ .../org/sonar/api/issue/action/Actions.java | 45 ++++++++++++++ .../sonar/api/issue/action/ActionTest.java | 36 ++++++----- .../org/sonar/server/issue/ActionService.java | 14 ++--- .../sonar/server/issue/ActionServiceTest.java | 30 ++++------ 5 files changed, 100 insertions(+), 85 deletions(-) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java index 6da2f1ecbc4..a797d20106c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java @@ -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 conditions; private final List 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 conditions() { return conditions; } + public Action setFunctions(Function... functions) { + this.functions.addAll(ImmutableList.copyOf(functions)); + return this; + } + public List 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 conditions = Lists.newArrayList(); - private List 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 index 00000000000..ae6ccb0e2cb --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java @@ -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 actions; + + public Actions() { + actions = newArrayList(); + } + + public Action add(String actionKey) { + Action action = new Action(actionKey); + this.actions.add(action); + return action; + } + + public List list() { + return actions; + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java index d961d134e21..d5a01d3ce6d 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/issue/action/ActionTest.java @@ -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"); } diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java b/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java index b8a4ac85fbe..a847cbaf7b2 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java @@ -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 actions; + private final Actions actions; - public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao, List 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.emptyList()); - } - public List listAvailableActions(final Issue issue) { - return newArrayList(Iterables.filter(actions, new Predicate() { + return newArrayList(Iterables.filter(actions.list(), new Predicate() { @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() { + return Iterables.find(actions.list(), new Predicate() { @Override public boolean apply(Action action) { return action.key().equals(actionKey); diff --git a/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java b/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java index c2d8eaa0400..8bbd125bcfe 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java @@ -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 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.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.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(); } -- 2.39.5