|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * BroadleafCommerce Integration |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 Broadleaf Commerce |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +/** |
| 21 | + * |
| 22 | + */ |
| 23 | +package org.broadleafcommerce.common.workflow; |
| 24 | + |
| 25 | +import org.broadleafcommerce.core.workflow.Activity; |
| 26 | +import org.broadleafcommerce.core.workflow.BaseActivity; |
| 27 | +import org.broadleafcommerce.core.workflow.DefaultProcessContextImpl; |
| 28 | +import org.broadleafcommerce.core.workflow.ProcessContext; |
| 29 | +import org.broadleafcommerce.core.workflow.ProcessContextFactory; |
| 30 | +import org.broadleafcommerce.core.workflow.SequenceProcessor; |
| 31 | +import org.broadleafcommerce.core.workflow.WorkflowException; |
| 32 | +import org.broadleafcommerce.core.workflow.state.RollbackFailureException; |
| 33 | +import org.broadleafcommerce.core.workflow.state.RollbackHandler; |
| 34 | +import org.broadleafcommerce.test.BaseTest; |
| 35 | +import org.testng.Assert; |
| 36 | +import org.testng.annotations.Test; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | + |
| 43 | +import javax.annotation.Resource; |
| 44 | + |
| 45 | +/** |
| 46 | + * Ensures that activities are rolled back in the correct order |
| 47 | + * |
| 48 | + * @author Phillip Verheyden (phillipuniverse) |
| 49 | + */ |
| 50 | +public class RollbackTest extends BaseTest { |
| 51 | + |
| 52 | + @Resource(name = "testRollbackWorkflow") |
| 53 | + protected SequenceProcessor testRollbackWorkflow; |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testRollbackOrder() { |
| 57 | + List<String> results = new ArrayList<String>(); |
| 58 | + boolean exceptionThrown = false; |
| 59 | + try { |
| 60 | + testRollbackWorkflow.doActivities(results); |
| 61 | + } catch (WorkflowException e) { |
| 62 | + exceptionThrown = true; |
| 63 | + } |
| 64 | + |
| 65 | + List<String> expected = Arrays.asList("Activity1", |
| 66 | + "Activity2", |
| 67 | + "ActivityA", |
| 68 | + "RollbackActivityA", |
| 69 | + "NestedActivityException", |
| 70 | + "RollbackActivity2", |
| 71 | + "RollbackActivity1"); |
| 72 | + Assert.assertTrue(exceptionThrown); |
| 73 | + Assert.assertEquals(results, expected, "Rollback occurred out of order"); |
| 74 | + } |
| 75 | + |
| 76 | + public static class SimpleActivity extends BaseActivity<ProcessContext<List<String>>> { |
| 77 | + |
| 78 | + protected String name; |
| 79 | + |
| 80 | + public SimpleActivity(String name) { |
| 81 | + this.name = name; |
| 82 | + } |
| 83 | + |
| 84 | + public String getName() { |
| 85 | + return name; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public ProcessContext<List<String>> execute(ProcessContext<List<String>> context) throws Exception { |
| 90 | + context.getSeedData().add(name); |
| 91 | + return context; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean getAutomaticallyRegisterRollbackHandler() { |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public static class SimpleRollbackHandler implements RollbackHandler<List<String>> { |
| 102 | + |
| 103 | + @Override |
| 104 | + public void rollbackState(Activity<? extends ProcessContext<List<String>>> activity, ProcessContext<List<String>> processContext, Map<String, Object> stateConfiguration) throws RollbackFailureException { |
| 105 | + processContext.getSeedData().add("Rollback" + ((SimpleActivity) activity).getName()); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + public static class NestedActivity extends BaseActivity<ProcessContext<List<String>>> { |
| 111 | + |
| 112 | + protected SequenceProcessor workflow; |
| 113 | + |
| 114 | + public NestedActivity(SequenceProcessor workflow) { |
| 115 | + this.workflow = workflow; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public ProcessContext<List<String>> execute(ProcessContext<List<String>> context) throws Exception { |
| 120 | + try { |
| 121 | + workflow.doActivities(context.getSeedData()); |
| 122 | + } catch (WorkflowException e) { |
| 123 | + context.getSeedData().add("NestedActivityException"); |
| 124 | + throw e; |
| 125 | + } |
| 126 | + return context; |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + public static class ExceptionActivity extends BaseActivity<ProcessContext<List<String>>> { |
| 132 | + |
| 133 | + @Override |
| 134 | + public ProcessContext<List<String>> execute(ProcessContext<List<String>> context) throws Exception { |
| 135 | + throw new RuntimeException(); |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + public static class DummyProcessContextFactory implements ProcessContextFactory<Object, Object> { |
| 141 | + |
| 142 | + @Override |
| 143 | + public ProcessContext<Object> createContext(Object preSeedData) throws WorkflowException { |
| 144 | + ProcessContext<Object> context = new DefaultProcessContextImpl<>(); |
| 145 | + context.setSeedData(preSeedData); |
| 146 | + return context; |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | +} |
0 commit comments