You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2006/09/13 22:22:35 UTC
svn commit: r443110 - in /tapestry/tapestry5/tapestry-core/trunk/src:
main/java/org/apache/tapestry/internal/
main/java/org/apache/tapestry/internal/services/
main/java/org/apache/tapestry/internal/structure/
main/java/org/apache/tapestry/internal/test...
Author: hlship
Date: Wed Sep 13 13:22:34 2006
New Revision: 443110
URL: http://svn.apache.org/viewvc?view=rev&rev=443110
Log:
Allow defaults for parameters to be set inside the default constructor (or as normal field defaults).
Allow parameter fields to be read and updated even if the parameter is not bound (with no side effects).
Added:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/StructureMessages.java
tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/structure/
tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/structure/StructureStrings.properties
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/InternalComponentResources.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageElementFactoryImpl.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageLoaderImpl.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ParameterWorker.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java
tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/templates.apt
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/components/Loop.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/pages/MerryChristmas.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/ParameterWorkerTest.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/structure/ComponentPageElementImplTest.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/components/Border.html
tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/pages/MerryChristmas.html
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/InternalComponentResources.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/InternalComponentResources.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/InternalComponentResources.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/InternalComponentResources.java Wed Sep 13 13:22:34 2006
@@ -63,4 +63,11 @@
*/
boolean isInvariant(String parameterName);
+ /**
+ * Returns true if the component has finished loading. Initially, this value will be false.
+ *
+ * @see org.apache.tapestry.runtime.PageLifecycleListener#containingPageDidLoad()
+ */
+ boolean isLoaded();
+
}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InternalClassTransformationImpl.java Wed Sep 13 13:22:34 2006
@@ -924,6 +924,23 @@
private void addConstructor()
{
+ String initializer = _idAllocator.allocateId("initializer");
+
+ try
+ {
+ CtConstructor defaultConstructor = _ctClass.getConstructor("()V");
+
+ CtMethod initializerMethod = defaultConstructor.toMethod(initializer, _ctClass);
+
+ _ctClass.addMethod(initializerMethod);
+ }
+ catch (Exception ex)
+ {
+ throw new RuntimeException(ex);
+ }
+
+ _formatter.format("convert default constructor: %s();\n\n", initializer);
+
int count = _constructorArgs.size();
CtClass[] types = new CtClass[count];
@@ -935,7 +952,13 @@
types[i] = arg.getType();
}
- _constructor.append("}");
+ // Add a call to the initializer; the method converted fromt the classes default
+ // constructor.
+
+ _constructor.append(" ");
+ _constructor.append(initializer);
+
+ _constructor.append("();\n\n}");
String constructorBody = _constructor.toString();
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageElementFactoryImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageElementFactoryImpl.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageElementFactoryImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageElementFactoryImpl.java Wed Sep 13 13:22:34 2006
@@ -102,7 +102,12 @@
// how the component elements are nested within the loading component's
// template.
- return new ComponentPageElementImpl(page, container, id, instantiator, model);
+ ComponentPageElementImpl result = new ComponentPageElementImpl(page, container, id,
+ instantiator, model, token.getLocation());
+
+ page.addLifecycleListener(result);
+
+ return result;
}
public ComponentPageElement newRootComponentElement(Page page, String componentName)
@@ -110,7 +115,11 @@
Instantiator instantiator = _componentInstantiatorSource.findInstantiator(componentName);
ComponentModel model = _componentInstantiatorSource.findComponentModel(componentName);
- return new ComponentPageElementImpl(page, instantiator, model);
+ ComponentPageElementImpl result = new ComponentPageElementImpl(page, instantiator, model);
+
+ page.addLifecycleListener(result);
+
+ return result;
}
public PageElement newRenderBodyElement(final ComponentPageElement component)
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageLoaderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageLoaderImpl.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageLoaderImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/PageLoaderImpl.java Wed Sep 13 13:22:34 2006
@@ -109,7 +109,6 @@
componentName);
_page.setRootElement(rootComponent);
- _page.addLifecycleListener(rootComponent.getComponent());
push(_componentQueue, rootComponent);
}
@@ -226,8 +225,6 @@
((StartComponentToken) token));
add(loadingComponent, activeComponent, newComponent);
-
- _page.addLifecycleListener(newComponent.getComponent());
// Make sure container knows about the new component.
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ParameterWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ParameterWorker.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ParameterWorker.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ParameterWorker.java Wed Sep 13 13:22:34 2006
@@ -101,16 +101,28 @@
String cachedFieldName, boolean cache, String fieldType, String resourcesFieldName,
ClassTransformation transformation)
{
- String name = transformation
- .addField(Modifier.PRIVATE, "boolean", fieldName + "_invariant");
+ String defaultFieldName = transformation.addField(Modifier.PRIVATE, fieldType, fieldName
+ + "_default");
- String body = String.format(
+ String invariantFieldName = transformation.addField(Modifier.PRIVATE, "boolean", fieldName
+ + "_invariant");
+
+ BodyBuilder builder = new BodyBuilder();
+ builder.begin();
+ builder.addln(
"%s = %s.isInvariant(\"%s\");",
- name,
+ invariantFieldName,
resourcesFieldName,
parameterName);
- transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, body);
+ // Store the current value of the field into the default field. This value will
+ // be used to reset the field after rendering.
+
+ builder.addln("%s = %s;", defaultFieldName, fieldName);
+ builder.end();
+
+ transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder
+ .toString());
// Now, when the component completes rendering, ensure that any variant parameters are
// are returned to default value. This isn't necessary when the parameter is not cached,
@@ -119,11 +131,11 @@
if (cache)
{
- BodyBuilder builder = new BodyBuilder();
- builder.addln("if (! %s)", name);
- builder.begin();
+ builder.clear();
- builder.addln("%s = %s;", fieldName, TransformUtils.getDefaultValue(fieldType));
+ builder.addln("if (! %s)", invariantFieldName);
+ builder.begin();
+ builder.addln("%s = %s;", fieldName, defaultFieldName);
builder.addln("%s = false;", cachedFieldName);
builder.end();
@@ -131,7 +143,7 @@
.toString());
}
- return name;
+ return invariantFieldName;
}
private void addWriterMethod(String fieldName, String cachedFieldName, boolean cache,
@@ -141,25 +153,27 @@
BodyBuilder builder = new BodyBuilder();
builder.begin();
+ // Before the component is loaded, updating the property sets the default value
+ // for the parameter. The value is stored in the field, but will be
+ // rolled into default field inside containingPageDidLoad().
+
+ builder.addln("if (! %s.isLoaded())", resourcesFieldName);
+ builder.begin();
+ builder.addln("%s = $1;", fieldName);
+ builder.addln("return;");
+ builder.end();
+
// Always start by updating the parameter; this will implicitly check for
// read-only or unbound parameters. $1 is the single parameter
// to the method.
- builder.addln("%s.writeParameter(\"%s\", ($w)$1);", resourcesFieldName, parameterName);
+ builder.addln("if (%s.isBound(\"%s\"))", resourcesFieldName, parameterName);
+ builder.addln(" %s.writeParameter(\"%s\", ($w)$1);", resourcesFieldName, parameterName);
- // The method won't get this far if it is readonly or invariant.
- // If caching, then stuff the value into the instance variable for later
- // access by the read method. If not caching, there's no point, since we're
- // just going to read the binding again as needed.
+ builder.addln("%s = $1;", fieldName);
if (cache)
- {
- builder.addln("if (%s.isRendering())", resourcesFieldName);
- builder.begin();
- builder.addln("%s = $1;", fieldName);
- builder.addln("%s = true;", cachedFieldName);
- builder.end();
- }
+ builder.addln("%s = %s.isRendering();", cachedFieldName, resourcesFieldName);
builder.end();
@@ -182,7 +196,15 @@
BodyBuilder builder = new BodyBuilder();
builder.begin();
- builder.addln("if (%s) return %s;", cachedFieldName, fieldName);
+ // While the component is still loading, or when the value for the component is cached,
+ // or if the value is not bound, then return the current value of the field.
+
+ builder.addln(
+ "if (%s || ! %s.isLoaded() || ! %<s.isBound(\"%s\")) return %s;",
+ cachedFieldName,
+ resourcesFieldName,
+ parameterName,
+ fieldName);
String cast = TransformUtils.getWrapperType(fieldType);
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java Wed Sep 13 13:22:34 2006
@@ -18,11 +18,15 @@
import java.util.List;
import java.util.Map;
+import org.apache.tapestry.BaseLocatable;
import org.apache.tapestry.Binding;
+import org.apache.tapestry.Location;
import org.apache.tapestry.MarkupWriter;
+import org.apache.tapestry.internal.TapestryException;
import org.apache.tapestry.internal.annotations.SuppressNullCheck;
import org.apache.tapestry.internal.services.Instantiator;
import org.apache.tapestry.model.ComponentModel;
+import org.apache.tapestry.model.ParameterModel;
import org.apache.tapestry.runtime.ComponentLifecycle;
import org.apache.tapestry.runtime.LifecycleEvent;
import org.apache.tapestry.runtime.PageLifecycleListener;
@@ -38,12 +42,19 @@
* within an overall page. Much of a component page element's behavior is delegated to user code,
* via a {@link org.apache.tapestry.runtime.ComponentLifecycle} instance.
* <p>
+ * Once instantiated, a ComponentPageElementImpl should be registered as a
+ * {@link org.apache.tapestry.runtime.PageLifecycleListener} with the containing
+ * {@link org.apache.tapestry.internal.structure.Page}. This could be done inside the constructors,
+ * but that tends to complicate unit tests, so its done by
+ * {@link org.apache.tapestry.internal.services.PageElementFactoryImpl}.
+ * <p>
* TODO: Possibly break this into two classes: the page element and the internal component resources
* implementation. For the moment, it works as a single class.
*
* @author Howard M. Lewis Ship
*/
-public class ComponentPageElementImpl implements ComponentPageElement, PageLifecycleListener
+public class ComponentPageElementImpl extends BaseLocatable implements ComponentPageElement,
+ PageLifecycleListener
{
private final Page _page;
@@ -71,6 +82,8 @@
private boolean _rendering;
+ private boolean _loaded;
+
private static final Map<Class, Class> PRIMITIVE_TO_WRAPPER = newMap();
static
@@ -88,11 +101,16 @@
/** Constructor for the root component of a page. */
public ComponentPageElementImpl(Page page, Instantiator instantiator, ComponentModel model)
{
- _page = page;
+ super(null);
+
_container = null;
_id = null;
- _componentModel = model;
+ // A little bit of duplication that's unavoiable if we want
+ // to keep the fields final.
+
+ _page = page;
+ _componentModel = model;
_component = instantiator.newInstance(this);
}
@@ -105,19 +123,27 @@
* @param container
* component immediately containing this component
* @param id
+ * unique (within the container) id for this component
* @param instantiator
* used to create the new component instance
* @param model
* defines the component
+ * @param location
+ * location of the element (within a template), used as part of exception reporting
*/
public ComponentPageElementImpl(Page page, ComponentPageElement container, String id,
- Instantiator instantiator, ComponentModel model)
+ Instantiator instantiator, ComponentModel model, Location location)
{
- _page = page;
+ super(location);
+
_container = container;
_id = id;
- _componentModel = model;
+ // A little bit of duplication that's unavoiable if we want
+ // to keep the fields final.
+
+ _page = page;
+ _componentModel = model;
_component = instantiator.newInstance(this);
}
@@ -151,16 +177,16 @@
Collections.reverse(ids);
- StringBuilder buffer = new StringBuilder(pageName);
+ StringBuilder builder = new StringBuilder(pageName);
int count = ids.size();
for (int i = 0; i < count; i++)
{
- buffer.append(i == 0 ? ':' : '.');
- buffer.append(ids.get(i));
+ builder.append(i == 0 ? ':' : '.');
+ builder.append(ids.get(i));
}
- _completeId = buffer.toString();
+ _completeId = builder.toString();
}
public Page getPage()
@@ -466,9 +492,42 @@
_component.containingPageDidDetach();
}
+ public boolean isLoaded()
+ {
+ return _loaded;
+ }
+
public void containingPageDidLoad()
{
+ verifyRequiredParametersAreBound();
+
+ // TODO: Verify that embedded components are
+ // referenced in template
+
+ _loaded = true;
+
_component.containingPageDidLoad();
+ }
+
+ private void verifyRequiredParametersAreBound()
+ {
+ List<String> unbound = newList();
+
+ for (String name : _componentModel.getParameterNames())
+ {
+ if (isBound(name))
+ continue;
+
+ ParameterModel model = _componentModel.getParameterModel(name);
+
+ if (model.isRequired())
+ unbound.add(name);
+ }
+
+ if (unbound.isEmpty())
+ return;
+
+ throw new TapestryException(StructureMessages.missingParameters(unbound, this), this, null);
}
}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/StructureMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/StructureMessages.java?view=auto&rev=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/StructureMessages.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/structure/StructureMessages.java Wed Sep 13 13:22:34 2006
@@ -0,0 +1,38 @@
+// Copyright 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.tapestry.internal.structure;
+
+import java.util.List;
+
+import org.apache.tapestry.Messages;
+import org.apache.tapestry.internal.MessagesImpl;
+import org.apache.tapestry.internal.annotations.Utility;
+import org.apache.tapestry.internal.ioc.IOCUtilities;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+@Utility
+final class StructureMessages
+{
+ private static final Messages MESSAGES = MessagesImpl.forClass(StructureMessages.class);
+
+ static String missingParameters(List<String> parameters, ComponentPageElement element)
+ {
+ return MESSAGES.format("missing-parameters", IOCUtilities.joinSorted(parameters), element
+ .getComponentModel().getComponentClassName());
+ }
+
+}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java Wed Sep 13 13:22:34 2006
@@ -27,6 +27,7 @@
import org.apache.tapestry.internal.services.PageLoader;
import org.apache.tapestry.internal.services.PagePool;
import org.apache.tapestry.internal.services.TemplateParser;
+import org.apache.tapestry.internal.structure.ComponentPageElement;
import org.apache.tapestry.internal.structure.Page;
import org.apache.tapestry.ioc.ObjectProvider;
import org.apache.tapestry.ioc.Registry;
@@ -170,5 +171,10 @@
protected final TemplateParser newTemplateParser()
{
return newMock(TemplateParser.class);
+ }
+
+ protected final ComponentPageElement newComponentPageElement()
+ {
+ return newMock(ComponentPageElement.class);
}
}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/test/BaseTestCase.java Wed Sep 13 13:22:34 2006
@@ -50,6 +50,7 @@
import org.apache.tapestry.ioc.def.ServiceDef;
import org.apache.tapestry.model.ComponentModel;
import org.apache.tapestry.model.MutableComponentModel;
+import org.apache.tapestry.model.ParameterModel;
import org.apache.tapestry.services.BindingFactory;
import org.apache.tapestry.services.ClassTransformation;
import org.apache.tapestry.services.ComponentClassResolver;
@@ -450,15 +451,48 @@
return newMock(WebRequest.class);
}
- protected final void train_provideInjection(InjectionProvider provider, String fieldName, String fieldType, ServiceLocator locator, ClassTransformation transformation, MutableComponentModel model, boolean result)
+ protected final void train_provideInjection(InjectionProvider provider, String fieldName,
+ String fieldType, ServiceLocator locator, ClassTransformation transformation,
+ MutableComponentModel model, boolean result)
{
provider.provideInjection(fieldName, fieldType, locator, transformation, model);
setReturnValue(result);
}
- protected final void train_toClass(ClassTransformation transformation, String type, Class classForType)
+ protected final void train_toClass(ClassTransformation transformation, String type,
+ Class classForType)
{
transformation.toClass(type);
setReturnValue(classForType);
+ }
+
+ protected final ParameterModel newParameterModel()
+ {
+ return newMock(ParameterModel.class);
+ }
+
+ protected final void train_getComponentClassName(ComponentModel model, String className)
+ {
+ model.getComponentClassName();
+ setReturnValue(className);
+ }
+
+ protected final void train_isRequired(ParameterModel model, boolean isRequired)
+ {
+ model.isRequired();
+ setReturnValue(isRequired);
+ }
+
+ protected final void train_getParameterModel(ComponentModel model, String parameterName,
+ ParameterModel parameterModel)
+ {
+ model.getParameterModel(parameterName);
+ setReturnValue(parameterModel);
+ }
+
+ protected final void train_getParameterNames(ComponentModel model, String... names)
+ {
+ model.getParameterNames();
+ setReturnValue(Arrays.asList(names));
}
}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/structure/StructureStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/structure/StructureStrings.properties?view=auto&rev=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/structure/StructureStrings.properties (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/structure/StructureStrings.properties Wed Sep 13 13:22:34 2006
@@ -0,0 +1,15 @@
+# Copyright 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.
+
+missing-parameters=Parameter(s) %s are required for %s, but have not been bound.
\ No newline at end of file
Modified: tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/templates.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/templates.apt?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/templates.apt (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/apt/guide/templates.apt Wed Sep 13 13:22:34 2006
@@ -18,22 +18,26 @@
Pages vs. Components
In Tapestry 5, pages and components are virtually identical, the only real difference is that
- pages come from a different package. In reality, what we call a "page" is really the page's <root component>.
+ pages come from a different Java package. In reality, what we call a "page" is really the page's <root component>.
Template Location
Component templates are stored with the component class file. The files have a ".html" extension, and are stored in
the same package as corresponding component class.
- Under a typical Maven directory structure, the Java class might be src/main/java/org/example/myapp/components/MyComponent.java.
- The corresponding template will be src/main/resources/org/example/myapp/components/MyComponent.html.
+ Under a typical Maven directory structure, the Java class for a component
+ might be <<<src/main/java/org/example/myapp/components/MyComponent.java>>>.
+ The corresponding template will be <<<src/main/resources/org/example/myapp/components/MyComponent.html>>>.
+
+ Likewise, the Java class for a component might be <<<src/main/java/org/example/myapp/pages/MyPage.java>>> and
+ the corresponding template will be <<<src/main/resources/org/example/myapp/pages/MyPage.html>>>.
The template and the compiled class will be packaged together in the WEB-INF/classes folder of the application WAR.
Tapestry Namespace
Component templates should include the Tapestry namespace, defining it in the root element of the template.
-
+
+----+
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
@@ -100,7 +104,56 @@
body (from the container's template) is to be rendered.
Components have control over if, and even how often, its body is rendered.
+
+ The following example is a Border component, that adds basic HTML elements
+ around the page specific content:
+
++---+
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+ <head>
+ <title>My Tapestry Application</title>
+ </head>
+ <body>
+ <t:body/>
+ </body>
+</html>
++---+
+
+ A page would use this component as follow:
+
++---+
+<t:comp type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+
+ My Page Specific Content
+
+</t:comp >
++---+
+
+Expansions
+
+ Another option for rendering output are <expansions>. Expansions
+ are special strings that may be emdedded in template bodies, and borrow Ant syntax.
+
++---+
+ Welcome, ${userId}!
++---+
+
+ Here, <<<$\{userId}>>> is the expansion. In this example, the userId property of the
+ component is extracted, converted to a string, and streamed into the output.
+
+ Under the covers, expansions are the same as parameter bindings. The default
+ binding prefix for expansions is "prop:" (that is, the name of a property), but
+ other binding prefixes are useful.
+
+ Tapestry 4 users will note that expansions are a easy replacement for the
+ Insert component, and for the \<span key="..."\> directive.
+
+ <<Alas, this is not yet implemented.>>
+
+ <<TODO: Allow expansions inside attribute values of non-components?>>
+
+
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/components/Loop.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/components/Loop.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/components/Loop.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/components/Loop.java Wed Sep 13 13:22:34 2006
@@ -14,6 +14,7 @@
package org.apache.tapestry.integration.app1.components;
+import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.ComponentClass;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.annotations.AfterRender;
@@ -22,13 +23,13 @@
@ComponentClass
public class Loop
{
- @Parameter(required = true)
- private int _min;
+ @Parameter
+ private int _min = 1;
- @Parameter(required = true)
- private int _max;
+ @Parameter
+ private int _max = 10;
- @Parameter(required = true)
+ @Parameter
private int _value;
@SetupRender
@@ -38,7 +39,7 @@
}
@AfterRender
- boolean startLoop()
+ boolean startLoop(MarkupWriter writer)
{
int newValue = _value + 1;
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/pages/MerryChristmas.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/pages/MerryChristmas.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/pages/MerryChristmas.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/integration/app1/pages/MerryChristmas.java Wed Sep 13 13:22:34 2006
@@ -19,26 +19,8 @@
@ComponentClass
public class MerryChristmas
{
- private int _value;
-
- public int getMin()
- {
- return 0;
- }
-
public int getMax()
{
- return 2;
- }
-
- public int getValue()
- {
- return _value;
- }
-
- public void setValue(int value)
- {
- _value = value;
+ return 3;
}
-
}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/ParameterWorkerTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/ParameterWorkerTest.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/ParameterWorkerTest.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/ParameterWorkerTest.java Wed Sep 13 13:22:34 2006
@@ -29,6 +29,10 @@
import org.testng.annotations.Test;
/**
+ * There's no point in trying to unit test the code generated by
+ * {@link org.apache.tapestry.internal.services.ParameterWorker}. Instead, we excercize
+ * ParameterWorker, and test that the generated code works correctly in a number of scenarios.
+ *
* @author Howard M. Lewis Ship
*/
public class ParameterWorkerTest extends InternalBaseTestCase
@@ -62,6 +66,8 @@
String value = "To be in Tapestry in the spring time ...";
+ train_isLoaded(resources, true);
+ train_isBound(resources, "invariantObject", true);
train_readParameter(resources, "invariantObject", String.class, value);
replay();
@@ -104,6 +110,8 @@
long value = 123456;
+ train_isLoaded(resources, true);
+ train_isBound(resources, "invariantPrimitive", true);
train_readParameter(resources, "invariantPrimitive", long.class, value);
replay();
@@ -129,6 +137,82 @@
verify();
}
+ /**
+ * This actually checks several things:
+ * <ul>
+ * <li>Changing a parameter property before the page loads doesn't update the binding</li>
+ * <li>Changing a parameter property changes the property AND the default value for the property</li>
+ * <li>Unbound parameters to do not attempt to read or update their bindings (they'll be optional)</li>
+ * </ul>
+ * @throws Exception
+ */
+ @Test
+ public void changes_before_load_become_defaults_and_dont_update_bindings() throws Exception
+ {
+ InternalComponentResources resources = newInternalComponentResources();
+
+ ComponentLifecycle component = setupForIntegrationTest(resources);
+
+ train_isLoaded(resources, false);
+
+ replay();
+
+ assertNull(_access.get(component, "object"));
+
+ verify();
+
+ train_isLoaded(resources, false);
+
+ replay();
+
+ _access.set(component, "object", "new-default");
+
+ verify();
+
+ train_isLoaded(resources, false);
+
+ replay();
+
+ assertEquals(_access.get(component, "object"), "new-default");
+
+ verify();
+
+ trainForPageDidLoad(resources);
+
+ replay();
+
+ component.containingPageDidLoad();
+
+ verify();
+
+ // For the set ...
+
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", false);
+ train_isRendering(resources, false);
+
+ // For the first read ...
+
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", false);
+
+ // For the second read (after postRenderCleanup) ...
+
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", false);
+
+ replay();
+
+ _access.set(component, "object", "new-value");
+ assertEquals(_access.get(component, "object"), "new-value");
+
+ component.postRenderCleanup();
+
+ assertEquals(_access.get(component, "object"), "new-default");
+
+ verify();
+ }
+
@Test
public void cached_object_read() throws Exception
{
@@ -136,6 +220,8 @@
ComponentLifecycle component = setupForIntegrationTest(resources);
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
train_readParameter(resources, "object", String.class, "first");
train_isRendering(resources, false);
@@ -147,6 +233,8 @@
// Keeps re-reading the parameter when not rendering.
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
train_readParameter(resources, "object", String.class, "second");
train_isRendering(resources, false);
@@ -158,6 +246,8 @@
// Now, when rendering is active, the value is cached
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
train_readParameter(resources, "object", String.class, "third");
train_isRendering(resources, true);
@@ -171,6 +261,8 @@
verify();
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
train_readParameter(resources, "object", String.class, "fourth");
train_isRendering(resources, false);
@@ -190,9 +282,13 @@
ComponentLifecycle component = setupForIntegrationTest(resources);
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
resources.writeParameter("object", "first");
train_isRendering(resources, false);
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
train_readParameter(resources, "object", String.class, "second");
train_isRendering(resources, false);
@@ -205,6 +301,8 @@
// Now try during rendering ...
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
resources.writeParameter("object", "third");
train_isRendering(resources, true);
@@ -217,6 +315,8 @@
// And the cached value is lost after rendering is complete.
+ train_isLoaded(resources, true);
+ train_isBound(resources, "object", true);
train_readParameter(resources, "object", String.class, "fourth");
train_isRendering(resources, false);
@@ -236,9 +336,14 @@
ComponentLifecycle component = setupForIntegrationTest(resources);
+ train_isLoaded(resources, true);
+ train_isBound(resources, "primitive", true);
resources.writeParameter("primitive", 321);
+
train_isRendering(resources, false);
+ train_isLoaded(resources, true);
+ train_isBound(resources, "primitive", true);
train_readParameter(resources, "primitive", int.class, 123);
train_isRendering(resources, false);
@@ -251,6 +356,8 @@
// Now try during rendering ...
+ train_isLoaded(resources, true);
+ train_isBound(resources, "primitive", true);
resources.writeParameter("primitive", 567);
train_isRendering(resources, true);
@@ -263,6 +370,8 @@
// And the cached value is lost after rendering is complete.
+ train_isLoaded(resources, true);
+ train_isBound(resources, "primitive", true);
train_readParameter(resources, "primitive", int.class, 890);
train_isRendering(resources, false);
@@ -275,6 +384,12 @@
verify();
}
+ protected final void train_isLoaded(InternalComponentResources resources, boolean isLoaded)
+ {
+ resources.isLoaded();
+ setReturnValue(isLoaded);
+ }
+
@Test
public void uncached_object_read() throws Exception
{
@@ -286,7 +401,11 @@
// Also note difference between field name and parameter name, due to Parameter.name() being
// specified.
+ train_isLoaded(resources, true);
+ train_isBound(resources, "uncached", true);
train_readParameter(resources, "uncached", String.class, "first");
+ train_isLoaded(resources, true);
+ train_isBound(resources, "uncached", true);
train_readParameter(resources, "uncached", String.class, "second");
replay();
@@ -297,6 +416,13 @@
verify();
}
+ protected void train_isBound(InternalComponentResources resources, String parameterName,
+ boolean isBound)
+ {
+ resources.isBound(parameterName);
+ setReturnValue(isBound);
+ }
+
@Test
public void uncached_object_write() throws Exception
{
@@ -308,7 +434,12 @@
// Also note difference between field name and parameter name, due to Parameter.name() being
// specified.
+ train_isLoaded(resources, true);
+ train_isBound(resources, "uncached", true);
resources.writeParameter("uncached", "first");
+
+ train_isLoaded(resources, true);
+ train_isBound(resources, "uncached", true);
train_readParameter(resources, "uncached", String.class, "second");
replay();
@@ -369,11 +500,7 @@
Instantiator instantiator = transformation.createInstantiator(transformedClass);
- train_isInvariant(resources, "invariantObject", true);
- train_isInvariant(resources, "invariantPrimitive", true);
- train_isInvariant(resources, "object", false);
- train_isInvariant(resources, "primitive", false);
- train_isInvariant(resources, "uncached", false);
+ trainForPageDidLoad(resources);
replay();
@@ -384,6 +511,15 @@
verify();
return component;
+ }
+
+ private void trainForPageDidLoad(InternalComponentResources resources)
+ {
+ train_isInvariant(resources, "invariantObject", true);
+ train_isInvariant(resources, "invariantPrimitive", true);
+ train_isInvariant(resources, "object", false);
+ train_isInvariant(resources, "primitive", false);
+ train_isInvariant(resources, "uncached", false);
}
protected final void train_isInvariant(InternalComponentResources resources,
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/structure/ComponentPageElementImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/structure/ComponentPageElementImplTest.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/structure/ComponentPageElementImplTest.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/structure/ComponentPageElementImplTest.java Wed Sep 13 13:22:34 2006
@@ -14,11 +14,15 @@
package org.apache.tapestry.internal.structure;
+
import org.apache.tapestry.Binding;
+import org.apache.tapestry.Location;
import org.apache.tapestry.internal.InternalComponentResources;
+import org.apache.tapestry.internal.TapestryException;
import org.apache.tapestry.internal.services.Instantiator;
import org.apache.tapestry.internal.test.InternalBaseTestCase;
import org.apache.tapestry.model.ComponentModel;
+import org.apache.tapestry.model.ParameterModel;
import org.apache.tapestry.runtime.ComponentLifecycle;
import org.easymock.EasyMock;
import org.testng.annotations.Test;
@@ -48,6 +52,97 @@
assertFalse(cpe.isBound("fred"));
assertTrue(cpe.isBound("barney"));
+
+ verify();
+ }
+
+ @Test
+ public void verify_required_parameters_all_are_bound()
+ {
+ Page page = newPage();
+ ComponentLifecycle component = newComponentLifecycle();
+ Instantiator ins = newInstantiator(component);
+ ComponentModel model = newComponentModel();
+ Binding binding = newBinding();
+
+ train_getParameterNames(model, "barney");
+
+ component.containingPageDidLoad();
+
+ replay();
+
+ ComponentPageElementImpl cpe = new ComponentPageElementImpl(page, ins, model);
+
+ cpe.addParameter("barney", binding);
+
+ cpe.containingPageDidLoad();
+
+ verify();
+ }
+
+ @Test
+ public void verify_required_parameters_unbound_but_not_required()
+ {
+ Page page = newPage();
+ ComponentLifecycle component = newComponentLifecycle();
+ Instantiator ins = newInstantiator(component);
+ ComponentModel model = newComponentModel();
+ ParameterModel pmodel = newParameterModel();
+
+ train_getParameterNames(model, "barney");
+ train_getParameterModel(model, "barney", pmodel);
+ train_isRequired(pmodel, false);
+
+ component.containingPageDidLoad();
+
+ replay();
+
+ ComponentPageElementImpl cpe = new ComponentPageElementImpl(page, ins, model);
+
+ cpe.containingPageDidLoad();
+
+ verify();
+ }
+
+ @Test
+ public void verify_required_parameters_unbound_and_required()
+ {
+ Page page = newPage();
+ ComponentPageElement container = newComponentPageElement();
+ ComponentLifecycle component = newComponentLifecycle();
+ Instantiator ins = newInstantiator(component);
+ ComponentModel model = newComponentModel();
+ ParameterModel pmodel = newParameterModel();
+ Location l = newLocation();
+
+ train_getParameterNames(model, "wilma", "barney", "fred");
+ train_getParameterModel(model, "wilma", pmodel);
+ train_isRequired(pmodel, true);
+
+ train_getParameterModel(model, "barney", pmodel);
+ train_isRequired(pmodel, false);
+
+ train_getParameterModel(model, "fred", pmodel);
+ train_isRequired(pmodel, true);
+
+ train_getComponentClassName(model, "foo.components.MyComponent");
+
+ replay();
+
+ ComponentPageElementImpl cpe = new ComponentPageElementImpl(page, container, "myid", ins,
+ model, l);
+
+ try
+ {
+ cpe.containingPageDidLoad();
+ }
+ catch (TapestryException ex)
+ {
+ assertEquals(
+ ex.getMessage(),
+ "Parameter(s) fred, wilma are required for foo.components.MyComponent, but have not been bound.");
+ assertSame(ex.getLocation(), l);
+ }
verify();
}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java Wed Sep 13 13:22:34 2006
@@ -245,6 +245,10 @@
r.cleanupThread();
}
+ /**
+ * This test fails at times and I'm not sure why. It's some kind of interaction with other tests
+ * but hard to figure out. Damn ThreadLocals!
+ */
@Test
public void registry_thread_cleanup()
{
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/components/Border.html
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/components/Border.html?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/components/Border.html (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/components/Border.html Wed Sep 13 13:22:34 2006
@@ -1,6 +1,6 @@
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
- <title>Tapestry Integration Test App 1</title>
+ <title>My Tapestry Application</title>
</head>
<body>
<t:body/>
Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/pages/MerryChristmas.html
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/pages/MerryChristmas.html?view=diff&rev=443110&r1=443109&r2=443110
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/pages/MerryChristmas.html (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/resources/org/apache/tapestry/integration/app1/pages/MerryChristmas.html Wed Sep 13 13:22:34 2006
@@ -1,11 +1,5 @@
<t:comp id="border" type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
- <p> The Loop component demonstrates reading and writing of properties with the prop: binding
- prefix.</p>
-
- <p>
- Merry Christmas:
- <t:comp id="loop" type="Loop" min="prop:min" max="prop:max" value="prop:value">
- Ho!
- </t:comp>
+ <p> The Loop component demonstrates reading and writing of properties with the prop: binding prefix.</p>
+ <p> Merry Christmas: <t:comp id="loop" type="Loop" max="prop:max"> Ho! </t:comp>
</p>
</t:comp>