Commit 069c2bc9 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub

fix(contract) only trigger multi-validator when needed (#2544)

Signed-off-by: default avatarRomain Bioteau <romain.bioteau@bonitasoft.com>
parent 83dccc73
......@@ -53,10 +53,14 @@ public class EmptySelectionMultivalidatorTest {
public void shouldReturnErrorWhenNoElementsAreChecked() {
final IObservableSet checkedElements = new WritableSet();
final WritableValue selectedDataObservable = new WritableValue();
final WritableValue currentPageObservable = new WritableValue();
currentPageObservable.setValue(mock(CreateContractInputFromBusinessObjectWizardPage.class));
final BusinessObjectData businessObjectData = aBusinessData().withClassname("org.test.Employee").build();
selectedDataObservable.setValue(businessObjectData);
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable, checkedElements,
new ArrayList<FieldToContractInputMapping>(), aPool().build(), new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class));
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable,
checkedElements,
new ArrayList<FieldToContractInputMapping>(), aPool().build(),
new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class), currentPageObservable);
assertThat(multiValidator.validate().isOK()).isFalse();
}
......@@ -65,17 +69,23 @@ public class EmptySelectionMultivalidatorTest {
final IObservableSet checkedElements = new WritableSet();
checkedElements.add(mock(FieldToContractInputMapping.class));
final WritableValue selectedDataObservable = new WritableValue();
final WritableValue currentPageObservable = new WritableValue();
currentPageObservable.setValue(mock(CreateContractInputFromBusinessObjectWizardPage.class));
final BusinessObjectData businessObjectData = aBusinessData().withClassname("org.test.Employee").build();
selectedDataObservable.setValue(businessObjectData);
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable, checkedElements,
new ArrayList<FieldToContractInputMapping>(), aPool().build(),new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class));
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable,
checkedElements,
new ArrayList<FieldToContractInputMapping>(), aPool().build(),
new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class),
currentPageObservable);
assertThat(multiValidator.validate().isOK()).isTrue();
}
@Test
public void shouldReturnWarningWhenOneElementIsMandatoryAndNotChecked() {
final IObservableSet checkedElements = new WritableSet();
final FieldToContractInputMapping rootMapping1 = new RelationFieldToContractInputMapping(Mockito.mock(RelationField.class));
final FieldToContractInputMapping rootMapping1 = new RelationFieldToContractInputMapping(
Mockito.mock(RelationField.class));
final SimpleField simpleField1 = new SimpleField();
simpleField1.setNullable(false);
final SimpleField simpleField2 = new SimpleField();
......@@ -85,22 +95,28 @@ public class EmptySelectionMultivalidatorTest {
final SimpleFieldToContractInputMapping childMapping2 = new SimpleFieldToContractInputMapping(simpleField2);
rootMapping1.addChild(childMapping1);
rootMapping1.addChild(childMapping2);
final FieldToContractInputMapping rootMapping2 = new RelationFieldToContractInputMapping(Mockito.mock(RelationField.class));
final FieldToContractInputMapping rootMapping2 = new RelationFieldToContractInputMapping(
Mockito.mock(RelationField.class));
checkedElements.add(rootMapping1);
checkedElements.add(rootMapping2);
checkedElements.add(childMapping2);
final WritableValue selectedDataObservable = new WritableValue();
final BusinessObjectData businessObjectData = aBusinessData().withClassname("org.test.Employee").build();
selectedDataObservable.setValue(businessObjectData);
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable, checkedElements,
Lists.newArrayList(rootMapping1, rootMapping2), aPool().build(),new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class));
final WritableValue currentPageObservable = new WritableValue();
currentPageObservable.setValue(mock(CreateContractInputFromBusinessObjectWizardPage.class));
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable,
checkedElements,
Lists.newArrayList(rootMapping1, rootMapping2), aPool().build(),
new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class),currentPageObservable);
assertThat(multiValidator.validate().isOK()).isFalse();
}
@Test
public void shouldReturnOkWhenElementIsMandatoryAndChecked() {
final IObservableSet checkedElements = new WritableSet();
final FieldToContractInputMapping rootMapping1 = new RelationFieldToContractInputMapping(Mockito.mock(RelationField.class));
final FieldToContractInputMapping rootMapping1 = new RelationFieldToContractInputMapping(
Mockito.mock(RelationField.class));
final SimpleField simpleField1 = new SimpleField();
simpleField1.setNullable(false);
final SimpleField simpleField2 = new SimpleField();
......@@ -110,7 +126,8 @@ public class EmptySelectionMultivalidatorTest {
final SimpleFieldToContractInputMapping childMapping2 = new SimpleFieldToContractInputMapping(simpleField2);
rootMapping1.addChild(childMapping1);
rootMapping1.addChild(childMapping2);
final FieldToContractInputMapping rootMapping2 = new RelationFieldToContractInputMapping(Mockito.mock(RelationField.class));
final FieldToContractInputMapping rootMapping2 = new RelationFieldToContractInputMapping(
Mockito.mock(RelationField.class));
checkedElements.add(rootMapping1);
checkedElements.add(rootMapping2);
checkedElements.add(childMapping2);
......@@ -118,8 +135,13 @@ public class EmptySelectionMultivalidatorTest {
final WritableValue selectedDataObservable = new WritableValue();
final BusinessObjectData businessObjectData = aBusinessData().withClassname("org.test.Employee").build();
selectedDataObservable.setValue(businessObjectData);
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable, checkedElements,
Lists.newArrayList(rootMapping1, rootMapping2), aPool().build(),new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class));
final WritableValue currentPageObservable = new WritableValue();
currentPageObservable.setValue(mock(CreateContractInputFromBusinessObjectWizardPage.class));
final EmptySelectionMultivalidator multiValidator = new EmptySelectionMultivalidator(selectedDataObservable,
checkedElements,
Lists.newArrayList(rootMapping1, rootMapping2), aPool().build(),
new WritableValue<GenerationOptions.EditMode>(EditMode.CREATE, EditMode.class),
currentPageObservable);
assertThat(multiValidator.validate().isOK()).isTrue();
}
......
......@@ -103,6 +103,7 @@ public class CreateContractInputFromBusinessObjectWizardPage extends WizardPage
private EmptySelectionMultivalidator multiValidator;
private ContractContainer contractContainer;
private UnselectLazyReferencesInMultipleContainer lazyFieldStatusProvider;
private WritableValue activeWizardPageObservable;
protected CreateContractInputFromBusinessObjectWizardPage(ContractContainer contractContainer,
GenerationOptions generationOptions,
......@@ -249,9 +250,9 @@ public class CreateContractInputFromBusinessObjectWizardPage extends WizardPage
});
createButtonListeners(checkedElements);
activeWizardPageObservable = new WritableValue<>();
multiValidator = new EmptySelectionMultivalidator(selectedDataObservable, checkedElements, mappings,
contract.eContainer(), generationOptions.getEditModeObservable());
contract.eContainer(), generationOptions.getEditModeObservable(), activeWizardPageObservable);
dbc.addValidationStatusProvider(multiValidator);
new Label(viewerComposite, SWT.NONE); //FILLER
......@@ -263,6 +264,12 @@ public class CreateContractInputFromBusinessObjectWizardPage extends WizardPage
ColumnViewerToolTipSupport.enableFor(treeViewer);
}
@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
activeWizardPageObservable.setValue(visible ? this : null);
}
protected void createButtonComposite(final Composite viewerComposite) {
final Composite buttonsComposite = new Composite(viewerComposite, SWT.NONE);
......
......@@ -28,6 +28,7 @@ import org.eclipse.core.databinding.validation.MultiValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
......@@ -41,19 +42,26 @@ public class EmptySelectionMultivalidator extends MultiValidator {
private final EObject container;
private final WritableValue selectedDataObservable;
private IObservableValue<EditMode> editModeObservable;
private IObservableValue<WizardPage> activeWizardPageObservable;
public EmptySelectionMultivalidator(final WritableValue selectedDataObservable, final IObservableSet checkedElements,
final List<FieldToContractInputMapping> mappings, final EObject container, IObservableValue<EditMode> editModeObservable) {
public EmptySelectionMultivalidator(final WritableValue selectedDataObservable,
final IObservableSet checkedElements,
final List<FieldToContractInputMapping> mappings,
final EObject container,
IObservableValue<EditMode> editModeObservable,
IObservableValue<WizardPage> activeWizardPageObservable) {
this.checkedElements = checkedElements;
this.mappings = mappings;
this.container = container;
this.selectedDataObservable = selectedDataObservable;
this.editModeObservable = editModeObservable;
this.activeWizardPageObservable = activeWizardPageObservable;
}
@Override
protected IStatus validate() {
if (selectedDataObservable.getValue() instanceof BusinessObjectData) {
if (activeWizardPageObservable.getValue() instanceof CreateContractInputFromBusinessObjectWizardPage
&& selectedDataObservable.getValue() instanceof BusinessObjectData) {
if (checkedElements.isEmpty()) {
return ValidationStatus.error(Messages.atLeastOneAttributeShouldBeSelectedError);
} else if(editModeObservable.getValue() == EditMode.CREATE){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment