KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > workflow > WorkflowTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: WorkflowTest.java 42625 2004-03-04 15:45:03Z egli $ */
19
20 package org.apache.lenya.cms.workflow;
21
22 import java.io.File JavaDoc;
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 /**
49  * To change the template for this generated type comment go to
50  * Window>Preferences>Java>Code Generation>Code and Comments
51  */

52 public class WorkflowTest extends AccessControlTest {
53     /**
54      * Constructor.
55      * @param test The test to execute.
56      */

57     public WorkflowTest(String JavaDoc test) {
58         super(test);
59     }
60
61     /**
62      * The main program for the WorkflowTest class
63      *
64      * @param args The command line arguments
65      */

66     public static void main(String JavaDoc[] args) {
67         args = PublicationHelper.extractPublicationArguments(args);
68         documentTypeName = args[0];
69         TestRunner.run(getSuite());
70     }
71
72     /**
73      * Returns the test suite.
74      * @return A test suite.
75      */

76     public static Test getSuite() {
77         return new TestSuite(WorkflowTest.class);
78     }
79
80     private static final String JavaDoc variableName = "is-live";
81     protected static final String JavaDoc URL = "/authoring/index.html";
82
83     /**
84      * Tests the workflow.
85      * @throws DocumentTypeBuildException when something went wrong.
86      * @throws WorkflowException when something went wrong.
87      * @throws AccessControlException when something went wrong.
88      * @throws PageEnvelopeException when something went wrong.
89      * @throws DocumentBuildException when something went wrong.
90      */

91     public void testWorkflow()
92         throws
93             DocumentTypeBuildException,
94             WorkflowException,
95             AccessControlException,
96             PageEnvelopeException,
97             DocumentBuildException {
98         Publication publication = PublicationHelper.getPublication();
99         String JavaDoc url = "/" + publication.getId() + URL;
100         Document document = publication.getDocumentBuilder().buildDocument(publication, url);
101
102         File JavaDoc configDir = new File JavaDoc(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 JavaDoc workflowId = type.getWorkflowFileName();
109
110         WorkflowFactory factory = WorkflowFactory.newInstance();
111
112         String JavaDoc[] 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     /**
186      * A test situation.
187      */

188     public static class TestSituation {
189         private String JavaDoc user;
190         private String JavaDoc event;
191         private boolean value;
192
193         /**
194          * Creates a new test situation.
195          * @param user The user.
196          * @param event The event.
197          * @param value The variable value.
198          */

199         public TestSituation(String JavaDoc user, String JavaDoc event, boolean value) {
200             this.user = user;
201             this.event = event;
202             this.value = value;
203         }
204
205         /**
206          * Returns the event.
207          * @return An event.
208          */

209         public String JavaDoc getEvent() {
210             return event;
211         }
212
213         /**
214          * Returns the user.
215          * @return A string.
216          */

217         public String JavaDoc getUser() {
218             return user;
219         }
220
221         /**
222          * Returns the value.
223          * @return A value.
224          */

225         public boolean getValue() {
226             return value;
227         }
228     }
229
230     private static String JavaDoc documentTypeName = "simple";
231
232 }
233
Popular Tags