Author: jkuhnert
Date: Sat Jun 23 10:55:47 2007
New Revision: 550071
URL: http://svn.apache.org/viewvc?view=rev&rev=550071
Log:
Back wit yee vile hashes!
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/CtClassSource.java
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/ParameterPropertyWorker.java
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/pageload/PageLoader.java
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/TestParameterPropertyWorker.java
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/mock/TestMockApplications.java
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/CtClassSource.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/CtClassSource.java?view=diff&rev=550071&r1=550070&r2=550071
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/CtClassSource.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/CtClassSource.java
Sat Jun 23 10:55:47 2007
@@ -15,7 +15,6 @@
import javassist.CtClass;
import javassist.NotFoundException;
-
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.service.ClassFabUtils;
@@ -87,8 +86,7 @@
}
catch (Throwable ex)
{
- throw new
ApplicationRuntimeException(EnhanceMessages.unableToWriteClass(ctClass, ex),
- ex);
+ throw new
ApplicationRuntimeException(EnhanceMessages.unableToWriteClass(ctClass, ex),
ex);
}
}
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/ParameterPropertyWorker.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/ParameterPropertyWorker.java?view=diff&rev=550071&r1=550070&r2=550071
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/ParameterPropertyWorker.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/ParameterPropertyWorker.java
Sat Jun 23 10:55:47 2007
@@ -31,7 +31,7 @@
/**
* Responsible for creating properties for connected parameters.
- *
+ *
* @author Howard M. Lewis Ship
* @since 4.0
*/
@@ -55,9 +55,8 @@
}
catch (RuntimeException ex)
{
- _errorLog.error(EnhanceMessages.errorAddingProperty(ps
- .getPropertyName(), op.getBaseClass(), ex), ps
- .getLocation(), ex);
+
_errorLog.error(EnhanceMessages.errorAddingProperty(ps.getPropertyName(),
op.getBaseClass(), ex),
+ ps.getLocation(), ex);
}
}
}
@@ -65,6 +64,13 @@
/**
* Performs the enhancement for a single parameter; this is about to change
* radically in release 4.0 but for the moment we're emulating 3.0
behavior.
+ *
+ * @param op
+ * Enhancement operation service.
+ * @param parameterName
+ * Name of the parameter being enhanced.
+ * @param ps
+ * Specification of parameter.
*/
private void performEnhancement(EnhancementOperation op, String
parameterName, IParameterSpecification ps)
@@ -72,19 +78,19 @@
// If the parameter name doesn't match, its because this is an alias
// for a true parameter; we ignore aliases.
- if (!parameterName.equals(ps.getParameterName()))
+ if (!parameterName.equals(ps.getParameterName()))
return;
-
+
String propertyName = ps.getPropertyName();
String specifiedType = ps.getType();
boolean cache = ps.getCache();
-
+
addParameter(op, parameterName, propertyName, specifiedType, cache,
ps.getLocation());
}
/**
* Adds a parameter as a (very smart) property.
- *
+ *
* @param op
* the enhancement operation
* @param parameterName
@@ -104,15 +110,15 @@
*/
public void addParameter(EnhancementOperation op, String parameterName,
- String propertyName, String specifiedType, boolean cache,
- Location location)
+ String propertyName, String specifiedType,
boolean cache,
+ Location location)
{
Defense.notNull(op, "op");
Defense.notNull(parameterName, "parameterName");
Defense.notNull(propertyName, "propertyName");
Class propertyType = EnhanceUtils.extractPropertyType(op,
propertyName, specifiedType);
-
+
// 3.0 would allow connected parameter properties to be fully
// implemented
// in the component class. This is not supported in 4.0 and an existing
@@ -126,24 +132,52 @@
String fieldName = "_$" + propertyName;
String defaultFieldName = fieldName + "$Default";
String cachedFieldName = fieldName + "$Cached";
-
+
op.addField(fieldName, propertyType);
op.addField(defaultFieldName, propertyType);
op.addField(cachedFieldName, boolean.class);
- buildAccessor(op, parameterName, propertyName, propertyType, fieldName,
- defaultFieldName, cachedFieldName, cache, location);
+ String bindingFieldName = buildBindingAccessor(op, fieldName,
parameterName, location);
+
+ buildAccessor(op, propertyName, propertyType, fieldName,
+ defaultFieldName, cachedFieldName, bindingFieldName,
+ cache, location);
buildMutator(op, parameterName, propertyName, propertyType, fieldName,
- defaultFieldName, cachedFieldName, location);
+ defaultFieldName, cachedFieldName, bindingFieldName,
location);
+
+ extendCleanupAfterRender(op, bindingFieldName, fieldName,
defaultFieldName, cachedFieldName);
+ }
+
+ String buildBindingAccessor(EnhancementOperation op, String fieldName,
String parameterName, Location location)
+ {
+ BodyBuilder body = new BodyBuilder();
+ body.begin();
+
+ String bindingFieldName = fieldName + "$Binding";
+
+ op.addField(bindingFieldName, IBinding.class);
+
+ body.addln("if ({0} == null)", bindingFieldName);
+ body.begin();
+ body.addln("{0} = getBinding(\"{1}\");", bindingFieldName,
parameterName);
+ body.end();
+
+ body.addln("return {0};", bindingFieldName);
- extendCleanupAfterRender(op, parameterName, propertyName, propertyType,
- fieldName, defaultFieldName, cachedFieldName);
+ body.end();
+
+ String methodName =
EnhanceUtils.createAccessorMethodName(bindingFieldName);
+
+ op.addMethod(Modifier.PUBLIC,
+ new MethodSignature(IBinding.class, methodName, new
Class[0], null),
+ body.toString(), location);
+
+ return methodName + "()";
}
- private void extendCleanupAfterRender(EnhancementOperation op,
- String parameterName, String propertyName, Class propertyType,
- String fieldName, String defaultFieldName, String cachedFieldName)
+ void extendCleanupAfterRender(EnhancementOperation op, String
bindingFieldName,
+ String fieldName, String defaultFieldName,
String cachedFieldName)
{
BodyBuilder cleanupBody = new BodyBuilder();
@@ -153,31 +187,20 @@
// unless the binding is invariant, in which case it can stick around
// for some future render.
- String bindingName = propertyName + "Binding";
-
- addBindingReference(cleanupBody, bindingName, parameterName);
-
- cleanupBody.addln("if ({0} && ! {1}.isInvariant())", cachedFieldName,
- bindingName);
+ cleanupBody.addln("if ({0} && ! {1}.isInvariant())", cachedFieldName,
bindingFieldName);
cleanupBody.begin();
cleanupBody.addln("{0} = false;", cachedFieldName);
cleanupBody.addln("{0} = {1};", fieldName, defaultFieldName);
cleanupBody.end();
op.extendMethodImplementation(IComponent.class,
- EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
cleanupBody.toString());
- }
-
- private void addBindingReference(BodyBuilder builder,
- String localVariableName, String parameterName)
- {
- builder.addln("{0} {1} = getBinding(\"{2}\");",
- IBinding.class.getName(), localVariableName,
parameterName);
+
EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE, cleanupBody.toString());
}
private void buildMutator(EnhancementOperation op, String parameterName,
- String propertyName, Class propertyType, String fieldName,
- String defaultFieldName, String cachedFieldName, Location location)
+ String propertyName, Class propertyType, String
fieldName,
+ String defaultFieldName, String cachedFieldName,
+ String bindingFieldName, Location location)
{
BodyBuilder builder = new BodyBuilder();
builder.begin();
@@ -196,15 +219,13 @@
// In the normal state, we update the binding first - and it's an error
// if the parameter is not bound.
- addBindingReference(builder, "binding", parameterName);
-
- builder.addln("if (binding == null)");
+ builder.addln("if ({0} == null)", bindingFieldName);
builder.addln(" throw new {0}(\"Parameter ''{1}'' is not bound and
can not be updated.\");",
- ApplicationRuntimeException.class.getName(),
parameterName);
+ ApplicationRuntimeException.class.getName(),
parameterName);
// Always updated the binding first (which may fail with an exception).
- builder.addln("binding.setObject(($w) $1);");
+ builder.addln("{0}.setObject(($w) $1);", bindingFieldName);
// While rendering, we store the updated value for fast
// access again (while the component is still rendering).
@@ -220,40 +241,35 @@
String mutatorMethodName =
EnhanceUtils.createMutatorMethodName(propertyName);
- op.addMethod(Modifier.PUBLIC,
- new MethodSignature(void.class, mutatorMethodName, new Class[]
{ propertyType }, null),
- builder.toString(), location);
+ op.addMethod(Modifier.PUBLIC,
+ new MethodSignature(void.class, mutatorMethodName, new
Class[] { propertyType }, null),
+ builder.toString(), location);
}
// Package private for testing
- void buildAccessor(EnhancementOperation op, String parameterName,
- String propertyName, Class propertyType, String fieldName,
- String defaultFieldName, String cachedFieldName, boolean cache,
- Location location)
+ void buildAccessor(EnhancementOperation op, String propertyName, Class
propertyType,
+ String fieldName, String defaultFieldName, String
cachedFieldName,
+ String bindingFieldName, boolean cache, Location
location)
{
BodyBuilder builder = new BodyBuilder();
builder.begin();
builder.addln("if ({0}) return {1};", cachedFieldName, fieldName);
-
- addBindingReference(builder, "binding", parameterName);
-
- builder.addln("if (binding == null) return {0};", defaultFieldName);
+ builder.addln("if ({0} == null) return {1};", bindingFieldName,
defaultFieldName);
String javaTypeName = ClassFabUtils.getJavaClassName(propertyType);
- builder.addln("{0} result = {1};", javaTypeName,
EnhanceUtils.createUnwrapExpression(op, "binding", propertyType));
+ builder.addln("{0} result = {1};", javaTypeName,
EnhanceUtils.createUnwrapExpression(op, bindingFieldName, propertyType));
// Values read via the binding are cached during the render of
// the component (if the parameter defines cache to be true, which
// is the default), or any time the binding is invariant
// (such as most bindings besides ExpressionBinding.
- String expression = cache ? "isRendering() || binding.isInvariant()"
- : "binding.isInvariant()";
+ String expression = cache ? "isRendering() || {0}.isInvariant()" :
"{0}.isInvariant()";
- builder.addln("if ({0})", expression);
+ builder.addln("if (" + expression + ")", bindingFieldName);
builder.begin();
builder.addln("{0} = result;", fieldName);
builder.addln("{0} = true;", cachedFieldName);
@@ -266,7 +282,7 @@
String accessorMethodName = op.getAccessorMethodName(propertyName);
op.addMethod(Modifier.PUBLIC, new MethodSignature(propertyType,
- accessorMethodName, null, null), builder.toString(), location);
+ accessorMethodName,
null, null), builder.toString(), location);
}
public void setErrorLog(ErrorLog errorLog)
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java?view=diff&rev=550071&r1=550070&r2=550071
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/html/Relation.java
Sat Jun 23 10:55:47 2007
@@ -64,8 +64,7 @@
Object href = getHref();
boolean ok = (href instanceof String) || (href instanceof IAsset);
if (!ok)
- throw new
ApplicationRuntimeException(HTMLMessages.stringOrIAssetExpected(),
- this.getLocation(), null);
+ throw new
ApplicationRuntimeException(HTMLMessages.stringOrIAssetExpected(),
getLocation(), null);
String url;
if (href instanceof String)
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/pageload/PageLoader.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/pageload/PageLoader.java?view=diff&rev=550071&r1=550070&r2=550071
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/pageload/PageLoader.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/pageload/PageLoader.java
Sat Jun 23 10:55:47 2007
@@ -243,8 +243,7 @@
{
if (!name.equals(parameterName))
{
- _log.warn(PageloadMessages.usedParameterAlias(contained,
- name,
parameterName, bspec.getLocation()));
+ _log.warn(PageloadMessages.usedParameterAlias(contained,
name, parameterName, bspec.getLocation()));
}
else if (pspec.isDeprecated())
_log.warn(PageloadMessages.deprecatedParameter(name,
bspec.getLocation(), contained.getType()));
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/TestParameterPropertyWorker.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/TestParameterPropertyWorker.java?view=diff&rev=550071&r1=550070&r2=550071
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/TestParameterPropertyWorker.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/TestParameterPropertyWorker.java
Sat Jun 23 10:55:47 2007
@@ -14,11 +14,6 @@
package org.apache.tapestry.enhance;
-import static org.easymock.EasyMock.expect;
-
-import java.lang.reflect.Modifier;
-import java.util.Collections;
-
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.ErrorLog;
import org.apache.hivemind.Location;
@@ -26,15 +21,20 @@
import org.apache.hivemind.service.MethodSignature;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.BaseComponentTestCase;
+import org.apache.tapestry.IBinding;
import org.apache.tapestry.IComponent;
import org.apache.tapestry.spec.IComponentSpecification;
import org.apache.tapestry.spec.IParameterSpecification;
import org.apache.tapestry.spec.ParameterSpecification;
+import static org.easymock.EasyMock.expect;
import org.testng.annotations.Test;
+import java.lang.reflect.Modifier;
+import java.util.Collections;
+
/**
* Tests for [EMAIL PROTECTED]
org.apache.tapestry.enhance.ParameterPropertyWorker}.
- *
+ *
* @author Howard M. Lewis Ship
*/
@Test
@@ -42,13 +42,13 @@
{
private ParameterSpecification buildParameterSpec(String parameterName,
String type,
- Location location)
+ Location location)
{
return buildParameterSpec(parameterName, parameterName, type,
location);
}
private ParameterSpecification buildParameterSpec(String parameterName,
String propertyName,
- String type, Location location)
+ String type, Location
location)
{
ParameterSpecification ps = new ParameterSpecification();
@@ -61,28 +61,28 @@
}
private IComponentSpecification buildComponentSpecification(String
parameterName,
- IParameterSpecification ps)
+
IParameterSpecification ps)
{
IComponentSpecification result = newSpec();
expect(result.getParameterNames()).andReturn(Collections.singletonList(parameterName));
expect(result.getParameter(parameterName)).andReturn(ps);
-
+
return result;
}
- public void testFailure() throws Exception
+ public void test_Failure() throws Exception
{
Location l = newLocation();
IComponentSpecification spec = buildComponentSpecification("wilma",
buildParameterSpec(
- "wilma",
- "String",
- l));
-
+ "wilma",
+ "String",
+ l));
+
EnhancementOperation op = newMock(EnhancementOperation.class);
-
+
Throwable ex = new ApplicationRuntimeException("Simulated error.");
expect(op.convertTypeName("String")).andThrow(ex);
@@ -91,10 +91,10 @@
ErrorLog log = newMock(ErrorLog.class);
log
- .error(
- "Error adding property wilma to class
org.apache.tapestry.BaseComponent: Simulated error.",
- l,
- ex);
+ .error(
+ "Error adding property wilma to class
org.apache.tapestry.BaseComponent: Simulated error.",
+ l,
+ ex);
replay();
@@ -106,12 +106,12 @@
verify();
}
- public void testSkipParameterAlias()
+ public void test_Skip_Parameter_Alias()
{
IComponentSpecification spec = buildComponentSpecification("barney",
buildParameterSpec(
- "fred",
- null,
- null));
+ "fred",
+ null,
+ null));
EnhancementOperation op = newMock(EnhancementOperation.class);
@@ -129,33 +129,49 @@
* slightly different control flow.
*/
- public void testStandard()
+ public void test_Standard()
{
IComponentSpecification spec = buildComponentSpecification("fred",
buildParameterSpec(
- "fred",
- null,
- null));
+ "fred",
+ null,
+ null));
EnhancementOperation op = newMock(EnhancementOperation.class);
expect(op.getPropertyType("fred")).andReturn(String.class);
-
+
op.claimProperty("fred");
+ String bindingFieldName = "_$fred$Binding";
+
op.addField("_$fred", String.class);
op.addField("_$fred$Default", String.class);
op.addField("_$fred$Cached", boolean.class);
+ op.addField("_$fred$Binding", IBinding.class);
+
+ BodyBuilder builder = new BodyBuilder();
+ builder.begin();
+ builder.addln("if ({0} == null)", bindingFieldName);
+ builder.begin();
+ builder.addln("{0} = getBinding(\"{1}\");", bindingFieldName, "fred");
+ builder.end();
+ builder.addln("return {0};", bindingFieldName);
+ builder.end();
+
+ String methodName =
EnhanceUtils.createAccessorMethodName(bindingFieldName);
+ op.addMethod(Modifier.PUBLIC,
+ new MethodSignature(IBinding.class, methodName, new
Class[0], null),
+ builder.toString(), null);
expect(op.getClassReference(String.class)).andReturn("_class$String");
- BodyBuilder builder = new BodyBuilder();
+ builder.clear();
builder.begin();
builder.addln("if (_$fred$Cached) return _$fred;");
- builder.addln("org.apache.tapestry.IBinding binding =
getBinding(\"fred\");");
- builder.addln("if (binding == null) return _$fred$Default;");
+ builder.addln("if (get_$fred$Binding() == null) return
_$fred$Default;");
builder.add("java.lang.String result = ");
- builder.addln("(java.lang.String) binding.getObject(_class$String);");
- builder.addln("if (isRendering() || binding.isInvariant())");
+ builder.addln("(java.lang.String)
get_$fred$Binding().getObject(_class$String);");
+ builder.addln("if (isRendering() ||
get_$fred$Binding().isInvariant())");
builder.begin();
builder.addln("_$fred = result;");
builder.addln("_$fred$Cached = true;");
@@ -165,11 +181,9 @@
expect(op.getAccessorMethodName("fred")).andReturn("getFred");
- op.addMethod(
- Modifier.PUBLIC,
- new MethodSignature(String.class, "getFred", null, null),
- builder.toString(),
- null);
+ op.addMethod(Modifier.PUBLIC, new MethodSignature(String.class,
"getFred", null, null),
+ builder.toString(),
+ null);
builder.clear();
@@ -180,13 +194,11 @@
builder.addln("return;");
builder.end();
- builder.addln("org.apache.tapestry.IBinding binding =
getBinding(\"fred\");");
-
- builder.addln("if (binding == null)");
+ builder.addln("if (get_$fred$Binding() == null)");
builder
- .addln(" throw new
org.apache.hivemind.ApplicationRuntimeException(\"Parameter 'fred' is not bound
and can not be updated.\");");
+ .addln(" throw new
org.apache.hivemind.ApplicationRuntimeException(\"Parameter 'fred' is not bound
and can not be updated.\");");
- builder.addln("binding.setObject(($w) $1);");
+ builder.addln("get_$fred$Binding().setObject(($w) $1);");
builder.addln("if (isRendering())");
builder.begin();
@@ -196,21 +208,20 @@
builder.end();
op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class,
"setFred", new Class[]
- { String.class }, null), builder.toString(), null);
+ { String.class }, null), builder.toString(), null);
BodyBuilder expectedCleanup = new BodyBuilder();
- expectedCleanup.addln("org.apache.tapestry.IBinding fredBinding =
getBinding(\"fred\");");
- expectedCleanup.addln("if (_$fred$Cached && !
fredBinding.isInvariant())");
+ expectedCleanup.addln("if (_$fred$Cached && !
get_$fred$Binding().isInvariant())");
expectedCleanup.begin();
expectedCleanup.addln("_$fred$Cached = false;");
expectedCleanup.addln("_$fred = _$fred$Default;");
expectedCleanup.end();
op.extendMethodImplementation(
- IComponent.class,
- EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
- expectedCleanup.toString());
+ IComponent.class,
+ EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
+ expectedCleanup.toString());
replay();
@@ -226,14 +237,10 @@
* but the binding is "fred".
*/
- public void testDifferentPropertyName()
+ public void test_Different_Property_Name()
{
Location l = newLocation();
- IComponentSpecification spec = buildComponentSpecification("myparam",
buildParameterSpec(
- "myparam",
- "fred",
- null,
- l));
+ IComponentSpecification spec = buildComponentSpecification("myparam",
buildParameterSpec("myparam", "fred", null, l));
EnhancementOperation op = newMock(EnhancementOperation.class);
@@ -241,20 +248,36 @@
op.claimProperty("fred");
+ String bindingFieldName = "_$fred$Binding";
+
op.addField("_$fred", String.class);
op.addField("_$fred$Default", String.class);
op.addField("_$fred$Cached", boolean.class);
+ op.addField("_$fred$Binding", IBinding.class);
+ BodyBuilder builder = new BodyBuilder();
+ builder.begin();
+ builder.addln("if ({0} == null)", bindingFieldName);
+ builder.begin();
+ builder.addln("{0} = getBinding(\"{1}\");", bindingFieldName,
"myparam");
+ builder.end();
+ builder.addln("return {0};", bindingFieldName);
+ builder.end();
+
+ String methodName =
EnhanceUtils.createAccessorMethodName(bindingFieldName);
+ op.addMethod(Modifier.PUBLIC,
+ new MethodSignature(IBinding.class, methodName, new
Class[0], null),
+ builder.toString(), l);
+
expect(op.getClassReference(String.class)).andReturn("_class$String");
- BodyBuilder builder = new BodyBuilder();
+ builder.clear();
builder.begin();
builder.addln("if (_$fred$Cached) return _$fred;");
- builder.addln("org.apache.tapestry.IBinding binding =
getBinding(\"myparam\");");
- builder.addln("if (binding == null) return _$fred$Default;");
+ builder.addln("if (get_$fred$Binding() == null) return
_$fred$Default;");
builder.add("java.lang.String result = ");
- builder.addln("(java.lang.String) binding.getObject(_class$String);");
- builder.addln("if (isRendering() || binding.isInvariant())");
+ builder.addln("(java.lang.String)
get_$fred$Binding().getObject(_class$String);");
+ builder.addln("if (isRendering() ||
get_$fred$Binding().isInvariant())");
builder.begin();
builder.addln("_$fred = result;");
builder.addln("_$fred$Cached = true;");
@@ -265,10 +288,10 @@
expect(op.getAccessorMethodName("fred")).andReturn("getFred");
op.addMethod(
- Modifier.PUBLIC,
- new MethodSignature(String.class, "getFred", null, null),
- builder.toString(),
- l);
+ Modifier.PUBLIC,
+ new MethodSignature(String.class, "getFred", null, null),
+ builder.toString(),
+ l);
builder.clear();
@@ -279,13 +302,11 @@
builder.addln("return;");
builder.end();
- builder.addln("org.apache.tapestry.IBinding binding =
getBinding(\"myparam\");");
-
- builder.addln("if (binding == null)");
+ builder.addln("if (get_$fred$Binding() == null)");
builder
- .addln(" throw new
org.apache.hivemind.ApplicationRuntimeException(\"Parameter 'myparam' is not
bound and can not be updated.\");");
+ .addln(" throw new
org.apache.hivemind.ApplicationRuntimeException(\"Parameter 'myparam' is not
bound and can not be updated.\");");
- builder.addln("binding.setObject(($w) $1);");
+ builder.addln("get_$fred$Binding().setObject(($w) $1);");
builder.addln("if (isRendering())");
builder.begin();
@@ -295,22 +316,20 @@
builder.end();
op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class,
"setFred", new Class[]
- { String.class }, null), builder.toString(), l);
+ { String.class }, null), builder.toString(), l);
BodyBuilder expectedCleanup = new BodyBuilder();
- expectedCleanup
- .addln("org.apache.tapestry.IBinding fredBinding =
getBinding(\"myparam\");");
- expectedCleanup.addln("if (_$fred$Cached && !
fredBinding.isInvariant())");
+ expectedCleanup.addln("if (_$fred$Cached && !
get_$fred$Binding().isInvariant())");
expectedCleanup.begin();
expectedCleanup.addln("_$fred$Cached = false;");
expectedCleanup.addln("_$fred = _$fred$Default;");
expectedCleanup.end();
op.extendMethodImplementation(
- IComponent.class,
- EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
- expectedCleanup.toString());
+ IComponent.class,
+ EnhanceUtils.CLEANUP_AFTER_RENDER_SIGNATURE,
+ expectedCleanup.toString());
replay();
@@ -321,7 +340,7 @@
verify();
}
- public void testPrimitiveType()
+ public void test_Primitive_Type()
{
Location l = newLocation();
@@ -330,11 +349,10 @@
BodyBuilder builder = new BodyBuilder();
builder.begin();
builder.addln("if (_$fred$Cached) return _$fred;");
- builder.addln("org.apache.tapestry.IBinding binding =
getBinding(\"barney\");");
- builder.addln("if (binding == null) return _$fred$Default;");
+ builder.addln("if (get_$fred$Binding() == null) return
_$fred$Default;");
builder.add("boolean result = ");
- builder.addln(EnhanceUtils.class.getName() + ".toBoolean(binding);");
- builder.addln("if (isRendering() || binding.isInvariant())");
+ builder.addln(EnhanceUtils.class.getName() +
".toBoolean(get_$fred$Binding());");
+ builder.addln("if (isRendering() ||
get_$fred$Binding().isInvariant())");
builder.begin();
builder.addln("_$fred = result;");
builder.addln("_$fred$Cached = true;");
@@ -345,28 +363,28 @@
expect(op.getAccessorMethodName("fred")).andReturn("isFred");
op.addMethod(
- Modifier.PUBLIC,
- new MethodSignature(boolean.class, "isFred", null, null),
- builder.toString(),
- l);
+ Modifier.PUBLIC,
+ new MethodSignature(boolean.class, "isFred", null, null),
+ builder.toString(),
+ l);
replay();
new ParameterPropertyWorker().buildAccessor(
- op,
- "barney",
- "fred",
- boolean.class,
- "_$fred",
- "_$fred$Default",
- "_$fred$Cached",
- true,
- l);
+ op,
+ "fred",
+ boolean.class,
+ "_$fred",
+ "_$fred$Default",
+ "_$fred$Cached",
+ "get_$fred$Binding()",
+ true,
+ l);
verify();
}
- public void testParameterCacheDisabled()
+ public void test_Parameter_Cache_Disabled()
{
Location l = newLocation();
@@ -375,11 +393,10 @@
BodyBuilder builder = new BodyBuilder();
builder.begin();
builder.addln("if (_$fred$Cached) return _$fred;");
- builder.addln("org.apache.tapestry.IBinding binding =
getBinding(\"barney\");");
- builder.addln("if (binding == null) return _$fred$Default;");
+ builder.addln("if (get_$fred$Binding() == null) return
_$fred$Default;");
builder.add("boolean result = ");
- builder.addln(EnhanceUtils.class.getName() + ".toBoolean(binding);");
- builder.addln("if (binding.isInvariant())");
+ builder.addln(EnhanceUtils.class.getName() +
".toBoolean(get_$fred$Binding());");
+ builder.addln("if (get_$fred$Binding().isInvariant())");
builder.begin();
builder.addln("_$fred = result;");
builder.addln("_$fred$Cached = true;");
@@ -390,23 +407,23 @@
expect(op.getAccessorMethodName("fred")).andReturn("isFred");
op.addMethod(
- Modifier.PUBLIC,
- new MethodSignature(boolean.class, "isFred", null, null),
- builder.toString(),
- l);
+ Modifier.PUBLIC,
+ new MethodSignature(boolean.class, "isFred", null, null),
+ builder.toString(),
+ l);
replay();
new ParameterPropertyWorker().buildAccessor(
- op,
- "barney",
- "fred",
- boolean.class,
- "_$fred",
- "_$fred$Default",
- "_$fred$Cached",
- false,
- l);
+ op,
+ "fred",
+ boolean.class,
+ "_$fred",
+ "_$fred$Default",
+ "_$fred$Cached",
+ "get_$fred$Binding()",
+ false,
+ l);
verify();
}
Modified:
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/mock/TestMockApplications.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/mock/TestMockApplications.java?view=diff&rev=550071&r1=550070&r2=550071
==============================================================================
---
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/mock/TestMockApplications.java
(original)
+++
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/mock/TestMockApplications.java
Sat Jun 23 10:55:47 2007
@@ -16,6 +16,7 @@
import ognl.Ognl;
import ognl.OgnlException;
+import ognl.OgnlRuntime;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.HiveMind;
import org.apache.hivemind.Resource;
@@ -34,6 +35,7 @@
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
+import java.beans.Introspector;
import java.io.*;
import java.util.*;
@@ -44,7 +46,7 @@
* The XML format is pretty simple, it contains declarations similar to a
web.xml deployment
* descriptor, a description of the active HttpSession (if any), a description
of the HttpRequest,
* and then a set of expectations for the output stream from the request.
- *
+ *
* @author Howard Lewis Ship
* @since 2.2
*/
@@ -56,15 +58,15 @@
public static final String DEFAULT_BASE_DIR = "./";
public static final String SCRIPTS_DIR = "src/scripts";
-
+
private static String _baseDir;
-
+
private String _testRootDirectory;
private String _path;
private String _fileName;
-
+
private Document _document;
private MockContext _context;
@@ -76,7 +78,7 @@
private MockRequest _request;
private MockResponse _response;
-
+
private int _requestNumber = 0;
private Map _ognlContext = Ognl.createDefaultContext(this);
@@ -96,9 +98,9 @@
private PrintStream _savedOut;
private PrintStream _savedErr;
-
+
private SAXBuilder _builder = new SAXBuilder();
-
+
/**
* Closes System.out and System.err, then restores them to their original
values.
*/
@@ -107,69 +109,69 @@
{
System.err.close();
System.setErr(_savedErr);
-
+
System.out.close();
System.setOut(_savedOut);
-
+
_requestNumber = 0;
_request = null;
_response = null;
}
-
+
@DataProvider(name = "mockTestScripts")
public Object[][] createTestParameters()
{
List data = new ArrayList();
-
+
File scriptsDir = new File(getBaseDirectory() + SCRIPTS_DIR);
-
+
String[] names = scriptsDir.list();
-
+
for (int i = 0; i < names.length; i++)
{
String name = names[i];
-
+
if (name.endsWith(".xml"))
{
data.add(new Object[] {
- getBaseDirectory() + "/src/test-data/",
- getBaseDirectory() + SCRIPTS_DIR + "/" + name,
- name
+ getBaseDirectory() + "/src/test-data/",
+ getBaseDirectory() + SCRIPTS_DIR + "/" + name,
+ name
});
}
}
-
+
return (Object[][])data.toArray(new Object[data.size()][3]);
}
-
+
public String toString()
{
StringBuffer buffer = new StringBuffer("MockTester[");
-
+
if (_document != null)
buffer.append(_document);
-
+
buffer.append(']');
return buffer.toString();
}
-
+
/**
* Invoked to execute the request cycle.
*/
@Test(dataProvider = "mockTestScripts", enabled = false)
- public void execute(String testRootDirectory, String path, String
fileName)
- throws Exception
+ public void execute(String testRootDirectory, String path, String fileName)
+ throws Exception
{
_testRootDirectory = testRootDirectory;
_path = path;
_fileName = fileName;
-
+
// setup and get environment ready
createLogs();
parse();
setup();
-
+
Element root = _document.getRootElement();
List l = root.getChildren("request");
@@ -183,10 +185,12 @@
executeRequest(request);
}
-
+
_servlet.destroy();
-
+
PropertyUtils.clearCache();
+ OgnlRuntime.clearCache();
+ Introspector.flushCaches();
}
private void executeRequest(Element request) throws IOException,
DocumentParseException
@@ -236,19 +240,21 @@
executeAssertions(request);
}
- private void parse()
- throws Exception
+ private void parse()
+ throws Exception
{
_document = _builder.build(_path);
}
-
+
private void setup() throws ServletException
{
Element root = _document.getRootElement();
-
+
if (!root.getName().equals("mock-test"))
throw new RuntimeException("Root element of " + _path + " must be
'mock-test'.");
-
+
+ System.setProperty("org.apache.tapestry.disable-caching", "false");
+
setupContext(root);
setupServlet(root);
}
@@ -258,12 +264,12 @@
_context = new MockContext(_testRootDirectory);
Element context = parent.getChild("context");
-
+
if (context == null)
return;
-
+
String name = context.getAttributeValue("name");
-
+
if (name != null)
_context.setServletContextName(name);
@@ -388,7 +394,7 @@
// mark this test as an error.
throw new ApplicationRuntimeException("Unable to instantiate servlet
class " + className
- + ".", t);
+ + ".", t);
}
public MockContext getContext()
@@ -453,7 +459,7 @@
return;
throw new AssertionError(buildTestName(name) + ": Expression '" +
expression
- + "' was not true.");
+ + "' was not true.");
}
@@ -483,8 +489,8 @@
return ((String) value).length() > 0;
throw new DocumentParseException("Expression '" + expression + "'
evaluates to ("
- + value.getClass().getName() + ") " + value
- + ", which cannot be interpreted as a boolean.");
+ + value.getClass().getName() + ") " +
value
+ + ", which cannot be interpreted as a
boolean.");
}
/**
@@ -494,8 +500,8 @@
* Attribute name is used in error messages.
*/
- private void executeRegexpAssertions(Element request)
- throws DocumentParseException
+ private void executeRegexpAssertions(Element request)
+ throws DocumentParseException
{
String outputString = null;
@@ -527,8 +533,8 @@
* Attribute name is used in error messages.
*/
- private void executeOutputAssertions(Element request)
- throws DocumentParseException
+ private void executeOutputAssertions(Element request)
+ throws DocumentParseException
{
String outputString = null;
@@ -541,10 +547,10 @@
String name = a.getAttributeValue("name");
String substring = a.getTextTrim();
-
+
if (HiveMind.isBlank(substring))
throw new DocumentParseException("Substring is null in " + a);
-
+
if (outputString == null)
outputString = _response.getOutputString();
@@ -560,8 +566,8 @@
* Attribute name is used in error messages.
*/
- private void executeNoOutputAssertions(Element request)
- throws DocumentParseException
+ private void executeNoOutputAssertions(Element request)
+ throws DocumentParseException
{
String outputString = null;
@@ -591,8 +597,8 @@
return _matcher;
}
- private Pattern compile(String pattern)
- throws DocumentParseException
+ private Pattern compile(String pattern)
+ throws DocumentParseException
{
Pattern result = (Pattern) _patternCache.get(pattern);
@@ -607,7 +613,7 @@
catch (MalformedPatternException ex)
{
throw new ApplicationRuntimeException("Malformed regular
expression: " + pattern
- + " in " + _path + ".", ex);
+ + " in " + _path + ".", ex);
}
_patternCache.put(pattern, result);
@@ -616,17 +622,17 @@
}
private void matchRegexp(String name, String text, String pattern)
- throws DocumentParseException
+ throws DocumentParseException
{
Pattern compiled = compile(pattern);
-
+
if (getMatcher().contains(text, compiled))
return;
-
+
System.err.println(text);
-
+
throw new AssertionError(buildTestName(name)
- + ": Response does not contain regular expression '" + pattern
+ "'.");
+ + ": Response does not contain regular
expression '" + pattern + "'.");
}
private void matchSubstring(String name, String text, String substring)
@@ -636,11 +642,11 @@
if (text.indexOf(substring) >= 0)
return;
-
+
System.err.println(text);
-
+
throw new AssertionError(buildTestName(name) + ":" + text + "\n
Response does not contain string '"
- + substring + "'.");
+ + substring + "'.");
}
private void matchNoSubstring(String name, String text, String substring)
@@ -654,7 +660,7 @@
System.err.println(text);
throw new AssertionError(buildTestName(name) + ": Response contains
string '"
- + substring + "'.");
+ + substring + "'.");
}
private void executeOutputMatchesAssertions(Element request) throws
DocumentParseException
@@ -676,7 +682,7 @@
}
private void executeOutputMatchAssertion(Element element, String
outputString)
- throws DocumentParseException
+ throws DocumentParseException
{
String name = element.getAttributeValue("name");
String value = element.getAttributeValue("subgroup");
@@ -691,7 +697,7 @@
PatternMatcher matcher = getMatcher();
Pattern compiled = compile(pattern);
-
+
List<Element> l = element.getChildren("match");
int count = l.size();
int i = 0;
@@ -700,30 +706,30 @@
{
MatchResult match = matcher.getMatch();
String actual = match.group(subgroup);
-
+
boolean matched = contentContains(l, actual);
-
+
if (i >= count)
{
System.err.println(outputString);
throw new AssertionError(buildTestName(name) + ": Too many
matches for '"
- + pattern + "'.");
+ + pattern + "'.");
}
-
+
if (!matched) {
System.err.println(outputString);
throw new AssertionError(buildTestName(name) + ": No expected
match found for "
- + "output of '" + actual + "'. ");
+ + "output of '" + actual + "'. ");
}
-
+
i++;
}
-
+
if (i < count)
{
System.err.println(outputString);
throw new AssertionError(buildTestName(name) + ": Too few matches
for '"
- + pattern + "' (expected " + count + " but got " + i +
").");
+ + pattern + "' (expected " + count + "
but got " + i + ").");
}
}
@@ -733,10 +739,10 @@
if (e.getTextTrim().equals(text))
return true;
}
-
+
return false;
}
-
+
private void executeExceptionAssertions(Element request)
{
List l = request.getChildren("assert-exception");
@@ -764,7 +770,7 @@
return;
throw new AssertionError(buildTestName(name) + " exception message ("
+ message
- + ") does not contain '" + value + "'.");
+ + ") does not contain '" + value + "'.");
}
private void executeCookieAssertions(Element request)
@@ -796,11 +802,11 @@
return;
throw new AssertionError(buildTestName(name) + ": Response cookie
'" + name
- + "': expected '" + value + "', but was '" +
cookies[i].getValue() + "'.");
+ + "': expected '" + value + "', but was
'" + cookies[i].getValue() + "'.");
}
throw new AssertionError(buildTestName(name) + ": Could not find
cookie named '"
- + name + "' in response.");
+ + name + "' in response.");
}
private String buildTestName(String name)
@@ -832,21 +838,21 @@
if (!contentType.equals(actualContentType))
throw new AssertionError(buildTestName(name) + " content-type was
'"
- + actualContentType + "', expected '" + contentType +
"'.");
+ + actualContentType + "', expected '" +
contentType + "'.");
byte[] actualContent = _response.getResponseBytes();
byte[] expectedContent = getFileContent(getBaseDirectory() + "/" +
path);
if (actualContent.length != expectedContent.length)
throw new AssertionError(buildTestName(name) + " actual length of "
- + actualContent.length + " bytes does not match expected
length of "
- + expectedContent.length + " bytes.");
+ + actualContent.length + " bytes does not
match expected length of "
+ + expectedContent.length + " bytes.");
for (int i = 0; i < actualContent.length; i++)
{
if (actualContent[i] != expectedContent[i])
throw new AssertionError(buildTestName(name)
- + " content mismatch at index + " + i + ".");
+ + " content mismatch at index + " + i
+ ".");
}
}
@@ -883,46 +889,46 @@
throw new ApplicationRuntimeException("Unable to read file '" +
path + "'.", ex);
}
}
-
- private void createLogs()
- throws Exception
+
+ private void createLogs()
+ throws Exception
{
File outDir = new File(getBaseDirectory() + LOGS_DIR);
-
+
if (!outDir.isDirectory())
outDir.mkdirs();
-
+
_savedOut = System.out;
_savedErr = System.err;
-
+
System.setOut(createPrintStream(outDir.getPath() + "/" + _fileName,
"out"));
System.setErr(createPrintStream(outDir.getPath() + "/" + _fileName,
"err"));
}
-
+
private PrintStream createPrintStream(String path, String extension)
throws Exception
{
File file = new File(path + "." + extension);
-
+
// Open and truncate file.
-
+
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
return new PrintStream(bos, true);
}
-
+
@AfterClass
public static void deleteDir()
{
File file = new File(getBaseDirectory() + "/target/.private");
-
+
if (!file.exists())
return;
-
+
deleteRecursive(file);
}
-
+
private static void deleteRecursive(File file)
{
if (file.isFile())
@@ -941,7 +947,7 @@
file.delete();
}
-
+
public static String getBaseDirectory()
{
if (_baseDir == null) {
@@ -953,7 +959,7 @@
_baseDir = _baseDir + "tapestry-framework/";
}
}
-
+
return _baseDir;
}
}