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;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.util.Arrays;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.container.ContainerUtil;
027: import org.apache.avalon.framework.context.DefaultContext;
028: import org.apache.cocoon.Constants;
029: import org.apache.cocoon.components.ContextHelper;
030: import org.apache.cocoon.core.container.ContainerTestCase;
031: import org.apache.cocoon.environment.Context;
032: import org.apache.cocoon.environment.ObjectModelHelper;
033: import org.apache.cocoon.environment.Request;
034: import org.apache.cocoon.environment.commandline.CommandLineContext;
035: import org.apache.cocoon.environment.commandline.CommandLineRequest;
036: import org.apache.cocoon.environment.mock.MockEnvironment;
037: import org.apache.cocoon.util.IOUtils;
038: import org.apache.commons.lang.SystemUtils;
039: import org.apache.excalibur.source.SourceResolver;
040:
041: /**
042: * Base class for Lenya tests which need the context information.
043: */
044: public class LenyaTestCase extends ContainerTestCase {
045:
046: protected DefaultContext context;
047:
048: protected void addContext(DefaultContext context) {
049: super .addContext(context);
050:
051: this .context = context;
052:
053: String tempPath = System.getProperty("tempDir",
054: "build/lenya/temp");
055: String contextRoot = System.getProperty("contextRoot",
056: "build/lenya/webapp");
057: getLogger().info(
058: "Adding context root entry [" + contextRoot + "]");
059:
060: File contextRootDir = new File(contextRoot);
061: context.put("context-root", contextRootDir);
062:
063: String testPath = System.getProperty("testPath", "build/test");
064: File testRootDir = new File(testPath);
065: context.put("test-path", testRootDir);
066:
067: Context envContext = new CommandLineContext(contextRoot);
068: ContainerUtil.enableLogging(envContext, getLogger());
069: context.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, envContext);
070:
071: File tempDir = new File(tempPath);
072:
073: File workDir = new File(tempDir, "work");
074: context.put("work-directory", workDir);
075:
076: File cacheDir = new File(tempDir, "cache");
077: context.put(Constants.CONTEXT_CACHE_DIR, cacheDir);
078:
079: File uploadDir = new File(tempDir, "upload");
080: context.put(Constants.CONTEXT_UPLOAD_DIR, uploadDir);
081:
082: context.put(Constants.CONTEXT_CLASS_LOADER, LenyaTestCase.class
083: .getClassLoader());
084: context.put(Constants.CONTEXT_CLASSPATH,
085: getClassPath(contextRoot));
086: // context.put(Constants.CONTEXT_CONFIG_URL, conf.toURL());
087: context.put(Constants.CONTEXT_DEFAULT_ENCODING, "ISO-8859-1");
088: }
089:
090: private Request request = null;
091:
092: protected Request getRequest() {
093: return this .request;
094: }
095:
096: protected void prepare() throws Exception {
097: File testPath = new File("build/test");
098: final String resourceName = LenyaTestCase.class.getName()
099: .replace('.', '/')
100: + ".xtest";
101: File resourceFile = new File(testPath, resourceName.replace(
102: '/', File.separatorChar));
103:
104: if (resourceFile.isFile()) {
105: getLogger().debug(
106: "Loading resource "
107: + resourceFile.getAbsolutePath());
108: prepare(new FileInputStream(resourceFile));
109: } else {
110: getLogger().debug("Resource not found " + resourceName);
111: }
112:
113: SourceResolver resolver = (SourceResolver) getManager().lookup(
114: SourceResolver.ROLE);
115: MockEnvironment env = new MockEnvironment(resolver);
116:
117: String pathInfo = getWebappUrl();
118:
119: this .request = new CommandLineRequest(env, "", "", pathInfo,
120: new HashMap(), getRequestParameters());
121: context.put("object-model.request", this .request);
122:
123: Map objectModel = new HashMap();
124: objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
125: Context envContext = (Context) context
126: .get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
127: objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, envContext);
128: context.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectModel);
129:
130: }
131:
132: protected String getWebappUrl() {
133: return "/test/authoring/index.html";
134: }
135:
136: protected Map getRequestParameters() {
137: return new HashMap();
138: }
139:
140: /**
141: * This builds the important ClassPath used by this class. It does so in a neutral way. It
142: * iterates in alphabetical order through every file in the lib directory and adds it to the
143: * classpath.
144: *
145: * Also, we add the files to the ClassLoader for the Cocoon system. In order to protect
146: * ourselves from skitzofrantic classloaders, we need to work with a known one.
147: *
148: * @param context The context path
149: * @return a <code>String</code> value
150: */
151: protected String getClassPath(final String context) {
152: StringBuffer buildClassPath = new StringBuffer();
153:
154: String classDir = context + "/WEB-INF/classes";
155: buildClassPath.append(classDir);
156:
157: File root = new File(context + "/WEB-INF/lib");
158: if (root.isDirectory()) {
159: File[] libraries = root.listFiles();
160: Arrays.sort(libraries);
161: for (int i = 0; i < libraries.length; i++) {
162: if (libraries[i].getAbsolutePath().endsWith(".jar")) {
163: buildClassPath
164: .append(File.pathSeparatorChar)
165: .append(
166: IOUtils
167: .getFullFilename(libraries[i]));
168: }
169: }
170: }
171:
172: buildClassPath.append(File.pathSeparatorChar).append(
173: SystemUtils.JAVA_CLASS_PATH);
174:
175: // Extra class path is necessary for non-classloader-aware java compilers to compile XSPs
176: // buildClassPath.append(File.pathSeparatorChar)
177: // .append(getExtraClassPath(context));
178:
179: getLogger().info("Context classpath: " + buildClassPath);
180: return buildClassPath.toString();
181: }
182:
183: }
|