You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2007/02/20 02:32:14 UTC

svn commit: r509404 - in /tapestry/tapestry5/tapestry-core/trunk/src: main/java/org/apache/tapestry/corelib/components/Output.java main/java/org/apache/tapestry/test/TapestryTestCase.java test/java/org/apache/tapestry/corelib/components/OutputTest.java

Author: hlship
Date: Mon Feb 19 17:32:13 2007
New Revision: 509404

URL: http://svn.apache.org/viewvc?view=rev&rev=509404
Log:
Add an Output component, used for formatting output (using a java.text.Format).

Added:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Output.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java
Modified:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/TapestryTestCase.java

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Output.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Output.java?view=auto&rev=509404
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Output.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/Output.java Mon Feb 19 17:32:13 2007
@@ -0,0 +1,96 @@
+// Copyright 2007 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.tapestry.corelib.components;
+
+import java.text.Format;
+
+import org.apache.tapestry.Binding;
+import org.apache.tapestry.ComponentResources;
+import org.apache.tapestry.MarkupWriter;
+import org.apache.tapestry.annotations.Inject;
+import org.apache.tapestry.annotations.Parameter;
+import org.apache.tapestry.annotations.SupportsInformalParameters;
+import org.apache.tapestry.ioc.internal.util.InternalUtils;
+import org.apache.tapestry.services.DefaultComponentParameterBindingSource;
+
+/**
+ * A component for formatting output. If the component is represented in the template using an
+ * element, then the element (plus any informal parameters) will be output around the formatted
+ * value.
+ */
+@SupportsInformalParameters
+public class Output
+{
+    /**
+     * The value to be output (before formatting). If the formatted value is blank, no output is
+     * produced.
+     */
+    @Parameter(required = true)
+    private Object _value;
+
+    /** The format to be applied to the object. */
+    @Parameter(required = true)
+    private Format _format;
+
+    /**
+     * The element name, derived from the component template. This can even be overridden manually
+     * if desired (for example, to sometimes render a surrounding element and other times not).
+     */
+    @Parameter("componentResources.elementName")
+    private String _elementName;
+
+    @Inject("infrastructure:DefaultComponentParameterBindingSource")
+    private DefaultComponentParameterBindingSource _defaultBindingSource;
+
+    @Inject
+    private ComponentResources _resources;
+
+    Binding defaultValue()
+    {
+        return _defaultBindingSource.createDefaultBinding("value", _resources);
+    }
+
+    boolean beginRender(MarkupWriter writer)
+    {
+        String formatted = _format.format(_value);
+
+        if (InternalUtils.isNonBlank(formatted))
+        {
+            if (_elementName != null)
+            {
+                writer.element(_elementName);
+
+                _resources.renderInformalParameters(writer);
+            }
+
+            writer.write(formatted);
+
+            if (_elementName != null)
+                writer.end();
+        }
+
+        return false;
+    }
+
+    // For testing.
+
+    void setup(Object value, Format format, String elementName, ComponentResources resources)
+    {
+        _value = value;
+        _format = format;
+        _elementName = elementName;
+        _resources = resources;
+    }
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/TapestryTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/TapestryTestCase.java?view=diff&rev=509404&r1=509403&r2=509404
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/TapestryTestCase.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/TapestryTestCase.java Mon Feb 19 17:32:13 2007
@@ -50,6 +50,7 @@
 import org.apache.tapestry.annotations.Inject;
 import org.apache.tapestry.annotations.Parameter;
 import org.apache.tapestry.beaneditor.PropertyModel;
+import org.apache.tapestry.internal.services.MarkupWriterImpl;
 import org.apache.tapestry.ioc.Location;
 import org.apache.tapestry.ioc.Messages;
 import org.apache.tapestry.ioc.Resource;
@@ -840,5 +841,34 @@
                         componentResources,
                         defaultBindingPrefix,
                         expression)).andReturn(binding);
+    }
+
+    /**
+     * Creates a new markup writer instance (not a markup writer mock). Output can be directed at
+     * the writer, which uses the default (HTML) markup model. The writer's toString() value
+     * represents all the collected markup in the writer.
+     * 
+     * @return
+     */
+    protected final MarkupWriter createMarkupWriter()
+    {
+        return new MarkupWriterImpl();
+    }
+
+    @SuppressWarnings("unchecked")
+    protected final void train_renderInformalParameters(ComponentResources resources, final MarkupWriter writer, final String... informals)
+    {
+        resources.renderInformalParameters(writer);
+        IAnswer answer = new IAnswer()
+        {
+            public Object answer() throws Throwable
+            {
+                writer.attributes(informals);
+    
+                return null;
+            }
+        };
+    
+        getMocksControl().andAnswer(answer);
     }
 }

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java?view=auto&rev=509404
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/corelib/components/OutputTest.java Mon Feb 19 17:32:13 2007
@@ -0,0 +1,116 @@
+// Copyright 2007 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.tapestry.corelib.components;
+
+import java.text.DecimalFormat;
+import java.text.FieldPosition;
+import java.text.Format;
+import java.text.ParsePosition;
+
+import org.apache.tapestry.ComponentResources;
+import org.apache.tapestry.MarkupWriter;
+import org.apache.tapestry.test.TapestryTestCase;
+import org.testng.annotations.Test;
+
+public class OutputTest extends TapestryTestCase
+{
+    private final Number _value = new Double(22.7d);
+
+    private final Format _format = new DecimalFormat("0.00");
+
+    @Test
+    public void simple_output()
+    {
+        MarkupWriter writer = createMarkupWriter();
+        ComponentResources resources = newComponentResources();
+
+        replay();
+
+        Output component = new Output();
+
+        component.setup(_value, _format, null, resources);
+
+        writer.element("root");
+        assertFalse(component.beginRender(writer));
+        writer.end();
+
+        verify();
+
+        assertEquals(writer.toString(), "<root>22.70</root>");
+    }
+
+    @Test
+    public void output_with_element_and_informals()
+    {
+        String elementName = "span";
+
+        MarkupWriter writer = createMarkupWriter();
+
+        ComponentResources resources = newComponentResources();
+
+        train_renderInformalParameters(resources, writer, "foo", "bar");
+
+        replay();
+
+        Output component = new Output();
+
+        component.setup(_value, _format, elementName, resources);
+
+        assertFalse(component.beginRender(writer));
+
+        verify();
+
+        assertEquals(writer.toString(), "<span foo=\"bar\">22.70</span>");
+    }
+
+    @Test
+    public void null_format_is_a_noop()
+    {
+        String elementName = "span";
+
+        MarkupWriter writer = createMarkupWriter();
+
+        ComponentResources resources = newComponentResources();
+
+        Format format = new Format()
+        {
+            @Override
+            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
+            {
+                return toAppendTo;
+            }
+
+            @Override
+            public Object parseObject(String source, ParsePosition pos)
+            {
+                return null;
+            }
+        };
+
+        replay();
+
+        Output component = new Output();
+
+        component.setup(_value, format, elementName, resources);
+
+        writer.element("root");
+        assertFalse(component.beginRender(writer));
+        writer.end();
+
+        verify();
+
+        assertEquals(writer.toString(), "<root></root>");
+    }
+}