001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: WorkflowTest.java 507914 2007-02-15 12:15:30Z andreas $ */
020:
021: package org.apache.lenya.cms.workflow;
022:
023: import org.apache.lenya.ac.AccessControlException;
024: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
025: import org.apache.lenya.cms.publication.Document;
026: import org.apache.lenya.cms.publication.DocumentFactory;
027: import org.apache.lenya.cms.publication.Publication;
028: import org.apache.lenya.cms.repository.RepositoryException;
029: import org.apache.lenya.cms.repository.Session;
030: import org.apache.lenya.workflow.Version;
031: import org.apache.lenya.workflow.WorkflowException;
032: import org.apache.lenya.workflow.Workflowable;
033:
034: /**
035: * To change the template for this generated type comment go to Window>Preferences>Java>Code
036: * Generation>Code and Comments
037: */
038: public class WorkflowTest extends AbstractAccessControlTest {
039:
040: private static final String variableName = "is_live";
041: protected static final String URL = "/authoring/index.html";
042:
043: protected String getWebappUrl() {
044: return "/test" + URL;
045: }
046:
047: /**
048: * Tests the workflow.
049: * @throws Exception when something went wrong.
050: */
051: public void testWorkflow() throws Exception {
052: Publication publication = getPublication("test");
053: String url = "/" + publication.getId() + URL;
054: DocumentFactory map = getFactory();
055: Document document = map.getFromURL(url);
056:
057: document.getRepositoryNode().lock();
058:
059: Session session = getSession(submitSituation);
060: Workflowable workflowable = WorkflowUtil.getWorkflowable(
061: getManager(), session, getLogger(), document);
062: if (workflowable.getVersions().length > 0) {
063: Version version = workflowable.getLatestVersion();
064: if (version.getValue(variableName) == true) {
065: invoke(document, deactivateSituation);
066: } else if (version.getState().equals("review")) {
067: invoke(document, rejectSituation);
068: }
069: }
070:
071: for (int situationIndex = 0; situationIndex < situations.length; situationIndex++) {
072: TestSituation situation = situations[situationIndex];
073: invoke(document, situation);
074: }
075:
076: document.getRepositoryNode().unlock();
077:
078: getLogger().info("Test completed.");
079: }
080:
081: protected void invoke(Document document, TestSituation situation)
082: throws AccessControlException, RepositoryException,
083: WorkflowException {
084: Session session = getSession(situation);
085: Workflowable instance = new DocumentWorkflowable(getManager(),
086: session, document, getLogger());
087: assertNotNull(instance);
088:
089: String event = situation.getEvent();
090:
091: getLogger().info("Event: " + event);
092:
093: WorkflowUtil.invoke(getManager(), session, getLogger(),
094: document, event);
095:
096: boolean value = instance.getLatestVersion().getValue(
097: variableName);
098:
099: getLogger().info("Variable: " + variableName + " = " + value);
100: getLogger()
101: .info(
102: "------------------------------------------------------");
103:
104: assertEquals(value, situation.getValue());
105: }
106:
107: protected Session getSession(TestSituation situation)
108: throws AccessControlException, RepositoryException {
109: Session session = login(situation.getUser());
110: getLogger().info(
111: "User: [" + session.getIdentity().getUser() + "]");
112: return session;
113: }
114:
115: private static final TestSituation submitSituation = new TestSituation(
116: "lenya", "submit", false);
117: private static final TestSituation rejectSituation = new TestSituation(
118: "alice", "reject", false);
119: private static final TestSituation deactivateSituation = new TestSituation(
120: "alice", "deactivate", false);
121: private static final TestSituation publishSituation = new TestSituation(
122: "alice", "publish", true);
123:
124: private static final TestSituation[] situations = {
125: submitSituation, rejectSituation, submitSituation,
126: publishSituation, deactivateSituation };
127:
128: /**
129: * A test situation.
130: */
131: public static class TestSituation {
132: private String user;
133: private String event;
134: private boolean value;
135:
136: /**
137: * Creates a new test situation.
138: * @param _user The user.
139: * @param _event The event.
140: * @param _value The variable value.
141: */
142: public TestSituation(String _user, String _event, boolean _value) {
143: this .user = _user;
144: this .event = _event;
145: this .value = _value;
146: }
147:
148: /**
149: * Returns the event.
150: * @return An event.
151: */
152: public String getEvent() {
153: return this .event;
154: }
155:
156: /**
157: * Returns the user.
158: * @return A string.
159: */
160: public String getUser() {
161: return this .user;
162: }
163:
164: /**
165: * Returns the value.
166: * @return A value.
167: */
168: public boolean getValue() {
169: return this.value;
170: }
171: }
172:
173: }
|