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: package org.apache.lenya.cms.usecase;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
026: import org.apache.lenya.cms.repository.Session;
027: import org.apache.lenya.cms.usecase.impl.TestUsecaseInvoker;
028:
029: /**
030: * Usecase test base class.
031: */
032: public abstract class AbstractUsecaseTest extends
033: AbstractAccessControlTest {
034:
035: /**
036: * The test.
037: * @throws Exception
038: */
039: public void testUsecase() throws Exception {
040:
041: Session session = getSession();
042: prepareUsecase();
043:
044: UsecaseInvoker invoker = null;
045: try {
046: invoker = (UsecaseInvoker) getManager().lookup(
047: TestUsecaseInvoker.ROLE);
048: invoker.setTestSession(session);
049: invoker.invoke(getRequest().getPathInfo(),
050: getUsecaseName(), getParameters());
051:
052: this .targetUrl = invoker.getTargetUrl();
053:
054: List errorMessages = invoker.getErrorMessages();
055: for (Iterator i = errorMessages.iterator(); i.hasNext();) {
056: UsecaseMessage message = (UsecaseMessage) i.next();
057: String m = message.getMessage();
058: String[] params = message.getParameters();
059: if (params != null) {
060: for (int j = 0; j < params.length; j++) {
061: m += " [" + params[j] + "]";
062: }
063: }
064: getLogger().error("Usecase error: " + m);
065: }
066:
067: assertEquals(invoker.getResult(), UsecaseInvoker.SUCCESS);
068: assertEquals(invoker.getErrorMessages().size(), 0);
069: } finally {
070: if (invoker != null) {
071: getManager().release(invoker);
072: }
073: }
074:
075: checkPostconditions();
076:
077: }
078:
079: private String targetUrl;
080:
081: protected String getTargetUrl() {
082: if (this .targetUrl == null) {
083: throw new IllegalStateException(
084: "The usecase has not yet been executed.");
085: }
086: return this .targetUrl;
087: }
088:
089: protected void prepareUsecase() throws Exception {
090: }
091:
092: protected Map getParameters() {
093: return new HashMap();
094: }
095:
096: protected abstract String getUsecaseName();
097:
098: protected void checkPostconditions() throws Exception {
099: }
100:
101: }
|