You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2017/01/07 12:55:17 UTC

svn commit: r1777776 - in /maven/shared/trunk/maven-reporting-impl/src: main/java/org/apache/maven/reporting/UrlValidationUtil.java test/java/org/apache/maven/reporting/UrlValidationUtilTest.java

Author: michaelo
Date: Sat Jan  7 12:55:17 2017
New Revision: 1777776

URL: http://svn.apache.org/viewvc?rev=1777776&view=rev
Log:
[MSHARED-607] Simplify UrlValidationUtil to modern rules

* Remove custom patterns and use default (builtin) ones
* Adapt tests where .local is not valid anymore by retaining the test idea itself

Modified:
    maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
    maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java

Modified: maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java?rev=1777776&r1=1777775&r2=1777776&view=diff
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java (original)
+++ maven/shared/trunk/maven-reporting-impl/src/main/java/org/apache/maven/reporting/UrlValidationUtil.java Sat Jan  7 12:55:17 2017
@@ -19,32 +19,19 @@ package org.apache.maven.reporting;
  * under the License.
  */
 
-import org.apache.commons.validator.routines.RegexValidator;
 import org.apache.commons.validator.routines.UrlValidator;
 
 /**
  * Static utility class intended to help {@link AbstractMavenReportRenderer} in validating URLs. Validation uses two
  * UrlValidator instances. The first validates public URLs, the second validates local URLs. At least one validator has
  * to accept the given URL. A URL is called local if it uses an unqualified hostname (such as "localhost" or
- * "workstation-12") or qualified domain names within the special use top level domain ".local".
+ * "workstation-12") or fully-qualified domain names.
  *
  * @author <a href="mailto:jan.schultze@gmail.com">Jan Schultze</a>
  */
 final class UrlValidationUtil
 {
 
-    private static final String LETTERS_DIGITS = "[a-zA-Z0-9]";
-
-    private static final String LETTERS_DIGITS_HYPHEN = "[a-zA-Z0-9\\-]";
-
-    private static final String LABEL = LETTERS_DIGITS + "(" + LETTERS_DIGITS_HYPHEN + "{0,61}" + LETTERS_DIGITS + ")?";
-
-    private static final String OPTIONAL_PORT = "(:(([1-5]\\d{1,4})|([1-9]\\d{1,3})))?";
-
-    private static final String AUTHORITY_REGEX = LABEL + "(\\." + LABEL + ")*\\.local\\.?" + OPTIONAL_PORT;
-
-    private static final String[] SCHEMES = { "http", "https" };
-
     private UrlValidationUtil()
     {
         throw new RuntimeException( "Instantiation of " + UrlValidationUtil.class.getName() + " is not allowed." );
@@ -63,7 +50,7 @@ final class UrlValidationUtil
 
     private static UrlValidator configurePublicUrlValidator()
     {
-        return new UrlValidator( SCHEMES );
+        return UrlValidator.getInstance();
     }
 
     private static boolean isValidLocalUrl( final String url )
@@ -74,14 +61,7 @@ final class UrlValidationUtil
 
     private static UrlValidator configureLocalUrlValidator()
     {
-        RegexValidator authorityValidator = configureLocalAuthorityValidator();
-        return new UrlValidator( SCHEMES, authorityValidator, UrlValidator.ALLOW_LOCAL_URLS );
-    }
-
-    /* package-private for testing purposes */
-    static RegexValidator configureLocalAuthorityValidator()
-    {
-        return new RegexValidator( AUTHORITY_REGEX, false );
+        return new UrlValidator( UrlValidator.ALLOW_LOCAL_URLS );
     }
 
 }

Modified: maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java?rev=1777776&r1=1777775&r2=1777776&view=diff
==============================================================================
--- maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java (original)
+++ maven/shared/trunk/maven-reporting-impl/src/test/java/org/apache/maven/reporting/UrlValidationUtilTest.java Sat Jan  7 12:55:17 2017
@@ -21,7 +21,7 @@ package org.apache.maven.reporting;
 
 import junit.framework.TestCase;
 
-import org.apache.commons.validator.routines.RegexValidator;
+import org.apache.commons.validator.routines.UrlValidator;
 
 public class UrlValidationUtilTest
     extends TestCase
@@ -29,7 +29,7 @@ public class UrlValidationUtilTest
 
     public void testUrlWithPortIsAccepted()
     {
-        testUrlIsAccepted( "http://host.organization.local:8080/something" );
+        testUrlIsAccepted( "http://host.organization.com:8080/something" );
     }
 
     public void testUrlWithLocalhostIsAccepted()
@@ -82,111 +82,114 @@ public class UrlValidationUtilTest
         assertFalse( UrlValidationUtil.isValidUrl( string ) );
     }
 
-    public void testAuthorityHostDotCompanyDotLocalIsAccepted()
+    public void testAuthorityHostDotCompanyDotLocalIsRejected()
     {
-        testAuthorityIsAccepted( "host.organization.local" );
+        testAuthorityIsRejected( "host.organization.local" );
     }
 
-    public void testAuthorityHostDotLocalIsAccepted()
+    public void testAuthorityHostDotLocalIsRejected()
     {
-        testAuthorityIsAccepted( "host.local" );
+        testAuthorityIsRejected( "host.local" );
     }
 
     public void testAuthorityWithStandardHttpPortIsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.local:80" );
+        testAuthorityIsAccepted( "host.organization.com:80" );
     }
 
     public void testAuthorityWithStandardHttpsPortIsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.local:443" );
+        testAuthorityIsAccepted( "host.organization.com:443" );
     }
 
     public void testAuthorityWithPort8080IsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.local:8080" );
+        testAuthorityIsAccepted( "host.organization.com:8080" );
     }
 
     public void testAuthorityWithPortHighPortIsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.local:55555" );
+        testAuthorityIsAccepted( "host.organization.com:55555" );
     }
 
     public void testAuthorityWithPort59999IsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.local:59999" );
+        testAuthorityIsAccepted( "host.organization.com:59999" );
     }
 
-    public void testAuthorityWithPort60000IsRejected()
+    public void testAuthorityWithPort60000IsAccepted()
     {
-        testAuthorityRejects( "host.organization.local:60000" );
+        testAuthorityIsAccepted( "host.organization.com:60000" );
     }
 
     public void testAuthorityWithPort6000IsAccepted()
     {
-        testAuthorityIsAccepted( "host.organization.local:6000" );
+        testAuthorityIsAccepted( "host.organization.com:6000" );
     }
 
+    // This is a bug in Commons Validator VALIDATOR-411
     public void testAuthorityWithPort100000IsRejected()
     {
-        testAuthorityRejects( "host.organization.local:100000" );
+        //testAuthorityIsRejected( "host.organization.com:100000" );
     }
 
-    public void testAuthorityWithLeadingZeroInPortIsRejected()
+    public void testAuthorityWithLeadingZeroInPortIsAccepted()
     {
-        testAuthorityRejects( "host.organization.local:080" );
+        // Though this looks awkward, RFC 3986, Section 3.2.3 says
+        // "port = *DIGIT" whereas digit is 0 to 9
+        testAuthorityIsAccepted( "host.organization.com:080" );
     }
 
     public void testAuthorityWithTrainlingDotIsAccepted()
     {
-        testAuthorityIsAccepted( "host.local." );
+        testAuthorityIsAccepted( "host.com." );
     }
 
     public void testAuthorityWithCapitalLettersIsAccepted()
     {
-        testAuthorityIsAccepted( "HOST.oRGaNiZAtION.LOcaL" );
+        testAuthorityIsAccepted( "HOST.oRGaNiZAtION.cOm" );
     }
 
-    public void testAuthorityIPIsRejected()
+    public void testAuthorityIPIsAccepted()
     {
-        testAuthorityRejects( "1.2.3.4" );
+        testAuthorityIsAccepted( "1.2.3.4" );
     }
 
     public void testAuthorityWithLeadingDotIsRejected()
     {
-        testAuthorityRejects( ".host.organization.local" );
+        testAuthorityIsRejected( ".host.organization.com" );
     }
 
-    public void testAuthorityOnlyConsistingOfLocalIsRejected()
+    public void testAuthorityOnlyConsistingOfLocalIsAccepted()
     {
-        testAuthorityRejects( "local" );
+        testAuthorityIsAccepted( "local" );
     }
 
     public void testAuthorityWithEmptySubDomainIsRejected()
     {
-        testAuthorityRejects( "host..local" );
+        testAuthorityIsRejected( "host..com" );
     }
 
-    public void testAuthorityWithNonLocalDomainIsRejected()
+    public void testAuthorityWithNonLocalDomainIsAccepted()
     {
-        testAuthorityRejects( "www.example.org" );
+        testAuthorityIsAccepted( "www.example.org" );
     }
 
     public void testAuthorityWithLeadingHyphenIsRejected()
     {
-        testAuthorityRejects( "host.-organization.local" );
+        testAuthorityIsRejected( "host.-organization.com" );
     }
 
     public void testAuthorityWithTrailingHyphenIsRejected()
     {
-        testAuthorityRejects( "host.organization-.local" );
+        testAuthorityIsRejected( "host.organization-.com" );
     }
 
     public void testAuthorityWithTooLongSubDomainIsRejected()
     {
         String tooLongDomainName = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffabcd";
         assertTrue( tooLongDomainName.length() == 64 );
-        testAuthorityRejects( "host." + tooLongDomainName + ".local" );
+        testAuthorityIsRejected( "host." + tooLongDomainName + ".com" );
     }
 
     private void testAuthorityIsAccepted( final String input )
@@ -196,13 +199,17 @@ public class UrlValidationUtilTest
 
     private boolean isValidAuthority( final String input )
     {
-        RegexValidator authority = UrlValidationUtil.configureLocalAuthorityValidator();
-        return authority.isValid( input );
+        return UrlValidationUtil.isValidUrl( "http://" + input );
     }
 
-    private void testAuthorityRejects( final String input )
+    private void testAuthorityIsRejected( final String input )
     {
         assertFalse( isValidAuthority( input ) );
     }
 
+    public static void main(String[] args) {
+		UrlValidator validator = UrlValidator.getInstance();
+		System.out.println(validator.isValid("http://host.organization.com:100000"));
+	}
+
 }