Author: ivaynberg
Date: Thu Dec 2 06:43:35 2010
New Revision: 1041290
URL: http://svn.apache.org/viewvc?rev=1041290&view=rev
Log:
Issue: WICKET-3214
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/validation/FormValidatorAdapter.java
(with props)
wicket/trunk/wicket/src/main/java/org/apache/wicket/validation/ValidatorAdapter.java
(with props)
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/FormValidatorBehaviorTest.java
(with props)
wicket/trunk/wicket/src/test/java/org/apache/wicket/validation/ValidatorBehaviorTest.java
(with props)
Removed:
wicket/trunk/wicket/src/main/java/org/apache/wicket/validation/IValidatorAddListener.java
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java?rev=1041290&r1=1041289&r2=1041290&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java Thu Dec
2 06:43:35 2010
@@ -4297,14 +4297,18 @@ public abstract class Component
/**
* Removes behavior from component
*
- * @param behavior
- * behavior to remove
+ * @param behaviors
+ * behaviors to remove
*
* @return this (to allow method call chaining)
*/
- public Component remove(final Behavior behavior)
+ public Component remove(final Behavior... behaviors)
{
- new Behaviors(this).remove(behavior);
+ Behaviors helper = new Behaviors(this);
+ for (Behavior behavior : behaviors)
+ {
+ helper.remove(behavior);
+ }
return this;
}
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=1041290&r1=1041289&r2=1041290&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
Thu Dec 2 06:43:35 2010
@@ -26,12 +26,14 @@ import java.util.Map;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
+import org.apache.wicket.markup.html.form.validation.FormValidatorAdapter;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
@@ -58,7 +60,6 @@ import org.apache.wicket.util.visit.Clas
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.apache.wicket.util.visit.Visits;
-import org.apache.wicket.validation.IValidatorAddListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -262,9 +263,6 @@ public class Form<T> extends WebMarkupCo
*/
private IFormSubmittingComponent defaultSubmittingComponent;
- /** multi-validators assigned to this form */
- private Object formValidators = null;
-
/**
* Maximum size of an upload in bytes. If null, the setting
* {...@link IApplicationSettings#getDefaultMaximumUploadSize()} is
used.
@@ -328,13 +326,13 @@ public class Form<T> extends WebMarkupCo
throw new IllegalArgumentException("Argument
`validator` cannot be null");
}
- // add the validator
- formValidators_add(validator);
-
- // see whether the validator listens for add events
- if (validator instanceof IValidatorAddListener)
+ if (validator instanceof Behavior)
+ {
+ add((Behavior)validator);
+ }
+ else
{
- ((IValidatorAddListener)validator).onAdded(this);
+ add(new FormValidatorAdapter(validator));
}
}
@@ -354,96 +352,37 @@ public class Form<T> extends WebMarkupCo
throw new IllegalArgumentException("Argument
`validator` cannot be null");
}
- IFormValidator removed = formValidators_remove(validator);
- if (removed == null)
+ Behavior match = null;
+ for (Behavior behavior : getBehaviors())
{
- throw new IllegalStateException(
- "Tried to remove form validator that was not
previously added. "
- + "Make sure your validator's equals()
implementation is sufficient");
- }
- addStateChange();
- }
-
- private final int formValidators_indexOf(IFormValidator validator)
- {
- if (formValidators != null)
- {
- if (formValidators instanceof IFormValidator)
+ if (behavior.equals(validator))
{
- final IFormValidator v =
(IFormValidator)formValidators;
- if (v == validator || v.equals(validator))
- {
- return 0;
- }
+ match = behavior;
+ break;
}
- else
+ else if (behavior instanceof FormValidatorAdapter)
{
- final IFormValidator[] validators =
(IFormValidator[])formValidators;
- for (int i = 0; i < validators.length; i++)
+ if
(((FormValidatorAdapter)behavior).getValidator().equals(validator))
{
- final IFormValidator v = validators[i];
- if (v == validator ||
v.equals(validator))
- {
- return i;
- }
+ match = behavior;
+ break;
}
}
}
- return -1;
- }
-
- private final IFormValidator formValidators_remove(IFormValidator
validator)
- {
- int index = formValidators_indexOf(validator);
- if (index != -1)
- {
- return formValidators_remove(index);
- }
- return null;
- }
- private final IFormValidator formValidators_remove(int index)
- {
- if (formValidators instanceof IFormValidator)
+ if (match != null)
{
- if (index == 0)
- {
- final IFormValidator removed =
(IFormValidator)formValidators;
- formValidators = null;
- return removed;
- }
- else
- {
- throw new IndexOutOfBoundsException();
- }
+ remove(match);
}
else
{
- final IFormValidator[] validators =
(IFormValidator[])formValidators;
- final IFormValidator removed = validators[index];
- // check if we can collapse array of 1 element into a
single object
- if (validators.length == 2)
- {
- formValidators = validators[1 - index];
- }
- else
- {
- IFormValidator[] newValidators = new
IFormValidator[validators.length - 1];
- int j = 0;
- for (int i = 0; i < validators.length; i++)
- {
- if (i != index)
- {
- newValidators[j++] =
validators[i];
- }
- }
- formValidators = newValidators;
- }
- return removed;
+
+ throw new IllegalStateException(
+ "Tried to remove form validator that was not
previously added. "
+ + "Make sure your validator's equals()
implementation is sufficient");
}
}
-
/**
* Clears the input from the form's nested children of type {...@link
FormComponent}. This method
* is typically called when a form needs to be reset.
@@ -560,24 +499,16 @@ public class Form<T> extends WebMarkupCo
*/
public final Collection<IFormValidator> getFormValidators()
{
- final int size = formValidators_size();
+ List<IFormValidator> validators = new
ArrayList<IFormValidator>();
- List<IFormValidator> validators = null;
-
- if (size == 0)
- {
- // form has no validators, use empty collection
- validators = Collections.emptyList();
- }
- else
+ for (Behavior behavior : getBehaviors())
{
- // form has validators, copy all into collection
- validators = new ArrayList<IFormValidator>(size);
- for (int i = 0; i < size; i++)
+ if (behavior instanceof IFormValidator)
{
- validators.add(formValidators_get(i));
+ validators.add((IFormValidator)behavior);
}
}
+
return Collections.unmodifiableCollection(validators);
}
@@ -1104,77 +1035,6 @@ public class Form<T> extends WebMarkupCo
}
/**
- * @param validator
- * The form validator to add to the formValidators Object
(which may be an array of
- * IFormValidators or a single instance, for efficiency)
- */
- private void formValidators_add(final IFormValidator validator)
- {
- if (formValidators == null)
- {
- formValidators = validator;
- }
- else
- {
- // Get current list size
- final int size = formValidators_size();
-
- // Create array that holds size + 1 elements
- final IFormValidator[] validators = new
IFormValidator[size + 1];
-
- // Loop through existing validators copying them
- for (int i = 0; i < size; i++)
- {
- validators[i] = formValidators_get(i);
- }
-
- // Add new validator to the end
- validators[size] = validator;
-
- // Save new validator list
- formValidators = validators;
- }
- }
-
- /**
- * Gets form validator from formValidators Object (which may be an
array of IFormValidators or a
- * single instance, for efficiency) at the given index
- *
- * @param index
- * The index of the validator to get
- * @return The form validator
- */
- private IFormValidator formValidators_get(int index)
- {
- if (formValidators == null)
- {
- throw new IndexOutOfBoundsException();
- }
- if (formValidators instanceof IFormValidator[])
- {
- return ((IFormValidator[])formValidators)[index];
- }
- return (IFormValidator)formValidators;
- }
-
- /**
- * @return The number of form validators in the formValidators Object
(which may be an array of
- * IFormValidators or a single instance, for efficiency)
- */
- private int formValidators_size()
- {
- if (formValidators == null)
- {
- return 0;
- }
- if (formValidators instanceof IFormValidator[])
- {
- return ((IFormValidator[])formValidators).length;
- }
- return 1;
- }
-
- /**
* Visits the form's children FormComponents and inform them that a new
user input is available
* in the Request
*/
@@ -1562,11 +1422,14 @@ public class Form<T> extends WebMarkupCo
tag.put("enctype", "multipart/form-data");
//
// require the application-encoding for
multipart/form-data to be sure to
- // get multipart-uploaded characters with the
proper encoding on the following request.
- //
- // see
http://stackoverflow.com/questions/546365/utf-8-text-is-garbled-when-form-is-posted-as-multipart-form-data
- //
- tag.put("accept-encoding",
getApplication().getRequestCycleSettings().getResponseRequestEncoding());
+ // get multipart-uploaded characters with the
proper encoding on the following
+// request.
+//
+// see
+//
http://stackoverflow.com/questions/546365/utf-8-text-is-garbled-when-form-is-posted-as-multipart-form-data
+//
+ tag.put("accept-encoding",
getApplication().getRequestCycleSettings()
+ .getResponseRequestEncoding());
}
else
{
@@ -1925,10 +1788,12 @@ public class Form<T> extends WebMarkupCo
*/
protected final void validateFormValidators()
{
- final int count = formValidators_size();
- for (int i = 0; i < count; i++)
+ for (Behavior behavior : getBehaviors())
{
- validateFormValidator(formValidators_get(i));
+ if (behavior instanceof IFormValidator)
+ {
+ validateFormValidator((IFormValidator)behavior);
+ }
}
}
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java?rev=1041290&r1=1041289&r2=1041290&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
Thu Dec 2 06:43:35 2010
@@ -34,11 +34,13 @@ import org.apache.wicket.IConverterLocat
import org.apache.wicket.Localizer;
import org.apache.wicket.Page;
import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IPropertyReflectionAwareModel;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;
+import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.lang.Classes;
import org.apache.wicket.util.lang.WicketObjects;
import org.apache.wicket.util.string.PrependingStringBuffer;
@@ -55,8 +57,8 @@ import org.apache.wicket.validation.INul
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidationError;
import org.apache.wicket.validation.IValidator;
-import org.apache.wicket.validation.IValidatorAddListener;
import org.apache.wicket.validation.ValidationError;
+import org.apache.wicket.validation.ValidatorAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -407,12 +409,6 @@ public abstract class FormComponent<T> e
private String typeName;
/**
- * The list of validators for this form component as either an
IValidator instance or an array
- * of IValidator instances.
- */
- private Object validators = null;
-
- /**
* @see org.apache.wicket.Component#Component(String)
*/
public FormComponent(final String id)
@@ -454,13 +450,59 @@ public abstract class FormComponent<T> e
{
throw new IllegalArgumentException("validator argument
cannot be null");
}
- // add the validator
- validators_add(validator);
- // see whether the validator listens for add events
- if (validator instanceof IValidatorAddListener)
+ if (validator instanceof Behavior)
+ {
+ add((Behavior)validator);
+ }
+ else
{
- ((IValidatorAddListener)validator).onAdded(this);
+ add((Behavior)new ValidatorAdapter(validator));
+ }
+ return this;
+ }
+
+ /**
+ * Removes a validator from the form component.
+ *
+ * @param validator
+ * validator
+ * @throws IllegalArgumentException
+ * if validator is null or not found
+ * @see IValidator
+ * @see #add(IValidator)
+ * @return form component for chaining
+ */
+ public final FormComponent<T> remove(final IValidator<? super T>
validator)
+ {
+ Args.notNull(validator, "validator");
+ Behavior match = null;
+ for (Behavior behavior : getBehaviors())
+ {
+ if (behavior.equals(validator))
+ {
+ match = behavior;
+ break;
+ }
+ else if (behavior instanceof ValidatorAdapter)
+ {
+ if
(((ValidatorAdapter<?>)behavior).getValidator().equals(validator))
+ {
+ match = behavior;
+ break;
+ }
+ }
+ }
+
+ if (match != null)
+ {
+ remove(match);
+ }
+ else
+ {
+ throw new IllegalStateException(
+ "Tried to remove validator that was not
previously added. "
+ + "Make sure your validator's equals()
implementation is sufficient");
}
return this;
}
@@ -557,7 +599,7 @@ public abstract class FormComponent<T> e
if (message == null)
{
- StringBuilder buffer = new StringBuilder();
+ StringBuilder buffer = new StringBuilder();
buffer.append("Could not locate error message for
component: ");
buffer.append(Classes.simpleName(getClass()));
buffer.append("@");
@@ -756,22 +798,20 @@ public abstract class FormComponent<T> e
*
* @return List of validators
*/
+ @SuppressWarnings("unchecked")
public final List<IValidator<? super T>> getValidators()
{
- final int size = validators_size();
- if (size == 0)
- {
- return Collections.emptyList();
- }
- else
+ final List<IValidator<? super T>> list = new
ArrayList<IValidator<? super T>>();
+
+ for (Behavior behavior : getBehaviors())
{
- final List<IValidator<? super T>> list = new
ArrayList<IValidator<? super T>>(size);
- for (int i = 0; i < size; i++)
+ if (behavior instanceof IValidator)
{
- list.add(validators_get(i));
+ list.add((IValidator<? super T>)behavior);
}
- return Collections.unmodifiableList(list);
}
+
+ return Collections.unmodifiableList(list);
}
/**
@@ -1046,79 +1086,6 @@ public abstract class FormComponent<T> e
}
/**
- * @param validator
- * The validator to add to the validators Object (which may
be an array of
- * IValidators or a single instance, for efficiency)
- */
- @SuppressWarnings("unchecked")
- private void validators_add(final IValidator<? super T> validator)
- {
- if (validators == null)
- {
- validators = validator;
- }
- else
- {
- // Get current list size
- final int size = validators_size();
-
- // Create array that holds size + 1 elements
- final IValidator<? super T>[] validators = new
IValidator[size + 1];
-
- // Loop through existing validators copying them
- for (int i = 0; i < size; i++)
- {
- validators[i] = validators_get(i);
- }
-
- // Add new validator to the end
- validators[size] = validator;
-
- // Save new validator list
- this.validators = validators;
- }
- }
-
- /**
- * Gets validator from validators Object (which may be an array of
IValidators or a single
- * instance, for efficiency) at the given index
- *
- * @param index
- * The index of the validator to get
- * @return The validator
- */
- @SuppressWarnings("unchecked")
- private IValidator<T> validators_get(int index)
- {
- if (validators == null)
- {
- throw new IndexOutOfBoundsException();
- }
- if (validators instanceof IValidator[])
- {
- return ((IValidator[])validators)[index];
- }
- return (IValidator<T>)validators;
- }
-
- /**
- * @return The number of validators in the validators Object (which may
be an array of
- * IValidators or a single instance, for efficiency)
- */
- private int validators_size()
- {
- if (validators == null)
- {
- return 0;
- }
- if (validators instanceof IValidator<?>[])
- {
- return ((IValidator[])validators).length;
- }
- return 1;
- }
-
- /**
* Converts and validates the conversion of the raw input string into
the object specified by
* {...@link FormComponent#getType()} and records any errors. Converted
value is available through
* {...@link FormComponent#getConvertedInput()}.
@@ -1423,30 +1390,30 @@ public abstract class FormComponent<T> e
/**
* Validates this component using the component's validators.
*/
+ @SuppressWarnings("unchecked")
protected final void validateValidators()
{
- final int size = validators_size();
-
final IValidatable<T> validatable = newValidatable();
- int i = 0;
- IValidator<T> validator = null;
-
boolean isNull = getConvertedInput() == null;
+ IValidator<T> validator = null;
+
try
{
- for (i = 0; i < size; i++)
+ for (Behavior behavior : getBehaviors())
{
- validator = validators_get(i);
-
- if (isNull == false || validator instanceof
INullAcceptingValidator<?>)
+ if (behavior instanceof IValidator)
{
- validator.validate(validatable);
- }
- if (!isValid())
- {
- break;
+ validator = (IValidator<T>)behavior;
+ if (isNull == false || validator
instanceof INullAcceptingValidator<?>)
+ {
+ validator.validate(validatable);
+ }
+ if (!isValid())
+ {
+ break;
+ }
}
}
}
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/validation/FormValidatorAdapter.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/validation/FormValidatorAdapter.java?rev=1041290&view=auto
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/validation/FormValidatorAdapter.java
(added)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/validation/FormValidatorAdapter.java
Thu Dec 2 06:43:35 2010
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.wicket.markup.html.form.validation;
+
+import org.apache.wicket.behavior.Behavior;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.FormComponent;
+
+/**
+ * Adapts {...@link IFormValidator} to {...@link Behavior}
+ *
+ * @author igor
+ */
+public class FormValidatorAdapter extends Behavior implements IFormValidator
+{
+ private static final long serialVersionUID = 1L;
+
+ private final IFormValidator validator;
+
+ /**
+ * Constructor
+ *
+ * @param validator
+ */
+ public FormValidatorAdapter(IFormValidator validator)
+ {
+ this.validator = validator;
+ }
+
+ /** {...@inheritdoc} */
+ public FormComponent<?>[] getDependentFormComponents()
+ {
+ return validator.getDependentFormComponents();
+ }
+
+ /** {...@inheritdoc} */
+ public void validate(Form<?> form)
+ {
+ validator.validate(form);
+ }
+
+ public IFormValidator getValidator()
+ {
+ return validator;
+ }
+
+
+}
\ No newline at end of file
Propchange:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/validation/FormValidatorAdapter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/validation/ValidatorAdapter.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/validation/ValidatorAdapter.java?rev=1041290&view=auto
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/validation/ValidatorAdapter.java
(added)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/validation/ValidatorAdapter.java
Thu Dec 2 06:43:35 2010
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.wicket.validation;
+
+import org.apache.wicket.behavior.Behavior;
+
+/**
+ * Adapts {...@link IValidator} to Behavior
+ *
+ * @author igor
+ * @param <T>
+ * type of validator's validatable
+ */
+public class ValidatorAdapter<T> extends Behavior implements IValidator<T>
+{
+ private static final long serialVersionUID = 1L;
+
+ private final IValidator<T> validator;
+
+ /**
+ * Constructor
+ *
+ * @param validator
+ * validator to be adapted
+ */
+ public ValidatorAdapter(IValidator<T> validator)
+ {
+ this.validator = validator;
+ }
+
+ /** {...@inheritdoc} */
+ public void validate(IValidatable<T> validatable)
+ {
+ validator.validate(validatable);
+ }
+
+ /**
+ * Gets adapted validator
+ *
+ * @return validator
+ */
+ public IValidator<T> getValidator()
+ {
+ return validator;
+ }
+}
Propchange:
wicket/trunk/wicket/src/main/java/org/apache/wicket/validation/ValidatorAdapter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/FormValidatorBehaviorTest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/FormValidatorBehaviorTest.java?rev=1041290&view=auto
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/FormValidatorBehaviorTest.java
(added)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/FormValidatorBehaviorTest.java
Thu Dec 2 06:43:35 2010
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.wicket.markup.html.form.validation;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.behavior.Behavior;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.IMarkupResourceStreamProvider;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.FormComponent;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+import org.apache.wicket.util.tester.FormTester;
+
+public class FormValidatorBehaviorTest extends WicketTestCase
+{
+ /**
+ * Tests validators are treated as behaviors
+ */
+ public void testActAsBehavior()
+ {
+ TestPage page = new TestPage();
+
+ tester.startPage(page);
+
+
assertFalse(tester.getPreviousRequests().contains("foo=\"bar\""));
+
+ MaxLenValidator validator = new MaxLenValidator(page.name);
+ page.form.add(validator);
+ tester.startPage(page);
+
+ tester.assertContains("foo=\"bar\"");
+
+ page.form.remove(validator);
+
+
assertFalse(tester.getPreviousRequests().contains("foo=\"bar\""));
+ }
+
+ /**
+ * Tests validators are treated as validators
+ */
+ public void testActAsValidator()
+ {
+ TestPage page = new TestPage();
+
+ tester.startPage(page);
+
+ FormTester ft = tester.newFormTester("form");
+ ft.setValue("name", "999999999");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ MaxLenValidator max = new MaxLenValidator(page.name);
+ page.form.add(max);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "999999999");
+ ft.submit();
+ assertEquals(1,
tester.getSession().getFeedbackMessages().size());
+ assertEquals("MAX", tester.getSession()
+ .getFeedbackMessages()
+ .iterator()
+ .next()
+ .getMessage()
+ .toString());
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "22");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ MinLenValidator min = new MinLenValidator(page.name);
+ page.form.add(min);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "22");
+ ft.submit();
+ assertEquals(1,
tester.getSession().getFeedbackMessages().size());
+ assertEquals("MIN", tester.getSession()
+ .getFeedbackMessages()
+ .iterator()
+ .next()
+ .getMessage()
+ .toString());
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "7777777");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ page.form.remove(min);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "22");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ page.form.remove(max);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "999999999");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ }
+
+ public static class MaxLenValidator extends Behavior implements
IFormValidator
+ {
+ private final int len = 8;
+ private final TextField<String> field;
+
+ public MaxLenValidator(TextField<String> field)
+ {
+ this.field = field;
+ }
+
+ @Override
+ public void onComponentTag(Component component, ComponentTag
tag)
+ {
+ tag.put("foo", "bar");
+ }
+
+ public FormComponent<?>[] getDependentFormComponents()
+ {
+ return new FormComponent[] { field };
+ }
+
+ public void validate(Form<?> form)
+ {
+ String value = field.getConvertedInput();
+ if (value.length() > len)
+ {
+ form.error("MAX");
+ }
+ }
+ }
+
+ public static class MinLenValidator implements IFormValidator
+ {
+
+ private int len = 5;
+ private final TextField<String> field;
+
+ public MinLenValidator(TextField<String> field)
+ {
+ this.field = field;
+ }
+
+ public FormComponent<?>[] getDependentFormComponents()
+ {
+ return new FormComponent[] { field };
+ }
+
+ public void validate(Form<?> form)
+ {
+ String value = field.getConvertedInput();
+ if (value.length() < len)
+ {
+ form.error("MIN");
+ }
+ }
+ }
+
+ public static class TestPage extends WebPage implements
IMarkupResourceStreamProvider
+ {
+ public TextField<String> name;
+ public Form form;
+
+ public TestPage()
+ {
+ form = new Form("form");
+ add(form);
+ name = new TextField<String>("name", Model.of(""));
+ form.add(name);
+ }
+
+ public IResourceStream getMarkupResourceStream(MarkupContainer
container,
+ Class<?> containerClass)
+ {
+ return new StringResourceStream(
+ "<form wicket:id='form'><input wicket:id='name'
type='text'/></form>");
+ }
+ }
+}
Propchange:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/FormValidatorBehaviorTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
wicket/trunk/wicket/src/test/java/org/apache/wicket/validation/ValidatorBehaviorTest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/validation/ValidatorBehaviorTest.java?rev=1041290&view=auto
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/validation/ValidatorBehaviorTest.java
(added)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/validation/ValidatorBehaviorTest.java
Thu Dec 2 06:43:35 2010
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.wicket.validation;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.behavior.Behavior;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.IMarkupResourceStreamProvider;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+import org.apache.wicket.util.tester.FormTester;
+
+/**
+ * Tests validator work as validators and behaviors
+ *
+ * @author igor
+ */
+public class ValidatorBehaviorTest extends WicketTestCase
+{
+
+ /**
+ * Tests validators are treated as behaviors
+ */
+ public void testActAsBehavior()
+ {
+ TestPage page = new TestPage();
+
+ tester.startPage(page);
+
assertFalse(tester.getLastResponseAsString().contains("foo=\"bar\""));
+
+ MaxLenValidator max = new MaxLenValidator();
+ page.name.add(max);
+ tester.startPage(page);
+ tester.assertContains("foo=\"bar\"");
+
+ page.name.remove(max);
+ tester.startPage(page);
+
assertFalse(tester.getLastResponseAsString().contains("foo=\"bar\""));
+ }
+
+ /**
+ * Tests validators are treated as validators
+ */
+ public void testActAsValidator()
+ {
+ TestPage page = new TestPage();
+
+ tester.startPage(page);
+
+ FormTester ft = tester.newFormTester("form");
+ ft.setValue("name", "999999999");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ MaxLenValidator max = new MaxLenValidator();
+ page.name.add(max);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "999999999");
+ ft.submit();
+ assertEquals(1,
tester.getSession().getFeedbackMessages().size());
+ assertEquals("MAX", tester.getSession()
+ .getFeedbackMessages()
+ .iterator()
+ .next()
+ .getMessage()
+ .toString());
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "22");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ MinLenValidator min = new MinLenValidator();
+ page.name.add(min);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "22");
+ ft.submit();
+ assertEquals(1,
tester.getSession().getFeedbackMessages().size());
+ assertEquals("MIN", tester.getSession()
+ .getFeedbackMessages()
+ .iterator()
+ .next()
+ .getMessage()
+ .toString());
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "7777777");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ page.name.remove(min);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "22");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+
+ page.name.remove(max);
+
+ ft = tester.newFormTester("form");
+ ft.setValue("name", "999999999");
+ ft.submit();
+ assertEquals(0,
tester.getSession().getFeedbackMessages().size());
+ }
+
+ public static class MaxLenValidator extends Behavior implements
IValidator<String>
+ {
+ private final int len = 8;
+
+ public void validate(IValidatable<String> validatable)
+ {
+ String value = validatable.getValue();
+ if (value.length() > len)
+ {
+ ValidationError error = new ValidationError();
+ error.setMessage("MAX");
+ validatable.error(error);
+ }
+ }
+
+ @Override
+ public void onComponentTag(Component component, ComponentTag
tag)
+ {
+ tag.put("foo", "bar");
+ }
+ }
+
+ public static class MinLenValidator implements IValidator<String>
+ {
+
+ private int len = 5;
+
+ public void validate(IValidatable<String> validatable)
+ {
+ String value = validatable.getValue();
+ if (value.length() < len)
+ {
+ ValidationError error = new ValidationError();
+ error.setMessage("MIN");
+ validatable.error(error);
+ }
+
+ }
+
+ }
+
+ public static class TestPage extends WebPage implements
IMarkupResourceStreamProvider
+ {
+ private TextField<String> name;
+
+ public TestPage()
+ {
+ Form form = new Form("form");
+ add(form);
+ name = new TextField<String>("name", Model.of(""));
+ form.add(name);
+ }
+
+ public IResourceStream getMarkupResourceStream(MarkupContainer
container,
+ Class<?> containerClass)
+ {
+ return new StringResourceStream(
+ "<form wicket:id='form'><input wicket:id='name'
type='text'/></form>");
+ }
+ }
+}
Propchange:
wicket/trunk/wicket/src/test/java/org/apache/wicket/validation/ValidatorBehaviorTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain