001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "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.apache.org/licenses/LICENSE-2.0
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 implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.corelib.components;
016:
017: import java.text.DecimalFormat;
018: import java.text.FieldPosition;
019: import java.text.Format;
020: import java.text.NumberFormat;
021: import java.text.ParsePosition;
022: import java.util.Locale;
023:
024: import org.apache.tapestry.ComponentResources;
025: import org.apache.tapestry.MarkupWriter;
026: import org.apache.tapestry.test.TapestryTestCase;
027: import org.testng.annotations.BeforeClass;
028: import org.testng.annotations.Test;
029:
030: public class OutputTest extends TapestryTestCase {
031: private final Number _value = 22.7d;
032:
033: private final NumberFormat _format = DecimalFormat
034: .getInstance(Locale.US);
035:
036: @BeforeClass
037: public void setup() {
038: ((DecimalFormat) _format).applyPattern("0.00");
039: }
040:
041: @Test
042: public void simple_output() {
043: MarkupWriter writer = createMarkupWriter();
044: ComponentResources resources = mockComponentResources();
045:
046: replay();
047:
048: Output component = new Output();
049:
050: component.setup(_value, _format, null, resources);
051:
052: writer.element("root");
053: assertFalse(component.beginRender(writer));
054: writer.end();
055:
056: verify();
057:
058: assertEquals(writer.toString(), "<root>22.70</root>");
059: }
060:
061: @Test
062: public void output_with_element_and_informals() {
063: String elementName = "span";
064:
065: MarkupWriter writer = createMarkupWriter();
066:
067: ComponentResources resources = mockComponentResources();
068:
069: train_renderInformalParameters(resources, writer, "foo", "bar");
070:
071: replay();
072:
073: Output component = new Output();
074:
075: component.setup(_value, _format, elementName, resources);
076:
077: assertFalse(component.beginRender(writer));
078:
079: verify();
080:
081: assertEquals(writer.toString(),
082: "<span foo=\"bar\">22.70</span>");
083: }
084:
085: @Test
086: public void null_format_is_a_noop() {
087: String elementName = "span";
088:
089: MarkupWriter writer = createMarkupWriter();
090:
091: ComponentResources resources = mockComponentResources();
092:
093: Format format = new Format() {
094: @Override
095: public StringBuffer format(Object obj,
096: StringBuffer toAppendTo, FieldPosition pos) {
097: return toAppendTo;
098: }
099:
100: @Override
101: public Object parseObject(String source, ParsePosition pos) {
102: return null;
103: }
104: };
105:
106: replay();
107:
108: Output component = new Output();
109:
110: component.setup(_value, format, elementName, resources);
111:
112: writer.element("root");
113: assertFalse(component.beginRender(writer));
114: writer.end();
115:
116: verify();
117:
118: assertEquals(writer.toString(), "<root></root>");
119: }
120: }
|