1 15 16 package com.sun.facelets; 17 18 import java.io.FileNotFoundException ; 19 import java.io.OutputStream ; 20 import java.io.StringWriter ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 import java.net.URL ; 24 import java.util.Set ; 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 ; 36 import javax.servlet.ServletContextEvent ; 37 import javax.servlet.ServletContextListener ; 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 filePath = this.getDirectory(); 53 54 private final URI 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 getContext() { 71 try { 72 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 73 URL url = cl.getResource(this.filePath); 74 if (url == null) { 75 throw new FileNotFoundException (cl.getResource("").getFile() 76 + this.filePath + " was not found"); 77 } else { 78 return new URI (url.toString()); 79 } 80 } catch (Exception e) { 81 throw new RuntimeException ("Error Initializing Context", e); 82 } 83 } 84 85 protected URL getLocalFile(String name) throws FileNotFoundException { 86 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 87 URL url = cl.getResource(this.filePath + "/" + name); 88 if (url == null) { 89 throw new FileNotFoundException (cl.getResource("").getFile() + name 90 + " was not found"); 91 } 92 return url; 93 } 94 95 private String getDirectory() { 96 return this.getClass().getName().substring(0, 97 this.getClass().getName().lastIndexOf('.')).replace('.', '/') + "/"; 98 } 99 100 protected void setUp() throws Exception { 101 super.setUp(); 102 URI 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 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 c = new SAXCompiler(); 119 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 (), null, null); 126 faces.setResponseWriter(rw); 127 } 128 129 public void setRequest(String method, String path, OutputStream os) { 130 this.servletRequest = new MockHttpServletRequest(this.servletContext, method, path); 131 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 { 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 154 } 155 } 156 157 protected void initFacesListener(ServletContext context) throws Exception { 158 ServletContextListener listener; 159 Class type; 160 try { 161 type = Class.forName("com.sun.faces.config.ConfigureListener"); 162 } catch (ClassNotFoundException e) { 163 try { 164 type = Class.forName(""); 165 } catch (ClassNotFoundException e2) { 166 throw new FileNotFoundException ( 167 "Either JSF-RI or MyFaces needs to be on the classpath with their supported Jars"); 168 } 169 } 170 listener = (ServletContextListener ) type.newInstance(); 171 listener.contextInitialized(new ServletContextEvent (context)); 172 } 173 174 protected void tearDown() throws Exception { 175 super.tearDown(); 176 this.servletContext = null; 177 } 178 179 public URL resolveUrl(String path) { 180 try { 181 return new URL (this.getContext().toURL(), path.substring(1)); 182 } catch (Exception e) { 183 throw new FacesException(e); 184 } 185 } 186 187 } 188 | Popular Tags |