1   package org.weda.workflow;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import junit.framework.TestCase;
6   import org.weda.action.ActionContainer;
7   import org.weda.action.ActionRegistry;
8   import org.weda.action.ActionState;
9   import org.weda.test.WedaTestCase;
10  import org.weda.action.TestAction;
11  import org.weda.action.TestActionContainer;
12  import org.weda.action.TestActionContainer3;
13  import org.weda.action.impl.ActionExecutionContextImpl;
14  import org.weda.cache.impl.SimpleCache;
15  import org.weda.workflow.impl.ActionEntry;
16  import org.weda.workflow.impl.DefaultPath;
17  import org.weda.workflow.impl.WorkflowImpl;
18  import org.weda.workflow.impl.PageEntry;
19  import org.weda.workflow.impl.ResultTypeEntry;
20  import org.weda.workflow.impl.ResultValueEntry;
21  import org.weda.workflow.impl.TargetClassEntry;
22  import org.weda.workflow.impl.TargetNameEntry;
23  
24  /**
25   *
26   * @author Mikhail Titov
27   */
28  public class WorkflowTest extends WedaTestCase {
29      
30      public WorkflowTest(String name) throws Exception {
31          super(name);
32      }
33      
34      public void test_direct() throws Exception {
35          WorkflowImpl flow = new WorkflowImpl();
36          flow.setName("workflow");
37          flow.setCacheManager(new TestCacheManager());
38          flow.setActionRegistry(new TestActionRegistry());
39          //
40          PageEntry pageEntry = new PageEntry();
41          pageEntry.setName("/(.*)List\\.html/");
42          flow.addEntry(pageEntry);
43          //
44          ActionEntry actionEntry = new ActionEntry();
45          actionEntry.setDefaultEntry(true);
46          pageEntry.addEntry(actionEntry);
47          //
48          TargetClassEntry targetClassEntry = new TargetClassEntry();
49          targetClassEntry.setDefaultEntry(true);
50          actionEntry.addEntry(targetClassEntry);
51          //
52          TargetNameEntry targetNameEntry = new TargetNameEntry();
53          targetNameEntry.setDefaultEntry(true);
54          targetClassEntry.addEntry(targetNameEntry);
55          //
56          ResultTypeEntry resultTypeEntry = new ResultTypeEntry();
57          resultTypeEntry.setDefaultEntry(true);
58          targetNameEntry.addEntry(resultTypeEntry);
59          //
60          ResultValueEntry valueEntry = new ResultValueEntry();
61          valueEntry.setDefaultEntry(true);
62          DefaultPath path = new DefaultPath();
63          path.setPath("{1}Edit.html");
64          valueEntry.addPathToPush(path);
65          resultTypeEntry.addEntry(valueEntry);
66          //
67          flow.init();
68          flow.setPathAdapter(new TestPathAdapter());
69          String resultPath = (String)flow.getNextPage("CompanyList.html", null);
70          assertNotNull(resultPath);
71          //assertEquals(false, resultPath.isRedirect());
72          assertEquals("CompanyEdit.html", resultPath);
73          //
74          flow.setUsePathAdapter(false);
75          Path resultPath2 = (Path)flow.getNextPage("CompanyList.html", null);
76          assertNotNull(resultPath2);
77          assertEquals(false, resultPath2.isRedirect());
78          assertEquals("CompanyEdit.html", resultPath2.getPath());
79      }
80      
81      public void test_config() throws Exception {
82          List<PageEntry> pageEntries = (List<PageEntry>)
83          registry.getConfiguration("org.weda.workflow.Workflow");
84          assertNotNull(pageEntries);
85          assertEquals(5, pageEntries.size());
86          PageEntry pageEntry = null;
87          for (PageEntry entry: pageEntries)
88              if ("/(.*)List\\.html/".equals(entry.getName())){
89                  pageEntry = entry;
90                  break;
91              }
92          assertNotNull(pageEntry);
93          pageEntry.init();
94          assertEquals("/(.*)List\\.html/", pageEntry.getName());
95          assertFalse(pageEntry.isDefaultEntry());
96          assertEquals(1, pageEntry.getEntries().size());
97          //
98          ActionEntry actionEntry = pageEntry.getEntries().get(0);
99          assertEquals(TestAction.class.getName(), actionEntry.getName());
100         assertFalse(actionEntry.isDefaultEntry());
101         assertEquals(1, actionEntry.getEntries().size());
102         //
103         TargetClassEntry targetClassEntry = actionEntry.getEntries().get(0);
104         assertEquals(Entry.DEFAULT_ENTRY_NAME, targetClassEntry.getName());
105         assertTrue(targetClassEntry.isDefaultEntry());
106         assertEquals(1, targetClassEntry.getEntries().size());
107         //
108         TargetNameEntry targetNameEntry = targetClassEntry.getEntries().get(0);
109         assertEquals(Entry.DEFAULT_ENTRY_NAME, targetNameEntry.getName());
110         assertTrue(targetNameEntry.isDefaultEntry());
111         assertEquals(1, targetNameEntry.getEntries().size());
112         //
113         ResultTypeEntry resultTypeEntry = targetNameEntry.getEntries().get(0);
114         assertEquals(ResultTypeEntry.SUCCESS_RESULT, resultTypeEntry.getName());
115         assertFalse(resultTypeEntry.isDefaultEntry());
116         assertEquals(1, resultTypeEntry.getEntries().size());
117         //
118         ResultValueEntry resultValueEntry = resultTypeEntry.getEntries().get(0);
119         assertEquals(Entry.DEFAULT_ENTRY_NAME, resultValueEntry.getName());
120         assertTrue(resultValueEntry.isDefaultEntry());
121         //
122 //        pageEntry = pageEntries.get(1);
123 //        pageEntry.init();
124 //        assertEquals(Entry.DEFAULT_ENTRY_NAME, pageEntry.getName());
125 //        assertTrue(pageEntry.isDefaultEntry());
126 //        assertEquals(1, pageEntry.getEntries().size());
127     }
128     
129     public void test_service() throws Exception{
130         Workflow flow = (Workflow)registry.getService(Workflow.class);
131         assertNotNull(flow);
132         ActionRegistry actionRegistry =
133                 (ActionRegistry)registry.getService(ActionRegistry.class);
134         ActionContainer container = new TestActionContainer();
135         List<ActionState> actionStates =
136                 actionRegistry.getActionsStates(
137                     container, container.getClass());
138         ActionExecutionContextImpl ctx = new ActionExecutionContextImpl();
139         ctx.setActionDescriptor(actionStates.get(0).getActionDescriptor());
140         ctx.setTargetObject(container);
141         actionRegistry.executeAction(ctx);
142         flow.setUsePathAdapter(false);
143         Path path = (Path)flow.getNextPage("CompanyList.html", null);
144         assertNotNull(path);
145         assertFalse(path.isRedirect());
146         assertEquals("CompanyEdit.html", path.getPath());
147         path = (Path)flow.getNextPage("Login.html", null);
148         assertNotNull(path);
149         assertFalse(path.isRedirect());
150         assertEquals("Login.html_", path.getPath());
151         //
152         container = new TestActionContainer3();
153         actionStates = 
154                 actionRegistry.getActionsStates(
155                     container, container.getClass());
156         ctx = new ActionExecutionContextImpl();
157         ctx.setActionDescriptor(actionStates.get(0).getActionDescriptor());
158         ctx.setTargetObject(container);
159         actionRegistry.executeAction(ctx);
160         flow.setUsePathAdapter(false);
161         path = (Path)flow.getNextPage("CompanyList.html", null);
162         assertNotNull(path);
163         assertFalse(path.isRedirect());
164         assertEquals("CompanyList.html", path.getPath());
165     }
166 }