1 17 18 19 20 package org.apache.lenya.cms.workflow; 21 22 import java.io.File ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import junit.textui.TestRunner; 27 28 import org.apache.lenya.ac.AccessControlException; 29 import org.apache.lenya.ac.Identity; 30 import org.apache.lenya.ac.Policy; 31 import org.apache.lenya.ac.Role; 32 import org.apache.lenya.ac.User; 33 import org.apache.lenya.ac.file.FileItemManager; 34 import org.apache.lenya.ac.impl.AccessControlTest; 35 import org.apache.lenya.cms.PublicationHelper; 36 import org.apache.lenya.cms.publication.Document; 37 import org.apache.lenya.cms.publication.DocumentBuildException; 38 import org.apache.lenya.cms.publication.DocumentType; 39 import org.apache.lenya.cms.publication.DocumentTypeBuildException; 40 import org.apache.lenya.cms.publication.DocumentTypeBuilder; 41 import org.apache.lenya.cms.publication.PageEnvelopeException; 42 import org.apache.lenya.cms.publication.Publication; 43 import org.apache.lenya.workflow.Event; 44 import org.apache.lenya.workflow.Situation; 45 import org.apache.lenya.workflow.WorkflowException; 46 import org.apache.lenya.workflow.WorkflowInstance; 47 48 52 public class WorkflowTest extends AccessControlTest { 53 57 public WorkflowTest(String test) { 58 super(test); 59 } 60 61 66 public static void main(String [] args) { 67 args = PublicationHelper.extractPublicationArguments(args); 68 documentTypeName = args[0]; 69 TestRunner.run(getSuite()); 70 } 71 72 76 public static Test getSuite() { 77 return new TestSuite(WorkflowTest.class); 78 } 79 80 private static final String variableName = "is-live"; 81 protected static final String URL = "/authoring/index.html"; 82 83 91 public void testWorkflow() 92 throws 93 DocumentTypeBuildException, 94 WorkflowException, 95 AccessControlException, 96 PageEnvelopeException, 97 DocumentBuildException { 98 Publication publication = PublicationHelper.getPublication(); 99 String url = "/" + publication.getId() + URL; 100 Document document = publication.getDocumentBuilder().buildDocument(publication, url); 101 102 File configDir = new File (publication.getDirectory(), FileItemManager.PATH); 103 assertTrue(configDir.exists()); 104 105 Policy policy = getPolicyManager().getPolicy(getAccreditableManager(), url); 106 107 DocumentType type = DocumentTypeBuilder.buildDocumentType(documentTypeName, publication); 108 String workflowId = type.getWorkflowFileName(); 109 110 WorkflowFactory factory = WorkflowFactory.newInstance(); 111 112 String [] emptyRoles = { 113 }; 114 Situation situation = factory.buildSituation(emptyRoles, "test", "127.0.0.1"); 115 116 WorkflowFactory.initHistory(document, workflowId, situation); 117 118 for (int situationIndex = 0; situationIndex < situations.length; situationIndex++) { 119 WorkflowInstance instance = null; 120 instance = factory.buildInstance(document); 121 assertNotNull(instance); 122 123 System.out.println("Current state: " + instance.getCurrentState()); 124 125 Identity identity = new Identity(); 126 User user = 127 getAccreditableManager().getUserManager().getUser( 128 situations[situationIndex].getUser()); 129 identity.addIdentifiable(user); 130 131 Role[] roles = policy.getRoles(identity); 132 System.out.print("Roles:"); 133 134 for (int roleIndex = 0; roleIndex < roles.length; roleIndex++) { 135 System.out.print(" " + roles[roleIndex]); 136 } 137 138 System.out.println(); 139 140 situation = null; 141 142 try { 143 situation = factory.buildSituation(roles, identity); 144 } catch (WorkflowException e1) { 145 e1.printStackTrace(System.err); 146 } 147 148 Event[] events = instance.getExecutableEvents(situation); 149 150 Event event = null; 151 System.out.print("Events:"); 152 153 for (int eventIndex = 0; eventIndex < events.length; eventIndex++) { 154 System.out.print(" " + events[eventIndex]); 155 156 if (events[eventIndex].getName().equals(situations[situationIndex].getEvent())) { 157 event = events[eventIndex]; 158 } 159 } 160 161 assertNotNull(event); 162 System.out.println(); 163 164 System.out.println("Executing event: " + event); 165 instance.invoke(situation, event); 166 167 assertTrue(instance.getValue(variableName) == situations[situationIndex].getValue()); 168 169 System.out.println( 170 "Variable: " + variableName + " = " + instance.getValue(variableName)); 171 System.out.println("------------------------------------------------------"); 172 } 173 174 System.out.println("Test completed."); 175 } 176 177 private static final TestSituation[] situations = 178 { 179 new TestSituation("lenya", "submit", false), 180 new TestSituation("roger", "reject", false), 181 new TestSituation("lenya", "submit", false), 182 new TestSituation("roger", "publish", true), 183 new TestSituation("roger", "deactivate", false)}; 184 185 188 public static class TestSituation { 189 private String user; 190 private String event; 191 private boolean value; 192 193 199 public TestSituation(String user, String event, boolean value) { 200 this.user = user; 201 this.event = event; 202 this.value = value; 203 } 204 205 209 public String getEvent() { 210 return event; 211 } 212 213 217 public String getUser() { 218 return user; 219 } 220 221 225 public boolean getValue() { 226 return value; 227 } 228 } 229 230 private static String documentTypeName = "simple"; 231 232 } 233 | Popular Tags |