You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2008/09/16 16:28:43 UTC
svn commit: r695902 - in /labs/magma/trunk/foundation-validation: ./ src/
src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/magma/ src/main/java/org/apache/magma/validation/
src/main/java/org/apache/magma/va...
Author: simoneg
Date: Tue Sep 16 07:28:41 2008
New Revision: 695902
URL: http://svn.apache.org/viewvc?rev=695902&view=rev
Log:
Initial import.
Added:
labs/magma/trunk/foundation-validation/pom.xml
labs/magma/trunk/foundation-validation/src/
labs/magma/trunk/foundation-validation/src/main/
labs/magma/trunk/foundation-validation/src/main/java/
labs/magma/trunk/foundation-validation/src/main/java/org/
labs/magma/trunk/foundation-validation/src/main/java/org/apache/
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidationInHandler.aj
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidatorInPropertyInfo.aj
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/CompoundValidator.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/HookValidationToHandler.aj
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationAnnotation.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationError.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validator.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validators.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/WarnForWrongUsage.aj
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/FloatingPointRange.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Length.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/LengthValidator.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Range.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RangeValidator.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Required.java
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RequiredValidator.java
labs/magma/trunk/foundation-validation/src/test/
labs/magma/trunk/foundation-validation/src/test/java/
labs/magma/trunk/foundation-validation/src/test/java/org/
labs/magma/trunk/foundation-validation/src/test/java/org/apache/
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/BaseConverterValidationTest.java
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/TestValidationClass.java
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/ValidationMapTest.java
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/LengthValidatorTest.java
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorGoodValidations.java
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorWrongValidations.java
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RequiredValidatorTest.java
Added: labs/magma/trunk/foundation-validation/pom.xml
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/pom.xml?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/pom.xml (added)
+++ labs/magma/trunk/foundation-validation/pom.xml Tue Sep 16 07:28:41 2008
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.magma</groupId>
+ <artifactId>magma-parent</artifactId>
+ <version>1</version>
+ </parent>
+ <groupId>org.apache.magma</groupId>
+ <artifactId>foundation-validation</artifactId>
+ <name>Magma Foundation Validation</name>
+ <version>0.0.1-SNAPSHOT</version>
+ <description>
+ Provides basic classes for validation in the Magma Framework
+ </description>
+ <packaging>magma</packaging>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.magma.tools</groupId>
+ <artifactId>maven-magma-plugin</artifactId>
+ <extensions>true</extensions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.magma</groupId>
+ <artifactId>foundation-beans</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.4</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.magma</groupId>
+ <artifactId>foundation-basics</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+
+</project>
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidationInHandler.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidationInHandler.aj?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidationInHandler.aj (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidationInHandler.aj Tue Sep 16 07:28:41 2008
@@ -0,0 +1,66 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.basics.LocalizableString;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.magma.beans.BeanHandler;
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.beans.PropertyInfo;
+
+public privileged aspect AddValidationInHandler {
+
+ public void BeanHandler.validate(String field, Object value) {
+ PropertyInfo property = data.getProperty(field);
+ if (property == null) throw new MagmaException("Cannot find a property named {0} in class {1}", field, bean.getClass().getName());
+ Validator v = property.getValidator();
+ if (v != null) {
+ List<LocalizableString> errs = v.validate(value);
+ if (errs != null) {
+ ValidationError err = new ValidationError(errs);
+ err.setSubject(property);
+ throw err;
+ }
+ }
+ }
+
+ public void BeanHandler.validate(String field) {
+ Object val = values.get(field);
+ validate(field, val);
+ }
+
+ public void BeanHandler.validate() {
+ Set<String> names = data.getPropertyNames();
+ ValidationError err = null;
+ for (String name : names) {
+ try {
+ validate(name);
+ } catch (ValidationError e) {
+ if (err == null) {
+ err = e;
+ } else {
+ err.join(e);
+ }
+ }
+ }
+ if (err != null) throw err;
+ }
+
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidatorInPropertyInfo.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidatorInPropertyInfo.aj?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidatorInPropertyInfo.aj (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/AddValidatorInPropertyInfo.aj Tue Sep 16 07:28:41 2008
@@ -0,0 +1,63 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.basics.utils.AnnotationToInstance;
+import org.apache.magma.beans.PropertyInfo;
+
+import java.beans.PropertyDescriptor;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Arrays;
+
+public aspect AddValidatorInPropertyInfo {
+
+ private CompoundValidator<Object> PropertyInfo.validator = null;
+
+ public CompoundValidator<Object> PropertyInfo.getValidator() {
+ return this.validator;
+ }
+
+ void PropertyInfo.setValidator(CompoundValidator<Object> validator) {
+ this.validator = validator;
+ }
+
+ after(PropertyInfo info, PropertyDescriptor desc) :
+ execution(* PropertyInfo.init(PropertyDescriptor, Class)) && target(info) && args(desc,..) {
+
+ List<Annotation> annotations = new ArrayList<Annotation>();
+ Method writeMethod = desc.getWriteMethod();
+ Method readMethod = desc.getReadMethod();
+ if (writeMethod != null) {
+ annotations.addAll(Arrays.asList(writeMethod.getAnnotations()));
+ }
+ if (readMethod != null) {
+ annotations.addAll(Arrays.asList(readMethod.getAnnotations()));
+ }
+ if (annotations.size() > 0) {
+ for (Annotation annotation : annotations) {
+ Validator validator = Validators.getValidatorFor(annotation);
+ if (validator != null) {
+ Validators.addValidator(info, validator);
+ }
+ }
+ }
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/CompoundValidator.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/CompoundValidator.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/CompoundValidator.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/CompoundValidator.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,102 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.validation.validators.LengthValidator;
+import org.apache.magma.validation.validators.RangeValidator;
+import org.apache.magma.validation.validators.Required;
+import org.apache.magma.validation.validators.RequiredValidator;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CompoundValidator<C> implements Validator<C> {
+
+ private List<Validator<? extends C>> validators = null;
+
+ public void addValidator(Validator<? extends C> validator) {
+ if (validators == null) validators = new ArrayList<Validator<? extends C>>(1);
+ validators.add(validator);
+ }
+
+ public List<LocalizableString> validate(C value) {
+ List<LocalizableString> errs = null;
+ if (this.validators != null) {
+ for (Validator validator : this.validators) {
+ List<LocalizableString> err = validator.validate(value);
+ if (err != null) {
+ if (errs == null) errs = new ArrayList<LocalizableString>();
+ errs.addAll(err);
+ }
+ }
+ }
+ return errs;
+ }
+
+ public List<Validator<? extends C>> getValidators() {
+ return validators;
+ }
+
+ public <T extends Validator<? extends C>> T getValidator(Class<T> clazz) {
+ if (this.validators == null) return null;
+ for (Validator<? extends C> val : this.validators) {
+ if (clazz.isAssignableFrom(val.getClass())) {
+ return (T)val;
+ }
+ }
+ return null;
+ }
+
+ protected <T extends Validator> T rawGetValidator(Class<T> clazz) {
+ if (this.validators == null) return null;
+ for (Validator<? extends C> val : this.validators) {
+ if (clazz.isAssignableFrom(val.getClass())) {
+ return (T)val;
+ }
+ }
+ return null;
+ }
+
+
+ public boolean acceptsNulls() {
+ return rawGetValidator(RequiredValidator.class) == null;
+ }
+
+ public int maxCharacterLength() {
+ int size = 0;
+ LengthValidator len = rawGetValidator(LengthValidator.class);
+ if (len != null) {
+ if (len.getMax() != len.getDefaultMax() && len.getMax() > size) {
+ size = len.getMax();
+ }
+ }
+
+ RangeValidator range = rawGetValidator(RangeValidator.class);
+ if (range != null) {
+ Number max = range.getMax();
+ if (max != null) {
+ String maxstr = max.toString();
+ if (maxstr.length() > size) {
+ size = maxstr.length();
+ }
+ }
+ }
+ return size;
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/HookValidationToHandler.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/HookValidationToHandler.aj?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/HookValidationToHandler.aj (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/HookValidationToHandler.aj Tue Sep 16 07:28:41 2008
@@ -0,0 +1,29 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.beans.BeanHandler;
+
+public aspect HookValidationToHandler {
+
+ declare precedence : *,HookValidationToHandler;
+
+ before() : execution(public void BeanHandler.commit()) {
+ ((BeanHandler)thisJoinPoint.getThis()).validate();
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationAnnotation.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationAnnotation.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationAnnotation.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationAnnotation.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,30 @@
+/*
+ * 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.magma.validation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.ANNOTATION_TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ValidationAnnotation {
+
+ public Class value() default ValidationAnnotation.class;
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationError.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationError.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationError.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/ValidationError.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,35 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.basics.MagmaException;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ValidationError extends MagmaException {
+
+ public ValidationError(LocalizableString error) {
+ super(error);
+ }
+
+ public ValidationError(List<LocalizableString> messages) {
+ super(messages);
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validator.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validator.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validator.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validator.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,27 @@
+/*
+ * 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.magma.validation;
+
+import java.util.List;
+
+import org.apache.magma.basics.LocalizableString;
+
+public interface Validator<T> {
+
+ public List<LocalizableString> validate(T value);
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validators.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validators.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validators.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/Validators.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,66 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.basics.utils.AnnotationToInstance;
+import org.apache.magma.beans.PropertyInfo;
+
+import java.lang.annotation.Annotation;
+
+public class Validators {
+
+ public static Validator getValidatorFor(Annotation annotation) {
+ Class annclass = annotation.annotationType();
+ ValidationAnnotation ann = (ValidationAnnotation)annclass.getAnnotation(ValidationAnnotation.class);
+ if (ann == null) return null;
+ Class valclass = null;
+ if (ann.value().equals(ValidationAnnotation.class)) {
+ String name = annclass.getName();
+ name += "Validator";
+ try {
+ valclass = Class.forName(name);
+ } catch (ClassNotFoundException e) {
+ return null;
+ }
+ } else {
+ valclass = ann.annotationType();
+ }
+ Validator ret = null;
+ try {
+ ret = (Validator)valclass.newInstance();
+ } catch (InstantiationException e) {
+ return null;
+ } catch (IllegalAccessException e) {
+ return null;
+ }
+ setupValidator(annotation, ret);
+ return ret;
+ }
+
+ public static void setupValidator(Annotation annotation, Validator ret) {
+ AnnotationToInstance.setup(annotation, ret);
+ }
+
+ public static void addValidator(PropertyInfo info, Validator validator) {
+ if (info.getValidator() == null) {
+ info.setValidator(new CompoundValidator<Object>());
+ }
+ info.getValidator().addValidator(validator);
+ }
+
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/WarnForWrongUsage.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/WarnForWrongUsage.aj?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/WarnForWrongUsage.aj (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/WarnForWrongUsage.aj Tue Sep 16 07:28:41 2008
@@ -0,0 +1,24 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.beans.MagmaBeanSupport;
+
+public aspect WarnForWrongUsage {
+
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/FloatingPointRange.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/FloatingPointRange.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/FloatingPointRange.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/FloatingPointRange.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,34 @@
+/*
+ * 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.magma.validation.validators;
+
+import org.apache.magma.validation.ValidationAnnotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@ValidationAnnotation(RangeValidator.class)
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface FloatingPointRange {
+
+ public double min() default Double.MIN_VALUE;
+ public double max() default Double.MAX_VALUE;
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Length.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Length.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Length.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Length.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,34 @@
+/*
+ * 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.magma.validation.validators;
+
+import org.apache.magma.validation.ValidationAnnotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@ValidationAnnotation
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Length {
+
+ public int min() default Integer.MIN_VALUE;
+ public int max() default Integer.MAX_VALUE;
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/LengthValidator.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/LengthValidator.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/LengthValidator.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/LengthValidator.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,90 @@
+/*
+ * 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.magma.validation.validators;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.validation.Validator;
+
+
+public class LengthValidator implements Validator<String> {
+
+ private int min = 0;
+ private int max = Integer.MAX_VALUE;
+ private String errorString = "LengthValidator.error";
+ private List<LocalizableString> myError = null;
+
+ public LengthValidator() {
+ }
+
+ public LengthValidator(int min, int max) {
+ this(min, max, null);
+ }
+
+ public LengthValidator(int min, int max, String error) {
+ this.min = min;
+ this.max = max;
+ if (error != null) errorString = error;
+ setupError();
+ }
+
+ public void setMin(int min) {
+ this.min = min;
+ setupError();
+ }
+
+ public void setMax(int max) {
+ this.max = max;
+ setupError();
+ }
+
+ public void setError(String error) {
+ this.errorString = error;
+ setupError();
+ }
+
+ private void setupError() {
+ if (this.myError == null) {
+ this.myError = new ArrayList<LocalizableString>(1);
+ } else {
+ this.myError.clear();
+ }
+ this.myError.add(new LocalizableString(errorString, min, max));
+ }
+
+ public List<LocalizableString> validate(String value) {
+ if (value == null) return null;
+ int len = value.length();
+ if (len < min || len > max) return myError;
+ return null;
+ }
+
+ public int getMin() {
+ return min;
+ }
+
+ public int getMax() {
+ return max;
+ }
+
+ public int getDefaultMax() {
+ return Integer.MAX_VALUE;
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Range.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Range.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Range.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Range.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,34 @@
+/*
+ * 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.magma.validation.validators;
+
+import org.apache.magma.validation.ValidationAnnotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@ValidationAnnotation
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Range {
+
+ public long min() default Long.MIN_VALUE;
+ public long max() default Long.MAX_VALUE;
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RangeValidator.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RangeValidator.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RangeValidator.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RangeValidator.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,115 @@
+/*
+ * 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.magma.validation.validators;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.validation.Validator;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+
+public class RangeValidator implements Validator<Number> {
+
+ private Number min;
+ private Number max;
+ private String errorString = "RangeValidator.error";
+ private List<LocalizableString> myError = null;
+
+ public RangeValidator() {
+ }
+
+ public RangeValidator(Number min, Number max) {
+ this(min, max, null);
+ }
+
+ public RangeValidator(Number min, Number max, String error) {
+ this.min = min;
+ this.max = max;
+ if (error != null) errorString = error;
+ setupError();
+ }
+
+ public void setMin(long min) {
+ this.min = min;
+ setupError();
+ }
+
+ public void setMax(long max) {
+ this.max = max;
+ setupError();
+ }
+
+ public void setMin(double min) {
+ this.min = min;
+ setupError();
+ }
+
+ public void setMax(double max) {
+ this.max = max;
+ setupError();
+ }
+
+ public void setError(String error) {
+ this.errorString = error;
+ setupError();
+ }
+
+ private void setupError() {
+ if (this.myError == null) {
+ this.myError = new ArrayList<LocalizableString>(1);
+ } else {
+ this.myError.clear();
+ }
+ this.myError.add(new LocalizableString(errorString, min, max));
+ }
+
+ public List<LocalizableString> validate(Number value) {
+ if (value == null) return null;
+ if (min != null) {
+ if (typeSafeCompare(value, min) < 0) return myError;
+ }
+ if (max != null) {
+ if (typeSafeCompare(value, max) > 0) return myError;
+ }
+ return null;
+ }
+
+ private int typeSafeCompare(Number a, Number b) {
+ if (a instanceof BigDecimal) {
+ if (b instanceof BigDecimal) {
+ return ((BigDecimal)a).compareTo((BigDecimal) b);
+ }
+ } else if (a instanceof BigInteger) {
+ if (b instanceof BigInteger) {
+ return ((BigInteger)a).compareTo((BigInteger) b);
+ }
+ }
+
+ return Double.compare(a.doubleValue(), b.doubleValue());
+ }
+
+ public Number getMin() {
+ return min;
+ }
+
+ public Number getMax() {
+ return max;
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Required.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Required.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Required.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/Required.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,31 @@
+/*
+ * 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.magma.validation.validators;
+
+import org.apache.magma.validation.ValidationAnnotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@ValidationAnnotation
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Required {
+
+}
Added: labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RequiredValidator.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RequiredValidator.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RequiredValidator.java (added)
+++ labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/RequiredValidator.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,43 @@
+/*
+ * 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.magma.validation.validators;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.validation.Validator;
+
+public class RequiredValidator implements Validator<Object> {
+
+ private List<LocalizableString> myError = new ArrayList<LocalizableString>(1);
+
+ public RequiredValidator() {
+ myError.add(new LocalizableString("RequiredValidator.required"));
+ }
+
+ public void setError(String error) {
+ myError.add(new LocalizableString(error));
+ }
+
+
+ public List<LocalizableString> validate(Object value) {
+ if (value == null) return myError;
+ return null;
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/BaseConverterValidationTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/BaseConverterValidationTest.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/BaseConverterValidationTest.java (added)
+++ labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/BaseConverterValidationTest.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,71 @@
+/*
+ * 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.magma.validation;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.validation.validators.RangeValidator;
+import org.apache.magma.validation.validators.RequiredValidator;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class BaseConverterValidationTest {
+
+ private CompoundValidator<Object> compound;
+
+ @Before
+ public void setupValidate() {
+ compound = new CompoundValidator<Object>();
+ compound.addValidator(new RequiredValidator());
+ compound.addValidator(new RangeValidator(0, 10));
+
+ RequiredValidator validator = compound.getValidator(RequiredValidator.class);
+ assertNotNull(validator);
+ }
+
+ @Test
+ public void workingValidations() {
+ nullOrthrow(compound.validate(2));
+ nullOrthrow(compound.validate(5));
+ }
+
+ @Test(expected=ValidationError.class)
+ public void fails1() {
+ nullOrthrow(compound.validate(null));
+ }
+
+ @Test(expected=ValidationError.class)
+ public void fails2() {
+ nullOrthrow(compound.validate(-1));
+ }
+
+ @Test(expected=ValidationError.class)
+ public void fails3() {
+ nullOrthrow(compound.validate(11));
+ }
+
+ private void nullOrthrow(List<LocalizableString> validate) {
+ if (validate == null) return;
+ throw new ValidationError(validate);
+ }
+
+
+}
Added: labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/TestValidationClass.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/TestValidationClass.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/TestValidationClass.java (added)
+++ labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/TestValidationClass.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,55 @@
+/*
+ * 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.magma.validation;
+
+import org.junit.Ignore;
+
+import org.apache.magma.beans.MagmaBean;
+import org.apache.magma.validation.validators.Length;
+import org.apache.magma.validation.validators.Range;
+import org.apache.magma.validation.validators.Required;
+
+@Ignore("This is not a test class")
+@MagmaBean
+public class TestValidationClass {
+ private int integerField = 0;
+ private String stringField = null;
+
+ public int getIntegerField() {
+ return integerField;
+ }
+
+ @Required
+ @Range(min=1, max=10)
+ public void setIntegerField(int integerField) {
+ this.integerField = integerField;
+ }
+
+ @Required
+ public String getStringField() {
+ return stringField;
+ }
+
+ @Length(min=3, max=30)
+ public void setStringField(String name) {
+ this.stringField = name;
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/ValidationMapTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/ValidationMapTest.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/ValidationMapTest.java (added)
+++ labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/ValidationMapTest.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,78 @@
+/*
+ * 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.magma.validation;
+
+import static org.junit.Assert.*;
+import org.apache.magma.beans.BeanData;
+import org.apache.magma.beans.BeanHandler;
+import org.apache.magma.beans.PropertyInfo;
+
+import java.util.List;
+
+import org.junit.Test;
+
+public class ValidationMapTest {
+
+ @Test
+ public void testInit() {
+ TestValidationClass bean = new TestValidationClass();
+ BeanData d = bean.beanData();
+
+ PropertyInfo property = d.getProperty("integerField");
+ CompoundValidator<Object> c = property.getValidator();
+ assertNotNull(c);
+ List<Validator<? extends Object>> validators = c.getValidators();
+ assertNotNull(validators);
+ assertTrue(validators.size() > 0);
+ }
+
+ @Test
+ public void validSetters() throws Exception {
+ TestValidationClass bean = new TestValidationClass();
+ BeanHandler handler = bean.handler();
+ handler.setValue("integerField", 5);
+ handler.setValue("stringField", "ciao");
+ handler.commit();
+
+ assertEquals("ciao", bean.getStringField());
+ assertEquals(5, bean.getIntegerField());
+ }
+
+ @Test(expected=ValidationError.class)
+ public void wrongSetters() throws Exception {
+ TestValidationClass bean = new TestValidationClass();
+ BeanHandler handler = bean.handler();
+ handler.setValue("integerField", 0);
+ handler.setValue("stringField", "a");
+ handler.commit();
+ }
+
+ @Test
+ public void jointException() throws Exception {
+ try {
+ TestValidationClass bean = new TestValidationClass();
+ BeanHandler handler = bean.handler();
+ handler.setValue("integerField", 0);
+ handler.setValue("stringField", "a");
+ handler.commit();
+ fail("exception ot thrown");
+ } catch (ValidationError e) {
+ assertEquals(2, e.getMessages().size());
+ }
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/LengthValidatorTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/LengthValidatorTest.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/LengthValidatorTest.java (added)
+++ labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/LengthValidatorTest.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,53 @@
+/*
+ * 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.magma.validation.validators;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.apache.magma.basics.LocalizableString;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class LengthValidatorTest {
+
+ private LengthValidator v;
+
+ @Before
+ public void createValidator() {
+ v = new LengthValidator(5, 15);
+ }
+
+ @Test
+ public void testValidateOk() {
+ List<LocalizableString> string = v.validate("testok");
+ assertNull(string);
+ string = v.validate(null);
+ assertNull(string);
+ }
+
+ @Test
+ public void testValidateError() throws Exception {
+ List<LocalizableString> string = v.validate("t");
+ assertNotNull(string);
+ string = v.validate("test a string too long for the validator");
+ assertNotNull(string);
+ }
+
+}
Added: labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorGoodValidations.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorGoodValidations.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorGoodValidations.java (added)
+++ labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorGoodValidations.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,52 @@
+/*
+ * 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.magma.validation.validators;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class RangeValidatorGoodValidations {
+
+ @DataPoint public static Number[]
+ set1 = {0,5,3},
+ set2 = {-10,0,-5},
+ set4 = {-10,0,-10},
+ set5 = {null,0,-10},
+ set6 = {-10,null,-9},
+ set7 = {new BigInteger("-10"), new BigInteger("0"), new BigInteger("-5")},
+ set8 = {new BigInteger("-10"), new BigInteger("0"), -5},
+ set7d = {new BigDecimal("-1.1"), new BigDecimal("0"), new BigDecimal("-0.5")},
+ set8d = {new BigDecimal("-1.1"), new BigDecimal("0"), -0.5f},
+ set9 = {null, null, null};
+
+ @Theory public void validates(Number[] numbers) {
+ RangeValidator v = new RangeValidator(numbers[0], numbers[1]);
+ assertThat(v.validate(numbers[2]), nullValue());
+ }
+}
Added: labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorWrongValidations.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorWrongValidations.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorWrongValidations.java (added)
+++ labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RangeValidatorWrongValidations.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,53 @@
+/*
+ * 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.magma.validation.validators;
+
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class RangeValidatorWrongValidations {
+
+ @DataPoint public static Number[]
+ set1 = {0,5,15},
+ set2 = {0,5,-20},
+ set3 = {-10,0,10},
+ set4 = {-10,0,-11},
+ set5 = {null,0,10},
+ set6 = {-10,null,-20},
+ set7 = {new BigInteger("-10"), new BigInteger("0"), new BigInteger("10")},
+ set8 = {new BigInteger("-10"), new BigInteger("0"), 10},
+ set7d = {new BigDecimal("-1.1"), new BigDecimal("0"), new BigDecimal("-1.5")},
+ set8d = {new BigDecimal("-1.1"), new BigDecimal("0"), -1.5f};
+
+ @Theory public void validates(Number[] numbers) {
+ RangeValidator v = new RangeValidator(numbers[0], numbers[1]);
+ assertThat(v.validate(numbers[2]), not(nullValue()));
+ }
+}
Added: labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RequiredValidatorTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RequiredValidatorTest.java?rev=695902&view=auto
==============================================================================
--- labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RequiredValidatorTest.java (added)
+++ labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/RequiredValidatorTest.java Tue Sep 16 07:28:41 2008
@@ -0,0 +1,33 @@
+/*
+ * 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.magma.validation.validators;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class RequiredValidatorTest {
+
+ @Test
+ public void testValidate() {
+ RequiredValidator v = new RequiredValidator();
+ assertNull(v.validate(""));
+ assertNull(v.validate(Integer.valueOf(1)));
+ assertNotNull(v.validate(null));
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org