Commit a46341b3 authored by Romain Bioteau's avatar Romain Bioteau
Browse files

Update EditingSupport for constract input and constraints to use

Databinding (with validation in update strategy).

Remove the ContractValidator

When validating, only checks each constraint dependencies
parent 19c842e1
......@@ -23,34 +23,34 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class Status2MessageSeverityTest {
public class StatusToMessageSeverityTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void should_convert_error_status_severity_to_error_message_severity() throws Exception {
assertThat(new Status2MessageSeverity(ValidationStatus.error("an error message")).toMessageSeverity()).isEqualTo(IMessage.ERROR);
assertThat(new StatusToMessageSeverity(ValidationStatus.error("an error message")).toMessageSeverity()).isEqualTo(IMessage.ERROR);
}
@Test
public void should_convert_warning_status_severity_to_warning_message_severity() throws Exception {
assertThat(new Status2MessageSeverity(ValidationStatus.warning("a warning message")).toMessageSeverity()).isEqualTo(IMessage.WARNING);
assertThat(new StatusToMessageSeverity(ValidationStatus.warning("a warning message")).toMessageSeverity()).isEqualTo(IMessage.WARNING);
}
@Test
public void should_convert_info_status_severity_to_info_message_severity() throws Exception {
assertThat(new Status2MessageSeverity(ValidationStatus.info("an info message")).toMessageSeverity()).isEqualTo(IMessage.INFORMATION);
assertThat(new StatusToMessageSeverity(ValidationStatus.info("an info message")).toMessageSeverity()).isEqualTo(IMessage.INFORMATION);
}
@Test
public void should_convert_ok_status_severity_to_none_message_severity() throws Exception {
assertThat(new Status2MessageSeverity(Status.OK_STATUS).toMessageSeverity()).isEqualTo(IMessage.NONE);
assertThat(new StatusToMessageSeverity(Status.OK_STATUS).toMessageSeverity()).isEqualTo(IMessage.NONE);
}
@Test
public void should_throw_an_IllegalArgumentException_if_severity_code_is_unknown() throws Exception {
final Status2MessageSeverity status2MessageSeverity = new Status2MessageSeverity(Status.CANCEL_STATUS);
final StatusToMessageSeverity status2MessageSeverity = new StatusToMessageSeverity(Status.CANCEL_STATUS);
thrown.expect(IllegalArgumentException.class);
......
......@@ -158,7 +158,6 @@ public class NamingUtils {
}
}
return max;
}
public static String convertToId(final Element item) {
......
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
......@@ -48,6 +48,7 @@ public abstract class CustomTextEMFObservableValueEditingSupport extends Observa
private TextCellEditor cellEditor;
private final IMessageManager messageManager;
private String controlId;
private EObject element;
public CustomTextEMFObservableValueEditingSupport(final ColumnViewer viewer, final EStructuralFeature featureToEdit,
final IMessageManager messageManager, final DataBindingContext dbc) {
......@@ -78,7 +79,8 @@ public abstract class CustomTextEMFObservableValueEditingSupport extends Observa
@Override
protected IObservableValue doCreateElementObservable(final Object element, final ViewerCell cell) {
checkArgument(element instanceof EObject);
final IObservableValue observableValue = EMFEditObservables.observeValue(TransactionUtil.getEditingDomain(element), (EObject) element,
this.element = (EObject) element;
final IObservableValue observableValue = EMFEditObservables.observeValue(TransactionUtil.getEditingDomain(element), this.element,
featureToEdit);
observableValue.addValueChangeListener(new ColumnViewerUpdateListener(getViewer(), element));
return observableValue;
......@@ -91,7 +93,7 @@ public abstract class CustomTextEMFObservableValueEditingSupport extends Observa
*/
@Override
protected Binding createBinding(final IObservableValue target, final IObservableValue model) {
final Binding binding = dbc.bindValue(target, model, taregtToModelConvertStrategy(), null);
final Binding binding = dbc.bindValue(target, model, targetToModelConvertStrategy(element), null);
final IObservableValue validationStatus = binding.getValidationStatus();
validationStatus.addValueChangeListener(new IValueChangeListener() {
......@@ -117,13 +119,13 @@ public abstract class CustomTextEMFObservableValueEditingSupport extends Observa
}
}
protected abstract UpdateValueStrategy taregtToModelConvertStrategy();
protected abstract UpdateValueStrategy targetToModelConvertStrategy(EObject element);
protected void validationStatusChanged(final IStatus status) {
updateTextEditorFeedback(status);
messageManager.removeAllMessages();
if (!status.isOK()) {
messageManager.addMessage("", status.getMessage(), null, new Status2MessageSeverity(status).toMessageSeverity());
messageManager.addMessage("", status.getMessage(), null, new StatusToMessageSeverity(status).toMessageSeverity());
}
}
......@@ -133,4 +135,5 @@ public abstract class CustomTextEMFObservableValueEditingSupport extends Observa
cellEditor.getControl().setData(SWTBotConstants.SWTBOT_WIDGET_ID_KEY, controlId);
return cellEditor;
}
}
......@@ -17,11 +17,11 @@ package org.bonitasoft.studio.common.jface.databinding;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.ui.forms.IMessage;
public class Status2MessageSeverity {
public class StatusToMessageSeverity {
private final IStatus status;
public Status2MessageSeverity(final IStatus status) {
public StatusToMessageSeverity(final IStatus status) {
this.status = status;
}
......
......@@ -30,7 +30,7 @@ down=Down
expression=Expression
errorMessage=Error message
noInputReferencedInConstraintExpression=No input is referenced in the expression of constraint: {0}
unknownInputReferencedInConstraintExpression=Unknown input \"{0}\" is referenced in the expression of constraint: {1}
unknownInputReferencedInConstraintExpression=Unknown input \"{0}\" is referenced in the expression of {1}
constraintEditorDescription=Type the constraint using MVEL scripting.\nUse Ctrl+Space fot content assist.
addContentToConstraint=Add content to constraint {0}
editContentToConstraint=Edit content to constraint {0}
......
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.contract.core.validation;
import static org.assertj.core.api.Assertions.assertThat;
import org.bonitasoft.studio.model.process.Contract;
import org.bonitasoft.studio.model.process.ContractConstraint;
import org.bonitasoft.studio.model.process.ProcessFactory;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.core.runtime.IStatus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author Romain Bioteau
*
*/
public class ContractConstraintDuplicationValidationRuleTest {
private ContractConstraintDuplicationValidationRule validationRule;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
validationRule = new ContractConstraintDuplicationValidationRule();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Test
public void should_appliesTo_contract() throws Exception {
assertThat(validationRule.appliesTo(ProcessFactory.eINSTANCE.createContractConstraint())).isFalse();
assertThat(validationRule.appliesTo(ProcessFactory.eINSTANCE.createContract())).isTrue();
}
@Test
public void should_validate_contract_return_a_valid_status_if_no_duplicated_constraint() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractConstraint constraint = ProcessFactory.eINSTANCE.createContractConstraint();
constraint.setName("legal age");
final ContractConstraint constraint2 = ProcessFactory.eINSTANCE.createContractConstraint();
constraint2.setName("name length");
final ContractConstraint constraint3 = ProcessFactory.eINSTANCE.createContractConstraint();
constraint3.setName("");
final ContractConstraint constraint4 = ProcessFactory.eINSTANCE.createContractConstraint();
contract.getConstraints().add(constraint);
contract.getConstraints().add(constraint2);
contract.getConstraints().add(constraint3);
contract.getConstraints().add(constraint4);
assertThat(validationRule.validate(contract).isOK()).isTrue();
}
@Test
public void should_validate_contract_return_an_error_status_if_duplicated_constraint() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractConstraint constraint = ProcessFactory.eINSTANCE.createContractConstraint();
constraint.setName("legal age");
contract.getConstraints().add(constraint);
final ContractConstraint constraint2 = ProcessFactory.eINSTANCE.createContractConstraint();
constraint2.setName("legal age");
contract.getConstraints().add(constraint2);
final IStatus status = validationRule.validate(contract);
assertThat(status.isOK()).isFalse();
assertThat(validationRule.getMessage(status)).isNotEmpty();
}
@Test
public void should_getMessage_return_status_message() throws Exception {
assertThat(validationRule.getMessage(ValidationStatus.ok())).isNotEmpty();
}
@Test(expected = AssertionFailedException.class)
public void should_getMessage_thrown_IllegalArgumentException() throws Exception {
validationRule.getMessage(null);
}
}
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
......@@ -18,57 +16,54 @@ package org.bonitasoft.studio.contract.core.validation;
import static org.assertj.core.api.Assertions.assertThat;
import org.bonitasoft.studio.contract.core.ContractConstraintUtil;
import org.bonitasoft.studio.model.process.Contract;
import org.bonitasoft.studio.model.process.ContractConstraint;
import org.bonitasoft.studio.model.process.ContractInput;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.bonitasoft.studio.model.process.ProcessFactory;
import org.eclipse.core.runtime.IStatus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author Romain Bioteau
*
*/
public class ContractInputDescriptionValidationRuleTest {
public class ContractConstraintInputValidatorTest {
private ContractInputDescriptionValidationRule validationRule;
private ContractConstraintInputValidator validator;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
validationRule = new ContractInputDescriptionValidationRule();
validator = new ContractConstraintInputValidator();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
@Test
public void should_validate_returns_a_valid_status() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractInput input = addInput(contract, "name", ContractInputType.TEXT, null);
final ContractConstraint constraint = ContractConstraintUtil.createConstraint("name length", "", "", input);
contract.getConstraints().add(constraint);
assertThat(validator.validate(constraint).isOK()).isTrue();
}
@Test
public void should_validate_a_contract_input_returns_an_error_status_for_input_with_too_long_description() throws Exception {
public void should_validate_returns_an_error_status_for_empty_inputs() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractInput contractInput = addInput(
contract,
"name",
ContractInputType.TEXT,
"kjdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddfgdfgdfkgdfkjghdfkjghdfkjghdfkjghdfkjghdfkjghdfkjghurtyuizeryueiz ueizryueizry ueizryezuirydddddddddddddddddddddddddddddddddddddddddfgdfgdfghdfkjghkjdfhgkjdfhgkjdfghkjdfghkjdfghkjhkjhkjdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
final IStatus status = validationRule.validate(contractInput);
assertThat(status.isOK()).isFalse();
final ContractConstraint constraint = ContractConstraintUtil.createConstraint("name length", "", "");
contract.getConstraints().add(constraint);
assertThat(validator.validate(constraint).isOK()).isFalse();
}
@Test
public void should_applies_to_contract_input() throws Exception {
public void should_validate_returns_an_error_status_for_unknown_inputs() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractInput contractInput = addInput(contract, "", ContractInputType.TEXT, null);
assertThat(validationRule.appliesTo(contractInput)).isTrue();
assertThat(validationRule.appliesTo(contract)).isFalse();
final ContractConstraint constraint = ContractConstraintUtil.createConstraint("name length", "", "");
constraint.getInputNames().add("unknownInput");
contract.getConstraints().add(constraint);
assertThat(validator.validate(constraint).isOK()).isFalse();
}
private ContractInput addInput(final Contract contract, final String inputName, final ContractInputType type, final String description) {
......
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.contract.core.validation;
import static org.assertj.core.api.Assertions.assertThat;
import org.bonitasoft.studio.contract.ContractPlugin;
import org.bonitasoft.studio.contract.core.ContractConstraintUtil;
import org.bonitasoft.studio.model.process.Contract;
import org.bonitasoft.studio.model.process.ContractConstraint;
import org.bonitasoft.studio.model.process.ContractInput;
import org.bonitasoft.studio.model.process.ContractInputType;
import org.bonitasoft.studio.model.process.ProcessFactory;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author Romain Bioteau
*
*/
public class ContractConstraintInputsValidationRuleTest {
private ContractConstraintInputsValidationRule validationRule;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
validationRule = new ContractConstraintInputsValidationRule();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Test
public void should_appliesTo_Contract_Constraint_with_an_expression() throws Exception {
final ContractConstraint contractConstraint = ProcessFactory.eINSTANCE.createContractConstraint();
assertThat(validationRule.appliesTo(contractConstraint)).isFalse();
contractConstraint.setExpression("");
assertThat(validationRule.appliesTo(contractConstraint)).isTrue();
assertThat(validationRule.appliesTo(ProcessFactory.eINSTANCE.createContract())).isFalse();
}
@Test
public void should_validate_returns_a_valid_status() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractInput input = addInput(contract, "name", ContractInputType.TEXT, null);
final ContractConstraint constraint = ContractConstraintUtil.createConstraint("name length", "", "", input);
contract.getConstraints().add(constraint);
assertThat(validationRule.validate(constraint).isOK()).isTrue();
}
@Test
public void should_validate_returns_an_error_status_for_empty_inputs() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractConstraint constraint = ContractConstraintUtil.createConstraint("name length", "", "");
contract.getConstraints().add(constraint);
assertThat(validationRule.validate(constraint).isOK()).isFalse();
}
@Test
public void should_validate_returns_an_error_status_for_unknown_inputs() throws Exception {
final Contract contract = ProcessFactory.eINSTANCE.createContract();
final ContractConstraint constraint = ContractConstraintUtil.createConstraint("name length", "", "");
constraint.getInputNames().add("unknownInput");
contract.getConstraints().add(constraint);
assertThat(validationRule.validate(constraint).isOK()).isFalse();
}
@Test
public void should_rule_id_be_set() throws Exception {
assertThat(validationRule.getRuleId()).isEqualTo(ContractConstraintInputsValidationRule.CONSTRAINT_INPUTS_ID);
}
@Test
public void should_getMessage_return_status_message_for_single_status() throws Exception {
final IStatus status = ValidationStatus.error("a single status error message");
assertThat(validationRule.getMessage(status)).isEqualTo(status.getMessage());
}
@Test
public void should_getMessage_return_status_message_for_empty_multi_status() throws Exception {
final MultiStatus status = new MultiStatus(ContractPlugin.PLUGIN_ID, 0, "", null);
assertThat(validationRule.getMessage(status)).isEmpty();
}
@Test
public void should_getMessage_return_aggregated_status_message_for_mulit_status() throws Exception {
final MultiStatus status = new MultiStatus(ContractPlugin.PLUGIN_ID, 0, "", null);
status.add(ValidationStatus.error("a status error message 1"));
status.add(ValidationStatus.error("a status error message 2"));
assertThat(validationRule.getMessage(status)).contains("message 1", "message 2");
}
@Test(expected = IllegalArgumentException.class)
public void should_getMessage_throw_IllegalArgumentException_for_null_status() throws Exception {
validationRule.getMessage(null);
}
private ContractInput addInput(final Contract contract, final String inputName, final ContractInputType type, final String description) {
final ContractInput contractInput = ProcessFactory.eINSTANCE.createContractInput();
contractInput.setName(inputName);
contractInput.setType(type);
contractInput.setDescription(description);
contract.getInputs().add(contractInput);
return contractInput;
}
}
/**
* Copyright (C) 2014 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.contract.core.validation;
import static org.assertj.core.api.Assertions.assertThat;
import org.bonitasoft.studio.model.process.ContractConstraint;
import org.bonitasoft.studio.model.process.ProcessFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author Romain Bioteau
*
*/
public class ContractConstraintNameValidationRuleTest {
private ContractConstraintNameValidationRule validationRule;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
validationRule = new ContractConstraintNameValidationRule();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
@Test
public void should_appliesTo_contract_constraint() throws Exception {
final ContractConstraint constraint = ProcessFactory.eINSTANCE.createContractConstraint();
<