Author: yradtsevich
Date: 2009-08-11 12:41:44 -0400 (Tue, 11 Aug 2009)
New Revision: 17047
Added:
workspace/yradtsevich/mock/
workspace/yradtsevich/mock/.classpath
workspace/yradtsevich/mock/.project
workspace/yradtsevich/mock/TagEvaluator/
workspace/yradtsevich/mock/lib/
workspace/yradtsevich/mock/lib/jsf-api.jar
workspace/yradtsevich/mock/lib/jsf-impl.jar
workspace/yradtsevich/mock/lib/mockrunner.jar
workspace/yradtsevich/mock/lib/shale-test-1.0.5.jar
workspace/yradtsevich/mock/src/
workspace/yradtsevich/mock/src/org/
workspace/yradtsevich/mock/src/org/apache/
workspace/yradtsevich/mock/src/org/apache/shale/
workspace/yradtsevich/mock/src/org/apache/shale/test/
workspace/yradtsevich/mock/src/org/apache/shale/test/mock/
workspace/yradtsevich/mock/src/org/apache/shale/test/mock/MockHttpServletResponse.java
workspace/yradtsevich/mock/src/org/jboss/
workspace/yradtsevich/mock/src/org/jboss/tools/
workspace/yradtsevich/mock/src/org/jboss/tools/vpe/
workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/
workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/MockServerEnvironment.java
workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluator.java
workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluatorSnippet.java
Log:
Example of using mock objects for jsp2html conversion. Initial commit.
Added: workspace/yradtsevich/mock/.classpath
===================================================================
--- workspace/yradtsevich/mock/.classpath (rev 0)
+++ workspace/yradtsevich/mock/.classpath 2009-08-11 16:41:44 UTC (rev 17047)
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con"
path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache
Tomcat v6.0"/>
+ <classpathentry kind="lib" path="lib/jsf-api.jar"/>
+ <classpathentry kind="lib" path="lib/jsf-impl.jar"/>
+ <classpathentry kind="lib" path="lib/mockrunner.jar"/>
+ <classpathentry kind="lib" path="lib/shale-test-1.0.5.jar"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
Added: workspace/yradtsevich/mock/.project
===================================================================
--- workspace/yradtsevich/mock/.project (rev 0)
+++ workspace/yradtsevich/mock/.project 2009-08-11 16:41:44 UTC (rev 17047)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>TagEvaluator</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
Added: workspace/yradtsevich/mock/lib/jsf-api.jar
===================================================================
(Binary files differ)
Property changes on: workspace/yradtsevich/mock/lib/jsf-api.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: workspace/yradtsevich/mock/lib/jsf-impl.jar
===================================================================
(Binary files differ)
Property changes on: workspace/yradtsevich/mock/lib/jsf-impl.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: workspace/yradtsevich/mock/lib/mockrunner.jar
===================================================================
(Binary files differ)
Property changes on: workspace/yradtsevich/mock/lib/mockrunner.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: workspace/yradtsevich/mock/lib/shale-test-1.0.5.jar
===================================================================
(Binary files differ)
Property changes on: workspace/yradtsevich/mock/lib/shale-test-1.0.5.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added:
workspace/yradtsevich/mock/src/org/apache/shale/test/mock/MockHttpServletResponse.java
===================================================================
---
workspace/yradtsevich/mock/src/org/apache/shale/test/mock/MockHttpServletResponse.java
(rev 0)
+++
workspace/yradtsevich/mock/src/org/apache/shale/test/mock/MockHttpServletResponse.java 2009-08-11
16:41:44 UTC (rev 17047)
@@ -0,0 +1,457 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.test.mock;
+
+import java.io.ByteArrayOutputStream;
+import java.io.CharArrayWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * <p>Mock implementation of
<code>HttpServletResponse</code>.</p>
+ *
+ * Yahor Radtsevich (yradtsevich): this is NOT the original implementation
+ * provided by The Apache Software Foundation,
+ * method {@link #setLocale(Locale)} has been modified.
+ * <P>
+ * $Id$
+ */
+
+public class MockHttpServletResponse implements HttpServletResponse {
+
+
+ // ------------------------------------------------------------ Constructors
+
+
+ /**
+ * <p>Return a default instance.</p>
+ */
+ public MockHttpServletResponse() { }
+
+
+ // ----------------------------------------------------- Mock Object Methods
+
+
+ /**
+ * <p>Retrieve the first value that was set for the specified header,
+ * if any. Otherwise, return <code>null</code>.</p>
+ *
+ * @param name Header name to look up
+ */
+ public String getHeader(String name) {
+ String match = name + ":";
+ Iterator headers = this.headers.iterator();
+ while (headers.hasNext()) {
+ String header = (String) headers.next();
+ if (header.startsWith(match)) {
+ return header.substring(match.length() + 1).trim();
+ }
+ }
+ return null;
+ }
+
+
+ /**
+ * <p>Return the text message for the HTTP status that was set.</p>
+ */
+ public String getMessage() {
+ return this.message;
+ }
+
+
+ /**
+ * <p>Return the HTTP status code that was set.</p>
+ */
+ public int getStatus() {
+ return this.status;
+ }
+
+
+ /**
+ * <p>Set the <code>ServletOutputStream</code> to be returned by a
call to
+ * <code>getOutputStream()</code>.</p>
+ *
+ * @param stream The <code>ServletOutputStream</code> instance to use
+ *
+ * @deprecated Let the <code>getOutputStream()</code> method create and
+ * return an instance of <code>MockServletOutputStream</code> for you
+ */
+ public void setOutputStream(ServletOutputStream stream) {
+ this.stream = stream;
+ }
+
+
+ /**
+ * <p>Set the <code>PrintWriter</code> to be returned by a call to
+ * <code>getWriter()</code>.</p>
+ *
+ * @param writer The <code>PrintWriter</code> instance to use
+ *
+ * @deprecated Let the <code>getWriter()</code> method create and return
+ * an instance of <code>MockPrintWriter</code> for you
+ */
+ public void setWriter(PrintWriter writer) {
+ this.writer = writer;
+ }
+
+
+ // ------------------------------------------------------ Instance Variables
+
+
+ private String encoding = "ISO-8859-1";
+ private String contentType = "text/html";
+ private List headers = new ArrayList();
+ private String message = null;
+ private int status = HttpServletResponse.SC_OK;
+ private ServletOutputStream stream = null;
+ private PrintWriter writer = null;
+
+
+ // -------------------------------------------- HttpServletResponse Methods
+
+
+ /** {@inheritDoc} */
+ public void addCookie(Cookie cookie) {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void addDateHeader(String name, long value) {
+
+ headers.add(name + ": " + formatDate(value));
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void addHeader(String name, String value) {
+
+ headers.add(name + ": " + value);
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void addIntHeader(String name, int value) {
+
+ headers.add(name + ": " + value);
+
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean containsHeader(String name) {
+
+ return getHeader(name) != null;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public String encodeRedirectUrl(String url) {
+
+ return encodeRedirectURL(url);
+
+ }
+
+
+ /** {@inheritDoc} */
+ public String encodeRedirectURL(String url) {
+
+ return url;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public String encodeUrl(String url) {
+
+ return encodeURL(url);
+
+ }
+
+
+ /** {@inheritDoc} */
+ public String encodeURL(String url) {
+
+ return url;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void sendError(int status) {
+
+ this.status = status;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void sendError(int status, String message) {
+
+ this.status = status;
+ this.message = message;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void sendRedirect(String location) {
+
+ this.status = HttpServletResponse.SC_MOVED_TEMPORARILY;
+ this.message = location;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setDateHeader(String name, long value) {
+
+ removeHeader(name);
+ addDateHeader(name, value);
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setHeader(String name, String value) {
+
+ removeHeader(name);
+ addHeader(name, value);
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setIntHeader(String name, int value) {
+
+ removeHeader(name);
+ addIntHeader(name, value);
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setStatus(int status) {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setStatus(int status, String message) {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ // ------------------------------------------------ ServletResponse Methods
+
+
+ /** {@inheritDoc} */
+ public void flushBuffer() {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public int getBufferSize() {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public String getCharacterEncoding() {
+
+ return this.encoding;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public String getContentType() {
+
+ return this.contentType;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public Locale getLocale() {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public ServletOutputStream getOutputStream() throws IOException {
+
+ if (stream == null) {
+ if (writer != null) {
+ throw new IllegalStateException("Cannot call getOutputStream() after
getWriter() has been called");
+ }
+ stream = new MockServletOutputStream(new ByteArrayOutputStream());
+ }
+ return stream;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public PrintWriter getWriter() throws IOException {
+
+ if (writer == null) {
+ if (stream != null) {
+ throw new IllegalStateException("Cannot call getWriter() after
getOutputStream() was called");
+ }
+ writer = new MockPrintWriter(new CharArrayWriter());
+ }
+ return writer;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean isCommitted() {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void reset() {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void resetBuffer() {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setBufferSize(int size) {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setCharacterEncoding(String charset) {
+
+ this.encoding = charset;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setContentLength(int length) {
+
+ throw new UnsupportedOperationException();
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setContentType(String type) {
+
+ contentType = type;
+
+ }
+
+
+ /** {@inheritDoc} */
+ public void setLocale(Locale locale) {
+ // commented by Yahor Radtsevich (yradtsevich)
+ // XXX: throw new UnsupportedOperationException();
+ }
+
+
+ // --------------------------------------------------------- Private Methods
+
+
+ /**
+ * <p>The date formatting helper we will use in
<code>httpTimestamp()</code>.
+ * Note that usage of this helper must be synchronized.</p>
+ */
+ private static SimpleDateFormat format =
+ new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
+ static {
+ format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ }
+
+
+ /**
+ * <p>Return a properly formatted String version of the specified
+ * date/time, formatted as required by the HTTP specification.</p>
+ *
+ * @param date Date/time, expressed as milliseconds since the epoch
+ */
+ private String formatDate(long date) {
+ return format.format(new Date(date));
+ }
+
+
+ /**
+ * <p>Remove any header that has been set with the specific name.</p>
+ *
+ * @param name Header name to look up
+ */
+ private void removeHeader(String name) {
+ String match = name + ":";
+ Iterator headers = this.headers.iterator();
+ while (headers.hasNext()) {
+ String header = (String) headers.next();
+ if (header.startsWith(match)) {
+ headers.remove();
+ return;
+ }
+ }
+ }
+
+
+}
Added: workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/MockServerEnvironment.java
===================================================================
--- workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/MockServerEnvironment.java
(rev 0)
+++
workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/MockServerEnvironment.java 2009-08-11
16:41:44 UTC (rev 17047)
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.tools.vpe.mock;
+
+import javax.faces.FactoryFinder;
+import javax.faces.application.ApplicationFactory;
+import javax.faces.component.UIViewRoot;
+import javax.faces.lifecycle.LifecycleFactory;
+import javax.faces.render.RenderKitFactory;
+
+import org.apache.shale.test.mock.MockApplication;
+import org.apache.shale.test.mock.MockExternalContext;
+import org.apache.shale.test.mock.MockFacesContext;
+import org.apache.shale.test.mock.MockFacesContextFactory;
+import org.apache.shale.test.mock.MockHttpServletRequest;
+import org.apache.shale.test.mock.MockHttpServletResponse;
+import org.apache.shale.test.mock.MockHttpSession;
+import org.apache.shale.test.mock.MockLifecycle;
+import org.apache.shale.test.mock.MockLifecycleFactory;
+import org.apache.shale.test.mock.MockRenderKit;
+import org.apache.shale.test.mock.MockResponseWriter;
+import org.apache.shale.test.mock.MockServletConfig;
+import org.apache.shale.test.mock.MockServletContext;
+
+import com.mockrunner.mock.web.MockPageContext;
+
+/**
+ * Provides some mock variables that are available on a real servlet container
+ * including JSF environment variables.
+ *
+ * @author Yahor Radtsevich (yradtsevich). This implementation is based on
+ * org.apache.shale.test.base.AbstractJsfTestCase.
+ */
+public class MockServerEnvironment {
+ protected final MockApplication application;
+ protected final MockServletConfig config;
+ protected final MockExternalContext externalContext;
+ protected final MockFacesContext facesContext;
+ protected final MockFacesContextFactory facesContextFactory;
+ protected final MockLifecycle lifecycle;
+ protected final MockLifecycleFactory lifecycleFactory;
+ protected final MockRenderKit renderKit;
+ protected final MockHttpServletRequest request;
+ protected final MockHttpServletResponse response;
+ protected final MockServletContext servletContext;
+ protected final MockHttpSession session;
+ protected final MockPageContext pageContext;
+ protected final MockResponseWriter mockResponseWriter;
+
+ public MockServerEnvironment(String encoding) {
+// Set up a new thread context class loader
+// Thread.currentThread().setContextClassLoader(
+// new URLClassLoader(new URL[0],
+// this.getClass().getClassLoader()));
+
+ // Set up Servlet API Objects
+ servletContext = new MockServletContext();
+ config = new MockServletConfig(servletContext);
+ session = new MockHttpSession();
+ session.setServletContext(servletContext);
+ request = new MockHttpServletRequest(session);
+ request.setServletContext(servletContext);
+ response = new MockHttpServletResponse();
+
+ // Set up JSF API Objects
+ FactoryFinder.releaseFactories();
+ FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
+ "org.apache.shale.test.mock.MockApplicationFactory");
+ FactoryFinder.setFactory(FactoryFinder.FACES_CONTEXT_FACTORY,
+ "org.apache.shale.test.mock.MockFacesContextFactory");
+ FactoryFinder.setFactory(FactoryFinder.LIFECYCLE_FACTORY,
+ "org.apache.shale.test.mock.MockLifecycleFactory");
+ FactoryFinder.setFactory(FactoryFinder.RENDER_KIT_FACTORY,
+ "org.apache.shale.test.mock.MockRenderKitFactory");
+
+// externalContext =
+// new MockExternalContext(servletContext, request, response);
+ lifecycleFactory = (MockLifecycleFactory)
+ FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
+ lifecycle = (MockLifecycle)
+ lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
+ facesContextFactory = (MockFacesContextFactory)
+ FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
+ facesContext = (MockFacesContext)
+ facesContextFactory.getFacesContext(servletContext,
+ request,
+ response,
+ lifecycle);
+ externalContext = (MockExternalContext)
+ facesContext.getExternalContext();
+ UIViewRoot root = new UIViewRoot();
+ root.setViewId("/viewId");
+ root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
+ facesContext.setViewRoot(root);
+ ApplicationFactory applicationFactory = (ApplicationFactory)
+ FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
+ application = (MockApplication) applicationFactory.getApplication();
+ facesContext.setApplication(application);
+ RenderKitFactory renderKitFactory = (RenderKitFactory)
+ FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
+ renderKit = new MockRenderKit();
+ renderKitFactory.addRenderKit(RenderKitFactory.HTML_BASIC_RENDER_KIT,
+ renderKit);
+
+ pageContext = new MockPageContext(this.config,
+ this.request, this.response);
+ mockResponseWriter = new MockResponseWriter(pageContext.getOut(),
+ "text/html", encoding);
+ facesContext.setResponseWriter(mockResponseWriter);
+ }
+
+ public MockApplication getApplication() {
+ return application;
+ }
+
+ public MockServletConfig getConfig() {
+ return config;
+ }
+
+ public MockExternalContext getExternalContext() {
+ return externalContext;
+ }
+
+ public MockFacesContext getFacesContext() {
+ return facesContext;
+ }
+
+ public MockFacesContextFactory getFacesContextFactory() {
+ return facesContextFactory;
+ }
+
+ public MockLifecycle getLifecycle() {
+ return lifecycle;
+ }
+
+ public MockLifecycleFactory getLifecycleFactory() {
+ return lifecycleFactory;
+ }
+
+ public MockRenderKit getRenderKit() {
+ return renderKit;
+ }
+
+ public MockHttpServletRequest getRequest() {
+ return request;
+ }
+
+ public MockHttpServletResponse getResponse() {
+ return response;
+ }
+
+ public MockServletContext getServletContext() {
+ return servletContext;
+ }
+
+ public MockHttpSession getSession() {
+ return session;
+ }
+
+ public MockPageContext getPageContext() {
+ return pageContext;
+ }
+
+ public MockResponseWriter getMockResponseWriter() {
+ return mockResponseWriter;
+ }
+}
Added: workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluator.java
===================================================================
--- workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluator.java
(rev 0)
+++ workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluator.java 2009-08-11
16:41:44 UTC (rev 17047)
@@ -0,0 +1,84 @@
+package org.jboss.tools.vpe.mock;
+
+import java.io.IOException;
+
+import javax.faces.render.Renderer;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.BodyContent;
+import javax.servlet.jsp.tagext.BodyTag;
+
+import org.apache.shale.test.mock.MockPrintWriter;
+
+import com.mockrunner.mock.web.MockPageContext;
+import com.sun.faces.taglib.jsf_core.ViewTag;
+
+/**
+ * Simple class that can create JSP tag instances and convert them
+ * to HTML code using {@link MockServerEnvironment}.
+ *
+ * @author Yahor Radtsevich (yradtsevich)
+ *
+ * @see TagEvaluatorSnippet TagEvaluatorSnippet implementation as
+ * an example of using of this class.
+ */
+public class TagEvaluator {
+ protected final MockServerEnvironment serverEnvironment;
+
+ public TagEvaluator(MockServerEnvironment serverEnvironment) {
+ this.serverEnvironment = serverEnvironment;
+ }
+
+ public void initTag(
+ String family,
+ String rendererType,
+ String rendererClass,
+ String componentType,
+ String componentClass)
+ throws JspException, InstantiationException,
+ IllegalAccessException, ClassNotFoundException {
+ Renderer renderer = (Renderer)
+ Class.forName(rendererClass).newInstance();
+ serverEnvironment.getFacesContext().getRenderKit()
+ .addRenderer(family, rendererType, renderer);
+ serverEnvironment.getApplication()
+ .addComponent(componentType, componentClass);
+
+ }
+
+ public <T extends BodyTag> T createBodyTag(Class<T> tagClass)
+ throws JspException,
+ InstantiationException, IllegalAccessException {
+ final MockPageContext pageContext = serverEnvironment.getPageContext();
+ ViewTag viewTag = new ViewTag();
+ viewTag.setPageContext(pageContext);
+ viewTag.doStartTag();
+ viewTag.doInitBody();
+ T bodyTag = tagClass.newInstance();
+ bodyTag.setParent(viewTag);
+ bodyTag.setPageContext(pageContext);
+ return bodyTag;
+ }
+
+ /*
+ * XXX: this implementation does not check the return values of
+ * bodyTag.do* methods. So it can process very specific tags only.
+ */
+ public String evaluateBodyTag(BodyTag bodyTag, BodyContent bodyContent)
+ throws JspException, InstantiationException,
+ IllegalAccessException, IOException
+ {
+ ViewTag viewTag = (ViewTag) bodyTag.getParent();
+ bodyTag.doStartTag();
+ bodyTag.setBodyContent(bodyContent);
+ bodyTag.doInitBody();
+
+ bodyTag.doAfterBody();
+ bodyTag.doEndTag();
+ viewTag.doAfterBody();
+ viewTag.getComponentInstance().encodeAll(
+ serverEnvironment.getFacesContext());
+
+ return String.valueOf(((MockPrintWriter)
+ serverEnvironment.getResponse().getWriter()).content());
+ }
+}
Added: workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluatorSnippet.java
===================================================================
--- workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluatorSnippet.java
(rev 0)
+++
workspace/yradtsevich/mock/src/org/jboss/tools/vpe/mock/TagEvaluatorSnippet.java 2009-08-11
16:41:44 UTC (rev 17047)
@@ -0,0 +1,62 @@
+package org.jboss.tools.vpe.mock;
+
+import org.apache.el.ValueExpressionLiteral;
+import org.apache.jasper.el.JspValueExpression;
+
+import com.mockrunner.mock.web.MockBodyContent;
+import com.mockrunner.mock.web.MockJspWriter;
+import com.sun.faces.taglib.html_basic.OutputTextTag;
+
+/**
+ * Example of using of TagEvaluator. Tested under JRE 1.6.
+ *
+ * @author Yahor Radtsevich (yradtsevich)
+ */
+public class TagEvaluatorSnippet {
+ /**
+ * Creates an equivalent of
+ * <P>
+ * <code>
+ * <h:outputText value="SomeValue
"id="SomeId">SomeBody</h:outputText>
+ * </code>
+ * <P>
+ * using mock objects, and prints the evaluated
+ * result to the output stream.
+ * <P>
+ * If all goes well, the output must be:
+ * <code>SomeBody<span
id="SomeId">SomeValue</span></code>
+ */
+ public static void main(String[] args) throws Exception {
+ TagEvaluator tagCreator = new TagEvaluator(
+ new MockServerEnvironment("cp1251"));
+
+ // initialize FacesContext and Application
+ // with tag renderer and component
+ tagCreator.initTag("javax.faces.Output",
+ "javax.faces.Text",
+ "com.sun.faces.renderkit.html_basic.TextRenderer",
+ "javax.faces.HtmlOutputText",
+ "javax.faces.component.html.HtmlOutputText");
+
+ // create an instance of the tag
+ OutputTextTag outputTextTag = tagCreator.createBodyTag(
+ OutputTextTag.class);
+
+ // initialize fields of the tag
+ JspValueExpression value = new JspValueExpression(
+ "/index.jsp(0,0) 'Mark'",
+ new ValueExpressionLiteral("SomeValue", Object.class));
+ outputTextTag.setValue(value);
+ outputTextTag.setId("SomeId");
+
+ // create body content of the tag
+ MockBodyContent bodyContent
+ = new MockBodyContent(new MockJspWriter());
+ bodyContent.print("SomeBody");
+
+ // evaluate the tag
+ String html = tagCreator.evaluateBodyTag(outputTextTag, bodyContent);
+
+ System.out.println(html);
+ }
+}