001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * Licensed under the Common Development and Distribution License,
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.sun.com/cddl/
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
012: * implied. See the License for the specific language governing
013: * permissions and limitations under the License.
014: */package com.sun.facelets;
015:
016: import java.io.FileNotFoundException;
017: import java.io.OutputStream;
018: import java.io.StringWriter;
019: import java.net.URI;
020: import java.net.URISyntaxException;
021: import java.net.URL;
022: import java.util.Set;
023:
024: import javax.faces.FacesException;
025: import javax.faces.FactoryFinder;
026: import javax.faces.application.Application;
027: import javax.faces.application.ApplicationFactory;
028: import javax.faces.component.UIViewRoot;
029: import javax.faces.context.FacesContext;
030: import javax.faces.context.FacesContextFactory;
031: import javax.faces.context.ResponseWriter;
032: import javax.faces.lifecycle.LifecycleFactory;
033: import javax.servlet.ServletContext;
034: import javax.servlet.ServletContextEvent;
035: import javax.servlet.ServletContextListener;
036:
037: import com.sun.facelets.compiler.Compiler;
038: import com.sun.facelets.compiler.SAXCompiler;
039: import com.sun.facelets.impl.DefaultFaceletFactory;
040: import com.sun.facelets.impl.ResourceResolver;
041: import com.sun.facelets.mock.MockHttpServletResponse;
042: import com.sun.facelets.mock.MockServletContext;
043: import com.sun.facelets.mock.MockHttpServletRequest;
044: import com.sun.faces.util.DebugUtil;
045:
046: import junit.framework.TestCase;
047:
048: public abstract class FaceletTestCase extends TestCase implements
049: ResourceResolver {
050:
051: private final String filePath = this .getDirectory();
052:
053: private final URI base = this .getContext();
054:
055: protected MockServletContext servletContext;
056:
057: protected MockHttpServletRequest servletRequest;
058:
059: protected MockHttpServletResponse servletResponse;
060:
061: private ApplicationFactory factoryApplication;
062:
063: private FacesContextFactory factoryFacesContext;
064:
065: private LifecycleFactory factoryLifecycle;
066:
067: private boolean initialized = false;
068:
069: protected URI getContext() {
070: try {
071: ClassLoader cl = Thread.currentThread()
072: .getContextClassLoader();
073: URL url = cl.getResource(this .filePath);
074: if (url == null) {
075: throw new FileNotFoundException(cl.getResource("")
076: .getFile()
077: + this .filePath + " was not found");
078: } else {
079: return new URI(url.toString());
080: }
081: } catch (Exception e) {
082: throw new RuntimeException("Error Initializing Context", e);
083: }
084: }
085:
086: protected URL getLocalFile(String name)
087: throws FileNotFoundException {
088: ClassLoader cl = Thread.currentThread().getContextClassLoader();
089: URL url = cl.getResource(this .filePath + "/" + name);
090: if (url == null) {
091: throw new FileNotFoundException(cl.getResource("")
092: .getFile()
093: + name + " was not found");
094: }
095: return url;
096: }
097:
098: private String getDirectory() {
099: return this .getClass().getName().substring(0,
100: this .getClass().getName().lastIndexOf('.')).replace(
101: '.', '/')
102: + "/";
103: }
104:
105: protected void setUp() throws Exception {
106: super .setUp();
107: URI context = this .getContext();
108:
109: this .servletContext = new MockServletContext(context);
110: this .servletRequest = new MockHttpServletRequest(
111: this .servletContext, context);
112: this .servletResponse = new MockHttpServletResponse();
113:
114: // initialize Faces
115: this .initFaces();
116:
117: FacesContext faces = this .factoryFacesContext
118: .getFacesContext(
119: this .servletContext,
120: this .servletRequest,
121: this .servletResponse,
122: this .factoryLifecycle
123: .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE));
124:
125: Compiler c = new SAXCompiler();
126: //c.setTrimmingWhitespace(true);
127: FaceletFactory factory = new DefaultFaceletFactory(c, this );
128: FaceletFactory.setInstance(factory);
129:
130: faces.setViewRoot(faces.getApplication().getViewHandler()
131: .createView(faces, "/test"));
132:
133: ResponseWriter rw = faces.getRenderKit().createResponseWriter(
134: new StringWriter(), null, null);
135: faces.setResponseWriter(rw);
136: }
137:
138: public void setRequest(String method, String path, OutputStream os) {
139: this .servletRequest = new MockHttpServletRequest(
140: this .servletContext, method, path);
141: //this.servletResponse = new MockHttpServletResponse(os);
142: this .factoryFacesContext
143: .getFacesContext(
144: this .servletContext,
145: this .servletRequest,
146: this .servletResponse,
147: this .factoryLifecycle
148: .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE));
149: }
150:
151: private void initFaces() throws Exception {
152: if (!this .initialized) {
153: this .initialized = true;
154: this .initFacesListener(this .servletContext);
155: this .factoryApplication = (ApplicationFactory) FactoryFinder
156: .getFactory(FactoryFinder.APPLICATION_FACTORY);
157: this .factoryFacesContext = (FacesContextFactory) FactoryFinder
158: .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
159: this .factoryLifecycle = (LifecycleFactory) FactoryFinder
160: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
161:
162: /*
163: Application application = this.factoryApplication.getApplication();
164: application.setViewHandler(new FaceletViewHandler(application
165: .getViewHandler()));
166: */
167: }
168: }
169:
170: protected void initFacesListener(ServletContext context)
171: throws Exception {
172: ServletContextListener listener;
173: Class type;
174: try {
175: type = Class
176: .forName("com.sun.faces.config.ConfigureListener");
177: } catch (ClassNotFoundException e) {
178: try {
179: type = Class.forName("");
180: } catch (ClassNotFoundException e2) {
181: throw new FileNotFoundException(
182: "Either JSF-RI or MyFaces needs to be on the classpath with their supported Jars");
183: }
184: }
185: listener = (ServletContextListener) type.newInstance();
186: listener.contextInitialized(new ServletContextEvent(context));
187: }
188:
189: protected void tearDown() throws Exception {
190: super .tearDown();
191: this .servletContext = null;
192: }
193:
194: public URL resolveUrl(String path) {
195: try {
196: return new URL(this .getContext().toURL(), path.substring(1));
197: } catch (Exception e) {
198: throw new FacesException(e);
199: }
200: }
201:
202: }
|