/* * [TestURL2.java] * * Summary: Test the two-parameter URL constructor, and the equivalent URI.resolve. * * Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-12-30 initial version */ package com.mindprod.test; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import static java.lang.System.*; /** * Test the two-parameter URL constructor, and the equivalent URI.resolve. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2010-12-30 initial version * @since 2010-12-30 */ public final class TestURL2 { /** * test the URL constructor with two parms, and the equivalent URI.resolve. * * @param originalURLString original context URL * @param location partial URL to override the original * @param expectedURLString What we expect the merged result to be. * @param notes notes */ private static void test( final String originalURLString, final String location, final String expectedURLString, final String notes ) { testURL( originalURLString, location, expectedURLString, notes ); testURI( originalURLString, location, expectedURLString, notes ); } /** * test URI.resolve. * * @param originalURIString original context URL * @param location partial URL to override the original * @param expectedURIString What we expect the merged result to be. * @param notes notes */ private static void testURI( final String originalURIString, final String location, final String expectedURIString, final String notes ) { out.println( "\n>>>testing URI.resolve: [" + originalURIString + "] [" + location + "] " + notes ); final URI originalURI; try { originalURI = new URI( originalURIString ); } catch ( URISyntaxException e ) { out.println( " original URI failed with URISyntaxException" ); return; } final URI mergedURI = originalURI.resolve( location ); final String mergedURIString = mergedURI.toString(); if ( mergedURIString.equals( expectedURIString ) ) { out.println( " OK: [" + mergedURIString + "]" ); } else { out.println( " unexpected resolved URI: [" + mergedURIString + "] expected URI: [" + expectedURIString + "]" ); } } /** * testURL the URL constructor with two parms. * * @param originalURLString original context URL * @param location partial URL to override the original * @param expectedURLString What we expect the merged result to be. * @param notes notes */ private static void testURL( final String originalURLString, final String location, final String expectedURLString, final String notes ) { out.println( "\n>>>testing URL constructor: [" + originalURLString + "] [" + location + "] " + notes ); final URL originalURL; try { originalURL = new URL( originalURLString ); } catch ( MalformedURLException e ) { out.println( " original URL failed with MalformedURLException" ); return; } final URL resolvedURL; try { resolvedURL = new URL( originalURL, location ); } catch ( MalformedURLException e ) { out.println( " resolve failed with MalformedURLException" ); return; } final String resolvedURLString = resolvedURL.toString(); if ( resolvedURLString.equals( expectedURLString ) ) { out.println( " OK: [" + resolvedURLString + "]" ); } else { out.println( " unexpected resolved URL: [" + resolvedURLString + "] expected URL: [" + expectedURLString + "]" ); } } /** * Test the URL constructor and URI resolve to see if it produces expected results. * * @param args not used */ public static void main( final String[] args ) { test( "http://www.softwareforwindows.com/submit.asp", "", "http://www.softwareforwindows.com/submit.asp", "empty override" ); test( "http://mindprod.com/jgloss.html", "/jgloss/jgloss.html", "http://mindprod.com/jgloss/jgloss.html", "common redirect pattern" ); test( "http://mindprod.com/", "/index.html", "http://mindprod.com/index.html", "common redirect pattern" ); test( "http://new.myfonts.com/fonts/linotype/frutiger/", "/fonts/adobe/frutiger/", "http://new.myfonts.com/fonts/adobe/frutiger/", "real world redirect pattern, trailing slashes" ); test( "http://new.myfonts.com/search?search%5Btext%5D=britannic", "/search/britannic/", "http://new.myfonts.com/search/britannic/", "real world redirect pattern, has query with %" ); test( "http://new.myfonts.com/search?search%5Btext%5D=century+old+style", "/search/century+old+style/", "http://new.myfonts.com/search/century+old+style/", "real world redirect pattern, has query with +" ); test( "http://www.allposters.com/-sp/Earth-by-Day-Posters_i357956_.htm?aid=1025105019", "/-st/World-Maps-Posters_c13013_.htm", "http://www.allposters.com/-st/World-Maps-Posters_c13013_.htm", "real world redirect pattern, has - and _" ); } }