incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1228251 - in /incubator/bval/trunk/bval-extras/src: main/java/org/apache/bval/extras/constraints/ test/java/org/ test/java/org/apache/ test/java/org/apache/bval/ test/java/org/apache/bval/extras/ test/java/org/apache/bval/extras/constraints/
Date Fri, 06 Jan 2012 16:35:32 GMT
Author: simonetripodi
Date: Fri Jan  6 16:35:32 2012
New Revision: 1228251

URL: http://svn.apache.org/viewvc?rev=1228251&view=rev
Log:
first checkion of InetAddress validator - partially imported from commons-validator

Added:
    incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java
  (with props)
    incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java
  (with props)
    incubator/bval/trunk/bval-extras/src/test/java/org/
    incubator/bval/trunk/bval-extras/src/test/java/org/apache/
    incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/
    incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/
    incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/
    incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java
  (with props)

Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java?rev=1228251&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java
(added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java
Fri Jan  6 16:35:32 2012
@@ -0,0 +1,51 @@
+/*
+ * 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.bval.extras.constraints;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.*;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * <p>
+ * --
+ * TODO - This class is NOT part of the bean_validation spec and might disappear
+ * as soon as a final version of the specification contains a similar functionality.
+ * --
+ * </p>
+ * Description: annotation to validate a java.io.File is a directory<br/>
+ */
+@Documented
+@Constraint( validatedBy = InetAddressValidator.class )
+@Target( { FIELD, ANNOTATION_TYPE, PARAMETER } )
+@Retention( RUNTIME )
+public @interface InetAddress {
+
+    Class<?>[] groups() default {};
+
+    String message() default "{org.apache.bval.extras.constraints.Directory.message}";
+
+    Class<? extends Payload>[] payload() default {};
+
+}

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddress.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java?rev=1228251&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java
(added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java
Fri Jan  6 16:35:32 2012
@@ -0,0 +1,54 @@
+/*
+ * 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.bval.extras.constraints;
+
+import java.util.regex.Pattern;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * Description: <br/>
+ */
+public class InetAddressValidator implements ConstraintValidator<InetAddress, String>
{
+
+    private static final Pattern IPV4_PATTERN = Pattern.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
+                                                                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
+                                                                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
+                                                                + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isValid(String value, ConstraintValidatorContext context) {
+        if (!IPV4_PATTERN.matcher(value).matches()) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void initialize(InetAddress parameters) {
+        // do nothing (as long as InetAddress has no properties)
+    }
+
+}

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/InetAddressValidator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java?rev=1228251&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java
(added)
+++ incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java
Fri Jan  6 16:35:32 2012
@@ -0,0 +1,92 @@
+/*
+ * 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.bval.extras.constraints;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for InetAddressValidator.
+ *
+ * @version $Revision$
+ */
+public class InetAddressValidatorTest {
+
+    private InetAddressValidator validator;
+
+    @Before
+    public void setUp() {
+        validator = new InetAddressValidator();
+    }
+
+    /**
+     * Test IPs that point to real, well-known hosts (without actually looking them up).
+     */
+    @Test
+    public void testInetAddressesFromTheWild() {
+        assertTrue("www.apache.org IP should be valid",       validator.isValid("140.211.11.130",
null));
+        assertTrue("www.l.google.com IP should be valid",     validator.isValid("72.14.253.103",
null));
+        assertTrue("fsf.org IP should be valid",              validator.isValid("199.232.41.5",
null));
+        assertTrue("appscs.ign.com IP should be valid",       validator.isValid("216.35.123.87",
null));
+    }
+
+    /**
+     * Test valid and invalid IPs from each address class.
+     */
+    @Test
+    public void testInetAddressesByClass() {
+        assertTrue("class A IP should be valid",              validator.isValid("24.25.231.12",
null));
+        assertFalse("illegal class A IP should be invalid",   validator.isValid("2.41.32.324",
null));
+
+        assertTrue("class B IP should be valid",              validator.isValid("135.14.44.12",
null));
+        assertFalse("illegal class B IP should be invalid",   validator.isValid("154.123.441.123",
null));
+
+        assertTrue("class C IP should be valid",              validator.isValid("213.25.224.32",
null));
+        assertFalse("illegal class C IP should be invalid",   validator.isValid("201.543.23.11",
null));
+
+        assertTrue("class D IP should be valid",              validator.isValid("229.35.159.6",
null));
+        assertFalse("illegal class D IP should be invalid",   validator.isValid("231.54.11.987",
null));
+
+        assertTrue("class E IP should be valid",              validator.isValid("248.85.24.92",
null));
+        assertFalse("illegal class E IP should be invalid",   validator.isValid("250.21.323.48",
null));
+    }
+
+    /**
+     * Test reserved IPs.
+     */
+    @Test
+    public void testReservedInetAddresses() {
+        assertTrue("localhost IP should be valid",            validator.isValid("127.0.0.1",
null));
+        assertTrue("broadcast IP should be valid",            validator.isValid("255.255.255.255",
null));
+    }
+
+    /**
+     * Test obviously broken IPs.
+     */
+    @Test
+    public void testBrokenInetAddresses() {
+        assertFalse("IP with characters should be invalid",   validator.isValid("124.14.32.abc",
null));
+        assertFalse("IP with three groups should be invalid", validator.isValid("23.64.12",
null));
+        assertFalse("IP with five groups should be invalid",  validator.isValid("26.34.23.77.234",
null));
+    }
+
+}

Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/InetAddressValidatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message