Commit 3843621b authored by Romain Bioteau's avatar Romain Bioteau

BS-14296 Contract Input Refactoring must stay at the same scope of the

orignal refactored element
parent 96b025cf
...@@ -22,6 +22,8 @@ import static org.bonitasoft.studio.model.expression.builders.OperationBuilder.a ...@@ -22,6 +22,8 @@ import static org.bonitasoft.studio.model.expression.builders.OperationBuilder.a
import static org.bonitasoft.studio.model.process.builders.ContractBuilder.aContract; import static org.bonitasoft.studio.model.process.builders.ContractBuilder.aContract;
import static org.bonitasoft.studio.model.process.builders.ContractConstraintBuilder.aContractConstraint; import static org.bonitasoft.studio.model.process.builders.ContractConstraintBuilder.aContractConstraint;
import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput; import static org.bonitasoft.studio.model.process.builders.ContractInputBuilder.aContractInput;
import static org.bonitasoft.studio.model.process.builders.DataBuilder.aData;
import static org.bonitasoft.studio.model.process.builders.PoolBuilder.aPool;
import static org.bonitasoft.studio.model.process.builders.TaskBuilder.aTask; import static org.bonitasoft.studio.model.process.builders.TaskBuilder.aTask;
import java.util.Set; import java.util.Set;
...@@ -31,6 +33,7 @@ import org.bonitasoft.studio.common.emf.tools.ExpressionHelper; ...@@ -31,6 +33,7 @@ import org.bonitasoft.studio.common.emf.tools.ExpressionHelper;
import org.bonitasoft.studio.model.expression.Expression; import org.bonitasoft.studio.model.expression.Expression;
import org.bonitasoft.studio.model.process.ContractContainer; import org.bonitasoft.studio.model.process.ContractContainer;
import org.bonitasoft.studio.model.process.ContractInput; import org.bonitasoft.studio.model.process.ContractInput;
import org.bonitasoft.studio.model.process.Pool;
import org.bonitasoft.studio.model.process.Task; import org.bonitasoft.studio.model.process.Task;
import org.bonitasoft.studio.model.process.provider.ProcessItemProviderAdapterFactory; import org.bonitasoft.studio.model.process.provider.ProcessItemProviderAdapterFactory;
import org.bonitasoft.studio.refactoring.core.RefactoringOperationType; import org.bonitasoft.studio.refactoring.core.RefactoringOperationType;
...@@ -38,7 +41,9 @@ import org.bonitasoft.studio.refactoring.core.script.IScriptRefactoringOperation ...@@ -38,7 +41,9 @@ import org.bonitasoft.studio.refactoring.core.script.IScriptRefactoringOperation
import org.bonitasoft.studio.refactoring.core.script.ScriptContainer; import org.bonitasoft.studio.refactoring.core.script.ScriptContainer;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl; import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -54,6 +59,8 @@ public class RefactorContractInputOperationTest { ...@@ -54,6 +59,8 @@ public class RefactorContractInputOperationTest {
private final IProgressMonitor monitor = new NullProgressMonitor(); private final IProgressMonitor monitor = new NullProgressMonitor();
@Mock @Mock
private IScriptRefactoringOperationFactory scriptRefactorOperationFactory; private IScriptRefactoringOperationFactory scriptRefactorOperationFactory;
@Mock
private EditingDomain domain;
@Test @Test
public void should_update_contract_input_reference_in_contract_input_expressions() throws Exception { public void should_update_contract_input_reference_in_contract_input_expressions() throws Exception {
...@@ -99,13 +106,39 @@ public class RefactorContractInputOperationTest { ...@@ -99,13 +106,39 @@ public class RefactorContractInputOperationTest {
final ContractInput contractInput = aTaskWithContract.getContract().getInputs() final ContractInput contractInput = aTaskWithContract.getContract().getInputs()
.get(0); .get(0);
final ContractInput newProcessInput = EcoreUtil.copy(contractInput);
newProcessInput.setName("newInputName");
final Set<ScriptContainer<?>> allScriptWithReferencedElement = refactorOperation.allScriptWithReferencedElement(new ContractInputRefactorPair( final Set<ScriptContainer<?>> allScriptWithReferencedElement = refactorOperation.allScriptWithReferencedElement(new ContractInputRefactorPair(
EcoreUtil.copy(contractInput), newProcessInput,
contractInput)); contractInput));
assertThat(allScriptWithReferencedElement.iterator().next().getModelElement().eContainer()).isNotNull(); assertThat(allScriptWithReferencedElement.iterator().next().getModelElement().eContainer()).isNotNull();
} }
@Test
public void should_not_refactor_input_with_same_name_in_another_contract_container() throws Exception {
final Pool process = aPool().havingContract(aContract().havingInput(aContractInput().withName("myInput")))
.havingData(aData().withName("aTextData")
.havingDefaultValue(ExpressionHelper.createContractInputExpression(aContractInput().withName("myInput").build())))
.havingElements(aTask().havingContract(aContract().havingInput(aContractInput().withName("myInput"))).havingData(aData().withName("aTextData")
.havingDefaultValue(ExpressionHelper.createContractInputExpression(aContractInput().withName("myInput").build()))))
.build();
final RefactorContractInputOperation refactorOperation = new RefactorContractInputOperation(process,
scriptRefactorOperationFactory,
RefactoringOperationType.UPDATE);
refactorOperation.setEditingDomain(domain);
final ContractInput processInput = process.getContract().getInputs().get(0);
final ContractInput newInput = EcoreUtil.copy(processInput);
newInput.setName("newName");
refactorOperation.addItemToRefactor(newInput, processInput);
CompoundCommand cc = new CompoundCommand();
cc = refactorOperation.doBuildCompoundCommand(cc, monitor);
assertThat(cc.getCommandList()).hasSize(1);
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void should_have_a_container() throws Exception { public void should_have_a_container() throws Exception {
new RefactorContractInputOperation(null, scriptRefactorOperationFactory, RefactoringOperationType.UPDATE); new RefactorContractInputOperation(null, scriptRefactorOperationFactory, RefactoringOperationType.UPDATE);
...@@ -115,12 +148,6 @@ public class RefactorContractInputOperationTest { ...@@ -115,12 +148,6 @@ public class RefactorContractInputOperationTest {
return new TransactionalEditingDomainImpl(new ProcessItemProviderAdapterFactory()); return new TransactionalEditingDomainImpl(new ProcessItemProviderAdapterFactory());
} }
private ContractContainer aTaskWithContract() {
return aTask().havingContract(aContract()
.havingInput(aContractInput().withName("firstName"))
.havingConstraint(aContractConstraint().withExpression("firstName.length > 0").havingInput("firstName"))).build();
}
private ContractContainer aTaskWithContractAndGlobalConstraint() { private ContractContainer aTaskWithContractAndGlobalConstraint() {
return aTask().havingContract(aContract() return aTask().havingContract(aContract()
.havingInput(aContractInput().withName("firstName")) .havingInput(aContractInput().withName("firstName"))
......
...@@ -41,6 +41,7 @@ import org.bonitasoft.studio.refactoring.core.script.ScriptContainer; ...@@ -41,6 +41,7 @@ import org.bonitasoft.studio.refactoring.core.script.ScriptContainer;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.command.CompoundCommand; import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
...@@ -71,12 +72,24 @@ public class RefactorContractInputOperation extends AbstractRefactorOperation<Co ...@@ -71,12 +72,24 @@ public class RefactorContractInputOperation extends AbstractRefactorOperation<Co
*/ */
@Override @Override
protected Set<ScriptContainer<?>> allScriptWithReferencedElement(final RefactorPair<ContractInput, ContractInput> pairRefactor) { protected Set<ScriptContainer<?>> allScriptWithReferencedElement(final RefactorPair<ContractInput, ContractInput> pairRefactor) {
final Set<ScriptContainer<?>> allScriptWithReferencedElement = super.allScriptWithReferencedElement(pairRefactor); final Set<ScriptContainer<?>> allScriptWithReferencedElement = newHashSet(
filter(super.allScriptWithReferencedElement(pairRefactor), inContractContainer(
ModelHelper.getFirstContainerOfType(pairRefactor.getOldValue(), ContractContainer.class))));
allScriptWithReferencedElement.addAll(constraintExpressionsReferencing(ModelHelper.getFirstContainerOfType(pairRefactor.getOldValue(), Contract.class), allScriptWithReferencedElement.addAll(constraintExpressionsReferencing(ModelHelper.getFirstContainerOfType(pairRefactor.getOldValue(), Contract.class),
pairRefactor.getOldValue())); pairRefactor.getOldValue()));
return allScriptWithReferencedElement; return allScriptWithReferencedElement;
} }
private Predicate<ScriptContainer<?>> inContractContainer(final ContractContainer contractContainer) {
return new Predicate<ScriptContainer<?>>() {
@Override
public boolean apply(ScriptContainer<?> sc) {
return EcoreUtil.equals(ModelHelper.getFirstContainerOfType(sc.getModelElement(), ContractContainer.class), contractContainer);
}
};
}
private Collection<? extends ScriptContainer<?>> constraintExpressionsReferencing(final Contract contract, final ContractInput contractInput) { private Collection<? extends ScriptContainer<?>> constraintExpressionsReferencing(final Contract contract, final ContractInput contractInput) {
return newHashSet(transform( return newHashSet(transform(
filter(contract.getConstraints(), filter(contract.getConstraints(),
...@@ -106,7 +119,7 @@ public class RefactorContractInputOperation extends AbstractRefactorOperation<Co ...@@ -106,7 +119,7 @@ public class RefactorContractInputOperation extends AbstractRefactorOperation<Co
private void updateContractInputExpressions(final CompoundCommand cc) { private void updateContractInputExpressions(final CompoundCommand cc) {
for (final Expression exp : filter(getAllElementOfTypeIn(container, Expression.class), withExpressionType(ExpressionConstants.CONTRACT_INPUT_TYPE))) { for (final Expression exp : filter(getAllElementOfTypeIn(container, Expression.class), withExpressionType(ExpressionConstants.CONTRACT_INPUT_TYPE))) {
for (final ContractInputRefactorPair pairToRefactor : filter(pairsToRefactor, matchingOldName(exp.getName()))) { for (final ContractInputRefactorPair pairToRefactor : filter(pairsToRefactor, matches(exp))) {
final ContractInput newValue = pairToRefactor.getNewValue(); final ContractInput newValue = pairToRefactor.getNewValue();
cc.append(new UpdateExpressionCommand(getEditingDomain(), exp, newValue != null ? createContractInputExpression(newValue) cc.append(new UpdateExpressionCommand(getEditingDomain(), exp, newValue != null ? createContractInputExpression(newValue)
: createDefaultExpression(exp))); : createDefaultExpression(exp)));
...@@ -119,12 +132,14 @@ public class RefactorContractInputOperation extends AbstractRefactorOperation<Co ...@@ -119,12 +132,14 @@ public class RefactorContractInputOperation extends AbstractRefactorOperation<Co
return ExpressionHelper.createConstantExpression("", exp.isReturnTypeFixed() ? exp.getReturnType() : String.class.getName()); return ExpressionHelper.createConstantExpression("", exp.isReturnTypeFixed() ? exp.getReturnType() : String.class.getName());
} }
private Predicate<ContractInputRefactorPair> matchingOldName(final String expressionName) { private Predicate<ContractInputRefactorPair> matches(final Expression expression) {
return new Predicate<ContractInputRefactorPair>() { return new Predicate<ContractInputRefactorPair>() {
@Override @Override
public boolean apply(final ContractInputRefactorPair refactorPair) { public boolean apply(final ContractInputRefactorPair refactorPair) {
return refactorPair.getOldValueName().equals(expressionName); return refactorPair.getOldValueName().equals(expression.getName())
&& EcoreUtil.equals(ModelHelper.getFirstContainerOfType(expression, ContractContainer.class),
ModelHelper.getFirstContainerOfType(refactorPair.getOldValue(), ContractContainer.class));
} }
}; };
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
package org.bonitasoft.studio.model.process.builders; package org.bonitasoft.studio.model.process.builders;
import org.bonitasoft.studio.model.Buildable; import org.bonitasoft.studio.model.Buildable;
import org.bonitasoft.studio.model.expression.Expression;
import org.bonitasoft.studio.model.expression.builders.ExpressionBuilder; import org.bonitasoft.studio.model.expression.builders.ExpressionBuilder;
import org.bonitasoft.studio.model.process.Data; import org.bonitasoft.studio.model.process.Data;
import org.bonitasoft.studio.model.process.DataAware; import org.bonitasoft.studio.model.process.DataAware;
...@@ -72,6 +73,11 @@ public class DataBuilder<T extends Data, B extends DataBuilder<T, B>> extends El ...@@ -72,6 +73,11 @@ public class DataBuilder<T extends Data, B extends DataBuilder<T, B>> extends El
return getThis(); return getThis();
} }
public B havingDefaultValue(final Expression defaultValueExpression) {
getBuiltInstance().setDefaultValue(defaultValueExpression);
return getThis();
}
public B havingDataType(final DataTypeBuilder<?, ?> dataType) { public B havingDataType(final DataTypeBuilder<?, ?> dataType) {
getBuiltInstance().setDataType(dataType.build()); getBuiltInstance().setDataType(dataType.build());
return getThis(); return getThis();
......
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