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;
}
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);
- }
- }
}
--- /dev/null
+/*
+ * 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;
+ }
+}
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");
@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);
@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");
}
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;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import java.util.Collections;
import java.util.Date;
import java.util.List;
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;
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);
@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);
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;
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;
private PropertiesDao propertiesDao;
private Settings settings;
- private List<Action> actions;
+ private Actions actions;
private ActionService actionService;
@Before
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);
}
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();
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));
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){
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){
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){
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
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();
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();
}