Skip to content

Commit

Permalink
CHE-4516: Check command goal name uniqueness ignoring string case con…
Browse files Browse the repository at this point in the history
…siderations on new goal creation (eclipse-che#4656)

CHE-4516: Check command goal name uniqueness ignoring string case considerations on new goal creation
  • Loading branch information
azatsarynnyy authored Mar 31, 2017
1 parent c0e058e commit dba4d09
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public interface EditorMessages extends Messages {
@Key("page.goal.new_goal.button.create")
String pageGoalNewGoalButtonCreate();

@Key("page.goal.new_goal.already_exists.message")
String pageGoalNewGoalAlreadyExistsMessage(String newGoalName);

@Key("page.projects.title")
String pageProjectsTitle();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void edit(CommandImpl command) {
}

/**
* This method is called every time when command is opening in the editor.
* Called every time when command is opening in the editor.
* Typically, implementor should do initial setup of the page with the {@link #editedCommand}.
*/
protected abstract void initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.che.ide.command.editor.page.AbstractCommandEditorPage;
import org.eclipse.che.ide.command.editor.page.CommandEditorPage;

import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -36,7 +37,7 @@ public class GoalPage extends AbstractCommandEditorPage implements GoalPageView.
private final DialogFactory dialogFactory;

/** Initial value of the command's goal. */
private String goalInitial;
private String initialGoal;

@Inject
public GoalPage(GoalPageView view,
Expand All @@ -60,13 +61,10 @@ public IsWidget getView() {

@Override
protected void initialize() {
final String goalId = editedCommand.getGoal();
final CommandGoal goal = goalRegistry.getGoalForId(goalId);

goalInitial = goal.getId();
initialGoal = editedCommand.getGoal();

view.setAvailableGoals(goalRegistry.getAllGoals());
view.setGoal(goal.getId());
view.setGoal(initialGoal);
}

@Override
Expand All @@ -75,9 +73,7 @@ public boolean isDirty() {
return false;
}

CommandGoal goal = goalRegistry.getGoalForId(editedCommand.getGoal());

return !(goalInitial.equals(goal.getId()));
return !(initialGoal.equals(editedCommand.getGoal()));
}

@Override
Expand All @@ -88,24 +84,49 @@ public void onGoalChanged(String goalId) {

@Override
public void onCreateGoal() {
InputCallback inputCallback = value -> {
Set<CommandGoal> goals = goalRegistry.getAllGoals();
goals.add(goalRegistry.getGoalForId(value));
createGoal("");
}

view.setAvailableGoals(goals);
view.setGoal(value);
/** Asks user for the the new goal name nad creates it if another one with the same name doesn't exists. */
private void createGoal(String initialName) {
final InputCallback inputCallback = value -> {
final String newGoalName = value.trim();

editedCommand.setGoal(value);
notifyDirtyStateChanged();
final Set<CommandGoal> allGoals = goalRegistry.getAllGoals();

final Optional<CommandGoal> existingGoal = allGoals.stream()
.filter(goal -> goal.getId().equalsIgnoreCase(newGoalName))
.findAny();

if (existingGoal.isPresent()) {
dialogFactory.createMessageDialog(messages.pageGoalNewGoalTitle(),
messages.pageGoalNewGoalAlreadyExistsMessage(existingGoal.get().getId()),
() -> createGoal(newGoalName)).show();
} else {
setGoal(newGoalName);
}
};

dialogFactory.createInputDialog(messages.pageGoalNewGoalTitle(),
messages.pageGoalNewGoalLabel(),
"",
0,
initialName,
0,
initialName.length(),
messages.pageGoalNewGoalButtonCreate(),
inputCallback,
null).show();
}

/** Set the specified goal name for the currently edited command. */
private void setGoal(String goalName) {
editedCommand.setGoal(goalName);

Set<CommandGoal> allGoals = goalRegistry.getAllGoals();
allGoals.add(goalRegistry.getGoalForId(goalName));

view.setAvailableGoals(allGoals);
view.setGoal(goalName);

notifyDirtyStateChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ page.goal.title=Goal
page.goal.new_goal.title=New command goal
page.goal.new_goal.label=Goal name:
page.goal.new_goal.button.create=Create
page.goal.new_goal.already_exists.message=Goal with name \"{0}\" already exists.

page.projects.title=Apply to
page.projects.table.header.project.label=Project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.Map;

import static org.eclipse.che.api.workspace.shared.Constants.COMMAND_GOAL_ATTRIBUTE_NAME;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
Expand Down Expand Up @@ -75,10 +71,7 @@ public void setUp() throws Exception {
when(goalRegistry.getGoalForId(anyString())).thenReturn(goal);

when(editedCommand.getApplicableContext()).thenReturn(editedCommandApplicableContext);

Map<String, String> attributes = new HashMap<>();
attributes.put(COMMAND_GOAL_ATTRIBUTE_NAME, COMMAND_GOAL_ID);
when(editedCommand.getAttributes()).thenReturn(attributes);
when(editedCommand.getGoal()).thenReturn(COMMAND_GOAL_ID);

page.setDirtyStateListener(dirtyStateListener);
page.edit(editedCommand);
Expand Down Expand Up @@ -110,6 +103,7 @@ public void shouldNotifyListenerWhenGoalChanged() throws Exception {

@Test
public void shouldCreateGoal() throws Exception {
// given
InputDialog inputDialog = mock(InputDialog.class);
when(dialogFactory.createInputDialog(anyString(),
anyString(),
Expand All @@ -121,8 +115,10 @@ public void shouldCreateGoal() throws Exception {
any(CancelCallback.class))).thenReturn(inputDialog);
String newGoalId = "new goal";

// when
page.onCreateGoal();

// then
ArgumentCaptor<InputCallback> inputCaptor = ArgumentCaptor.forClass(InputCallback.class);
verify(dialogFactory).createInputDialog(anyString(),
anyString(),
Expand Down

0 comments on commit dba4d09

Please sign in to comment.