Author: craigmcc Date: Sat Dec 31 13:34:57 2005 New Revision: 360451 URL: http://svn.apache.org/viewcvs?rev=360451&view=rev Log: Make MockRenderKit.createResponseWriter() functional.
Added: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockResponseWriter.java (with props) Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockRenderKit.java Modified: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockRenderKit.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockRenderKit.java?rev=360451&r1=360450&r2=360451&view=diff ============================================================================== --- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockRenderKit.java (original) +++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockRenderKit.java Sat Dec 31 13:34:57 2005 @@ -84,7 +84,7 @@ String contentTypeList, String characterEncoding) { - throw new UnsupportedOperationException(); + return new MockResponseWriter(writer, contentTypeList, characterEncoding); } Added: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockResponseWriter.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockResponseWriter.java?rev=360451&view=auto ============================================================================== --- struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockResponseWriter.java (added) +++ struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockResponseWriter.java Sat Dec 31 13:34:57 2005 @@ -0,0 +1,319 @@ +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed 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.IOException; +import java.io.Writer; +import javax.faces.component.UIComponent; +import javax.faces.context.ResponseWriter; + +/** + * <p>Mock implementation of <code>javax.faces.context.ResponseWriter.</p> + */ +public class MockResponseWriter extends ResponseWriter { + + + // ------------------------------------------------------------ Constructors + + + public MockResponseWriter(Writer writer, String contentType, String characterEncoding) { + this.writer = writer; + this.contentType = contentType; + this.characterEncoding = characterEncoding; + } + + + // ------------------------------------------------------ Instance Variables + + + private String characterEncoding = null; + private String contentType = "text/html"; + private boolean open = false; // Is an element currently open? + private Writer writer = null; + + + // ----------------------------------------------------- Mock Object Methods + + + + // -------------------------------------------------- ResponseWriter Methods + + + public ResponseWriter cloneWithWriter(Writer writer) { + return new MockResponseWriter(writer, contentType, characterEncoding); + } + + + public void endDocument() throws IOException { + finish(); + writer.flush(); + } + + + public void endElement(String name) throws IOException { + if (open) { + writer.write("/"); + finish(); + } else { + writer.write("</"); + writer.write(name); + writer.write(">"); + } + } + + + public String getCharacterEncoding() { + return this.characterEncoding; + } + + + public String getContentType() { + return this.contentType; + } + + + public void flush() throws IOException { + finish(); + } + + + public void startDocument() throws IOException { + ; // Do nothing + } + + + public void startElement(String name, UIComponent component) throws IOException { + if (name == null) { + throw new NullPointerException(); + } + finish(); + writer.write('<'); + writer.write(name); + open = true; + } + + + public void writeAttribute(String name, Object value, String property) throws IOException { + if ((name == null) || (value == null)) { + throw new NullPointerException(); + } + if (!open) { + throw new IllegalStateException(); + } + writer.write(" "); + writer.write(name); + writer.write("=\""); + if (value instanceof String) { + string((String) value); + } else { + string(value.toString()); + } + writer.write("\""); + } + + + public void writeComment(Object comment) throws IOException { + if (comment == null) { + throw new NullPointerException(); + } + finish(); + writer.write("<!-- "); + if (comment instanceof String) { + writer.write((String) comment); + } else { + writer.write(comment.toString()); + } + writer.write(" -->"); + } + + + public void writeText(Object text, String property) throws IOException { + if (text == null) { + throw new NullPointerException(); + } + finish(); + if (text instanceof String) { + string((String) text); + } else { + string(text.toString()); + } + } + + + public void writeText(char text[], int off, int len) throws IOException { + if (text == null) { + throw new NullPointerException(); + } + if ((off < 0) || (off > text.length) || (len < 0) || (len > text.length)) { + throw new IndexOutOfBoundsException(); + } + finish(); + string(text, off, len); + } + + + public void writeURIAttribute(String name, Object value, String property) throws IOException { + if ((name == null) || (value == null)) { + throw new NullPointerException(); + } + if (!open) { + throw new IllegalStateException(); + } + writer.write(" "); + writer.write(name); + writer.write("=\""); + if (value instanceof String) { + string((String) value); + } else { + string(value.toString()); + } + writer.write("\""); + } + + + // ---------------------------------------------------------- Writer Methods + + + public void close() throws IOException { + finish(); + writer.close(); + } + + + public void write(char cbuf[], int off, int len) throws IOException { + finish(); + writer.write(cbuf, off, len); + } + + + // --------------------------------------------------------- Support Methods + + + /** + * <p>Write the specified character, filtering if necessary.</p> + * + * @param ch Character to be written + */ + private void character(char ch) throws IOException { + + if (ch <= 0xff) { + // In single byte characters, replace only the five + // characters for which well-known entities exist in XML + if (ch == 0x22) { + writer.write("""); + } else if (ch == 0x26) { + writer.write("&"); + } else if (ch == 0x27) { + writer.write("'"); + } else if (ch == 0x3C) { + writer.write("<"); + } else if (ch == 0X3E) { + writer.write(">"); + } else { + writer.write(ch); + } + } else { + if (substitution()) { + numeric(writer, ch); + } else { + writer.write(ch); + } + } + + } + + + /** + * <p>Close any element that is currently open.</p> + * + * @exception IOException if an input/output error occurs + */ + private void finish() throws IOException { + + if (open) { + writer.write(">"); + open = false; + } + + } + + + /** + * <p>Write a numeric character reference for specified character + * to the specfied writer.</p> + * + * @param writer Writer we are writing to + * @param ch Character to be translated and appended + */ + private void numeric(Writer writer, char ch) throws IOException { + + writer.write("&#"); + writer.write(String.valueOf(ch)); + writer.write(";"); + + } + + + /** + * <p>Write the specified characters (after performing suitable + * replacement of characters by corresponding entities).</p> + * + * @param text Character array containing text to be written + * @param off Starting offset (zero relative) + * @param len Number of characters to be written + */ + private void string(char text[], int off, int len) throws IOException { + + // Process the specified characters + for (int i = off; i < (off + len); i++) { + character(text[i]); + } + + } + + + /** + * <p>Write the specified string (after performing suitable + * replacement of characters by corresponding entities).</p> + * + * @param s String to be filtered and written + */ + private void string(String s) throws IOException { + + for (int i = 0; i < s.length(); i++) { + character(s.charAt(i)); + } + + } + + + /** + * <p>Return true if entity substitution should be performed on double + * byte character values.</p> + */ + private boolean substitution() { + + if ("UTF-8".equals(characterEncoding) || "UTF-16".equals(characterEncoding)) { + return false; + } else { + return true; + } + + } + + +} Propchange: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockResponseWriter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/shale/trunk/test-framework/src/java/org/apache/shale/test/mock/MockResponseWriter.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]