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 2005/10/30 17:18:14 UTC
svn commit: r329587 - in /jakarta/tapestry/trunk: ./
annotations/src/descriptor/META-INF/
annotations/src/documentation/content/xdocs/tapestry-annotations/
annotations/src/java/org/apache/tapestry/annotations/
annotations/src/test/org/apache/tapestry/a...
Author: hlship
Date: Sun Oct 30 08:18:02 2005
New Revision: 329587
URL: http://svn.apache.org/viewcvs?rev=329587&view=rev
Log:
TAPESTRY-602: Add annotation to define default value of property
Added:
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValue.java
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValueAnnotationWorker.java
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/SecondaryAnnotationWorker.java
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotationEnhancementWorkerTest.java
- copied, changed from r329512, jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestAnnotationEnhancementWorker.java
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/InitialValueAnnotationWorkerTest.java
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/PersistAnnotationWorkerTest.java
- copied, changed from r329512, jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestPersistAnnotationWorker.java
Removed:
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestAnnotationEnhancementWorker.java
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestPersistAnnotationWorker.java
Modified:
jakarta/tapestry/trunk/annotations/src/descriptor/META-INF/hivemodule.xml
jakarta/tapestry/trunk/annotations/src/documentation/content/xdocs/tapestry-annotations/index.xml
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationEnhancementWorker.java
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationMessages.java
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationStrings.properties
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationUtils.java
jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/PersistAnnotationWorker.java
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/BaseAnnotationTestCase.java
jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/DeprecatedBean.java
jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/enhance/SpecifiedPropertyWorker.java
jakarta/tapestry/trunk/status.xml
Modified: jakarta/tapestry/trunk/annotations/src/descriptor/META-INF/hivemodule.xml
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/descriptor/META-INF/hivemodule.xml?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/descriptor/META-INF/hivemodule.xml (original)
+++ jakarta/tapestry/trunk/annotations/src/descriptor/META-INF/hivemodule.xml Sun Oct 30 08:18:02 2005
@@ -28,6 +28,7 @@
<construct class="AnnotationEnhancementWorker">
<set-configuration property="methodWorkers" configuration-id="MethodWorkers"/>
<set-configuration property="classWorkers" configuration-id="ClassWorkers"/>
+ <set-service property="secondaryAnnotationWorker" service-id="SecondaryAnnotationWorkers"/>
</construct>
</invoke-factory>
@@ -133,6 +134,26 @@
</construct>
</invoke-factory>
+ </service-point>
+
+ <configuration-point id="SecondaryAnnotationWorkers" schema-id="hivemind.lib.ChainContribution">
+
+ Defines a chain of command for secondary annotation workers; workers that operate after
+ any MethodWorkers.
+
+ </configuration-point>
+
+ <contribution configuration-id="SecondaryAnnotationWorkers">
+ <command id="initial-value" object="instance:InitialValueAnnotationWorker"/>
+ </contribution>
+
+ <service-point id="SecondaryAnnotationWorkers" interface="SecondaryAnnotationWorker">
+
+ Chain of command for secondary annnotation workers that operate after any MethodWorkers.
+
+ <invoke-factory service-id="hivemind.lib.ChainFactory">
+ <construct configuration-id="SecondaryAnnotationWorkers"/>
+ </invoke-factory>
</service-point>
</module>
Modified: jakarta/tapestry/trunk/annotations/src/documentation/content/xdocs/tapestry-annotations/index.xml
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/documentation/content/xdocs/tapestry-annotations/index.xml?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/documentation/content/xdocs/tapestry-annotations/index.xml (original)
+++ jakarta/tapestry/trunk/annotations/src/documentation/content/xdocs/tapestry-annotations/index.xml Sun Oct 30 08:18:02 2005
@@ -223,6 +223,53 @@
</section>
+ <section>
+ <title>InitialValue</title>
+
+
+ <p>
+ The <link href="&javadoc;/InitialValue.html">InitialValue</link>
+ annotation allows a default value to be specified for a property.
+ The property may also be persistent (via the <link href="#Persist">@Persist</link>
+ annotation).
+ </p>
+
+ <p>
+ The value is a binding reference; the reference will be evaluated
+ when the page is first loaded, and again when the page is detached and returned
+ to the page pool. The default binding prefix is "ognl:".
+ </p>
+
+ <p>
+ The annotation is attached to an accessor method:
+ </p>
+
+ <source>
+ @InitialValue("request.serverName")
+ public abstract String getServerName();
+ </source>
+
+
+ <p>
+ In many cases, where the initial value is a constant, a better approach is to set the initial value
+ from the component's finishLoad() method.
+ </p>
+
+
+ <source>
+ public abstract int getMaxAttempts();
+
+ public abstract void setMaxAttempts(int maxAttempts);
+
+ protected void finishLoad()
+ {
+ setMaxAttempts(5);
+ }
+ </source>
+
+
+ </section>
+
<section>
<title>InjectAsset</title>
@@ -563,7 +610,8 @@
<p>
This annotation works exactly like a <link href="&spec;#spec.property"><property></link>
-element, except that the initial-value attribute can't be specified.
+element, except that the initial-value attribute can't be specified. Use the <link href="#InitialValue">@InitialValue</link>
+annotation to set the property's initial value.
</p>
</section>
Modified: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationEnhancementWorker.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationEnhancementWorker.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationEnhancementWorker.java (original)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationEnhancementWorker.java Sun Oct 30 08:18:02 2005
@@ -47,6 +47,8 @@
private Map _classWorkers;
+ private SecondaryAnnotationWorker _secondaryAnnotationWorker;
+
public void setClassWorkers(Map classWorkers)
{
_classWorkers = classWorkers;
@@ -107,6 +109,19 @@
{
performMethodEnhancement(op, spec, method, a, classResource);
}
+
+ try
+ {
+ // Remember; _secondaryWorker is a chain-of-command, so this returns true
+ // if any command in the chain returns true.
+
+ if (_secondaryAnnotationWorker.canEnhance(method))
+ _secondaryAnnotationWorker.peformEnhancement(op, spec, method, classResource);
+ }
+ catch (Exception ex)
+ {
+ _errorLog.error(AnnotationMessages.failureEnhancingMethod(method, ex), null, ex);
+ }
}
void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec,
@@ -120,8 +135,10 @@
try
{
- Location location = new DescribedLocation(classResource, AnnotationMessages
- .methodAnnotation(annotation, method));
+ Location location = AnnotationUtils.buildLocationForAnnotation(
+ method,
+ annotation,
+ classResource);
worker.performEnhancement(op, spec, method, location);
}
catch (Exception ex)
@@ -147,5 +164,10 @@
public void setClassResolver(ClassResolver classResolver)
{
_classResolver = classResolver;
+ }
+
+ public void setSecondaryAnnotationWorker(SecondaryAnnotationWorker secondaryWorker)
+ {
+ _secondaryAnnotationWorker = secondaryWorker;
}
}
Modified: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationMessages.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationMessages.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationMessages.java (original)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationMessages.java Sun Oct 30 08:18:02 2005
@@ -94,4 +94,9 @@
{
return _formatter.format("missing-equals-in-meta", value);
}
+
+ static String failureEnhancingMethod(Method method, Exception cause)
+ {
+ return _formatter.format("failure-enhancing-method", method, cause);
+ }
}
Modified: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationStrings.properties
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationStrings.properties?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationStrings.properties (original)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationStrings.properties Sun Oct 30 08:18:02 2005
@@ -23,4 +23,5 @@
binding-wrong-format=The specified binding, ''{0}'', is not properly formatted. It should be ''name=binding reference''.
method-annotation=Annotation {0} of {1}
class-annotation=Annotation {0} of class {1}
-missing-equals-in-meta=The meta value ''{0}'' must include an equals sign to seperate the key and the value.
\ No newline at end of file
+missing-equals-in-meta=The meta value ''{0}'' must include an equals sign to seperate the key and the value.
+failure-enhancing-method=Error enhancing method {0}: {1}
Modified: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationUtils.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationUtils.java (original)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/AnnotationUtils.java Sun Oct 30 08:18:02 2005
@@ -15,9 +15,13 @@
package org.apache.tapestry.annotations;
import java.beans.Introspector;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.apache.hivemind.ApplicationRuntimeException;
+import org.apache.hivemind.Location;
+import org.apache.hivemind.Resource;
+import org.apache.tapestry.util.DescribedLocation;
/**
* @author Howard M. Lewis Ship
@@ -26,6 +30,16 @@
public class AnnotationUtils
{
+ /**
+ * Determines the property name for a method, by stripping off the is/get/set prefix and
+ * decapitalizing the first name.
+ *
+ * @param method
+ * accessor method (get/set/is)
+ * @return the property name for the method
+ * @throws ApplicationRuntimeException
+ * if the method is not an accessor or mutator method
+ */
public static String getPropertyName(Method method)
{
String name = method.getName();
@@ -68,5 +82,13 @@
if (method.getParameterTypes().length != 1)
throw new ApplicationRuntimeException(AnnotationMessages.wrongParameterCount(method));
+ }
+
+ public static Location buildLocationForAnnotation(Method method, Annotation annotation,
+ Resource classResource)
+ {
+ return new DescribedLocation(classResource, AnnotationMessages.methodAnnotation(
+ annotation,
+ method));
}
}
Added: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValue.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValue.java?rev=329587&view=auto
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValue.java (added)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValue.java Sun Oct 30 08:18:02 2005
@@ -0,0 +1,42 @@
+// Copyright 2005 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.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * An annotation used to provide the initial value of a transient or persistent property.
+ *
+ * @author Howard M. Lewis Ship
+ * @since 4.0
+ * @see org.apache.tapestry.annotations.PersistAnnotationWorker
+ * @see org.apache.tapestry.annotations.InitialValueAnnotationWorker
+ */
+@Target(
+{ ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface InitialValue {
+ /**
+ * The initial value for the property as a binding reference that is evaluated when the page is
+ * first constructed, and again every time the page is detached and returned to the page pool.
+ * The default binding prefix is "ognl:".
+ */
+ String value();
+}
Added: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValueAnnotationWorker.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValueAnnotationWorker.java?rev=329587&view=auto
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValueAnnotationWorker.java (added)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/InitialValueAnnotationWorker.java Sun Oct 30 08:18:02 2005
@@ -0,0 +1,71 @@
+// Copyright 2005 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.annotations;
+
+import java.lang.reflect.Method;
+
+import org.apache.hivemind.Location;
+import org.apache.hivemind.Resource;
+import org.apache.tapestry.enhance.EnhancementOperation;
+import org.apache.tapestry.spec.IComponentSpecification;
+import org.apache.tapestry.spec.IPropertySpecification;
+import org.apache.tapestry.spec.PropertySpecification;
+
+/**
+ * Looks for {@link org.apache.tapestry.annotations.InitialValue} annotations on methods that don't
+ * have a {@link org.apache.tapestry.annotations.Persist} annotation (that's handled by
+ * {@link org.apache.tapestry.annotations.PersistAnnotationWorker}); adds an
+ * {@link org.apache.tapestry.spec.IPropertySpecification} for the property, so that its initial
+ * value may be set.
+ *
+ * @author Howard M. Lewis Ship
+ * @since 4.0
+ */
+public class InitialValueAnnotationWorker implements SecondaryAnnotationWorker
+{
+ /**
+ * Returns true if the method has the InitialValue annotation.
+ */
+ public boolean canEnhance(Method method)
+ {
+ return method.getAnnotation(InitialValue.class) != null;
+ }
+
+ public void peformEnhancement(EnhancementOperation op, IComponentSpecification spec,
+ Method method, Resource classResource)
+ {
+ InitialValue iv = method.getAnnotation(InitialValue.class);
+
+ if (iv == null)
+ return;
+
+ if (method.getAnnotation(Persist.class) != null)
+ return;
+
+ Location location = AnnotationUtils.buildLocationForAnnotation(method, iv, classResource);
+
+ String propertyName = AnnotationUtils.getPropertyName(method);
+
+ // Define a transient property
+
+ IPropertySpecification pspec = new PropertySpecification();
+
+ pspec.setName(propertyName);
+ pspec.setLocation(location);
+ pspec.setInitialValue(iv.value());
+
+ spec.addPropertySpecification(pspec);
+ }
+}
Modified: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/PersistAnnotationWorker.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/PersistAnnotationWorker.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/PersistAnnotationWorker.java (original)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/PersistAnnotationWorker.java Sun Oct 30 08:18:02 2005
@@ -26,6 +26,10 @@
* Allow a property to be marked as persistent, and (optionally) allows a strategy to be set. Works
* by adding a new {@link org.apache.tapestry.spec.IPropertySpecification} to the
* {@link org.apache.tapestry.spec.IComponentSpecification}.
+ * <p>
+ * The {@link org.apache.tapestry.annotations.Persist} annotation may <em>optionally</em> by
+ * accompanied by a {@link org.apache.tapestry.annotations.InitialValue} annotation; this sets the
+ * initial value, as a binding reference, used to initialize and re-initialize the property.
*
* @author Howard M. Lewis Ship
* @since 4.0
@@ -37,6 +41,7 @@
Method method, Location location)
{
Persist p = method.getAnnotation(Persist.class);
+ InitialValue iv = method.getAnnotation(InitialValue.class);
String propertyName = AnnotationUtils.getPropertyName(method);
String stategy = p.value();
@@ -46,6 +51,7 @@
pspec.setName(propertyName);
pspec.setPersistence(stategy);
pspec.setLocation(location);
+ pspec.setInitialValue(iv == null ? null : iv.value());
spec.addPropertySpecification(pspec);
}
Added: jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/SecondaryAnnotationWorker.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/SecondaryAnnotationWorker.java?rev=329587&view=auto
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/SecondaryAnnotationWorker.java (added)
+++ jakarta/tapestry/trunk/annotations/src/java/org/apache/tapestry/annotations/SecondaryAnnotationWorker.java Sun Oct 30 08:18:02 2005
@@ -0,0 +1,57 @@
+// Copyright 2005 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.annotations;
+
+import java.lang.reflect.Method;
+
+import org.apache.hivemind.Resource;
+import org.apache.tapestry.enhance.EnhancementOperation;
+import org.apache.tapestry.spec.IComponentSpecification;
+
+/**
+ * A chain-of-command interface for secondary annotation workers; workers that must execute
+ * <em>after</em> other workers.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public interface SecondaryAnnotationWorker
+{
+ /**
+ * Sees if the particular worker can process the method (because it handles an annotation
+ * associated with the method).
+ *
+ * @param method
+ * to check
+ * @return true if the worker should be invoked, false otherwise
+ */
+ public boolean canEnhance(Method method);
+
+ /**
+ * Invoked an <em>all</em> workers in the command chain, if <em>any</em> worker returns true
+ * from {@link #canEnhance(Method)}.
+ *
+ * @param op
+ * enhancement operation
+ * @param spec
+ * specification for the component being enhanced
+ * @param method
+ * the method
+ * @param classResource
+ * a resource representing the class; combined with the method a
+ * {@link org.apache.hivemind.Location} can be created
+ */
+ public void peformEnhancement(EnhancementOperation op, IComponentSpecification spec,
+ Method method, Resource classResource);
+}
Modified: jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java (original)
+++ jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotatedPage.java Sun Oct 30 08:18:02 2005
@@ -27,7 +27,7 @@
import org.apache.tapestry.html.BasePage;
/**
- * Used by {@link org.apache.tapestry.annotations.TestAnnotationEnhancementWorker}. Also a chance
+ * Used by {@link org.apache.tapestry.annotations.AnnotationEnhancementWorkerTest}. Also a chance
* to try each of the annotations out.
*
* @author Howard M. Lewis Ship
@@ -59,6 +59,10 @@
@Persist("client")
public abstract String getClientPersistentProperty();
+ @Persist
+ @InitialValue("user.naturalName")
+ public abstract int getPersistentPropertyWithInitialValue();
+
@InjectAsset("stylesheet")
public abstract IAsset getStylesheetAsset();
@@ -136,4 +140,7 @@
@InjectScript("foo.script")
public abstract IScript getScript();
+
+ @InitialValue("fred")
+ public abstract int getPropertyWithInitialValue();
}
Copied: jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotationEnhancementWorkerTest.java (from r329512, jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestAnnotationEnhancementWorker.java)
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotationEnhancementWorkerTest.java?p2=jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotationEnhancementWorkerTest.java&p1=jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestAnnotationEnhancementWorker.java&r1=329512&r2=329587&rev=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestAnnotationEnhancementWorker.java (original)
+++ jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/AnnotationEnhancementWorkerTest.java Sun Oct 30 08:18:02 2005
@@ -21,12 +21,12 @@
import org.apache.hivemind.ClassResolver;
import org.apache.hivemind.ErrorLog;
+import org.apache.hivemind.Location;
import org.apache.hivemind.Resource;
import org.apache.hivemind.impl.DefaultClassResolver;
import org.apache.tapestry.enhance.EnhancementOperation;
import org.apache.tapestry.spec.IComponentSpecification;
import org.apache.tapestry.util.DescribedLocation;
-import org.easymock.MockControl;
/**
* Tests for {@link org.apache.tapestry.annotations.AnnotationEnhancementWorker}.
@@ -35,15 +35,14 @@
* @since 4.0
*/
-public class TestAnnotationEnhancementWorker extends BaseAnnotationTestCase
+public class AnnotationEnhancementWorkerTest extends BaseAnnotationTestCase
{
protected EnhancementOperation newOp(Class baseClass)
{
- MockControl control = newControl(EnhancementOperation.class);
- EnhancementOperation op = (EnhancementOperation) control.getMock();
+ EnhancementOperation op = (EnhancementOperation) newMock(EnhancementOperation.class);
op.getBaseClass();
- control.setReturnValue(baseClass);
+ setReturnValue(op, baseClass);
return op;
}
@@ -53,6 +52,19 @@
return Collections.singletonMap(annotationClass, worker);
}
+ private class NoOp implements SecondaryAnnotationWorker
+ {
+ public boolean canEnhance(Method method)
+ {
+ return false;
+ }
+
+ public void peformEnhancement(EnhancementOperation op, IComponentSpecification spec,
+ Method method, Resource classResource)
+ {
+ }
+ }
+
/**
* No method annotations registered.
*/
@@ -65,6 +77,7 @@
AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
worker.setMethodWorkers(Collections.EMPTY_MAP);
+ worker.setSecondaryAnnotationWorker(new NoOp());
worker.performEnhancement(op, spec);
@@ -78,15 +91,11 @@
EnhancementOperation op = newOp(AnnotatedPage.class);
IComponentSpecification spec = newSpec();
- MethodAnnotationEnhancementWorker methodWorker = (MethodAnnotationEnhancementWorker) newMock(MethodAnnotationEnhancementWorker.class);
+ MethodAnnotationEnhancementWorker methodWorker = newMethodAnnotationEnhancementWorker();
Method m = findMethod(AnnotatedPage.class, "getInjectedObject");
- DescribedLocation location = newMethodLocation(
- resolver,
- AnnotatedPage.class,
- m,
- InjectObject.class);
+ Location location = newMethodLocation(AnnotatedPage.class, m, InjectObject.class);
methodWorker.performEnhancement(op, spec, m, location);
@@ -95,25 +104,21 @@
AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
worker.setMethodWorkers(newMap(InjectObject.class, methodWorker));
worker.setClassResolver(resolver);
+ worker.setSecondaryAnnotationWorker(new NoOp());
worker.performEnhancement(op, spec);
verifyControls();
}
- protected DescribedLocation newMethodLocation(ClassResolver resolver, Class baseClass,
- Method m, Class annotationClass)
+ protected MethodAnnotationEnhancementWorker newMethodAnnotationEnhancementWorker()
{
- Resource classResource = newResource(resolver, baseClass);
-
- return new DescribedLocation(classResource, AnnotationMessages.methodAnnotation(m
- .getAnnotation(annotationClass), m));
+ return (MethodAnnotationEnhancementWorker) newMock(MethodAnnotationEnhancementWorker.class);
}
- private DescribedLocation newClassLocation(ClassResolver resolver, Class baseClass,
- Class annotationClass)
+ private DescribedLocation newClassLocation(Class baseClass, Class annotationClass)
{
- Resource classResource = newResource(resolver, baseClass);
+ Resource classResource = newResource(baseClass);
Annotation annotation = baseClass.getAnnotation(annotationClass);
return new DescribedLocation(classResource, AnnotationMessages.classAnnotation(
@@ -128,15 +133,11 @@
EnhancementOperation op = newOp(AnnotatedPageSubclass.class);
IComponentSpecification spec = newSpec();
- MethodAnnotationEnhancementWorker methodWorker = (MethodAnnotationEnhancementWorker) newMock(MethodAnnotationEnhancementWorker.class);
+ MethodAnnotationEnhancementWorker methodWorker = newMethodAnnotationEnhancementWorker();
Method m = findMethod(AnnotatedPageSubclass.class, "getInjectedObject");
- DescribedLocation location = newMethodLocation(
- resolver,
- AnnotatedPageSubclass.class,
- m,
- InjectObject.class);
+ Location location = newMethodLocation(AnnotatedPageSubclass.class, m, InjectObject.class);
methodWorker.performEnhancement(op, spec, m, location);
@@ -145,6 +146,7 @@
AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
worker.setMethodWorkers(newMap(InjectObject.class, methodWorker));
worker.setClassResolver(resolver);
+ worker.setSecondaryAnnotationWorker(new NoOp());
worker.performEnhancement(op, spec);
@@ -161,20 +163,14 @@
EnhancementOperation op = newOp(AnnotatedPage.class);
IComponentSpecification spec = newSpec();
- MockControl methodWorkerc = newControl(MethodAnnotationEnhancementWorker.class);
- MethodAnnotationEnhancementWorker methodWorker = (MethodAnnotationEnhancementWorker) methodWorkerc
- .getMock();
+ MethodAnnotationEnhancementWorker methodWorker = newMethodAnnotationEnhancementWorker();
Method m = findMethod(AnnotatedPage.class, "getInjectedObject");
- DescribedLocation location = newMethodLocation(
- resolver,
- AnnotatedPage.class,
- m,
- InjectObject.class);
+ Location location = newMethodLocation(AnnotatedPage.class, m, InjectObject.class);
methodWorker.performEnhancement(op, spec, m, location);
- methodWorkerc.setThrowable(t);
+ setThrowable(methodWorker, t);
log
.error(
@@ -190,17 +186,13 @@
worker.setMethodWorkers(newMap(InjectObject.class, methodWorker));
worker.setErrorLog(log);
worker.setClassResolver(resolver);
+ worker.setSecondaryAnnotationWorker(new NoOp());
worker.performEnhancement(op, spec);
verifyControls();
}
- private ErrorLog newLog()
- {
- return (ErrorLog) newMock(ErrorLog.class);
- }
-
public void testClassAnnotation()
{
ClassResolver resolver = new DefaultClassResolver();
@@ -210,10 +202,7 @@
ClassAnnotationEnhancementWorker classWorker = (ClassAnnotationEnhancementWorker) newMock(ClassAnnotationEnhancementWorker.class);
- DescribedLocation location = newClassLocation(
- resolver,
- DeprecatedBean.class,
- Deprecated.class);
+ DescribedLocation location = newClassLocation(DeprecatedBean.class, Deprecated.class);
classWorker.performEnhancement(op, spec, DeprecatedBean.class, location);
@@ -222,6 +211,7 @@
AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
worker.setClassWorkers(newMap(Deprecated.class, classWorker));
worker.setClassResolver(resolver);
+ worker.setSecondaryAnnotationWorker(new NoOp());
worker.performEnhancement(op, spec);
@@ -236,19 +226,14 @@
EnhancementOperation op = newOp(DeprecatedBean.class);
IComponentSpecification spec = newSpec();
- MockControl classWorkerc = newControl(ClassAnnotationEnhancementWorker.class);
- ClassAnnotationEnhancementWorker classWorker = (ClassAnnotationEnhancementWorker) classWorkerc
- .getMock();
+ ClassAnnotationEnhancementWorker classWorker = (ClassAnnotationEnhancementWorker) newMock(ClassAnnotationEnhancementWorker.class);
Throwable t = new RuntimeException("Simulated failure.");
- DescribedLocation location = newClassLocation(
- resolver,
- DeprecatedBean.class,
- Deprecated.class);
+ DescribedLocation location = newClassLocation(DeprecatedBean.class, Deprecated.class);
classWorker.performEnhancement(op, spec, DeprecatedBean.class, location);
- classWorkerc.setThrowable(t);
+ setThrowable(classWorker, t);
log
.error(
@@ -263,6 +248,7 @@
worker.setClassWorkers(newMap(Deprecated.class, classWorker));
worker.setErrorLog(log);
worker.setClassResolver(resolver);
+ worker.setSecondaryAnnotationWorker(new NoOp());
worker.performEnhancement(op, spec);
@@ -278,9 +264,72 @@
AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
worker.setClassWorkers(Collections.EMPTY_MAP);
+ worker.setSecondaryAnnotationWorker(new NoOp());
worker.performEnhancement(op, spec);
verifyControls();
+ }
+
+ public void testSecondaryEnhancementWorker()
+ {
+ SecondaryAnnotationWorker secondary = newSecondaryAnnotationWorker();
+
+ EnhancementOperation op = newOp();
+ IComponentSpecification spec = newSpec();
+ Method method = findMethod(AnnotatedPage.class, "getPropertyWithInitialValue");
+
+ Resource classResource = newResource(AnnotatedPage.class);
+
+ secondary.canEnhance(method);
+ setReturnValue(secondary, true);
+
+ secondary.peformEnhancement(op, spec, method, classResource);
+
+ replayControls();
+
+ AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
+ worker.setSecondaryAnnotationWorker(secondary);
+ worker.setMethodWorkers(Collections.EMPTY_MAP);
+
+ worker.performMethodEnhancement(op, spec, method, classResource);
+
+ verifyControls();
+ }
+
+ public void testSecondaryEnhancementWorkerFailure()
+ {
+ SecondaryAnnotationWorker secondary = newSecondaryAnnotationWorker();
+
+ EnhancementOperation op = newOp();
+ IComponentSpecification spec = newSpec();
+ ErrorLog log = newLog();
+
+ Method method = findMethod(AnnotatedPage.class, "getPropertyWithInitialValue");
+
+ Resource classResource = newResource(AnnotatedPage.class);
+
+ RuntimeException cause = new RuntimeException("Forced.");
+
+ secondary.canEnhance(method);
+ setThrowable(secondary, cause);
+
+ log.error(AnnotationMessages.failureEnhancingMethod(method, cause), null, cause);
+
+ replayControls();
+
+ AnnotationEnhancementWorker worker = new AnnotationEnhancementWorker();
+ worker.setSecondaryAnnotationWorker(secondary);
+ worker.setMethodWorkers(Collections.EMPTY_MAP);
+ worker.setErrorLog(log);
+
+ worker.performMethodEnhancement(op, spec, method, classResource);
+
+ verifyControls();
+ }
+
+ protected SecondaryAnnotationWorker newSecondaryAnnotationWorker()
+ {
+ return (SecondaryAnnotationWorker) newMock(SecondaryAnnotationWorker.class);
}
}
Modified: jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/BaseAnnotationTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/BaseAnnotationTestCase.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/BaseAnnotationTestCase.java (original)
+++ jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/BaseAnnotationTestCase.java Sun Oct 30 08:18:02 2005
@@ -16,7 +16,8 @@
import java.lang.reflect.Method;
-import org.apache.hivemind.ClassResolver;
+import org.apache.hivemind.ErrorLog;
+import org.apache.hivemind.Location;
import org.apache.hivemind.Resource;
import org.apache.hivemind.test.HiveMindTestCase;
import org.apache.hivemind.util.ClasspathResource;
@@ -52,9 +53,24 @@
return (EnhancementOperation) newMock(EnhancementOperation.class);
}
- protected Resource newResource(ClassResolver resolver, Class clazz)
+ protected Resource newResource(Class clazz)
{
- return new ClasspathResource(resolver, clazz.getName().replace('.', '/'));
+ return new ClasspathResource(getClassResolver(), clazz.getName().replace('.', '/'));
+ }
+
+ protected ErrorLog newLog()
+ {
+ return (ErrorLog) newMock(ErrorLog.class);
+ }
+
+ protected Location newMethodLocation(Class baseClass, Method m, Class annotationClass)
+ {
+ Resource classResource = newResource(baseClass);
+
+ return AnnotationUtils.buildLocationForAnnotation(
+ m,
+ m.getAnnotation(annotationClass),
+ classResource);
}
}
Modified: jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/DeprecatedBean.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/DeprecatedBean.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/DeprecatedBean.java (original)
+++ jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/DeprecatedBean.java Sun Oct 30 08:18:02 2005
@@ -15,7 +15,7 @@
package org.apache.tapestry.annotations;
/**
- * Used by {@link org.apache.tapestry.annotations.TestAnnotationEnhancementWorker} to check that
+ * Used by {@link org.apache.tapestry.annotations.AnnotationEnhancementWorkerTest} to check that
* class annotation enhancements are recognized.
*
* @author Howard Lewis Ship
Added: jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/InitialValueAnnotationWorkerTest.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/InitialValueAnnotationWorkerTest.java?rev=329587&view=auto
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/InitialValueAnnotationWorkerTest.java (added)
+++ jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/InitialValueAnnotationWorkerTest.java Sun Oct 30 08:18:02 2005
@@ -0,0 +1,103 @@
+// Copyright 2005 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.annotations;
+
+import java.lang.reflect.Method;
+
+import org.apache.hivemind.Location;
+import org.apache.hivemind.Resource;
+import org.apache.tapestry.enhance.EnhancementOperation;
+import org.apache.tapestry.spec.ComponentSpecification;
+import org.apache.tapestry.spec.IComponentSpecification;
+import org.apache.tapestry.spec.IPropertySpecification;
+
+/**
+ * Tests for {@link org.apache.tapestry.annotations.InitialValueAnnotationWorker}.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public class InitialValueAnnotationWorkerTest extends BaseAnnotationTestCase
+{
+ public void testCanEnhance()
+ {
+ InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
+
+ assertEquals(false, worker.canEnhance(findMethod(AnnotatedPage.class, "getMapBean")));
+ assertEquals(true, worker.canEnhance(findMethod(
+ AnnotatedPage.class,
+ "getPropertyWithInitialValue")));
+ }
+
+ public void testEnhanceNonMatch()
+ {
+ EnhancementOperation op = newOp();
+ IComponentSpecification spec = newSpec();
+ Method method = findMethod(AnnotatedPage.class, "getMapBean");
+ Resource resource = newResource(AnnotatedPage.class);
+
+ replayControls();
+
+ InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
+
+ worker.peformEnhancement(op, spec, method, resource);
+
+ verifyControls();
+ }
+
+ public void testEnhancePersistAnnotationPresent()
+ {
+ EnhancementOperation op = newOp();
+ IComponentSpecification spec = newSpec();
+ Method method = findMethod(AnnotatedPage.class, "getPersistentPropertyWithInitialValue");
+ Resource resource = newResource(AnnotatedPage.class);
+
+ replayControls();
+
+ InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
+
+ worker.peformEnhancement(op, spec, method, resource);
+
+ verifyControls();
+ }
+
+ public void testJustInitialValue()
+ {
+ EnhancementOperation op = newOp();
+ IComponentSpecification spec = new ComponentSpecification();
+ Method method = findMethod(AnnotatedPage.class, "getPropertyWithInitialValue");
+ Resource resource = newResource(AnnotatedPage.class);
+
+ replayControls();
+
+ InitialValueAnnotationWorker worker = new InitialValueAnnotationWorker();
+
+ worker.peformEnhancement(op, spec, method, resource);
+
+ verifyControls();
+
+ IPropertySpecification ps = spec.getPropertySpecification("propertyWithInitialValue");
+
+ assertEquals("propertyWithInitialValue", ps.getName());
+ assertEquals(false, ps.isPersistent());
+ assertEquals("fred", ps.getInitialValue());
+
+ Location expectedLocation = newMethodLocation(
+ AnnotatedPage.class,
+ method,
+ InitialValue.class);
+
+ assertEquals(expectedLocation, ps.getLocation());
+ }
+}
Copied: jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/PersistAnnotationWorkerTest.java (from r329512, jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestPersistAnnotationWorker.java)
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/PersistAnnotationWorkerTest.java?p2=jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/PersistAnnotationWorkerTest.java&p1=jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestPersistAnnotationWorker.java&r1=329512&r2=329587&rev=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/TestPersistAnnotationWorker.java (original)
+++ jakarta/tapestry/trunk/annotations/src/test/org/apache/tapestry/annotations/PersistAnnotationWorkerTest.java Sun Oct 30 08:18:02 2005
@@ -28,9 +28,32 @@
* @author Howard M. Lewis Ship
* @since 4.0
*/
-public class TestPersistAnnotationWorker extends TestAnnotationEnhancementWorker
+public class PersistAnnotationWorkerTest extends AnnotationEnhancementWorkerTest
{
- public void testInject()
+ public void testDefaultStrategy()
+ {
+ Location l = newLocation();
+
+ EnhancementOperation op = newOp();
+ IComponentSpecification spec = new ComponentSpecification();
+
+ replayControls();
+
+ Method m = findMethod(AnnotatedPage.class, "getPersistentProperty");
+
+ new PersistAnnotationWorker().performEnhancement(op, spec, m, l);
+
+ verifyControls();
+
+ IPropertySpecification ps = spec.getPropertySpecification("persistentProperty");
+
+ assertEquals("session", ps.getPersistence());
+ assertEquals("persistentProperty", ps.getName());
+ assertSame(l, ps.getLocation());
+ assertNull(ps.getInitialValue());
+ }
+
+ public void testStrategySpecified()
{
Location l = newLocation();
@@ -50,5 +73,30 @@
assertEquals("client", ps.getPersistence());
assertEquals("clientPersistentProperty", ps.getName());
assertSame(l, ps.getLocation());
+ assertNull(ps.getInitialValue());
+ }
+
+ public void testWithInitialValue()
+ {
+ Location l = newLocation();
+
+ EnhancementOperation op = newOp();
+ IComponentSpecification spec = new ComponentSpecification();
+
+ replayControls();
+
+ Method m = findMethod(AnnotatedPage.class, "getPersistentPropertyWithInitialValue");
+
+ new PersistAnnotationWorker().performEnhancement(op, spec, m, l);
+
+ verifyControls();
+
+ IPropertySpecification ps = spec
+ .getPropertySpecification("persistentPropertyWithInitialValue");
+
+ assertEquals("session", ps.getPersistence());
+ assertEquals("persistentPropertyWithInitialValue", ps.getName());
+ assertSame(l, ps.getLocation());
+ assertEquals("user.naturalName", ps.getInitialValue());
}
}
Modified: jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/enhance/SpecifiedPropertyWorker.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/enhance/SpecifiedPropertyWorker.java?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/enhance/SpecifiedPropertyWorker.java (original)
+++ jakarta/tapestry/trunk/framework/src/java/org/apache/tapestry/enhance/SpecifiedPropertyWorker.java Sun Oct 30 08:18:02 2005
@@ -35,6 +35,8 @@
*
* @author Howard M. Lewis Ship
* @since 4.0
+ * @see org.apache.tapestry.annotations.PersistAnnotationWorker
+ * @see org.apache.tapestry.annotations.InitialValueAnnotationWorker
*/
public class SpecifiedPropertyWorker implements EnhancementWorker
{
Modified: jakarta/tapestry/trunk/status.xml
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/status.xml?rev=329587&r1=329586&r2=329587&view=diff
==============================================================================
--- jakarta/tapestry/trunk/status.xml (original)
+++ jakarta/tapestry/trunk/status.xml Sun Oct 30 08:18:02 2005
@@ -59,6 +59,7 @@
<action type="fix" dev="HLS" fixes-bug="TAPESTRY-699"><div> tag around form hidden fields should not be visible</action>
<action type="fix" dev="HLS" fixes-bug="TAPESTRY-711">Do not give focus to disabled fields</action>
<action type="fix" dev="HLS" fixes-bug="TAPESTRY-715">LinkSubmit component should render its body, even when disabled</action>
+ <action type="fix" dev="HLS" fixes-bug="TAPESTRY-602">Add annotation to define default value of property</action>
</release>
<release version="4.0-beta-11" date="Oct 16 2005">
<action type="fix" dev="HLS" fixes-bug="TAPESTRY-650">ClassNotFoundException thrown when deserializing an object from a client persistent property</action>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org