Fixed bug in HostPortConstraint and HostPortListConstraint

Signed-off-by: niclas <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/a587218a
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/a587218a
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/a587218a

Branch: refs/heads/develop
Commit: a587218a5a7adcbb8acad650aab186310a5cfff4
Parents: 5dfe7e3
Author: niclas <[email protected]>
Authored: Mon May 22 14:03:10 2017 +0800
Committer: niclas <[email protected]>
Committed: Mon May 22 14:03:10 2017 +0800

----------------------------------------------------------------------
 .../library/constraints/HostPortConstraint.java |  4 +++
 .../constraints/HostPortListConstraint.java     | 14 +++++++--
 .../constraints/annotation/HostPort.java        |  2 +-
 .../constraints/HostPortConstraintTest.java     | 32 ++++++++++++++++++++
 .../constraints/HostPortListConstraintTest.java | 24 +++++++++++++++
 5 files changed, 73 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a587218a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java
----------------------------------------------------------------------
diff --git 
a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java
 
b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java
index bc84c7c..6039f74 100644
--- 
a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java
+++ 
b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortConstraint.java
@@ -34,6 +34,10 @@ public class HostPortConstraint
     @Override
     public boolean isValid( HostPort annotation, String value )
     {
+        if( value.startsWith( "localhost" ) )
+        {
+            value = "localhost.my" + value.substring( 9 );
+        }
         return VALIDATOR.isValid( "http://"; + value );
     }
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a587218a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java
----------------------------------------------------------------------
diff --git 
a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java
 
b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java
index c878c0f..4a39876 100644
--- 
a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java
+++ 
b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/HostPortListConstraint.java
@@ -31,16 +31,26 @@ import static java.util.Arrays.stream;
 public class HostPortListConstraint
     implements Constraint<HostPortList, String>
 {
-    private static final UrlValidator VALIDATOR = new UrlValidator( new 
String[]{ "http" } );
+    private static final UrlValidator VALIDATOR = new UrlValidator( new 
String[]{ "http" }, UrlValidator.NO_FRAGMENTS );
 
     @Override
     public boolean isValid( HostPortList annotation, String value )
     {
-        return stream( value.split( "[ ,]" ) )
+        return stream( value.split( "[ ,]+" ) )
+            .map( this::handleLocalHost )
             .map( this::prefixProtocol )
             .allMatch( VALIDATOR::isValid );
     }
 
+    private String handleLocalHost( String entry )
+    {
+        if( entry.startsWith( "localhost" ) )
+        {
+            return "localhost.my" + entry.substring( 9 );
+        }
+        return entry;
+    }
+
     private String prefixProtocol( String value )
     {
         return "http://"; + value;

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a587218a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java
----------------------------------------------------------------------
diff --git 
a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java
 
b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java
index 62cac1f..6a3b508 100644
--- 
a/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java
+++ 
b/libraries/constraints/src/main/java/org/apache/polygene/library/constraints/annotation/HostPort.java
@@ -27,7 +27,7 @@ import 
org.apache.polygene.library.constraints.HostPortConstraint;
 
 /**
  * Marks a property as being a string, with a "host:port", where host is a 
valid hostname, IPv4 address or IPv6 address
- * and port is in the range of 0 to 65535
+ * and port is in the range of 0 to 65535. The port is optional.
  */
 @ConstraintDeclaration
 @Retention( RetentionPolicy.RUNTIME )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a587218a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java
----------------------------------------------------------------------
diff --git 
a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java
 
b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java
index cacd34c..477f95d 100644
--- 
a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java
+++ 
b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortConstraintTest.java
@@ -32,6 +32,14 @@ public class HostPortConstraintTest extends 
AbstractPolygeneTest
 {
 
     @Test
+    public void givenValidHostWithoutPortWhenSettingPropertyExpectSuccess()
+        throws Exception
+    {
+        SomeValue someValue = transientBuilderFactory.newTransient( 
SomeValue.class );
+        someValue.hostPort().set( "habba.zout.com" );
+    }
+
+    @Test
     public void givenValidHostPortWhenSettingPropertyExpectSuccess()
         throws Exception
     {
@@ -39,6 +47,30 @@ public class HostPortConstraintTest extends 
AbstractPolygeneTest
         someValue.hostPort().set( "habba.zout.com:1234" );
     }
 
+    @Test
+    public void givenLocalHostWithoutPortWhenSettingPropertyExpectSuccess()
+        throws Exception
+    {
+        SomeValue someValue = transientBuilderFactory.newTransient( 
SomeValue.class );
+        someValue.hostPort().set( "localhost" );
+    }
+
+    @Test
+    public void givenLocalHostWithSmallPortWhenSettingPropertyExpectSuccess()
+        throws Exception
+    {
+        SomeValue someValue = transientBuilderFactory.newTransient( 
SomeValue.class );
+        someValue.hostPort().set( "localhost:9" );
+    }
+
+    @Test
+    public void givenLocalHostWithLargePortWhenSettingPropertyExpectSuccess()
+        throws Exception
+    {
+        SomeValue someValue = transientBuilderFactory.newTransient( 
SomeValue.class );
+        someValue.hostPort().set( "localhost:1234" );
+    }
+
     @Test( expected = ConstraintViolationException.class )
     public void 
givenInvalidHostNameWhenSettingPropertyExpectConstrainViolation()
         throws Exception

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a587218a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java
----------------------------------------------------------------------
diff --git 
a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java
 
b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java
index 3e6ce38..430ffd7 100644
--- 
a/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java
+++ 
b/libraries/constraints/src/test/java/org/apache/polygene/library/constraints/HostPortListConstraintTest.java
@@ -32,6 +32,30 @@ public class HostPortListConstraintTest extends 
AbstractPolygeneTest
 {
 
     @Test
+    public void givenValidHostWithoutPortWhenSettingPropertyExpectSuccess()
+        throws Exception
+    {
+        SomeValue someValue = transientBuilderFactory.newTransient( 
SomeValue.class );
+        someValue.hostPort().set( "habba.zout.com" );
+    }
+
+    @Test
+    public void givenValidHostListWithoutPortWhenSettingPropertyExpectSuccess()
+        throws Exception
+    {
+        SomeValue someValue = transientBuilderFactory.newTransient( 
SomeValue.class );
+        someValue.hostPort().set( "habba.zout.com, localhost google.com" );
+    }
+
+    @Test
+    public void 
givenValidHostListWithSomePortWhenSettingPropertyExpectSuccess()
+        throws Exception
+    {
+        SomeValue someValue = transientBuilderFactory.newTransient( 
SomeValue.class );
+        someValue.hostPort().set( "habba.zout.com, localhost:8080, google.com" 
);
+    }
+
+    @Test
     public void givenValidHostPortWhenSettingPropertyExpectSuccess()
         throws Exception
     {

Reply via email to