KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > FaceletTestCase


1 /**
2  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3  * Licensed under the Common Development and Distribution License,
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.sun.com/cddl/
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15
16 package com.sun.facelets;
17
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.faces.FacesException;
27 import javax.faces.FactoryFinder;
28 import javax.faces.application.Application;
29 import javax.faces.application.ApplicationFactory;
30 import javax.faces.component.UIViewRoot;
31 import javax.faces.context.FacesContext;
32 import javax.faces.context.FacesContextFactory;
33 import javax.faces.context.ResponseWriter;
34 import javax.faces.lifecycle.LifecycleFactory;
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.ServletContextEvent JavaDoc;
37 import javax.servlet.ServletContextListener JavaDoc;
38
39 import com.sun.facelets.compiler.Compiler;
40 import com.sun.facelets.compiler.SAXCompiler;
41 import com.sun.facelets.impl.DefaultFaceletFactory;
42 import com.sun.facelets.impl.ResourceResolver;
43 import com.sun.facelets.mock.MockHttpServletResponse;
44 import com.sun.facelets.mock.MockServletContext;
45 import com.sun.facelets.mock.MockHttpServletRequest;
46 import com.sun.faces.util.DebugUtil;
47
48 import junit.framework.TestCase;
49
50 public abstract class FaceletTestCase extends TestCase implements ResourceResolver {
51
52     private final String JavaDoc filePath = this.getDirectory();
53
54     private final URI JavaDoc base = this.getContext();
55
56     protected MockServletContext servletContext;
57
58     protected MockHttpServletRequest servletRequest;
59
60     protected MockHttpServletResponse servletResponse;
61
62     private ApplicationFactory factoryApplication;
63
64     private FacesContextFactory factoryFacesContext;
65
66     private LifecycleFactory factoryLifecycle;
67
68     private boolean initialized = false;
69
70     protected URI JavaDoc getContext() {
71         try {
72             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
73             URL JavaDoc url = cl.getResource(this.filePath);
74             if (url == null) {
75                 throw new FileNotFoundException JavaDoc(cl.getResource("").getFile()
76                         + this.filePath + " was not found");
77             } else {
78                 return new URI JavaDoc(url.toString());
79             }
80         } catch (Exception JavaDoc e) {
81             throw new RuntimeException JavaDoc("Error Initializing Context", e);
82         }
83     }
84
85     protected URL JavaDoc getLocalFile(String JavaDoc name) throws FileNotFoundException JavaDoc {
86         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
87         URL JavaDoc url = cl.getResource(this.filePath + "/" + name);
88         if (url == null) {
89             throw new FileNotFoundException JavaDoc(cl.getResource("").getFile() + name
90                     + " was not found");
91         }
92         return url;
93     }
94
95     private String JavaDoc getDirectory() {
96         return this.getClass().getName().substring(0,
97                 this.getClass().getName().lastIndexOf('.')).replace('.', '/') + "/";
98     }
99
100     protected void setUp() throws Exception JavaDoc {
101         super.setUp();
102         URI JavaDoc context = this.getContext();
103
104         this.servletContext = new MockServletContext(context);
105         this.servletRequest = new MockHttpServletRequest(this.servletContext,
106                 context);
107         this.servletResponse = new MockHttpServletResponse();
108
109         // initialize Faces
110
this.initFaces();
111
112         FacesContext faces = this.factoryFacesContext.getFacesContext(this.servletContext,
113                 this.servletRequest, this.servletResponse,
114                 this.factoryLifecycle
115                         .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE));
116         
117         
118         Compiler JavaDoc c = new SAXCompiler();
119         //c.setTrimmingWhitespace(true);
120
FaceletFactory factory = new DefaultFaceletFactory(c, this);
121         FaceletFactory.setInstance(factory);
122         
123         faces.setViewRoot(faces.getApplication().getViewHandler().createView(faces, "/test"));
124         
125         ResponseWriter rw = faces.getRenderKit().createResponseWriter(new StringWriter JavaDoc(), null, null);
126         faces.setResponseWriter(rw);
127     }
128     
129     public void setRequest(String JavaDoc method, String JavaDoc path, OutputStream JavaDoc os) {
130         this.servletRequest = new MockHttpServletRequest(this.servletContext, method, path);
131         //this.servletResponse = new MockHttpServletResponse(os);
132
this.factoryFacesContext.getFacesContext(this.servletContext,
133                 this.servletRequest, this.servletResponse,
134                 this.factoryLifecycle
135                         .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE));
136     }
137
138     private void initFaces() throws Exception JavaDoc {
139         if (!this.initialized) {
140             this.initialized = true;
141             this.initFacesListener(this.servletContext);
142             this.factoryApplication = (ApplicationFactory) FactoryFinder
143                     .getFactory(FactoryFinder.APPLICATION_FACTORY);
144             this.factoryFacesContext = (FacesContextFactory) FactoryFinder
145                     .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
146             this.factoryLifecycle = (LifecycleFactory) FactoryFinder
147                     .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
148
149             /*
150             Application application = this.factoryApplication.getApplication();
151             application.setViewHandler(new FaceletViewHandler(application
152                     .getViewHandler()));
153             */

154         }
155     }
156
157     protected void initFacesListener(ServletContext JavaDoc context) throws Exception JavaDoc {
158         ServletContextListener JavaDoc listener;
159         Class JavaDoc type;
160         try {
161             type = Class.forName("com.sun.faces.config.ConfigureListener");
162         } catch (ClassNotFoundException JavaDoc e) {
163             try {
164                 type = Class.forName("");
165             } catch (ClassNotFoundException JavaDoc e2) {
166                 throw new FileNotFoundException JavaDoc(
167                         "Either JSF-RI or MyFaces needs to be on the classpath with their supported Jars");
168             }
169         }
170         listener = (ServletContextListener JavaDoc) type.newInstance();
171         listener.contextInitialized(new ServletContextEvent JavaDoc(context));
172     }
173
174     protected void tearDown() throws Exception JavaDoc {
175         super.tearDown();
176         this.servletContext = null;
177     }
178
179     public URL JavaDoc resolveUrl(String JavaDoc path) {
180         try {
181             return new URL JavaDoc(this.getContext().toURL(), path.substring(1));
182         } catch (Exception JavaDoc e) {
183             throw new FacesException(e);
184         }
185     }
186
187 }
188
Popular Tags