Author: simoneg
Date: Tue Jan 12 16:06:11 2010
New Revision: 898396

URL: http://svn.apache.org/viewvc?rev=898396&view=rev
Log:
Added exact value validation

Added:
    
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValue.java
    
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValueValidator.java
    
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/ExactValueValidatorGoodTest.java

Added: 
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValue.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValue.java?rev=898396&view=auto
==============================================================================
--- 
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValue.java
 (added)
+++ 
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValue.java
 Tue Jan 12 16:06:11 2010
@@ -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 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 MagValExactValue {
+       
+       public String value();
+       
+}

Added: 
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValueValidator.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValueValidator.java?rev=898396&view=auto
==============================================================================
--- 
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValueValidator.java
 (added)
+++ 
labs/magma/trunk/foundation-validation/src/main/java/org/apache/magma/validation/validators/MagValExactValueValidator.java
 Tue Jan 12 16:06:11 2010
@@ -0,0 +1,55 @@
+package org.apache.magma.validation.validators;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.beans.PropertyInfo;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+import org.apache.magma.validation.Validator;
+
+public class MagValExactValueValidator implements Validator<Object> {
+
+       private String value;
+       private Object objval;
+       
+       public int maxCharacterLength() {
+               if (value == null) return 0;
+               return value.length();
+       }
+
+       public List<LocalizableString> validate(Object bean, PropertyInfo 
property, Object fvalue) {
+               Converter conv = null;
+               if (property != null) {
+                       conv = property.getConverter();
+               } else {
+                       conv = Converters.getConverterFor(fvalue.getClass());
+               }
+               if (value == null && fvalue != null) {
+                       return Arrays.asList(new LocalizableString("Must be 
empty"));                   
+               }
+               if (objval == null) {
+                       objval = conv.from(value);
+               }
+               if (objval == null && fvalue != null) {
+                       return Arrays.asList(new LocalizableString("Must be 
empty"));                   
+               }
+               if (!objval.equals(fvalue)) {
+                       return Arrays.asList(new LocalizableString("Must be 
{0}", value));                      
+               }
+               return null;
+       }
+
+       public String getValue() {
+               return value;
+       }
+
+       public void setValue(String value) {
+               this.value = value;
+               this.objval = null;
+       }
+
+       
+       
+}

Added: 
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/ExactValueValidatorGoodTest.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/ExactValueValidatorGoodTest.java?rev=898396&view=auto
==============================================================================
--- 
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/ExactValueValidatorGoodTest.java
 (added)
+++ 
labs/magma/trunk/foundation-validation/src/test/java/org/apache/magma/validation/validators/ExactValueValidatorGoodTest.java
 Tue Jan 12 16:06:11 2010
@@ -0,0 +1,46 @@
+/*
+ * 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 ExactValueValidatorGoodTest {
+
+       @DataPoint public static Object[]
+       set1 = {"abc","abc"},
+       set2 = {"10",new Integer(10)},
+       set4 = {"true",Boolean.TRUE};
+       
+       @Theory public void validates(Object[] strs) throws Throwable {
+               MagValExactValueValidator val = new MagValExactValueValidator();
+               val.setValue((String)strs[0]);
+               assertThat("Does not validate "+ strs[0] + " - " + strs[1], 
val.validate(null,null,strs[1]), nullValue());
+       }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to