diff --git a/README.md b/README.md index c969988..a66b044 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ URL normalization (or URL canonicalization) in general is the process by which URLs are modified and standardized in a consistent manner. The goal of the normalization process is to transform a URL into a normalized or canonical URL so it is possible to determine if two syntactically different URLs may be equivalent. For more detail see http://en.wikipedia.org/wiki/URL_normalization -Rather than providing several traditional types of normalization for SEO purpose this java libraries provides transforming URLs into comparable and therefore sortable URLs. You can use this code whenever a URL is used as (primary) key in your application or storage system. This library produces URL by inverting the domain level labels. +Rather than providing several traditional types of normalization for SEO purpose this java libraries provides transforming URLs into comparable and therefore sortable URLs. You can use this code whenever a URL is used as (primary) key in your application or storage system. This library produces URL by inverting the domain level labels by default, but also gives the option not to. ## Examples diff --git a/pom.xml b/pom.xml index 4598ad6..68bf6b4 100644 --- a/pom.xml +++ b/pom.xml @@ -1,58 +1,58 @@ - 4.0.0 - ch.sentric - url-normalization - jar - 1.0.0 - url-normalization - https://github.com/sentric/url-normalization - - https://github.com/sentric/url-normalization.git - https://github.com/sentric/url-normalization.git - https://github.com/sentric/url-normalization.git - - - - log4j - log4j - 1.2.17 - - - commons-collections - commons-collections - 3.2.1 - - - org.apache.commons - commons-lang3 - 3.1 - - - junit - junit - 4.10 - test - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - 1.6 - 1.6 - - - - maven-assembly-plugin - 2.3 - - src/main/assembly/jar-with-dependencies.xml - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + ch.sentric + url-normalization + jar + 1.0.0 + url-normalization + https://github.com/sentric/url-normalization + + https://github.com/sentric/url-normalization.git + https://github.com/sentric/url-normalization.git + https://github.com/sentric/url-normalization.git + + + + log4j + log4j + 1.2.17 + + + commons-collections + commons-collections + 3.2.1 + + + org.apache.commons + commons-lang3 + 3.1 + + + junit + junit + 4.10 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + maven-assembly-plugin + 2.3 + + src/main/assembly/jar-with-dependencies.xml + + + + diff --git a/src/main/assembly/jar-with-dependencies.xml b/src/main/assembly/jar-with-dependencies.xml index b70f652..ba49846 100644 --- a/src/main/assembly/jar-with-dependencies.xml +++ b/src/main/assembly/jar-with-dependencies.xml @@ -1,17 +1,17 @@ - jar-with-dependencies - - jar - - false - - - true - / - - + xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> + jar-with-dependencies + + jar + + false + + + true + / + + \ No newline at end of file diff --git a/src/main/java/ch/sentric/Authority.java b/src/main/java/ch/sentric/Authority.java index 893c91e..1617037 100644 --- a/src/main/java/ch/sentric/Authority.java +++ b/src/main/java/ch/sentric/Authority.java @@ -27,120 +27,113 @@ public class Authority { /** * Constructor, initializing a authority. - * - * @param hostName - * the host name - * @param port - * the port - * @param user - * the username - * @param password - * the password + * + * @param hostName the host name + * @param port the port + * @param user the username + * @param password the password */ public Authority(final HostName hostName, final int port, final String user, final String password) { - this.hostName = hostName; - this.port = port; - this.user = user; - this.password = password; + this.hostName = hostName; + this.port = port; + this.user = user; + this.password = password; } /** * Constructor, initializing a authority. - * - * @param hostName - * the host name - * @param port - * the port - * @param userInfo - * the user info, separeted by :, e.g user:password + * + * @param hostName the host name + * @param port the port + * @param userInfo the user info, separeted by :, e.g user:password */ public Authority(final HostName hostName, final int port, final String userInfo) { - String user = null; - String password = null; - if (null != userInfo) { - final String[] userInfoParts = userInfo.split(":", 2); - if (userInfoParts.length == 2) { - user = userInfoParts[0]; - password = userInfoParts[1]; - } - } - this.hostName = hostName; - this.port = port; - this.user = user; - this.password = password; + String user = null; + String password = null; + if (null != userInfo) { + final String[] userInfoParts = userInfo.split(":", 2); + if (userInfoParts.length == 2) { + user = userInfoParts[0]; + password = userInfoParts[1]; + } + } + this.hostName = hostName; + this.port = port; + this.user = user; + this.password = password; } public HostName getHostName() { - return this.hostName; + return this.hostName; } public int getPort() { - return this.port; + return this.port; } public String getUser() { - return this.user; + return this.user; } public String getPassword() { - return this.password; + return this.password; } public String getAsString() { - return (this.user != null || this.password != null ? this.user + ":" + this.password + "@" : "") + this.hostName.getAsString() + (this.port != -1 ? ":" + this.port : ""); + return (this.user != null || this.password != null ? this.user + ":" + this.password + "@" : "") + this.hostName.getAsString() + (this.port != -1 ? ":" + this.port : ""); } - public String getOptimizedForProximityOrder() { - return this.hostName.getOptimizedForProximityOrder(); + public String getOptimizedForProximityOrder(Boolean reverseDomainParts) { + return this.hostName.getOptimizedForProximityOrder(reverseDomainParts); } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.hostName == null) ? 0 : this.hostName.hashCode()); - result = prime * result + ((this.password == null) ? 0 : this.password.hashCode()); - result = prime * result + this.port; - result = prime * result + ((this.user == null) ? 0 : this.user.hashCode()); - return result; + final int prime = 31; + int result = 1; + result = prime * result + ((this.hostName == null) ? 0 : this.hostName.hashCode()); + result = prime * result + ((this.password == null) ? 0 : this.password.hashCode()); + result = prime * result + this.port; + result = prime * result + ((this.user == null) ? 0 : this.user.hashCode()); + return result; } @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Authority other = (Authority) obj; - if (this.hostName == null) { - if (other.hostName != null) { - return false; - } - } else if (!this.hostName.equals(other.hostName)) { - return false; - } - if (this.password == null) { - if (other.password != null) { - return false; - } - } else if (!this.password.equals(other.password)) { - return false; - } - if (this.port != other.port) { - return false; - } - if (this.user == null) { - if (other.user != null) { - return false; - } - } else if (!this.user.equals(other.user)) { - return false; - } - return true; + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Authority other = (Authority) obj; + if (this.hostName == null) { + if (other.hostName != null) { + return false; + } + } else if (!this.hostName.equals(other.hostName)) { + return false; + } + if (this.password == null) { + if (other.password != null) { + return false; + } + } else if (!this.password.equals(other.password)) { + return false; + } + if (this.port != other.port) { + return false; + } + if (this.user == null) { + if (other.user != null) { + return false; + } + } else if (!this.user.equals(other.user)) { + return false; + } + return true; } } diff --git a/src/main/java/ch/sentric/DomainName.java b/src/main/java/ch/sentric/DomainName.java index e24adfd..15662ca 100644 --- a/src/main/java/ch/sentric/DomainName.java +++ b/src/main/java/ch/sentric/DomainName.java @@ -31,103 +31,105 @@ public class DomainName implements HostName { /** * The constructor, initializing the domain. - * - * @param domain - * as string + * + * @param domain as string */ public DomainName(final String domain) { - final StringTokenizer tokenizer = new StringTokenizer(domain, DOMAIN_NAME_DELIMITER); - while (tokenizer.hasMoreTokens()) { - final String current = tokenizer.nextToken(); - this.parts.add(current.toLowerCase(Locale.ENGLISH)); - } + final StringTokenizer tokenizer = new StringTokenizer(domain, DOMAIN_NAME_DELIMITER); + while (tokenizer.hasMoreTokens()) { + final String current = tokenizer.nextToken(); + this.parts.add(current.toLowerCase(Locale.ENGLISH)); + } } @Override public String getAsString() { - return concatenate(this.parts, DOMAIN_NAME_DELIMITER); + return concatenate(this.parts, DOMAIN_NAME_DELIMITER); } public String getAsReversedString() { - return reverseConcatenate(this.parts, DOMAIN_NAME_DELIMITER); + return reverseConcatenate(this.parts, DOMAIN_NAME_DELIMITER); } private List stripWWW(final List list) { - final ArrayList result = new ArrayList(list.size()); - - boolean isFirst = true; - for (final String item : list) { - if (isFirst && item.equalsIgnoreCase("www")) { - continue; - } - isFirst = false; - result.add(item); - } - - return result; + final ArrayList result = new ArrayList(list.size()); + + boolean isFirst = true; + for (final String item : list) { + if (isFirst && item.equalsIgnoreCase("www")) { + continue; + } + isFirst = false; + result.add(item); + } + + return result; } private String concatenate(final List list, final String separator) { - final StringBuilder builder = new StringBuilder(); - final ListIterator it = list.listIterator(); + final StringBuilder builder = new StringBuilder(); + final ListIterator it = list.listIterator(); - while (it.hasNext()) { - builder.append(it.next()); + while (it.hasNext()) { + builder.append(it.next()); - if (it.hasNext()) { - builder.append(separator); - } - } - return builder.toString(); + if (it.hasNext()) { + builder.append(separator); + } + } + return builder.toString(); } private String reverseConcatenate(final List list, final String separator) { - final StringBuilder builder = new StringBuilder(); - final ListIterator it = list.listIterator(list.size()); + final StringBuilder builder = new StringBuilder(); + final ListIterator it = list.listIterator(list.size()); - while (it.hasPrevious()) { - builder.append(it.previous()); + while (it.hasPrevious()) { + builder.append(it.previous()); - if (it.hasPrevious()) { - builder.append(separator); - } - } - return builder.toString(); + if (it.hasPrevious()) { + builder.append(separator); + } + } + return builder.toString(); } @Override - public String getOptimizedForProximityOrder() { - return reverseConcatenate(stripWWW(this.parts), DOMAIN_NAME_DELIMITER); + public String getOptimizedForProximityOrder(Boolean reverseDomain) { + if (reverseDomain) + return reverseConcatenate(stripWWW(this.parts), DOMAIN_NAME_DELIMITER); + else + return concatenate(stripWWW(this.parts), DOMAIN_NAME_DELIMITER); } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((this.parts == null) ? 0 : this.parts.hashCode()); - return result; + final int prime = 31; + int result = 1; + result = prime * result + ((this.parts == null) ? 0 : this.parts.hashCode()); + return result; } @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final DomainName other = (DomainName) obj; - if (this.parts == null) { - if (other.parts != null) { - return false; - } - } else if (!this.parts.equals(other.parts)) { - return false; - } - return true; + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final DomainName other = (DomainName) obj; + if (this.parts == null) { + if (other.parts != null) { + return false; + } + } else if (!this.parts.equals(other.parts)) { + return false; + } + return true; } } \ No newline at end of file diff --git a/src/main/java/ch/sentric/EscapedFragmentEncoder.java b/src/main/java/ch/sentric/EscapedFragmentEncoder.java index 77562fe..f5c8f10 100644 --- a/src/main/java/ch/sentric/EscapedFragmentEncoder.java +++ b/src/main/java/ch/sentric/EscapedFragmentEncoder.java @@ -15,6 +15,8 @@ */ package ch.sentric; +import sun.security.action.GetPropertyAction; + import java.io.CharArrayWriter; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; @@ -23,12 +25,10 @@ import java.security.AccessController; import java.util.BitSet; -import sun.security.action.GetPropertyAction; - /** * Modified Version of {@link java.net.URLEncoder} to match GoogleBots * specifications for AJAX Applications. - * + *

* Ranges that are converted: *

    *
  • %00..20
  • @@ -37,10 +37,10 @@ *
  • %2B
  • *
  • %7F..FF
  • *
- * + *

* All Javadoc has been removed to ensure corectness. Please see JavaDoc of the * original {java.net.UrlEncoder} instead. - * + * * @see https * ://code.google.com/intl/de-DE/web/ajaxcrawling/docs/specification.html */ @@ -52,20 +52,20 @@ public final class EscapedFragmentEncoder { static { - needEncoding = new BitSet(256); - int i; - for (i = 0x00; i <= 0x20; i++) { - needEncoding.set(i); - } - needEncoding.set(0x23); - needEncoding.set(0x25); - needEncoding.set(0x26); - needEncoding.set(0x2B); - for (i = 0x7F; i <= 0xFF; i++) { - needEncoding.set(i); - } - - dfltEncName = AccessController.doPrivileged(new GetPropertyAction("file.encoding")); + needEncoding = new BitSet(256); + int i; + for (i = 0x00; i <= 0x20; i++) { + needEncoding.set(i); + } + needEncoding.set(0x23); + needEncoding.set(0x25); + needEncoding.set(0x26); + needEncoding.set(0x2B); + for (i = 0x7F; i <= 0xFF; i++) { + needEncoding.set(i); + } + + dfltEncName = AccessController.doPrivileged(new GetPropertyAction("file.encoding")); } private EscapedFragmentEncoder() { @@ -73,67 +73,63 @@ private EscapedFragmentEncoder() { /** * Encodes the given string using the default encoding. - * - * @param s - * the string being encoded + * + * @param s the string being encoded * @return the encoded string or the input in case of an error */ @Deprecated public static String encode(final String s) { - String str = null; + String str = null; - try { - str = encode(s, dfltEncName); - } catch (final UnsupportedEncodingException e) { - // The system should always have the platform default - str = s; - } + try { + str = encode(s, dfltEncName); + } catch (final UnsupportedEncodingException e) { + // The system should always have the platform default + str = s; + } - return str; + return str; } /** * Returns the encoded string. - * - * @param s - * the string to encode - * @param enc - * the name of a supported {@code Charset} + * + * @param s the string to encode + * @param enc the name of a supported {@code Charset} * @return the encoded string - * @throws UnsupportedEncodingException - * if the given encoding is invalid + * @throws UnsupportedEncodingException if the given encoding is invalid */ public static String encode(final String s, final String enc) throws UnsupportedEncodingException { - boolean needToChange = false; - final StringBuffer out = new StringBuffer(s.length()); - Charset charset; - final CharArrayWriter charArrayWriter = new CharArrayWriter(); - - if (enc == null) { - throw new NullPointerException("charsetName"); - } - try { - charset = Charset.forName(enc); - } catch (final IllegalCharsetNameException e) { - throw new UnsupportedEncodingException(enc); - } catch (final UnsupportedCharsetException e) { - throw new UnsupportedEncodingException(enc); - } - - for (int i = 0; i < s.length();) { - int c = s.charAt(i); - // System.out.println("Examining character: " + c); - if (!needEncoding.get(c)) { - // System.out.println("Storing: " + c); - out.append((char) c); - i++; - } else { - // convert to external encoding before hex conversion - do { - charArrayWriter.write(c); - /* + boolean needToChange = false; + final StringBuffer out = new StringBuffer(s.length()); + Charset charset; + final CharArrayWriter charArrayWriter = new CharArrayWriter(); + + if (enc == null) { + throw new NullPointerException("charsetName"); + } + try { + charset = Charset.forName(enc); + } catch (final IllegalCharsetNameException e) { + throw new UnsupportedEncodingException(enc); + } catch (final UnsupportedCharsetException e) { + throw new UnsupportedEncodingException(enc); + } + + for (int i = 0; i < s.length(); ) { + int c = s.charAt(i); + // System.out.println("Examining character: " + c); + if (!needEncoding.get(c)) { + // System.out.println("Storing: " + c); + out.append((char) c); + i++; + } else { + // convert to external encoding before hex conversion + do { + charArrayWriter.write(c); + /* * If this character represents the start of a Unicode * surrogate pair, then pass in two characters. It's not * clear what should be done if a bytes reserved in the @@ -141,55 +137,55 @@ public static String encode(final String s, final String enc) throws Unsupported * pair. For now, just treat it as if it were any other * character. */ - if (c >= 0xD800 && c <= 0xDBFF) { + if (c >= 0xD800 && c <= 0xDBFF) { /* * System.out.println(Integer.toHexString(c) + * " is high surrogate"); */ - if ((i + 1) < s.length()) { - final int d = s.charAt(i + 1); + if ((i + 1) < s.length()) { + final int d = s.charAt(i + 1); /* * System.out.println("\tExamining " + * Integer.toHexString(d)); */ - if (d >= 0xDC00 && d <= 0xDFFF) { + if (d >= 0xDC00 && d <= 0xDFFF) { /* * System.out.println("\t" + * Integer.toHexString(d) + * " is low surrogate"); */ - charArrayWriter.write(d); - i++; - } - } - } - i++; - } while (i < s.length() && needEncoding.get((c = s.charAt(i)))); - - charArrayWriter.flush(); - final String str = new String(charArrayWriter.toCharArray()); - final byte[] ba = str.getBytes(charset); - for (final byte element : ba) { - out.append('%'); - char ch = Character.forDigit((element >> 4) & 0xF, 16); - // converting to use uppercase letter as part of - // the hex value if ch is a letter. - if (Character.isLetter(ch)) { - ch -= caseDiff; - } - out.append(ch); - ch = Character.forDigit(element & 0xF, 16); - if (Character.isLetter(ch)) { - ch -= caseDiff; - } - out.append(ch); - } - charArrayWriter.reset(); - needToChange = true; - } - } - - return (needToChange ? out.toString() : s); + charArrayWriter.write(d); + i++; + } + } + } + i++; + } while (i < s.length() && needEncoding.get((c = s.charAt(i)))); + + charArrayWriter.flush(); + final String str = new String(charArrayWriter.toCharArray()); + final byte[] ba = str.getBytes(charset); + for (final byte element : ba) { + out.append('%'); + char ch = Character.forDigit((element >> 4) & 0xF, 16); + // converting to use uppercase letter as part of + // the hex value if ch is a letter. + if (Character.isLetter(ch)) { + ch -= caseDiff; + } + out.append(ch); + ch = Character.forDigit(element & 0xF, 16); + if (Character.isLetter(ch)) { + ch -= caseDiff; + } + out.append(ch); + } + charArrayWriter.reset(); + needToChange = true; + } + } + + return (needToChange ? out.toString() : s); } } diff --git a/src/main/java/ch/sentric/HostName.java b/src/main/java/ch/sentric/HostName.java index 7c4331a..7606edc 100644 --- a/src/main/java/ch/sentric/HostName.java +++ b/src/main/java/ch/sentric/HostName.java @@ -22,14 +22,14 @@ public interface HostName { /** * This method will return an IP address as it is and a Domain Name with the * parts in reversed order. - * + * * @return optimized host name */ - public String getOptimizedForProximityOrder(); + public String getOptimizedForProximityOrder(Boolean reverseDomainParts); /** * Returns the hostname. - * + * * @return hostname */ public String getAsString(); diff --git a/src/main/java/ch/sentric/HostNameFactory.java b/src/main/java/ch/sentric/HostNameFactory.java index 5b0298b..c34c12a 100644 --- a/src/main/java/ch/sentric/HostNameFactory.java +++ b/src/main/java/ch/sentric/HostNameFactory.java @@ -23,16 +23,15 @@ public class HostNameFactory { /** * Factory method to create a hostname as {@link IPv4Address} or * {@link DomainName}. - * - * @param parsable - * the string to create the hostname for + * + * @param parsable the string to create the hostname for * @return a {@link HostName} */ public HostName build(final String parsable) { - final long ipv4 = IPv4Address.parseIPv4String(parsable); - if (IPv4Address.ILLEGAL_IPV4 != ipv4) { - return new IPv4Address(ipv4); - } - return new DomainName(parsable); + final long ipv4 = IPv4Address.parseIPv4String(parsable); + if (IPv4Address.ILLEGAL_IPV4 != ipv4) { + return new IPv4Address(ipv4); + } + return new DomainName(parsable); } } diff --git a/src/main/java/ch/sentric/IPv4Address.java b/src/main/java/ch/sentric/IPv4Address.java index 4795ece..b8254c5 100644 --- a/src/main/java/ch/sentric/IPv4Address.java +++ b/src/main/java/ch/sentric/IPv4Address.java @@ -32,94 +32,96 @@ public class IPv4Address implements HostName { /** * Constructor, initializing the ip v4 address. - * - * @param address - * the address to parse + * + * @param address the address to parse */ public IPv4Address(final long address) { - if (address < 0 || address > MAX_IPV4) { - throw new IllegalArgumentException(address + " is not in the range of a valid IPv4 address. 0 to " + String.valueOf(MAX_IPV4)); - } - this.address = address; + if (address < 0 || address > MAX_IPV4) { + throw new IllegalArgumentException(address + " is not in the range of a valid IPv4 address. 0 to " + String.valueOf(MAX_IPV4)); + } + this.address = address; } @Override public String getAsString() { - long remaining = this.address; - final long[] parts = new long[4]; - long modBase = 256; - long base = 1; - for (int i = 3; i >= 0; --i) { - parts[i] = ((remaining % modBase) / base); - remaining -= parts[i]; - base = modBase; - modBase *= 256l; - } - return parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3]; + long remaining = this.address; + final long[] parts = new long[4]; + long modBase = 256; + long base = 1; + for (int i = 3; i >= 0; --i) { + parts[i] = ((remaining % modBase) / base); + remaining -= parts[i]; + base = modBase; + modBase *= 256l; + } + return parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3]; } /** * Parse the given {@link String} to long. - * - * @param parsable - * the string to parse + * + * @param parsable the string to parse * @return the long representation */ public static long parseIPv4String(final String parsable) { - long result = 0; - final Matcher matcher = PATTERN.matcher(parsable); + long result = 0; + final Matcher matcher = PATTERN.matcher(parsable); - if (!matcher.find() || matcher.groupCount() != 4) { - result = -1; - } else { - long base = 1; - for (int i = 4; i > 0; --i) { - final String match = matcher.group(i); - final int number = Integer.parseInt(match); - result += number * base; - base *= 256; - } - } - return result; + if (!matcher.find() || matcher.groupCount() != 4) { + result = -1; + } else { + long base = 1; + for (int i = 4; i > 0; --i) { + final String match = matcher.group(i); + final int number = Integer.parseInt(match); + result += number * base; + base *= 256; + } + } + return result; } + /** + * @param reverseDomainParts Ignored: reversing IP addresses would not make sense + * @return the ip address in string format (xxx.xxx.xxx.xxx) + */ @Override - public String getOptimizedForProximityOrder() { - return getAsString(); + public String getOptimizedForProximityOrder(Boolean reverseDomainParts) { + return getAsString(); } public long getAddress() { - return this.address; + return this.address; } public void setAddress(final long address) { - this.address = address; + this.address = address; } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (int) (this.address ^ (this.address >>> 32)); - return result; + final int prime = 31; + int result = 1; + result = prime * result + (int) (this.address ^ (this.address >>> 32)); + return result; } @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final IPv4Address other = (IPv4Address) obj; - if (this.address != other.address) { - return false; - } - return true; + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final IPv4Address other = (IPv4Address) obj; + if (this.address != other.address) { + return false; + } + return true; } } \ No newline at end of file diff --git a/src/main/java/ch/sentric/Path.java b/src/main/java/ch/sentric/Path.java index ca7ef46..38a0a37 100644 --- a/src/main/java/ch/sentric/Path.java +++ b/src/main/java/ch/sentric/Path.java @@ -15,7 +15,9 @@ */ package ch.sentric; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; /** * The {@link Path} representing a path delimited by '/'. @@ -27,116 +29,124 @@ public class Path { /** * Constructor, initializing a path. - * - * @param path - * the path as string + * + * @param path the path as string */ public Path(final String path) { - this.pathParts = validate(path).split(PATH_SEPARATOR); + this.pathParts = validate(path).split(PATH_SEPARATOR); } /** * Constructor, initializing a path. - * - * @param pathParts - * the path parts as string array + * + * @param pathParts the path parts as string array */ public Path(final String[] pathParts) { - this.pathParts = pathParts.clone(); + this.pathParts = pathParts.clone(); } /** * Returns a percent codec based encoded path. - * + * * @return endocoded path */ public Path getReEncoded() { - final String[] newPathParts = new String[getPathParts().length]; - for (int i = 0; i < getPathParts().length; i++) { - newPathParts[i] = percentCodec.encodePathPart(percentCodec.decode(getPathParts()[i])); - } - return new Path(newPathParts); + final List newPathParts = new ArrayList(); + for (int i = 0; i < getPathParts().length; i++) { + if (".".equals(getPathParts()[i])) + continue; + if ("..".equals(getPathParts()[i])) { + newPathParts.remove(i - 1); + continue; + } + newPathParts.add(percentCodec.encodePathPart(percentCodec.decode(getPathParts()[i]))); + } + + String[] strResult = new String[newPathParts.size()]; + newPathParts.toArray(strResult); + + return new Path(strResult); } /** * Returns a new Path with . and .. parts removed. - * + *

* TODO see bixo for implementation * src/main/java/bixo/urldb/SimpleUrlNormalizer.java - * + * * @return new path with . and .. parts removed */ public Path removeRelativePathParts() { - return new Path(getPathParts()); + return new Path(getPathParts()); } /** * Returns a new Path with a trailing default page like index.html removed. - * + *

* TODO see bixo for implementation * src/main/java/bixo/urldb/SimpleUrlNormalizer.java - * + * * @return new Path with a trailing default page like index.html removed */ public Path removeDefaultPage() { - return new Path(getPathParts()); + return new Path(getPathParts()); } /** * Returns the path, delimited by '/'. Removes jsession and phpsessid from * path. - * + * * @return path */ public String getAsString() { - final StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); - boolean isFirst = true; - for (final String part : getPathParts()) { - if (isFirst) { - isFirst = false; - } else { - builder.append(PATH_SEPARATOR); - } - builder.append(part); - } - return builder.toString(); + boolean isFirst = true; + for (final String part : getPathParts()) { + if (isFirst) { + isFirst = false; + } else { + builder.append(PATH_SEPARATOR); + } + builder.append(part); + } + return builder.toString(); } private String[] getPathParts() { - return this.pathParts; + return this.pathParts; } private String validate(final String path) { - if (path.contains(";jsessionid") || path.contains(";JSESSIONID")) { - return path.substring(0, path.lastIndexOf(";")); - } - return path; + if (path.contains(";jsessionid") || path.contains(";JSESSIONID")) { + return path.substring(0, path.lastIndexOf(";")); + } + return path; } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + Arrays.hashCode(pathParts); - return result; + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(pathParts); + return result; } @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Path other = (Path) obj; - if (!Arrays.equals(pathParts, other.pathParts)) { - return false; - } - return true; + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Path other = (Path) obj; + if (!Arrays.equals(pathParts, other.pathParts)) { + return false; + } + return true; } } diff --git a/src/main/java/ch/sentric/PercentCodec.java b/src/main/java/ch/sentric/PercentCodec.java index dcb0739..ea9cae2 100644 --- a/src/main/java/ch/sentric/PercentCodec.java +++ b/src/main/java/ch/sentric/PercentCodec.java @@ -50,85 +50,85 @@ public class PercentCodec { private static final String HEX_CODES = "0123456789abcdefABCDEF"; public String encodePathPart(final String pathPart) { - return encode(pathPart, RESERVED_PATH_CHARS); + return encode(pathPart, RESERVED_PATH_CHARS); } public String encodeQueryComponent(final String queryComponent) { - return encode(queryComponent, RESERVED_QUERY_CHARS); + return encode(queryComponent, RESERVED_QUERY_CHARS); } public String encode(final String component, final String reservedChars) { - final StringBuilder result = new StringBuilder(); - for (int i = 0; i < component.length();) { - final int codePoint = component.codePointAt(i); - if (codePoint == 0x0020) { - result.append('+'); - } else if (codePoint >= 0x007F) { - result.append(encodeCodePoint(codePoint)); - } else if ((codePoint < 0x0020) || (reservedChars.indexOf((char) codePoint) != -1)) { - result.append(String.format("%%%02x", codePoint)); - } else { - result.append((char) codePoint); - } - - i += Character.charCount(codePoint); - } - - return result.toString(); + final StringBuilder result = new StringBuilder(); + for (int i = 0; i < component.length(); ) { + final int codePoint = component.codePointAt(i); + if (codePoint == 0x0020) { + result.append('+'); + } else if (codePoint >= 0x007F) { + result.append(encodeCodePoint(codePoint)); + } else if ((codePoint < 0x0020) || (reservedChars.indexOf((char) codePoint) != -1)) { + result.append(String.format("%%%02x", codePoint)); + } else { + result.append((char) codePoint); + } + + i += Character.charCount(codePoint); + } + + return result.toString(); } public String decode(final String url) { - // FUTURE - handle unsupported %uHHHH sequences for Unicode code points. - // FUTURE - detect & handle incorrectly encoded URLs + // FUTURE - handle unsupported %uHHHH sequences for Unicode code points. + // FUTURE - detect & handle incorrectly encoded URLs - // First, try to catch unescaped '%' characters. - final String result = escapeIsolatedPercentSigns(url); + // First, try to catch unescaped '%' characters. + final String result = escapeIsolatedPercentSigns(url); - try { - return URLDecoder.decode(result, Charset.defaultCharset().toString()); - } catch (final UnsupportedEncodingException e) { - throw new IllegalStateException("Unexpected exception during URL decoding", e); - } + try { + return URLDecoder.decode(result, Charset.defaultCharset().toString()); + } catch (final UnsupportedEncodingException e) { + throw new IllegalStateException("Unexpected exception during URL decoding", e); + } } private static String encodeCodePoint(final int codepoint) { - try { - final int[] codepoints = { codepoint }; - final byte[] bytes = new String(codepoints, 0, 1).getBytes(Charset.defaultCharset().toString()); - - final StringBuilder result = new StringBuilder(); - for (final byte value : bytes) { - result.append(String.format("%%%02x", value)); - } - - return result.toString(); - } catch (final UnsupportedEncodingException e) { - throw new IllegalStateException("Unexpected exception during URL encoding", e); - } + try { + final int[] codepoints = {codepoint}; + final byte[] bytes = new String(codepoints, 0, 1).getBytes(Charset.defaultCharset().toString()); + + final StringBuilder result = new StringBuilder(); + for (final byte value : bytes) { + result.append(String.format("%%%02x", value)); + } + + return result.toString(); + } catch (final UnsupportedEncodingException e) { + throw new IllegalStateException("Unexpected exception during URL encoding", e); + } } private static String escapeIsolatedPercentSigns(final String in) { - StringBuilder builder = null; - int offset = 0; - int lastOffset = 0; - while ((offset = in.indexOf('%', offset)) != -1) { - - offset += 1; - - if (offset > in.length() - 2 || HEX_CODES.indexOf(in.charAt(offset)) == -1 || HEX_CODES.indexOf(in.charAt(offset + 1)) == -1) { - if (null == builder) { - builder = new StringBuilder(); - } - builder.append(in.substring(lastOffset, offset)); - builder.append("25"); - lastOffset = offset; - } - } - if (null != builder) { - builder.append(in.substring(lastOffset)); - return builder.toString(); - } - - return in; + StringBuilder builder = null; + int offset = 0; + int lastOffset = 0; + while ((offset = in.indexOf('%', offset)) != -1) { + + offset += 1; + + if (offset > in.length() - 2 || HEX_CODES.indexOf(in.charAt(offset)) == -1 || HEX_CODES.indexOf(in.charAt(offset + 1)) == -1) { + if (null == builder) { + builder = new StringBuilder(); + } + builder.append(in.substring(lastOffset, offset)); + builder.append("25"); + lastOffset = offset; + } + } + if (null != builder) { + builder.append(in.substring(lastOffset)); + return builder.toString(); + } + + return in; } } \ No newline at end of file diff --git a/src/main/java/ch/sentric/Query.java b/src/main/java/ch/sentric/Query.java index 9d2cc63..82addc7 100644 --- a/src/main/java/ch/sentric/Query.java +++ b/src/main/java/ch/sentric/Query.java @@ -29,115 +29,111 @@ public class Query { /** * The constructor, initializing a query. - * - * @param list - * list of {@lin k QueryKeyValuePair}s - * @param delimiter - * the delimiter + * + * @param list list of {@lin k QueryKeyValuePair}s + * @param delimiter the delimiter */ public Query(final List list, final char delimiter) { - if (null == list) { - this.list = new ArrayList(0); - } else { - this.list = list; - } + if (null == list) { + this.list = new ArrayList(0); + } else { + this.list = list; + } - this.delimiter = delimiter; + this.delimiter = delimiter; } /** * Empty constructor, initializing the query with standard delimiter. */ public Query() { - this(null, STANDARD_DELIMITER); + this(null, STANDARD_DELIMITER); } public String getAsString() { - return getAsString(false, false); + return getAsString(false, false); } /** * Returns the query as {@link String}. When prefixQuestionMark is true, the * query starts with '?'. When sort is true the query is sorted. - * - * @param prefixQuestionMark - * true when query should start with '?' - * @param sort - * true when sorting is requested + * + * @param prefixQuestionMark true when query should start with '?' + * @param sort true when sorting is requested * @return the query as {@link String} */ public String getAsString(final boolean prefixQuestionMark, final boolean sort) { - if (this.list.size() == 0) { - return ""; - } - List list; - if (sort) { - list = new ArrayList(this.list); - Collections.sort(list); - } else { - list = this.list; - } + if (this.list.size() == 0) { + return ""; + } + List list; + if (sort) { + list = new ArrayList(this.list); + Collections.sort(list); + } else { + list = this.list; + } - return (prefixQuestionMark ? "?" : "") + listToString(list); + return (prefixQuestionMark ? "?" : "") + listToString(list); } /** * Returns a sorted query as {@link String} without a leading '?'. - * + * * @return sorted query */ public String getAsSortedString() { - return getAsString(false, true); + return getAsString(false, true); } private String listToString(final List list) { - final StringBuilder builder = new StringBuilder(); - boolean isFirst = true; - for (final QueryKeyValuePair pair : list) { - if (!isFirst) { - builder.append(delimiter); - } - builder.append(pair.getKey()); - if (null != pair.getValue()) { - builder.append("=").append(pair.getValue()); - } - isFirst = false; - } - return builder.toString(); + final StringBuilder builder = new StringBuilder(); + boolean isFirst = true; + for (final QueryKeyValuePair pair : list) { + if (!isFirst) { + builder.append(delimiter); + } + builder.append(pair.getKey()); + if (null != pair.getValue()) { + builder.append("=").append(pair.getValue()); + } + isFirst = false; + } + return builder.toString(); } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + delimiter; - result = prime * result + ((list == null) ? 0 : list.hashCode()); - return result; + final int prime = 31; + int result = 1; + result = prime * result + delimiter; + result = prime * result + ((list == null) ? 0 : list.hashCode()); + return result; } @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Query other = (Query) obj; - if (delimiter != other.delimiter) { - return false; - } - if (list == null) { - if (other.list != null) { - return false; - } - } else if (!list.equals(other.list)) { - return false; - } - return true; + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Query other = (Query) obj; + if (delimiter != other.delimiter) { + return false; + } + if (list == null) { + if (other.list != null) { + return false; + } + } else if (!list.equals(other.list)) { + return false; + } + return true; } } \ No newline at end of file diff --git a/src/main/java/ch/sentric/QueryFactory.java b/src/main/java/ch/sentric/QueryFactory.java index 2b062dd..188552f 100644 --- a/src/main/java/ch/sentric/QueryFactory.java +++ b/src/main/java/ch/sentric/QueryFactory.java @@ -15,13 +15,13 @@ */ package ch.sentric; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.Predicate; + import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.Predicate; - /** *

* The QueryFactory parses the in order to assemble a list of key and value @@ -34,7 +34,7 @@ public class QueryFactory { /** * URL query string filters to apply. - * + *

*

    *
  • WebTrends (WT.): see * http://www.heureka.com/upload/AdministrationUsersGuide.pdf, Chapter 27 @@ -46,87 +46,87 @@ public class QueryFactory { *
*/ private static ArrayList filters = new ArrayList(Arrays.asList("utm", "WT.", "OVKEY", "YSMKEY", "OVRAW", "YSMRAW", "OVMTC", "YSMMTC", "OVADID", "YSMADID", - "OVADID", "YSMADID", "OVKWID", "YSMKWID", "OVCAMPGID", "YSMCAMPGID", "OVADGRPID", "YSMADGRPID")); + "OVADID", "YSMADID", "OVKWID", "YSMKWID", "OVCAMPGID", "YSMCAMPGID", "OVADGRPID", "YSMADGRPID")); public Query build(final String q) { - if (null == q || "".equalsIgnoreCase(q)) { - return new Query(); - } - final ArrayList list = new ArrayList(0); + if (null == q || "".equalsIgnoreCase(q)) { + return new Query(); + } + final ArrayList list = new ArrayList(0); - ParserState state = ParserState.START; - final StringTokenizer tokenizer = new StringTokenizer(q, "=&", true); - String key = null; - while (tokenizer.hasMoreTokens()) { - final String token = tokenizer.nextToken(); + ParserState state = ParserState.START; + final StringTokenizer tokenizer = new StringTokenizer(q, "=&", true); + String key = null; + while (tokenizer.hasMoreTokens()) { + final String token = tokenizer.nextToken(); - switch (state) { - case DELIMITER: - if (token.equals("&")) { - state = ParserState.KEY; - } - break; + switch (state) { + case DELIMITER: + if (token.equals("&")) { + state = ParserState.KEY; + } + break; - case KEY: - if (!token.equals("=") && !token.equals("&") && !token.equalsIgnoreCase("PHPSESSID") && !token.equalsIgnoreCase("JSESSIONID")) { - key = token; - state = ParserState.EQUAL; - } - break; + case KEY: + if (!token.equals("=") && !token.equals("&") && !token.equalsIgnoreCase("PHPSESSID") && !token.equalsIgnoreCase("JSESSIONID")) { + key = token; + state = ParserState.EQUAL; + } + break; - case EQUAL: - if (token.equals("=")) { - state = ParserState.VALUE; - } else if (token.equals("&")) { - list.add(new QueryKeyValuePair(key, null)); - state = ParserState.KEY; - } - break; + case EQUAL: + if (token.equals("=")) { + state = ParserState.VALUE; + } else if (token.equals("&")) { + list.add(new QueryKeyValuePair(key, null)); + state = ParserState.KEY; + } + break; - case VALUE: - if (!token.equals("=") && !token.equals("&")) { - if (token.contains(";jsessionid") || token.contains(";JSESSIONID")) { - list.add(new QueryKeyValuePair(key, token.substring(0, token.lastIndexOf(";")))); - } else { - list.add(new QueryKeyValuePair(key, token)); - } - state = ParserState.DELIMITER; - } else if (token.equals("&")) { - list.add(new QueryKeyValuePair(key, null)); - state = ParserState.KEY; - } - break; + case VALUE: + if (!token.equals("=") && !token.equals("&")) { + if (token.contains(";jsessionid") || token.contains(";JSESSIONID")) { + list.add(new QueryKeyValuePair(key, token.substring(0, token.lastIndexOf(";")))); + } else { + list.add(new QueryKeyValuePair(key, token)); + } + state = ParserState.DELIMITER; + } else if (token.equals("&")) { + list.add(new QueryKeyValuePair(key, null)); + state = ParserState.KEY; + } + break; - case START: - if (!token.equalsIgnoreCase("PHPSESSID") && !token.equalsIgnoreCase("JSESSIONID")) { - key = token; - state = ParserState.EQUAL; - } - break; + case START: + if (!token.equalsIgnoreCase("PHPSESSID") && !token.equalsIgnoreCase("JSESSIONID")) { + key = token; + state = ParserState.EQUAL; + } + break; - default: - break; - } - } - CollectionUtils.filter(list, new Predicate() { + default: + break; + } + } + CollectionUtils.filter(list, new Predicate() { - @Override - public boolean evaluate(final Object object) { - boolean allowedQueryParameter = true; - final QueryKeyValuePair queryKeyValuePair = (QueryKeyValuePair) object; - for (final String filter : filters) { - if (queryKeyValuePair.getKey().startsWith(filter)) { - allowedQueryParameter = false; - } - } - return allowedQueryParameter; - } - }); + @Override + public boolean evaluate(final Object object) { + boolean allowedQueryParameter = true; + final QueryKeyValuePair queryKeyValuePair = (QueryKeyValuePair) object; + for (final String filter : filters) { + if (queryKeyValuePair.getKey().startsWith(filter)) { + allowedQueryParameter = false; + } + } + return allowedQueryParameter; + } + }); - return new Query(list, '&'); + return new Query(list, '&'); } private enum ParserState { - KEY, VALUE, DELIMITER, EQUAL, START + KEY, VALUE, DELIMITER, EQUAL, START } } diff --git a/src/main/java/ch/sentric/QueryKeyValuePair.java b/src/main/java/ch/sentric/QueryKeyValuePair.java index d80d7b7..8e2fa5b 100644 --- a/src/main/java/ch/sentric/QueryKeyValuePair.java +++ b/src/main/java/ch/sentric/QueryKeyValuePair.java @@ -27,57 +27,55 @@ class QueryKeyValuePair implements Comparable { /** * The constructor, initializing the object. - * - * @param key - * the key - * @param value - * the value + * + * @param key the key + * @param value the value */ public QueryKeyValuePair(final String key, final String value) { - this.key = key; - if (null == value) { - this.value = ""; - } else { - this.value = value; - } + this.key = key; + if (null == value) { + this.value = ""; + } else { + this.value = value; + } } @Override public int compareTo(final QueryKeyValuePair other) { - final int keyComparission = getKey().compareTo(other.getKey()); - if (keyComparission != 0) { - return keyComparission; - } - return getValue().compareTo(other.getValue()); + final int keyComparission = getKey().compareTo(other.getKey()); + if (keyComparission != 0) { + return keyComparission; + } + return getValue().compareTo(other.getValue()); } @Override public int hashCode() { - return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers - append(getKey()).append(getValue()).toHashCode(); + return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers + append(getKey()).append(getValue()).toHashCode(); } @Override public boolean equals(final Object obj) { - if (obj == null) { - return false; - } - if (obj == this) { - return true; - } - if (obj.getClass() != getClass()) { - return false; - } + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (obj.getClass() != getClass()) { + return false; + } - final QueryKeyValuePair rhs = (QueryKeyValuePair) obj; - return new EqualsBuilder().append(getKey(), rhs.getKey()).append(getValue(), rhs.getValue()).isEquals(); + final QueryKeyValuePair rhs = (QueryKeyValuePair) obj; + return new EqualsBuilder().append(getKey(), rhs.getKey()).append(getValue(), rhs.getValue()).isEquals(); } public String getKey() { - return key; + return key; } public String getValue() { - return value; + return value; } } \ No newline at end of file diff --git a/src/main/java/ch/sentric/URL.java b/src/main/java/ch/sentric/URL.java index b66e0d0..6f8a2d6 100644 --- a/src/main/java/ch/sentric/URL.java +++ b/src/main/java/ch/sentric/URL.java @@ -15,14 +15,14 @@ */ package ch.sentric; +import org.apache.commons.lang3.StringUtils; + import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; -import org.apache.commons.lang3.StringUtils; - /** * The url class. */ @@ -37,189 +37,194 @@ public class URL { /** * The constructor, initializing a url from {@link String}. - * - * @param url - * as string - * @throws MalformedURLException - * when url could not be parsed + * + * @param url as string + * @throws MalformedURLException when url could not be parsed */ public URL(final String url) throws MalformedURLException { - this.givenInputUrl = url; - this.parse(url); + this.givenInputUrl = url; + this.parse(url); } /** * The constructor, initializing a url from {@link URI}. - * - * @param uri - * as URI - * @throws MalformedURLException - * when url could not be parsed + * + * @param uri as URI + * @throws MalformedURLException when url could not be parsed */ public URL(final URI uri) throws MalformedURLException { - this.givenInputUrl = uri.toString(); - parse(uri.toString()); + this.givenInputUrl = uri.toString(); + parse(uri.toString()); } private void parse(final String url) throws MalformedURLException { - final java.net.URL urlObj = new java.net.URL(url); + final java.net.URL urlObj = new java.net.URL(url); - this.scheme = urlObj.getProtocol(); - final HostName hostName = new HostNameFactory().build(urlObj.getHost()); - this.authority = new Authority(hostName, urlObj.getPort(), urlObj.getUserInfo()); - this.query = new QueryFactory().build(urlObj.getQuery()); - this.path = new Path(urlObj.getPath()); - this.fragment = urlObj.getRef(); + this.scheme = urlObj.getProtocol(); + final HostName hostName = new HostNameFactory().build(urlObj.getHost()); + this.authority = new Authority(hostName, urlObj.getPort(), urlObj.getUserInfo()); + this.query = new QueryFactory().build(urlObj.getQuery()); + this.path = new Path(urlObj.getPath()); + this.fragment = urlObj.getRef(); } /** * Returns a {@link URI} representation of this object or null when not * valid. All fragments will be removed from the original URL. - * + * * @return the uri - * @throws URISyntaxException - * when the uri couldn't be parsed + * @throws URISyntaxException when the uri couldn't be parsed */ public URI getURI() throws URISyntaxException { - URI uri = null; - if (StringUtils.isNotBlank(getFragment())) { - uri = new URI(getUrlWithoutFragment()); - } else { - uri = new URI(getGivenInputUrl()); - } - return uri; + URI uri = null; + if (StringUtils.isNotBlank(getFragment())) { + uri = new URI(getUrlWithoutFragment()); + } else { + uri = new URI(getGivenInputUrl()); + } + return uri; } public String getUrlWithoutFragment() { - return getGivenInputUrl().substring(0, getGivenInputUrl().indexOf("#")); + return getGivenInputUrl().substring(0, getGivenInputUrl().indexOf("#")); } public String getGivenInputUrl() { - return this.givenInputUrl; + return this.givenInputUrl; } public String getScheme() { - return this.scheme; + return this.scheme; } public Authority getAuthority() { - return this.authority; + return this.authority; } public Query getQuery() { - return this.query; + return this.query; } public Path getPath() { - return this.path; + return this.path; } public String getFragment() { - return this.fragment; + return this.fragment; } /** * Replaces white spaces with '+' characters, removes jsession and phpsessid * parameters. - * + * * @return a url without white spaces and jession or phpsessid */ public String getRepairedUrl() { - return this.scheme + "://" + this.authority.getAsString() + this.path.getReEncoded().getAsString() + this.query.getAsString(true, false) - + (this.fragment == null ? "" : "#" + this.fragment); + return this.scheme + "://" + this.authority.getAsString() + this.path.getReEncoded().getAsString() + this.query.getAsString(true, false) + + (this.fragment == null ? "" : "#" + this.fragment); } + /** + * By default, this reverses the domain parts in order to maintain backwards compatibility for those already using the library + */ public String getNormalizedUrl() { - return this.authority.getOptimizedForProximityOrder() + this.path.getReEncoded().getAsString() + this.query.getAsString(true, true); + return getNormalizedUrl(true); + } + + /** + * Gives you the option to either reverse the domain parts (www.example.com => com.example) or not (www.example.com => example.com) + * it still does all the other magic: drops the w's, lowercase the domain, sort parameters, etc. + */ + public String getNormalizedUrl(Boolean reverseDomainParts) { + return this.authority.getOptimizedForProximityOrder(reverseDomainParts) + this.path.getReEncoded().getAsString() + this.query.getAsString(true, true); } /** * Resolve the ip address from the authority. - * + * * @return ip address as string - * @throws UnknownHostException - * when ip can not be resolved + * @throws UnknownHostException when ip can not be resolved */ public String resolveIp() throws UnknownHostException { - final String ip = InetAddress.getByName(this.getAuthority().getAsString()).getHostAddress(); - if (ip.equals("127.0.0.1")) { - throw new UnknownHostException("IP" + ip + "not valid"); - } - return ip; + final String ip = InetAddress.getByName(this.getAuthority().getAsString()).getHostAddress(); + if (ip.equals("127.0.0.1")) { + throw new UnknownHostException("IP" + ip + "not valid"); + } + return ip; } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (this.authority == null ? 0 : this.authority.hashCode()); - result = prime * result + (this.fragment == null ? 0 : this.fragment.hashCode()); - result = prime * result + (this.givenInputUrl == null ? 0 : this.givenInputUrl.hashCode()); - result = prime * result + (this.path == null ? 0 : this.path.hashCode()); - result = prime * result + (this.query == null ? 0 : this.query.hashCode()); - result = prime * result + (this.scheme == null ? 0 : this.scheme.hashCode()); - return result; + final int prime = 31; + int result = 1; + result = prime * result + (this.authority == null ? 0 : this.authority.hashCode()); + result = prime * result + (this.fragment == null ? 0 : this.fragment.hashCode()); + result = prime * result + (this.givenInputUrl == null ? 0 : this.givenInputUrl.hashCode()); + result = prime * result + (this.path == null ? 0 : this.path.hashCode()); + result = prime * result + (this.query == null ? 0 : this.query.hashCode()); + result = prime * result + (this.scheme == null ? 0 : this.scheme.hashCode()); + return result; } @Override public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final URL other = (URL) obj; - if (this.authority == null) { - if (other.authority != null) { - return false; - } - } else if (!this.authority.equals(other.authority)) { - return false; - } - if (this.fragment == null) { - if (other.fragment != null) { - return false; - } - } else if (!this.fragment.equals(other.fragment)) { - return false; - } - if (this.givenInputUrl == null) { - if (other.givenInputUrl != null) { - return false; - } - } else if (!this.givenInputUrl.equals(other.givenInputUrl)) { - return false; - } - if (this.path == null) { - if (other.path != null) { - return false; - } - } else if (!this.path.equals(other.path)) { - return false; - } - if (this.query == null) { - if (other.query != null) { - return false; - } - } else if (!this.query.equals(other.query)) { - return false; - } - if (this.scheme == null) { - if (other.scheme != null) { - return false; - } - } else if (!this.scheme.equals(other.scheme)) { - return false; - } - return true; + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final URL other = (URL) obj; + if (this.authority == null) { + if (other.authority != null) { + return false; + } + } else if (!this.authority.equals(other.authority)) { + return false; + } + if (this.fragment == null) { + if (other.fragment != null) { + return false; + } + } else if (!this.fragment.equals(other.fragment)) { + return false; + } + if (this.givenInputUrl == null) { + if (other.givenInputUrl != null) { + return false; + } + } else if (!this.givenInputUrl.equals(other.givenInputUrl)) { + return false; + } + if (this.path == null) { + if (other.path != null) { + return false; + } + } else if (!this.path.equals(other.path)) { + return false; + } + if (this.query == null) { + if (other.query != null) { + return false; + } + } else if (!this.query.equals(other.query)) { + return false; + } + if (this.scheme == null) { + if (other.scheme != null) { + return false; + } + } else if (!this.scheme.equals(other.scheme)) { + return false; + } + return true; } @Override public String toString() { - return this.givenInputUrl; + return this.givenInputUrl; } } diff --git a/src/main/java/ch/sentric/UrlUtil.java b/src/main/java/ch/sentric/UrlUtil.java index c02a23f..5aee91e 100644 --- a/src/main/java/ch/sentric/UrlUtil.java +++ b/src/main/java/ch/sentric/UrlUtil.java @@ -41,218 +41,204 @@ public class UrlUtil { /** * retrieves the top-level-domain for an optimized-site-url in reversed * order. - * + *

* Example: uk.co.bbc.subdomain.www becomes uk.co.bbc - * - * @param normalizedOrderSite - * site in reverse order + * + * @param normalizedOrderSite site in reverse order * @return top level-domain in reverse order */ public static String siteToTopLevel(final String normalizedOrderSite) { - return reverseUrlOrder(urlToTopLevel(reverseUrlOrder(normalizedOrderSite))); + return reverseUrlOrder(urlToTopLevel(reverseUrlOrder(normalizedOrderSite))); } /** * retrieves the top-level-domain for an url in normal order. - * + *

* Example: www.subdomain.bbc.co.uk becomes bbc.co.uk - * - * @param url - * site in normal order + * + * @param url site in normal order * @return top level-domain in normal order */ public static String urlToTopLevel(final String url) { - final Matcher matcher = REGEXP.matcher(url); - if (matcher.find()) { - return url.substring(matcher.start(), matcher.end()); - } else { - return url; - } + final Matcher matcher = REGEXP.matcher(url); + if (matcher.find()) { + return url.substring(matcher.start(), matcher.end()); + } else { + return url; + } } /** * retrieves the parent-level-domain for an optimized-site-url in reversed * order. - * + *

* Example: uk.co.bbc.subdomain.forum becomes uk.co.bbc.subdomain - * - * @param normalizedOrderSite - * site in reverse order + * + * @param normalizedOrderSite site in reverse order * @return parent level-domain in reverse order */ public static String getParentSite(final String normalizedOrderSite) { - return reverseUrlOrder(getParentUrl(reverseUrlOrder(normalizedOrderSite))); + return reverseUrlOrder(getParentUrl(reverseUrlOrder(normalizedOrderSite))); } /** * retrieves the parent-level-domain for an url in normal order. - * + *

* Example: forum.subdomain.bbc.co.uk becomes subdomain.bbc.co.uk - * - * @param nurl - * site in normal order + * + * @param nurl site in normal order * @return parent level-domain in normal order */ public static String getParentUrl(final String url) { - final String topLevelUrl = urlToTopLevel(url); - if (topLevelUrl.equals(url)) { - return url; - } else { - return url.substring(url.indexOf(".")); - } + final String topLevelUrl = urlToTopLevel(url); + if (topLevelUrl.equals(url)) { + return url; + } else { + return url.substring(url.indexOf(".")); + } } /** * retrieves the subdomain-domain for an optimized-site-url. - * + *

* Example: http://www.subdomain.bbc.co.uk/foo/bar becomes www.subdomain - * - * @param url - * the url of the site + * + * @param url the url of the site * @return subdomain */ public static String subDomain(final URL url) { - String subdomain = "www"; - if (IPv4Address.parseIPv4String(url.getAuthority().getAsString()) > -1) { - subdomain = url.getAuthority().getAsString(); - } else { - final Matcher matcher = REGEXP.matcher(url.getAuthority().getAsString()); - if (matcher.find() && matcher.start() != 0) { - subdomain = url.getAuthority().getAsString().substring(0, matcher.start() - 1); - } - } - return subdomain; + String subdomain = "www"; + if (IPv4Address.parseIPv4String(url.getAuthority().getAsString()) > -1) { + subdomain = url.getAuthority().getAsString(); + } else { + final Matcher matcher = REGEXP.matcher(url.getAuthority().getAsString()); + if (matcher.find() && matcher.start() != 0) { + subdomain = url.getAuthority().getAsString().substring(0, matcher.start() - 1); + } + } + return subdomain; } /** * reverses the Order of the URL-Parts. - * + *

* Example: uk.co.bbc.subdomain becomes subdomain.bbc.co.uk - * - * @param site - * url without protocol + * + * @param site url without protocol * @return site in reversed order */ public static String reverseUrlOrder(final String site) { - if (IPv4Address.parseIPv4String(site) > -1) { - return site; - } else { - return reverseString(site, "."); - } + if (IPv4Address.parseIPv4String(site) > -1) { + return site; + } else { + return reverseString(site, "."); + } } /** * Check whether the URL is an AJAX URL that could be converted to a * GoogeBot URL. - * - * @see https + * + * @param url the original url to check for #! + * @return true when the url is a escape-fragmentable url + * @see https * ://code.google.com/intl/de-DE/web/ajaxcrawling/docs/getting-started * .html - * @param url - * the original url to check for #! - * @return true when the url is a escape-fragmentable url */ public static boolean isEscapeFragmentableUrl(final URL url) { - return url.getFragment() != null && url.getFragment().length() > 0 && url.getFragment().charAt(0) == '!'; + return url.getFragment() != null && url.getFragment().length() > 0 && url.getFragment().charAt(0) == '!'; } /** * @see UrlUtil#isEscapeFragmentUrl(String) */ public static boolean isEscapeFragmentUrl(final URL url) { - return isEscapeFragmentUrl(url.getGivenInputUrl()); + return isEscapeFragmentUrl(url.getGivenInputUrl()); } /** * @see UrlUtil#isEscapeFragmentUrl(String) */ public static boolean isEscapeFragmentUrl(final URI url) { - return isEscapeFragmentUrl(url.toString()); + return isEscapeFragmentUrl(url.toString()); } /** * Check whether the URL is an AJAX URL that could be convered to a GoogeBot * URL. - * - * @see https + * + * @param url the original url to check for _escaped_fragment_ + * @return true when the url is an escape-fragment url + * @see https * ://code.google.com/intl/de-DE/web/ajaxcrawling/docs/getting-started * .html - * @param url - * the original url to check for _escaped_fragment_ - * @return true when the url is an escape-fragment url */ public static boolean isEscapeFragmentUrl(final String url) { - return url.matches("^.*[?&]_escaped_fragment_=.*$"); + return url.matches("^.*[?&]_escaped_fragment_=.*$"); } /** * Converts to Googlebot-URL. - * - * @see https + * + * @param url the original url with #! + * @return the escaped-fragment url + * @throws UnsupportedEncodingException encoding not found + * @throws MalformedURLException url malformed + * @see https * ://code.google.com/intl/de-DE/web/ajaxcrawling/docs/getting-started * .html - * @param url - * the original url with #! - * @return the escaped-fragment url - * @throws UnsupportedEncodingException - * encoding not found - * @throws MalformedURLException - * url malformed */ public static URL toEscapedFragmentUrl(final URL url) throws MalformedURLException, UnsupportedEncodingException { - if (!isEscapeFragmentableUrl(url)) { - throw new IllegalArgumentException("the given URL is no escape-fragmentable url: " + url); - } - final String urlWithoutFragment = url.getUrlWithoutFragment(); - final char delimiter = urlWithoutFragment.contains("?") ? '&' : '?'; - final String encodedFragment = EscapedFragmentEncoder.encode(url.getFragment().substring(1), Charset.defaultCharset().toString()); - return new URL(urlWithoutFragment + delimiter + "_escaped_fragment_=" + encodedFragment); + if (!isEscapeFragmentableUrl(url)) { + throw new IllegalArgumentException("the given URL is no escape-fragmentable url: " + url); + } + final String urlWithoutFragment = url.getUrlWithoutFragment(); + final char delimiter = urlWithoutFragment.contains("?") ? '&' : '?'; + final String encodedFragment = EscapedFragmentEncoder.encode(url.getFragment().substring(1), Charset.defaultCharset().toString()); + return new URL(urlWithoutFragment + delimiter + "_escaped_fragment_=" + encodedFragment); } /** * @see #fromEscapedFragmentUrl(URL) */ public static URI fromEscapedFragmentUrl(final URI uri) throws UnsupportedEncodingException, MalformedURLException { - try { - return new URI(fromEscapedFragmentUrl(new URL(uri)).getGivenInputUrl()); - } catch (final URISyntaxException e) { - throw new MalformedURLException(e.toString()); - } + try { + return new URI(fromEscapedFragmentUrl(new URL(uri)).getGivenInputUrl()); + } catch (final URISyntaxException e) { + throw new MalformedURLException(e.toString()); + } } /** * Converts back from Googlebot-URL. - * - * @see https + * + * @param url the escaped-fragment url + * @return the original url with #! + * @throws UnsupportedEncodingException encoding not found + * @throws MalformedURLException url malformed + * @see https * ://code.google.com/intl/de-DE/web/ajaxcrawling/docs/getting-started * .html - * @param url - * the escaped-fragment url - * @return the original url with #! - * @throws UnsupportedEncodingException - * encoding not found - * @throws MalformedURLException - * url malformed */ public static URL fromEscapedFragmentUrl(final URL url) throws UnsupportedEncodingException, MalformedURLException { - if (!isEscapeFragmentUrl(url)) { - throw new IllegalArgumentException("the given URL is no escape-fragmented url: " + url); - } - final String urlAsString = url.getGivenInputUrl(); - final String encodedFragment = urlAsString.replaceFirst("^.*[?&]_escaped_fragment_=", ""); - final String decodedFragment = URLDecoder.decode(encodedFragment, Charset.defaultCharset().toString()); - return new URL(urlAsString.replaceFirst("[?&]_escaped_fragment_=.*$", "#!" + decodedFragment)); + if (!isEscapeFragmentUrl(url)) { + throw new IllegalArgumentException("the given URL is no escape-fragmented url: " + url); + } + final String urlAsString = url.getGivenInputUrl(); + final String encodedFragment = urlAsString.replaceFirst("^.*[?&]_escaped_fragment_=", ""); + final String decodedFragment = URLDecoder.decode(encodedFragment, Charset.defaultCharset().toString()); + return new URL(urlAsString.replaceFirst("[?&]_escaped_fragment_=.*$", "#!" + decodedFragment)); } private static String reverseString(final String input, final String separator) { - final StringTokenizer tokenizer = new StringTokenizer(input, separator); - final StringBuilder sb = new StringBuilder(); - boolean first = true; - while (tokenizer.hasMoreTokens()) { - sb.insert(0, tokenizer.nextToken() + (first ? "" : separator)); - first = false; - } - return sb.toString(); + final StringTokenizer tokenizer = new StringTokenizer(input, separator); + final StringBuilder sb = new StringBuilder(); + boolean first = true; + while (tokenizer.hasMoreTokens()) { + sb.insert(0, tokenizer.nextToken() + (first ? "" : separator)); + first = false; + } + return sb.toString(); } } diff --git a/src/test/java/ch/sentric/AuthorityTest.java b/src/test/java/ch/sentric/AuthorityTest.java index da33fe7..6592079 100644 --- a/src/test/java/ch/sentric/AuthorityTest.java +++ b/src/test/java/ch/sentric/AuthorityTest.java @@ -25,15 +25,15 @@ public class AuthorityTest { @Test public void equalsShouldReturnTrue() { - final Authority auth1 = new Authority(new DomainName("gamespot.com"), -1, null); - final Authority auth2 = new Authority(new DomainName("gamespot.com"), -1, null); - Assert.assertTrue("equals should be true", auth1.equals(auth2)); + final Authority auth1 = new Authority(new DomainName("gamespot.com"), -1, null); + final Authority auth2 = new Authority(new DomainName("gamespot.com"), -1, null); + Assert.assertTrue("equals should be true", auth1.equals(auth2)); } @Test public void equalsShouldReturnFalse() { - final Authority auth1 = new Authority(new DomainName("gamespot.com"), -1, null); - final Authority auth2 = new Authority(new DomainName("gamespot2.com"), -1, null); - Assert.assertFalse("equals should be false", auth1.equals(auth2)); + final Authority auth1 = new Authority(new DomainName("gamespot.com"), -1, null); + final Authority auth2 = new Authority(new DomainName("gamespot2.com"), -1, null); + Assert.assertFalse("equals should be false", auth1.equals(auth2)); } } diff --git a/src/test/java/ch/sentric/DomainNameTest.java b/src/test/java/ch/sentric/DomainNameTest.java index e9e53dd..605d219 100644 --- a/src/test/java/ch/sentric/DomainNameTest.java +++ b/src/test/java/ch/sentric/DomainNameTest.java @@ -15,11 +15,11 @@ */ package ch.sentric; -import java.util.Locale; - import org.junit.Assert; import org.junit.Test; +import java.util.Locale; + /** * The {@link DomainName} test class. */ @@ -27,49 +27,56 @@ public class DomainNameTest { @Test public void getAsStringShouldReturnGivenDomainAsString() { - Assert.assertEquals("www.koch.ro", new DomainName("www.koch.ro").getAsString()); - Assert.assertEquals("faz.net", new DomainName("faz.net").getAsString()); - Assert.assertEquals("äö.mydomain.de", new DomainName("äö.mydomain.de").getAsString()); + Assert.assertEquals("www.koch.ro", new DomainName("www.koch.ro").getAsString()); + Assert.assertEquals("faz.net", new DomainName("faz.net").getAsString()); + Assert.assertEquals("äö.mydomain.de", new DomainName("äö.mydomain.de").getAsString()); } @Test public void getAsStringShouldLowerCaseGivenDomain() { - Assert.assertEquals("www.KOCH.rO".toLowerCase(Locale.ENGLISH), new DomainName("www.KOCH.rO").getAsString()); - Assert.assertEquals("faz.Net".toLowerCase(Locale.ENGLISH), new DomainName("faz.Net").getAsString()); - Assert.assertEquals("ÄÖ.mydomain.org".toLowerCase(Locale.ENGLISH), new DomainName("ÄÖ.mydomain.org").getAsString()); + Assert.assertEquals("www.KOCH.rO".toLowerCase(Locale.ENGLISH), new DomainName("www.KOCH.rO").getAsString()); + Assert.assertEquals("faz.Net".toLowerCase(Locale.ENGLISH), new DomainName("faz.Net").getAsString()); + Assert.assertEquals("ÄÖ.mydomain.org".toLowerCase(Locale.ENGLISH), new DomainName("ÄÖ.mydomain.org").getAsString()); } @Test public void testGetAsReversedString() { - Assert.assertEquals("ro.koch.www", new DomainName("www.KOCH.rO").getAsReversedString()); - Assert.assertEquals("www.koch.ro", new DomainName("ro.koch.www").getAsReversedString()); - Assert.assertEquals("net.faz", new DomainName("faz.Net").getAsReversedString()); - Assert.assertEquals("faz.net", new DomainName("net.faz").getAsReversedString()); - Assert.assertEquals("org.mydomain.ao", new DomainName("ao.mydomain.org").getAsReversedString()); - Assert.assertEquals("ao.mydomain.org", new DomainName("org.mydomain.ao").getAsReversedString()); + Assert.assertEquals("ro.koch.www", new DomainName("www.KOCH.rO").getAsReversedString()); + Assert.assertEquals("www.koch.ro", new DomainName("ro.koch.www").getAsReversedString()); + Assert.assertEquals("net.faz", new DomainName("faz.Net").getAsReversedString()); + Assert.assertEquals("faz.net", new DomainName("net.faz").getAsReversedString()); + Assert.assertEquals("org.mydomain.ao", new DomainName("ao.mydomain.org").getAsReversedString()); + Assert.assertEquals("ao.mydomain.org", new DomainName("org.mydomain.ao").getAsReversedString()); } @Test public void testGetOptimizedForProximityOrder() { - Assert.assertEquals("ro.koch", new DomainName("www.KOCH.rO").getOptimizedForProximityOrder()); - Assert.assertEquals("ro.koch", new DomainName("WWW.KOCH.rO").getOptimizedForProximityOrder()); - Assert.assertEquals("net.faz", new DomainName("faz.Net").getOptimizedForProximityOrder()); - Assert.assertEquals("net.faz.ww", new DomainName("ww.faz.Net").getOptimizedForProximityOrder()); - Assert.assertEquals("net.faz.wwww", new DomainName("wwww.faz.Net").getOptimizedForProximityOrder()); - Assert.assertEquals("obscure.www.net", new DomainName("net.www.obscure").getOptimizedForProximityOrder()); + Assert.assertEquals("ro.koch", new DomainName("www.KOCH.rO").getOptimizedForProximityOrder(true)); + Assert.assertEquals("ro.koch", new DomainName("WWW.KOCH.rO").getOptimizedForProximityOrder(true)); + Assert.assertEquals("net.faz", new DomainName("faz.Net").getOptimizedForProximityOrder(true)); + Assert.assertEquals("net.faz.ww", new DomainName("ww.faz.Net").getOptimizedForProximityOrder(true)); + Assert.assertEquals("net.faz.wwww", new DomainName("wwww.faz.Net").getOptimizedForProximityOrder(true)); + Assert.assertEquals("obscure.www.net", new DomainName("net.www.obscure").getOptimizedForProximityOrder(true)); + + Assert.assertEquals("koch.ro", new DomainName("www.KOCH.rO").getOptimizedForProximityOrder(false)); + Assert.assertEquals("koch.ro", new DomainName("WWW.KOCH.rO").getOptimizedForProximityOrder(false)); + Assert.assertEquals("faz.net", new DomainName("faz.Net").getOptimizedForProximityOrder(false)); + Assert.assertEquals("ww.faz.net", new DomainName("ww.faz.Net").getOptimizedForProximityOrder(false)); + Assert.assertEquals("wwww.faz.net", new DomainName("wwww.faz.Net").getOptimizedForProximityOrder(false)); + Assert.assertEquals("net.www.obscure", new DomainName("net.www.obscure").getOptimizedForProximityOrder(false)); } @Test public void equalsShouldReturnTrue() { - final DomainName domain1 = new DomainName("gamespot.com"); - final DomainName domain2 = new DomainName("gamespot.com"); - Assert.assertTrue("equals should be true", domain1.equals(domain2)); + final DomainName domain1 = new DomainName("gamespot.com"); + final DomainName domain2 = new DomainName("gamespot.com"); + Assert.assertTrue("equals should be true", domain1.equals(domain2)); } @Test public void equalsShouldReturnFalse() { - final DomainName domain1 = new DomainName("gamespot.com"); - final DomainName domain2 = new DomainName("gamespot2.com"); - Assert.assertFalse("equals should be false", domain1.equals(domain2)); + final DomainName domain1 = new DomainName("gamespot.com"); + final DomainName domain2 = new DomainName("gamespot2.com"); + Assert.assertFalse("equals should be false", domain1.equals(domain2)); } } diff --git a/src/test/java/ch/sentric/EscapedFragmentEncoderTest.java b/src/test/java/ch/sentric/EscapedFragmentEncoderTest.java index c0b4d08..49aad36 100644 --- a/src/test/java/ch/sentric/EscapedFragmentEncoderTest.java +++ b/src/test/java/ch/sentric/EscapedFragmentEncoderTest.java @@ -15,13 +15,12 @@ */ package ch.sentric; -import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; - import junit.framework.Assert; - import org.junit.Test; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; + /** * The {@link EscapedFragmentEncoder} test class. */ @@ -29,40 +28,40 @@ public class EscapedFragmentEncoderTest { @Test public void encodeShouldNotChangeEquals() throws UnsupportedEncodingException { - assertEncoding("=", "="); + assertEncoding("=", "="); } @Test public void encodeShouldChangeAmp() throws UnsupportedEncodingException { - assertEncoding("&", "%26"); + assertEncoding("&", "%26"); } @Test public void encodeShouldChangeSpace() throws UnsupportedEncodingException { - assertEncoding(" ", "%20"); + assertEncoding(" ", "%20"); } @Test public void encodeShouldChangeHash() throws UnsupportedEncodingException { - assertEncoding("#", "%23"); + assertEncoding("#", "%23"); } @Test public void encodeShouldChangePercent() throws UnsupportedEncodingException { - assertEncoding("%", "%25"); + assertEncoding("%", "%25"); } @Test public void encodeShouldNotChangeExplanation() throws UnsupportedEncodingException { - assertEncoding("!", "!"); + assertEncoding("!", "!"); } @Test public void encodeShouldChangeFullString() throws UnsupportedEncodingException { - assertEncoding("key1=value1&key2=value2", "key1=value1%26key2=value2"); + assertEncoding("key1=value1&key2=value2", "key1=value1%26key2=value2"); } private void assertEncoding(final String from, final String to) throws UnsupportedEncodingException { - Assert.assertEquals(to, EscapedFragmentEncoder.encode(from, Charset.defaultCharset().toString())); + Assert.assertEquals(to, EscapedFragmentEncoder.encode(from, Charset.defaultCharset().toString())); } } diff --git a/src/test/java/ch/sentric/IPv4AddressTest.java b/src/test/java/ch/sentric/IPv4AddressTest.java index 8805404..5e2b5af 100644 --- a/src/test/java/ch/sentric/IPv4AddressTest.java +++ b/src/test/java/ch/sentric/IPv4AddressTest.java @@ -15,13 +15,14 @@ */ package ch.sentric; -import static org.junit.Assert.*; +import org.junit.Assert; +import org.junit.Test; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; /** * The {@link IPv4Address} test class. @@ -30,121 +31,121 @@ public class IPv4AddressTest { @Test public void testParseRegularIPv4AddressWorks() { - final ArrayList cases = new ArrayList(); - cases.add(new int[] { 10, 11, 12, 13 }); - cases.add(new int[] { 254, 11, 12, 13 }); - runParseTests(cases); + final ArrayList cases = new ArrayList(); + cases.add(new int[]{10, 11, 12, 13}); + cases.add(new int[]{254, 11, 12, 13}); + runParseTests(cases); } @Test public void testParseOneDigitIPv4AddressWorks() { - final ArrayList cases = new ArrayList(); - cases.add(new int[] { 1, 11, 12, 13 }); - cases.add(new int[] { 10, 1, 12, 13 }); - cases.add(new int[] { 10, 11, 1, 13 }); - cases.add(new int[] { 10, 11, 12, 1 }); - runParseTests(cases); + final ArrayList cases = new ArrayList(); + cases.add(new int[]{1, 11, 12, 13}); + cases.add(new int[]{10, 1, 12, 13}); + cases.add(new int[]{10, 11, 1, 13}); + cases.add(new int[]{10, 11, 12, 1}); + runParseTests(cases); } @Test public void testParseTooShortIPv4AddressFails() { - runFailingParseTests(new String[] { "10.11.12", "10.11.12." }); + runFailingParseTests(new String[]{"10.11.12", "10.11.12."}); } @Test public void testParseOutOfRangeIPv4AddressFails() { - runFailingParseTests(new String[] { "256.11.12.10", "10.256.12.13", "10.11.256.13", "10.11.12.256", "356.11.12.10", "10.356.12.13", "10.11.356.13", "10.11.12.356" }); + runFailingParseTests(new String[]{"256.11.12.10", "10.256.12.13", "10.11.256.13", "10.11.12.256", "356.11.12.10", "10.356.12.13", "10.11.356.13", "10.11.12.356"}); } @Test public void testParseTooLongIPv4AddressFails() { - runFailingParseTests(new String[] { "10.11.12.1.2", "10.11.12.1." }); + runFailingParseTests(new String[]{"10.11.12.1.2", "10.11.12.1."}); } @Test public void testParseNonTrimmedIPv4AddressFails() { - runFailingParseTests(new String[] { "10.11.12.13 ", " 10.11.12.13 ", " 10.11.12.13" }); + runFailingParseTests(new String[]{"10.11.12.13 ", " 10.11.12.13 ", " 10.11.12.13"}); } @Test public void testBuildRegularIPv4AddressesWorks() { - final ArrayList cases = new ArrayList(); - cases.add(new int[] { 1, 11, 12, 13 }); - cases.add(new int[] { 10, 1, 12, 13 }); - cases.add(new int[] { 10, 11, 1, 13 }); - cases.add(new int[] { 10, 11, 12, 1 }); - cases.add(new int[] { 89, 185, 254, 1 }); - cases.add(new int[] { 254, 185, 254, 1 }); - cases.add(new int[] { 255, 255, 255, 255 }); - cases.add(new int[] { 1, 1, 1, 1 }); - runBuildTests(cases); + final ArrayList cases = new ArrayList(); + cases.add(new int[]{1, 11, 12, 13}); + cases.add(new int[]{10, 1, 12, 13}); + cases.add(new int[]{10, 11, 1, 13}); + cases.add(new int[]{10, 11, 12, 1}); + cases.add(new int[]{89, 185, 254, 1}); + cases.add(new int[]{254, 185, 254, 1}); + cases.add(new int[]{255, 255, 255, 255}); + cases.add(new int[]{1, 1, 1, 1}); + runBuildTests(cases); } @Test public void testInstantiateOutOfRangeIpFails() { - try { - new IPv4Address(-1); - fail("IP address -1 is invalid"); - } catch (final Exception e) { - // expected - } - try { - new IPv4Address((long) Math.pow(2, 32)); - fail("IP address >=2^32 is invalid"); - } catch (final Exception e) { - // expected - } + try { + new IPv4Address(-1); + fail("IP address -1 is invalid"); + } catch (final Exception e) { + // expected + } + try { + new IPv4Address((long) Math.pow(2, 32)); + fail("IP address >=2^32 is invalid"); + } catch (final Exception e) { + // expected + } } @Test public void equalsShouldReturnTrue() { - final IPv4Address ip1 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.12")); - final IPv4Address ip2 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.12")); - Assert.assertTrue("equals should be true", ip1.equals(ip2)); + final IPv4Address ip1 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.12")); + final IPv4Address ip2 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.12")); + Assert.assertTrue("equals should be true", ip1.equals(ip2)); } @Test public void equalsShouldReturnFalse() { - final IPv4Address ip1 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.12")); - final IPv4Address ip2 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.13")); - Assert.assertFalse("equals should be false", ip1.equals(ip2)); + final IPv4Address ip1 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.12")); + final IPv4Address ip2 = new IPv4Address(IPv4Address.parseIPv4String("12.12.12.13")); + Assert.assertFalse("equals should be false", ip1.equals(ip2)); } private void runBuildTests(final ArrayList cases) { - for (final int[] c : cases) { - try { - final IPv4Address ip = new IPv4Address(calculateLong(c)); - assertEquals(buildIPv4(c), ip.getAsString()); - } catch (final Exception e) { - fail(buildIPv4(c) + " " + e); - } - } + for (final int[] c : cases) { + try { + final IPv4Address ip = new IPv4Address(calculateLong(c)); + assertEquals(buildIPv4(c), ip.getAsString()); + } catch (final Exception e) { + fail(buildIPv4(c) + " " + e); + } + } } private long calculateLong(final int[] numbers) { - int base = 1; - long result = 0; - for (int i = 3; i >= 0; --i) { - result += (long) numbers[i] * (long) base; - base *= 256; - } - return result; + int base = 1; + long result = 0; + for (int i = 3; i >= 0; --i) { + result += (long) numbers[i] * (long) base; + base *= 256; + } + return result; } private String buildIPv4(final int[] numbers) { - return numbers[0] + "." + numbers[1] + "." + numbers[2] + "." + numbers[3]; + return numbers[0] + "." + numbers[1] + "." + numbers[2] + "." + numbers[3]; } private void runParseTests(final List cases) { - for (final int[] c : cases) { - assertEquals(c.toString(), calculateLong(c), IPv4Address.parseIPv4String(buildIPv4(c))); - } + for (final int[] c : cases) { + assertEquals(c.toString(), calculateLong(c), IPv4Address.parseIPv4String(buildIPv4(c))); + } } private void runFailingParseTests(final String[] cases) { - for (final String c : cases) { - final long parseResult = IPv4Address.parseIPv4String(c); - assertEquals("String: <" + c + "> did not fail, gave " + parseResult, IPv4Address.ILLEGAL_IPV4, parseResult); - } + for (final String c : cases) { + final long parseResult = IPv4Address.parseIPv4String(c); + assertEquals("String: <" + c + "> did not fail, gave " + parseResult, IPv4Address.ILLEGAL_IPV4, parseResult); + } } } diff --git a/src/test/java/ch/sentric/PathTest.java b/src/test/java/ch/sentric/PathTest.java index 2324aea..0835cef 100644 --- a/src/test/java/ch/sentric/PathTest.java +++ b/src/test/java/ch/sentric/PathTest.java @@ -15,32 +15,43 @@ */ package ch.sentric; -import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * The {@link Path} test class. */ public class PathTest { @Test public void testGetAsString() { - Assert.assertEquals("/hello/world", new Path("/hello/world").getAsString()); + assertEquals("/hello/world", new Path("/hello/world").getAsString()); } @Test public void getAsStringShouldRemoveJsession() { - Assert.assertEquals("/1270777-1779518.html", new Path("/1270777-1779518.html;jsessionid=9ADD207E33B1E66CE6121BC73AADB986").getAsString()); + assertEquals("/1270777-1779518.html", new Path("/1270777-1779518.html;jsessionid=9ADD207E33B1E66CE6121BC73AADB986").getAsString()); } @Test public void getAsStringShouldReturnSemicolonSplittedPath() { - Assert.assertEquals("/wirtschaft/soziales/0;1518;769292;00.html", new Path("/wirtschaft/soziales/0;1518;769292;00.html").getAsString()); + assertEquals("/wirtschaft/soziales/0;1518;769292;00.html", new Path("/wirtschaft/soziales/0;1518;769292;00.html").getAsString()); } @Test public void getAsStringShouldNotRemovePhpsess() { - // this is a query, not a path - Assert.assertEquals("/1270777-1779518.html?phpsessid=9ADD207E33B1E66CE6121BC73AADB986", - new Path("/1270777-1779518.html?phpsessid=9ADD207E33B1E66CE6121BC73AADB986").getAsString()); + // this is a query, not a path + assertEquals("/1270777-1779518.html?phpsessid=9ADD207E33B1E66CE6121BC73AADB986", + new Path("/1270777-1779518.html?phpsessid=9ADD207E33B1E66CE6121BC73AADB986").getAsString()); + } + + @Test + public void getReEncoded() { + assertEquals("/test2", new Path("/test1/../test2").getReEncoded().getAsString()); + assertEquals("/test1/test2", new Path("/test1/./test2").getReEncoded().getAsString()); + assertEquals("/test1/test2", new Path("/test1/test2").getReEncoded().getAsString()); + assertEquals("", new Path("/").getReEncoded().getAsString()); + assertEquals("", new Path("").getReEncoded().getAsString()); + assertEquals("", new Path("///").getReEncoded().getAsString()); } } \ No newline at end of file diff --git a/src/test/java/ch/sentric/PercentCodecTest.java b/src/test/java/ch/sentric/PercentCodecTest.java index bef5ef6..6904b5b 100644 --- a/src/test/java/ch/sentric/PercentCodecTest.java +++ b/src/test/java/ch/sentric/PercentCodecTest.java @@ -27,30 +27,30 @@ public class PercentCodecTest { @Test public void testDecodeIgnoresIsolatedPercent() { - Assert.assertEquals("a%b", codec.decode("a%b")); - Assert.assertEquals("%", codec.decode("%")); - Assert.assertEquals("%%", codec.decode("%%")); - Assert.assertEquals("% %", codec.decode("% %")); - Assert.assertEquals("%% ", codec.decode("%% ")); - Assert.assertEquals("hallo%welt%wie%gehts", codec.decode("hallo%welt%wie%gehts")); - Assert.assertEquals(" % ", codec.decode("%20%%20")); + Assert.assertEquals("a%b", codec.decode("a%b")); + Assert.assertEquals("%", codec.decode("%")); + Assert.assertEquals("%%", codec.decode("%%")); + Assert.assertEquals("% %", codec.decode("% %")); + Assert.assertEquals("%% ", codec.decode("%% ")); + Assert.assertEquals("hallo%welt%wie%gehts", codec.decode("hallo%welt%wie%gehts")); + Assert.assertEquals(" % ", codec.decode("%20%%20")); } @Test public void testEncodeQueryComponent() { - Assert.assertEquals("%25%26%3b%3d%3a%3f%23", codec.encodeQueryComponent("%&;=:?#")); - Assert.assertEquals("h%25a%26l%3bl%3do%3a%3fw%23elt", codec.encodeQueryComponent("h%a&l;l=o:?w#elt")); - Assert.assertEquals("", codec.encodeQueryComponent("")); - Assert.assertEquals("/!'()@[],", codec.encodeQueryComponent("/!'()@[],")); + Assert.assertEquals("%25%26%3b%3d%3a%3f%23", codec.encodeQueryComponent("%&;=:?#")); + Assert.assertEquals("h%25a%26l%3bl%3do%3a%3fw%23elt", codec.encodeQueryComponent("h%a&l;l=o:?w#elt")); + Assert.assertEquals("", codec.encodeQueryComponent("")); + Assert.assertEquals("/!'()@[],", codec.encodeQueryComponent("/!'()@[],")); } @Test public void testEncodeWhiteSpace() { - Assert.assertEquals("+", codec.encodeQueryComponent(" ")); - Assert.assertEquals("+", codec.encodePathPart(" ")); - Assert.assertEquals("++", codec.encodeQueryComponent(" ")); - Assert.assertEquals("++", codec.encodePathPart(" ")); - Assert.assertEquals("a+a", codec.encodeQueryComponent("a a")); - Assert.assertEquals("a+a", codec.encodePathPart("a a")); + Assert.assertEquals("+", codec.encodeQueryComponent(" ")); + Assert.assertEquals("+", codec.encodePathPart(" ")); + Assert.assertEquals("++", codec.encodeQueryComponent(" ")); + Assert.assertEquals("++", codec.encodePathPart(" ")); + Assert.assertEquals("a+a", codec.encodeQueryComponent("a a")); + Assert.assertEquals("a+a", codec.encodePathPart("a a")); } } \ No newline at end of file diff --git a/src/test/java/ch/sentric/QueryFactoryTest.java b/src/test/java/ch/sentric/QueryFactoryTest.java index c9a870c..94e2563 100644 --- a/src/test/java/ch/sentric/QueryFactoryTest.java +++ b/src/test/java/ch/sentric/QueryFactoryTest.java @@ -15,102 +15,102 @@ */ package ch.sentric; -import static org.junit.Assert.*; - import org.apache.commons.lang3.StringUtils; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * The {@link QueryFactory} test class. */ public class QueryFactoryTest { @Test public void buildShouldWorkWithSimpleQueries() { - assertRoundTrip("a=b"); - assertRoundTrip("a1=b1"); - assertRoundTrip("aa=bb"); - assertRoundTrip("alles=klar"); + assertRoundTrip("a=b"); + assertRoundTrip("a1=b1"); + assertRoundTrip("aa=bb"); + assertRoundTrip("alles=klar"); } @Test public void buildShouldWorkWithMultipleParameterQueries() { - assertRoundTrip("a=b&b=c"); - assertRoundTrip("a1=b1&b1=c1"); - assertRoundTrip("aa=bb&bb=aa"); - assertRoundTrip("alles=klar&auf=der&andrea=doria"); + assertRoundTrip("a=b&b=c"); + assertRoundTrip("a1=b1&b1=c1"); + assertRoundTrip("aa=bb&bb=aa"); + assertRoundTrip("alles=klar&auf=der&andrea=doria"); } @Test public void buildSortQuery() { - final Query query = new QueryFactory().build("b=a&a=b"); - assertEquals("a=b&b=a", query.getAsSortedString()); + final Query query = new QueryFactory().build("b=a&a=b"); + assertEquals("a=b&b=a", query.getAsSortedString()); } @Test public void buildShouldIgnoreJessionId() { - final Query query = new QueryFactory().build("val=12&a=15&x=10;jsessionid=9ADD207E33B1E66CE6121BC73AADB986"); - assertEquals("val=12&a=15&x=10", query.getAsString()); + final Query query = new QueryFactory().build("val=12&a=15&x=10;jsessionid=9ADD207E33B1E66CE6121BC73AADB986"); + assertEquals("val=12&a=15&x=10", query.getAsString()); } @Test public void buildShouldIgnoreJessionIdWhenOrdering() { - final Query query = new QueryFactory().build("val=12&a=15&x=10;jsessionid=9ADD207E33B1E66CE6121BC73AADB986"); - assertEquals("a=15&val=12&x=10", query.getAsSortedString()); + final Query query = new QueryFactory().build("val=12&a=15&x=10;jsessionid=9ADD207E33B1E66CE6121BC73AADB986"); + assertEquals("a=15&val=12&x=10", query.getAsSortedString()); } @Test public void buildShouldReturnNothingWhenJessionIsTheOnlyParameter() { - final Query query = new QueryFactory().build("jsessionid=9ADD207E33B1E66CE6121BC73AADB986"); - assertEquals(StringUtils.EMPTY, query.getAsSortedString()); + final Query query = new QueryFactory().build("jsessionid=9ADD207E33B1E66CE6121BC73AADB986"); + assertEquals(StringUtils.EMPTY, query.getAsSortedString()); } @Test public void buildShouldReturnNothingWhenPhpsessidIsTheOnlyParameter() { - final Query query = new QueryFactory().build("phpsessid=9ADD207E33B1E66CE6121BC73AADB986"); - assertEquals(StringUtils.EMPTY, query.getAsSortedString()); + final Query query = new QueryFactory().build("phpsessid=9ADD207E33B1E66CE6121BC73AADB986"); + assertEquals(StringUtils.EMPTY, query.getAsSortedString()); } @Test public void buildShouldSortQueryWithEqualKeys() { - final Query query = new QueryFactory().build("a=b&a=a"); - assertEquals("a=a&a=b", query.getAsSortedString()); + final Query query = new QueryFactory().build("a=b&a=a"); + assertEquals("a=a&a=b", query.getAsSortedString()); } @Test public void buildShouldRemoveGoogleUrlTrackingParameter() { - final Query query = new QueryFactory().build("utm_campaign=Feed%3A+TheSouthwesternSunRss+%28The+Southwestern+Sun+RSS%29&utm_medium=feed&utm_source=feedburner"); - assertEquals(StringUtils.EMPTY, query.getAsSortedString()); + final Query query = new QueryFactory().build("utm_campaign=Feed%3A+TheSouthwesternSunRss+%28The+Southwestern+Sun+RSS%29&utm_medium=feed&utm_source=feedburner"); + assertEquals(StringUtils.EMPTY, query.getAsSortedString()); } @Test public void buildShouldRemoveGoogleGifRequestTrackingParameter() { - final Query query = new QueryFactory() - .build("utmwv=4&utmn=769876874&utmhn=example.com&utmcs=ISO-8859-1&utmsr=1280x1024&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=9.0%20%20r115&utmcn=1&utmdt=GATC012%20setting%20variables&utmhid=2059107202&utmr=0&utmp=/auto/GATC012.html?utm_source=www.gatc012.org&utm_campaign=campaign+gatc012&utm_term=keywords+gatc012&utm_content=content+gatc012&utm_medium=medium+gatc012&utmac=UA-30138-1&utmcc=__utma%3D97315849.1774621898.1207701397.1207701397.1207701397.1%3B... "); - assertEquals(StringUtils.EMPTY, query.getAsSortedString()); + final Query query = new QueryFactory() + .build("utmwv=4&utmn=769876874&utmhn=example.com&utmcs=ISO-8859-1&utmsr=1280x1024&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=9.0%20%20r115&utmcn=1&utmdt=GATC012%20setting%20variables&utmhid=2059107202&utmr=0&utmp=/auto/GATC012.html?utm_source=www.gatc012.org&utm_campaign=campaign+gatc012&utm_term=keywords+gatc012&utm_content=content+gatc012&utm_medium=medium+gatc012&utmac=UA-30138-1&utmcc=__utma%3D97315849.1774621898.1207701397.1207701397.1207701397.1%3B... "); + assertEquals(StringUtils.EMPTY, query.getAsSortedString()); } @Test public void buildShouldRemoveWebtrendsRequestTrackingParameter() { - Query query = new QueryFactory().build("WT.vtid=abcdefghij&a=b&WT.mc_id=EmailCampaign1&WT.si_n=name1;name2&WT.si_x=position1;position2."); - assertEquals("a=b", query.getAsSortedString()); - // but not lowercase - query = new QueryFactory().build("wt.vtid=abcdefghij&a=b"); - assertEquals("a=b&wt.vtid=abcdefghij", query.getAsSortedString()); + Query query = new QueryFactory().build("WT.vtid=abcdefghij&a=b&WT.mc_id=EmailCampaign1&WT.si_n=name1;name2&WT.si_x=position1;position2."); + assertEquals("a=b", query.getAsSortedString()); + // but not lowercase + query = new QueryFactory().build("wt.vtid=abcdefghij&a=b"); + assertEquals("a=b&wt.vtid=abcdefghij", query.getAsSortedString()); } @Test public void buildShouldRemoveYahooRequestTrackingParameter() { - Query query = new QueryFactory().build("OVRAW=cheap%20television&OVKEY=television&OVMTC=advanced&OVKWID=4317717511&OVADID=7306185511"); - assertEquals(StringUtils.EMPTY, query.getAsSortedString()); - query = new QueryFactory().build("YSMCAMPGID=123456&YSMADGRPID=654321"); - assertEquals(StringUtils.EMPTY, query.getAsSortedString()); - // but not lowercase - query = new QueryFactory().build("ysmcampgid=123456&YSMADGRPID=654321"); - assertEquals("ysmcampgid=123456", query.getAsSortedString()); + Query query = new QueryFactory().build("OVRAW=cheap%20television&OVKEY=television&OVMTC=advanced&OVKWID=4317717511&OVADID=7306185511"); + assertEquals(StringUtils.EMPTY, query.getAsSortedString()); + query = new QueryFactory().build("YSMCAMPGID=123456&YSMADGRPID=654321"); + assertEquals(StringUtils.EMPTY, query.getAsSortedString()); + // but not lowercase + query = new QueryFactory().build("ysmcampgid=123456&YSMADGRPID=654321"); + assertEquals("ysmcampgid=123456", query.getAsSortedString()); } private void assertRoundTrip(final String q) { - final Query query = new QueryFactory().build(q); - assertEquals(q, query.getAsString()); + final Query query = new QueryFactory().build(q); + assertEquals(q, query.getAsString()); } } diff --git a/src/test/java/ch/sentric/QueryKeyValuePairTest.java b/src/test/java/ch/sentric/QueryKeyValuePairTest.java index 4a0e3e4..427b689 100644 --- a/src/test/java/ch/sentric/QueryKeyValuePairTest.java +++ b/src/test/java/ch/sentric/QueryKeyValuePairTest.java @@ -23,10 +23,10 @@ */ public class QueryKeyValuePairTest { static final class Fixture { - static QueryKeyValuePair x = new QueryKeyValuePair("foo", "bar"); - static QueryKeyValuePair y = new QueryKeyValuePair("foo", "bar"); - static QueryKeyValuePair z = new QueryKeyValuePair("foo", "bar"); - static QueryKeyValuePair notx = new QueryKeyValuePair("bar", "foo"); + static QueryKeyValuePair x = new QueryKeyValuePair("foo", "bar"); + static QueryKeyValuePair y = new QueryKeyValuePair("foo", "bar"); + static QueryKeyValuePair z = new QueryKeyValuePair("foo", "bar"); + static QueryKeyValuePair notx = new QueryKeyValuePair("bar", "foo"); } @Test @@ -34,25 +34,23 @@ static final class Fixture { * A class is equal to itself. */ public void testEqualToSelf() { - Assert.assertTrue("Class equal to itself.", Fixture.x.equals(Fixture.x)); + Assert.assertTrue("Class equal to itself.", Fixture.x.equals(Fixture.x)); } /** * x.equals(WrongType) must return false; - * */ @Test public void testPassIncompatibleTypeIsFalse() { - Assert.assertFalse("Passing incompatible object to equals should return false", Fixture.x.equals("string")); + Assert.assertFalse("Passing incompatible object to equals should return false", Fixture.x.equals("string")); } /** * x.equals(null) must return false; - * */ @Test public void testNullReferenceIsFalse() { - Assert.assertFalse("Passing null to equals should return false", Fixture.x == null); + Assert.assertFalse("Passing null to equals should return false", Fixture.x == null); } /** @@ -61,8 +59,8 @@ public void testNullReferenceIsFalse() { */ @Test public void testEqualsIsReflexiveIsSymmetric() { - Assert.assertTrue("Reflexive test fail x,y", Fixture.x.equals(Fixture.y)); - Assert.assertTrue("Symmetric test fail y", Fixture.y.equals(Fixture.x)); + Assert.assertTrue("Reflexive test fail x,y", Fixture.x.equals(Fixture.y)); + Assert.assertTrue("Symmetric test fail y", Fixture.y.equals(Fixture.x)); } /** @@ -71,9 +69,9 @@ public void testEqualsIsReflexiveIsSymmetric() { */ @Test public void testEqualsIsTransitive() { - Assert.assertTrue("Transitive test fails x,y", Fixture.x.equals(Fixture.y)); - Assert.assertTrue("Transitive test fails y,z", Fixture.y.equals(Fixture.z)); - Assert.assertTrue("Transitive test fails x,z", Fixture.x.equals(Fixture.z)); + Assert.assertTrue("Transitive test fails x,y", Fixture.x.equals(Fixture.y)); + Assert.assertTrue("Transitive test fails y,z", Fixture.y.equals(Fixture.z)); + Assert.assertTrue("Transitive test fails x,z", Fixture.x.equals(Fixture.z)); } /** @@ -81,12 +79,12 @@ public void testEqualsIsTransitive() { */ @Test public void testEqualsIsConsistent() { - Assert.assertTrue("Consistent test fail x,y", Fixture.x.equals(Fixture.y)); - Assert.assertTrue("Consistent test fail x,y", Fixture.x.equals(Fixture.y)); - Assert.assertTrue("Consistent test fail x,y", Fixture.x.equals(Fixture.y)); - Assert.assertFalse(Fixture.notx.equals(Fixture.x)); - Assert.assertFalse(Fixture.notx.equals(Fixture.x)); - Assert.assertFalse(Fixture.notx.equals(Fixture.x)); + Assert.assertTrue("Consistent test fail x,y", Fixture.x.equals(Fixture.y)); + Assert.assertTrue("Consistent test fail x,y", Fixture.x.equals(Fixture.y)); + Assert.assertTrue("Consistent test fail x,y", Fixture.x.equals(Fixture.y)); + Assert.assertFalse(Fixture.notx.equals(Fixture.x)); + Assert.assertFalse(Fixture.notx.equals(Fixture.x)); + Assert.assertFalse(Fixture.notx.equals(Fixture.x)); } /** @@ -94,9 +92,9 @@ public void testEqualsIsConsistent() { */ @Test public void testHashcodeIsConsistent() { - final int initial_hashcode = Fixture.x.hashCode(); - Assert.assertEquals("Consistent hashcode test fails", initial_hashcode, Fixture.x.hashCode()); - Assert.assertEquals("Consistent hashcode test fails", initial_hashcode, Fixture.x.hashCode()); + final int initial_hashcode = Fixture.x.hashCode(); + Assert.assertEquals("Consistent hashcode test fails", initial_hashcode, Fixture.x.hashCode()); + Assert.assertEquals("Consistent hashcode test fails", initial_hashcode, Fixture.x.hashCode()); } /** @@ -105,20 +103,19 @@ public void testHashcodeIsConsistent() { */ @Test public void testHashcodeTwoEqualsObjectsProduceSameNumber() { - final int xhashcode = Fixture.x.hashCode(); - final int yhashcode = Fixture.y.hashCode(); - Assert.assertEquals("Equal object, return equal hashcode test fails", xhashcode, yhashcode); + final int xhashcode = Fixture.x.hashCode(); + final int yhashcode = Fixture.y.hashCode(); + Assert.assertEquals("Equal object, return equal hashcode test fails", xhashcode, yhashcode); } /** * A more optimal implementation of hashcode ensures that if the objects are * unequal different integers are produced. - * */ @Test public void testHashcodeTwoUnEqualObjectsProduceDifferentNumber() { - final int xhashcode = Fixture.x.hashCode(); - final int yhashcode = Fixture.notx.hashCode(); - Assert.assertTrue("Equal object, return unequal hashcode test fails", !(xhashcode == yhashcode)); + final int xhashcode = Fixture.x.hashCode(); + final int yhashcode = Fixture.notx.hashCode(); + Assert.assertTrue("Equal object, return unequal hashcode test fails", !(xhashcode == yhashcode)); } } diff --git a/src/test/java/ch/sentric/URLTest.java b/src/test/java/ch/sentric/URLTest.java index aa9f65d..96d8ffc 100644 --- a/src/test/java/ch/sentric/URLTest.java +++ b/src/test/java/ch/sentric/URLTest.java @@ -15,14 +15,16 @@ */ package ch.sentric; -import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; /** * The {@link URL} test class. @@ -43,196 +45,196 @@ public class URLTest { @Before public void setUp() throws Exception { - this.standardPlainUrl = new URL("http://www.domain.com/"); - this.standardUrlWithPort = new URL("http://www.domain.com:80"); - this.standardUrlWithSubDomain = new URL("http://test.domain.com/"); - this.urlWithSubDomainAndPath = new URL("http://test.domain.com/test/path"); - this.urlWithSpaceInPath = new URL("http://www.domain.com/tes t/pat h"); - this.urlWithSeveralSpacesInPath = new URL("http://www.domain.com/tes t/pat h"); - this.urlWithUnsortedQuery = new URL("http://www.domain.com/test/path.php?val=12&a=15&x=10"); - this.urlWithUnsortedQueryAndSpace = new URL("http://www.domain.com/te st/path.php?val=12&a=15&x=10"); - this.urlWithUserAndPass = new URL("http://user:pass@www.domain.com/test/path.php?val=12&a=15&x=10"); - this.urlWithHttpsSchemaAndFragment = new URL("https://www.domain.com/test/path.php?val=12&a=15&x=10#4"); - this.urlWithTrailingSlashInPathAndQuery = new URL("http://www.domain.com/test/path/?val=12&a=15&x=10"); + this.standardPlainUrl = new URL("http://www.domain.com/"); + this.standardUrlWithPort = new URL("http://www.domain.com:80"); + this.standardUrlWithSubDomain = new URL("http://test.domain.com/"); + this.urlWithSubDomainAndPath = new URL("http://test.domain.com/test/path"); + this.urlWithSpaceInPath = new URL("http://www.domain.com/tes t/pat h"); + this.urlWithSeveralSpacesInPath = new URL("http://www.domain.com/tes t/pat h"); + this.urlWithUnsortedQuery = new URL("http://www.domain.com/test/path.php?val=12&a=15&x=10"); + this.urlWithUnsortedQueryAndSpace = new URL("http://www.domain.com/te st/path.php?val=12&a=15&x=10"); + this.urlWithUserAndPass = new URL("http://user:pass@www.domain.com/test/path.php?val=12&a=15&x=10"); + this.urlWithHttpsSchemaAndFragment = new URL("https://www.domain.com/test/path.php?val=12&a=15&x=10#4"); + this.urlWithTrailingSlashInPathAndQuery = new URL("http://www.domain.com/test/path/?val=12&a=15&x=10"); } @Test public void testGetAuthority() { - assertEquals("www.domain.com", this.standardPlainUrl.getAuthority().getAsString()); - assertEquals("www.domain.com:80", this.standardUrlWithPort.getAuthority().getAsString()); - assertEquals("test.domain.com", this.standardUrlWithSubDomain.getAuthority().getAsString()); - assertEquals("test.domain.com", this.urlWithSubDomainAndPath.getAuthority().getAsString()); - assertEquals("www.domain.com", this.urlWithSpaceInPath.getAuthority().getAsString()); - assertEquals("www.domain.com", this.urlWithSeveralSpacesInPath.getAuthority().getAsString()); - assertEquals("www.domain.com", this.urlWithUnsortedQuery.getAuthority().getAsString()); - assertEquals("www.domain.com", this.urlWithUnsortedQueryAndSpace.getAuthority().getAsString()); - assertEquals("user:pass@www.domain.com", this.urlWithUserAndPass.getAuthority().getAsString()); - assertEquals("www.domain.com", this.urlWithHttpsSchemaAndFragment.getAuthority().getAsString()); - assertEquals("www.domain.com", this.urlWithTrailingSlashInPathAndQuery.getAuthority().getAsString()); + assertEquals("www.domain.com", this.standardPlainUrl.getAuthority().getAsString()); + assertEquals("www.domain.com:80", this.standardUrlWithPort.getAuthority().getAsString()); + assertEquals("test.domain.com", this.standardUrlWithSubDomain.getAuthority().getAsString()); + assertEquals("test.domain.com", this.urlWithSubDomainAndPath.getAuthority().getAsString()); + assertEquals("www.domain.com", this.urlWithSpaceInPath.getAuthority().getAsString()); + assertEquals("www.domain.com", this.urlWithSeveralSpacesInPath.getAuthority().getAsString()); + assertEquals("www.domain.com", this.urlWithUnsortedQuery.getAuthority().getAsString()); + assertEquals("www.domain.com", this.urlWithUnsortedQueryAndSpace.getAuthority().getAsString()); + assertEquals("user:pass@www.domain.com", this.urlWithUserAndPass.getAuthority().getAsString()); + assertEquals("www.domain.com", this.urlWithHttpsSchemaAndFragment.getAuthority().getAsString()); + assertEquals("www.domain.com", this.urlWithTrailingSlashInPathAndQuery.getAuthority().getAsString()); } @Test public void testGetFragment() { - assertEquals(null, this.standardPlainUrl.getFragment()); - assertEquals(null, this.standardUrlWithPort.getFragment()); - assertEquals(null, this.standardUrlWithSubDomain.getFragment()); - assertEquals(null, this.urlWithSubDomainAndPath.getFragment()); - assertEquals(null, this.urlWithSpaceInPath.getFragment()); - assertEquals(null, this.urlWithSeveralSpacesInPath.getFragment()); - assertEquals(null, this.urlWithUnsortedQuery.getFragment()); - assertEquals(null, this.urlWithUnsortedQueryAndSpace.getFragment()); - assertEquals(null, this.urlWithUserAndPass.getFragment()); - assertEquals("4", this.urlWithHttpsSchemaAndFragment.getFragment()); - assertEquals(null, this.urlWithTrailingSlashInPathAndQuery.getFragment()); + assertEquals(null, this.standardPlainUrl.getFragment()); + assertEquals(null, this.standardUrlWithPort.getFragment()); + assertEquals(null, this.standardUrlWithSubDomain.getFragment()); + assertEquals(null, this.urlWithSubDomainAndPath.getFragment()); + assertEquals(null, this.urlWithSpaceInPath.getFragment()); + assertEquals(null, this.urlWithSeveralSpacesInPath.getFragment()); + assertEquals(null, this.urlWithUnsortedQuery.getFragment()); + assertEquals(null, this.urlWithUnsortedQueryAndSpace.getFragment()); + assertEquals(null, this.urlWithUserAndPass.getFragment()); + assertEquals("4", this.urlWithHttpsSchemaAndFragment.getFragment()); + assertEquals(null, this.urlWithTrailingSlashInPathAndQuery.getFragment()); } @Test public void testGetGivenInputUrl() { - assertEquals("http://www.domain.com/", this.standardPlainUrl.getGivenInputUrl()); - assertEquals("http://www.domain.com:80", this.standardUrlWithPort.getGivenInputUrl()); - assertEquals("http://test.domain.com/", this.standardUrlWithSubDomain.getGivenInputUrl()); - assertEquals("http://test.domain.com/test/path", this.urlWithSubDomainAndPath.getGivenInputUrl()); - assertEquals("http://www.domain.com/tes t/pat h", this.urlWithSpaceInPath.getGivenInputUrl()); - assertEquals("http://www.domain.com/tes t/pat h", this.urlWithSeveralSpacesInPath.getGivenInputUrl()); - assertEquals("http://www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUnsortedQuery.getGivenInputUrl()); - assertEquals("http://www.domain.com/te st/path.php?val=12&a=15&x=10", this.urlWithUnsortedQueryAndSpace.getGivenInputUrl()); - assertEquals("http://user:pass@www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUserAndPass.getGivenInputUrl()); - assertEquals("https://www.domain.com/test/path.php?val=12&a=15&x=10#4", this.urlWithHttpsSchemaAndFragment.getGivenInputUrl()); - assertEquals("http://www.domain.com/test/path/?val=12&a=15&x=10", this.urlWithTrailingSlashInPathAndQuery.getGivenInputUrl()); + assertEquals("http://www.domain.com/", this.standardPlainUrl.getGivenInputUrl()); + assertEquals("http://www.domain.com:80", this.standardUrlWithPort.getGivenInputUrl()); + assertEquals("http://test.domain.com/", this.standardUrlWithSubDomain.getGivenInputUrl()); + assertEquals("http://test.domain.com/test/path", this.urlWithSubDomainAndPath.getGivenInputUrl()); + assertEquals("http://www.domain.com/tes t/pat h", this.urlWithSpaceInPath.getGivenInputUrl()); + assertEquals("http://www.domain.com/tes t/pat h", this.urlWithSeveralSpacesInPath.getGivenInputUrl()); + assertEquals("http://www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUnsortedQuery.getGivenInputUrl()); + assertEquals("http://www.domain.com/te st/path.php?val=12&a=15&x=10", this.urlWithUnsortedQueryAndSpace.getGivenInputUrl()); + assertEquals("http://user:pass@www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUserAndPass.getGivenInputUrl()); + assertEquals("https://www.domain.com/test/path.php?val=12&a=15&x=10#4", this.urlWithHttpsSchemaAndFragment.getGivenInputUrl()); + assertEquals("http://www.domain.com/test/path/?val=12&a=15&x=10", this.urlWithTrailingSlashInPathAndQuery.getGivenInputUrl()); } @Test public void getURIShouldReturnUriFromGivenUrl() throws URISyntaxException { - assertEquals(this.standardPlainUrl.getGivenInputUrl(), this.standardPlainUrl.getURI().toString()); + assertEquals(this.standardPlainUrl.getGivenInputUrl(), this.standardPlainUrl.getURI().toString()); } @Test public void testGetNormalizedUrl() { - assertEquals("com.domain", this.standardPlainUrl.getNormalizedUrl()); - assertEquals("com.domain", this.standardUrlWithPort.getNormalizedUrl()); - assertEquals("com.domain.test", this.standardUrlWithSubDomain.getNormalizedUrl()); - assertEquals("com.domain.test/test/path", this.urlWithSubDomainAndPath.getNormalizedUrl()); - assertEquals("com.domain/tes+t/pat+h", this.urlWithSpaceInPath.getNormalizedUrl()); - assertEquals("com.domain/tes++++t/pat+h", this.urlWithSeveralSpacesInPath.getNormalizedUrl()); - assertEquals("com.domain/test/path.php?a=15&val=12&x=10", this.urlWithUnsortedQuery.getNormalizedUrl()); - assertEquals("com.domain/te+st/path.php?a=15&val=12&x=10", this.urlWithUnsortedQueryAndSpace.getNormalizedUrl()); - assertEquals("com.domain/test/path.php?a=15&val=12&x=10", this.urlWithUserAndPass.getNormalizedUrl()); - assertEquals("com.domain/test/path?a=15&val=12&x=10", this.urlWithTrailingSlashInPathAndQuery.getNormalizedUrl()); + assertEquals("com.domain", this.standardPlainUrl.getNormalizedUrl()); + assertEquals("com.domain", this.standardUrlWithPort.getNormalizedUrl()); + assertEquals("com.domain.test", this.standardUrlWithSubDomain.getNormalizedUrl()); + assertEquals("com.domain.test/test/path", this.urlWithSubDomainAndPath.getNormalizedUrl()); + assertEquals("com.domain/tes+t/pat+h", this.urlWithSpaceInPath.getNormalizedUrl()); + assertEquals("com.domain/tes++++t/pat+h", this.urlWithSeveralSpacesInPath.getNormalizedUrl()); + assertEquals("com.domain/test/path.php?a=15&val=12&x=10", this.urlWithUnsortedQuery.getNormalizedUrl()); + assertEquals("com.domain/te+st/path.php?a=15&val=12&x=10", this.urlWithUnsortedQueryAndSpace.getNormalizedUrl()); + assertEquals("com.domain/test/path.php?a=15&val=12&x=10", this.urlWithUserAndPass.getNormalizedUrl()); + assertEquals("com.domain/test/path?a=15&val=12&x=10", this.urlWithTrailingSlashInPathAndQuery.getNormalizedUrl()); } @Test public void testGetPath() { - assertEquals("", this.standardPlainUrl.getPath().getAsString()); - assertEquals("", this.standardUrlWithPort.getPath().getAsString()); - assertEquals("", this.standardUrlWithSubDomain.getPath().getAsString()); - assertEquals("/test/path", this.urlWithSubDomainAndPath.getPath().getAsString()); - assertEquals("/tes t/pat h", this.urlWithSpaceInPath.getPath().getAsString()); - assertEquals("/tes t/pat h", this.urlWithSeveralSpacesInPath.getPath().getAsString()); - assertEquals("/test/path.php", this.urlWithUnsortedQuery.getPath().getAsString()); - assertEquals("/te st/path.php", this.urlWithUnsortedQueryAndSpace.getPath().getAsString()); - assertEquals("/test/path.php", this.urlWithUserAndPass.getPath().getAsString()); - assertEquals("/test/path.php", this.urlWithHttpsSchemaAndFragment.getPath().getAsString()); - assertEquals("/test/path", this.urlWithTrailingSlashInPathAndQuery.getPath().getAsString()); + assertEquals("", this.standardPlainUrl.getPath().getAsString()); + assertEquals("", this.standardUrlWithPort.getPath().getAsString()); + assertEquals("", this.standardUrlWithSubDomain.getPath().getAsString()); + assertEquals("/test/path", this.urlWithSubDomainAndPath.getPath().getAsString()); + assertEquals("/tes t/pat h", this.urlWithSpaceInPath.getPath().getAsString()); + assertEquals("/tes t/pat h", this.urlWithSeveralSpacesInPath.getPath().getAsString()); + assertEquals("/test/path.php", this.urlWithUnsortedQuery.getPath().getAsString()); + assertEquals("/te st/path.php", this.urlWithUnsortedQueryAndSpace.getPath().getAsString()); + assertEquals("/test/path.php", this.urlWithUserAndPass.getPath().getAsString()); + assertEquals("/test/path.php", this.urlWithHttpsSchemaAndFragment.getPath().getAsString()); + assertEquals("/test/path", this.urlWithTrailingSlashInPathAndQuery.getPath().getAsString()); } @Test public void testGetQuery() { - assertEquals("", this.standardPlainUrl.getQuery().getAsString()); - assertEquals("", this.standardUrlWithPort.getQuery().getAsString()); - assertEquals("", this.standardUrlWithSubDomain.getQuery().getAsString()); - assertEquals("", this.urlWithSubDomainAndPath.getQuery().getAsString()); - assertEquals("", this.urlWithSpaceInPath.getQuery().getAsString()); - assertEquals("", this.urlWithSeveralSpacesInPath.getQuery().getAsString()); - assertEquals("val=12&a=15&x=10", this.urlWithUnsortedQuery.getQuery().getAsString()); - assertEquals("a=15&val=12&x=10", this.urlWithUnsortedQuery.getQuery().getAsSortedString()); - assertEquals("val=12&a=15&x=10", this.urlWithUnsortedQueryAndSpace.getQuery().getAsString()); - assertEquals("a=15&val=12&x=10", this.urlWithUnsortedQueryAndSpace.getQuery().getAsSortedString()); - assertEquals("val=12&a=15&x=10", this.urlWithUserAndPass.getQuery().getAsString()); - assertEquals("a=15&val=12&x=10", this.urlWithUserAndPass.getQuery().getAsSortedString()); - assertEquals("val=12&a=15&x=10", this.urlWithHttpsSchemaAndFragment.getQuery().getAsString()); - assertEquals("a=15&val=12&x=10", this.urlWithHttpsSchemaAndFragment.getQuery().getAsSortedString()); - assertEquals("val=12&a=15&x=10", this.urlWithTrailingSlashInPathAndQuery.getQuery().getAsString()); - assertEquals("a=15&val=12&x=10", this.urlWithTrailingSlashInPathAndQuery.getQuery().getAsSortedString()); + assertEquals("", this.standardPlainUrl.getQuery().getAsString()); + assertEquals("", this.standardUrlWithPort.getQuery().getAsString()); + assertEquals("", this.standardUrlWithSubDomain.getQuery().getAsString()); + assertEquals("", this.urlWithSubDomainAndPath.getQuery().getAsString()); + assertEquals("", this.urlWithSpaceInPath.getQuery().getAsString()); + assertEquals("", this.urlWithSeveralSpacesInPath.getQuery().getAsString()); + assertEquals("val=12&a=15&x=10", this.urlWithUnsortedQuery.getQuery().getAsString()); + assertEquals("a=15&val=12&x=10", this.urlWithUnsortedQuery.getQuery().getAsSortedString()); + assertEquals("val=12&a=15&x=10", this.urlWithUnsortedQueryAndSpace.getQuery().getAsString()); + assertEquals("a=15&val=12&x=10", this.urlWithUnsortedQueryAndSpace.getQuery().getAsSortedString()); + assertEquals("val=12&a=15&x=10", this.urlWithUserAndPass.getQuery().getAsString()); + assertEquals("a=15&val=12&x=10", this.urlWithUserAndPass.getQuery().getAsSortedString()); + assertEquals("val=12&a=15&x=10", this.urlWithHttpsSchemaAndFragment.getQuery().getAsString()); + assertEquals("a=15&val=12&x=10", this.urlWithHttpsSchemaAndFragment.getQuery().getAsSortedString()); + assertEquals("val=12&a=15&x=10", this.urlWithTrailingSlashInPathAndQuery.getQuery().getAsString()); + assertEquals("a=15&val=12&x=10", this.urlWithTrailingSlashInPathAndQuery.getQuery().getAsSortedString()); } @Test public void testGetRepairedUrl() { - assertEquals("http://www.domain.com", this.standardPlainUrl.getRepairedUrl()); - assertEquals("http://www.domain.com:80", this.standardUrlWithPort.getRepairedUrl()); - assertEquals("http://test.domain.com", this.standardUrlWithSubDomain.getRepairedUrl()); - assertEquals("http://test.domain.com/test/path", this.urlWithSubDomainAndPath.getRepairedUrl()); - assertEquals("http://www.domain.com/tes+t/pat+h", this.urlWithSpaceInPath.getRepairedUrl()); - assertEquals("http://www.domain.com/tes++++t/pat+h", this.urlWithSeveralSpacesInPath.getRepairedUrl()); - assertEquals("http://www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUnsortedQuery.getRepairedUrl()); - assertEquals("http://www.domain.com/te+st/path.php?val=12&a=15&x=10", this.urlWithUnsortedQueryAndSpace.getRepairedUrl()); - assertEquals("http://user:pass@www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUserAndPass.getRepairedUrl()); - assertEquals("https://www.domain.com/test/path.php?val=12&a=15&x=10#4", this.urlWithHttpsSchemaAndFragment.getRepairedUrl()); - assertEquals("http://www.domain.com/test/path?val=12&a=15&x=10", this.urlWithTrailingSlashInPathAndQuery.getRepairedUrl()); + assertEquals("http://www.domain.com", this.standardPlainUrl.getRepairedUrl()); + assertEquals("http://www.domain.com:80", this.standardUrlWithPort.getRepairedUrl()); + assertEquals("http://test.domain.com", this.standardUrlWithSubDomain.getRepairedUrl()); + assertEquals("http://test.domain.com/test/path", this.urlWithSubDomainAndPath.getRepairedUrl()); + assertEquals("http://www.domain.com/tes+t/pat+h", this.urlWithSpaceInPath.getRepairedUrl()); + assertEquals("http://www.domain.com/tes++++t/pat+h", this.urlWithSeveralSpacesInPath.getRepairedUrl()); + assertEquals("http://www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUnsortedQuery.getRepairedUrl()); + assertEquals("http://www.domain.com/te+st/path.php?val=12&a=15&x=10", this.urlWithUnsortedQueryAndSpace.getRepairedUrl()); + assertEquals("http://user:pass@www.domain.com/test/path.php?val=12&a=15&x=10", this.urlWithUserAndPass.getRepairedUrl()); + assertEquals("https://www.domain.com/test/path.php?val=12&a=15&x=10#4", this.urlWithHttpsSchemaAndFragment.getRepairedUrl()); + assertEquals("http://www.domain.com/test/path?val=12&a=15&x=10", this.urlWithTrailingSlashInPathAndQuery.getRepairedUrl()); } @Test public void testGetScheme() { - assertEquals("http", this.standardPlainUrl.getScheme()); - assertEquals("http", this.standardUrlWithPort.getScheme()); - assertEquals("http", this.standardUrlWithSubDomain.getScheme()); - assertEquals("http", this.urlWithSubDomainAndPath.getScheme()); - assertEquals("http", this.urlWithSpaceInPath.getScheme()); - assertEquals("http", this.urlWithSeveralSpacesInPath.getScheme()); - assertEquals("http", this.urlWithUnsortedQuery.getScheme()); - assertEquals("http", this.urlWithUnsortedQueryAndSpace.getScheme()); - assertEquals("http", this.urlWithUserAndPass.getScheme()); - assertEquals("https", this.urlWithHttpsSchemaAndFragment.getScheme()); + assertEquals("http", this.standardPlainUrl.getScheme()); + assertEquals("http", this.standardUrlWithPort.getScheme()); + assertEquals("http", this.standardUrlWithSubDomain.getScheme()); + assertEquals("http", this.urlWithSubDomainAndPath.getScheme()); + assertEquals("http", this.urlWithSpaceInPath.getScheme()); + assertEquals("http", this.urlWithSeveralSpacesInPath.getScheme()); + assertEquals("http", this.urlWithUnsortedQuery.getScheme()); + assertEquals("http", this.urlWithUnsortedQueryAndSpace.getScheme()); + assertEquals("http", this.urlWithUserAndPass.getScheme()); + assertEquals("https", this.urlWithHttpsSchemaAndFragment.getScheme()); } @Test public void uriConstructor() throws MalformedURLException, URISyntaxException { - this.standardPlainUrl = new URL(new URI("http://www.domain.com/")); - assertEquals("http://www.domain.com/", this.standardPlainUrl.getGivenInputUrl()); - assertEquals("http://www.domain.com", this.standardPlainUrl.getRepairedUrl()); - assertEquals("", this.standardPlainUrl.getQuery().getAsString()); - assertEquals("", this.standardPlainUrl.getPath().getAsString()); - assertEquals("com.domain", this.standardPlainUrl.getNormalizedUrl()); - assertEquals(null, this.standardPlainUrl.getFragment()); - assertEquals("www.domain.com", this.standardPlainUrl.getAuthority().getAsString()); + this.standardPlainUrl = new URL(new URI("http://www.domain.com/")); + assertEquals("http://www.domain.com/", this.standardPlainUrl.getGivenInputUrl()); + assertEquals("http://www.domain.com", this.standardPlainUrl.getRepairedUrl()); + assertEquals("", this.standardPlainUrl.getQuery().getAsString()); + assertEquals("", this.standardPlainUrl.getPath().getAsString()); + assertEquals("com.domain", this.standardPlainUrl.getNormalizedUrl()); + assertEquals(null, this.standardPlainUrl.getFragment()); + assertEquals("www.domain.com", this.standardPlainUrl.getAuthority().getAsString()); } @Test public void getUriShouldRemoveAllFragments() throws URISyntaxException, MalformedURLException { - try { - final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html#BLREL-1124499-1-2010/11#ref=rss").getURI(); - assertNull(uri.getFragment()); - } catch (final URISyntaxException e) { - fail("getUri() should not throw URISyntaxException when url contains two fragments"); - } + try { + final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html#BLREL-1124499-1-2010/11#ref=rss").getURI(); + assertNull(uri.getFragment()); + } catch (final URISyntaxException e) { + fail("getUri() should not throw URISyntaxException when url contains two fragments"); + } } @Test public void getUriShouldNotThrowEceptionWhenUrlContainsMoreThanOneFragment() throws URISyntaxException, MalformedURLException { - try { - final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html#BLREL-1124499-1-2010/11#ref=rss#article_38873#feed_383882").getURI(); - assertNull(uri.getFragment()); - } catch (final URISyntaxException e) { - fail("getUri() should not throw URISyntaxException when url contains more than one fragment"); - } + try { + final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html#BLREL-1124499-1-2010/11#ref=rss#article_38873#feed_383882").getURI(); + assertNull(uri.getFragment()); + } catch (final URISyntaxException e) { + fail("getUri() should not throw URISyntaxException when url contains more than one fragment"); + } } @Test public void getUriShouldNotThrowEceptionWhenUrlContainsOneFragment() throws URISyntaxException, MalformedURLException { - try { - final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html#BLREL-1124499-1-2010/11").getURI(); - assertNull(uri.getFragment()); - } catch (final URISyntaxException e) { - fail("getUri() should not throw URISyntaxException when url contains one fragment"); - } + try { + final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html#BLREL-1124499-1-2010/11").getURI(); + assertNull(uri.getFragment()); + } catch (final URISyntaxException e) { + fail("getUri() should not throw URISyntaxException when url contains one fragment"); + } } @Test public void getUriShouldNotThrowEceptionWhenUrlContainsNoFragment() throws URISyntaxException, MalformedURLException { - try { - final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html").getURI(); - assertNull("getFragment() expected to be null when url does not contain a '#'", uri.getFragment()); - } catch (final URISyntaxException e) { - fail("getUri() should not throw URISyntaxException when url contains no fragment"); - } + try { + final URI uri = new URL("http://www.spiegel.de/sport/fussball/0,1518,558100,00.html").getURI(); + assertNull("getFragment() expected to be null when url does not contain a '#'", uri.getFragment()); + } catch (final URISyntaxException e) { + fail("getUri() should not throw URISyntaxException when url contains no fragment"); + } } } diff --git a/src/test/java/ch/sentric/UrlUtilTest.java b/src/test/java/ch/sentric/UrlUtilTest.java index 46062c9..f8df199 100644 --- a/src/test/java/ch/sentric/UrlUtilTest.java +++ b/src/test/java/ch/sentric/UrlUtilTest.java @@ -15,12 +15,14 @@ */ package ch.sentric; -import static org.junit.Assert.*; +import org.junit.Test; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Test-Class for the {@link UrlUtil}. @@ -29,183 +31,183 @@ public class UrlUtilTest { @Test public void siteToTopLevelShouldReturnTopLevelDomain() { - assertEquals("org.wikipedia", UrlUtil.siteToTopLevel("org.wikipedia.de.wwww")); + assertEquals("org.wikipedia", UrlUtil.siteToTopLevel("org.wikipedia.de.wwww")); } @Test public void siteToTopLevelShouldReturnSame() { - assertEquals("wikipedia", UrlUtil.siteToTopLevel("wikipedia")); + assertEquals("wikipedia", UrlUtil.siteToTopLevel("wikipedia")); } @Test public void siteToTopLevelShouldReturnSameIp() { - assertEquals("1.2.3.4", UrlUtil.siteToTopLevel("1.2.3.4")); + assertEquals("1.2.3.4", UrlUtil.siteToTopLevel("1.2.3.4")); } @Test public void siteToTopLevelShouldReturnRealDomain() { - assertEquals("uk.co.bbc", UrlUtil.siteToTopLevel("uk.co.bbc.subdomain.www")); + assertEquals("uk.co.bbc", UrlUtil.siteToTopLevel("uk.co.bbc.subdomain.www")); } @Test public void siteToTopLevelShouldReturnRealDomain2() { - assertEquals("museum.trolley.newyork", UrlUtil.siteToTopLevel("museum.trolley.newyork.centralpark.www")); + assertEquals("museum.trolley.newyork", UrlUtil.siteToTopLevel("museum.trolley.newyork.centralpark.www")); } @Test public void siteToTopLevelShouldReturnRealDomain3() { - assertEquals("com.livefilestore.bay.sanfrancisco", UrlUtil.siteToTopLevel("com.livefilestore.bay.sanfrancisco.www")); + assertEquals("com.livefilestore.bay.sanfrancisco", UrlUtil.siteToTopLevel("com.livefilestore.bay.sanfrancisco.www")); } @Test public void isEscapedFragmentableShouldReturnTrue() throws MalformedURLException { - assertTrue(UrlUtil.isEscapeFragmentableUrl(new URL("http://some-request-url/test1#!my-request"))); + assertTrue(UrlUtil.isEscapeFragmentableUrl(new URL("http://some-request-url/test1#!my-request"))); } @Test public void isEscapedFragmentableShouldReturnFalse() throws MalformedURLException { - assertFalse(UrlUtil.isEscapeFragmentableUrl(new URL("http://some-request-url/test1#someparamateer=somevalue"))); + assertFalse(UrlUtil.isEscapeFragmentableUrl(new URL("http://some-request-url/test1#someparamateer=somevalue"))); } @Test public void isEscapedFragmentShouldReturnTrue() throws MalformedURLException { - assertTrue(UrlUtil.isEscapeFragmentUrl(new URL("http://some-request-url/test1?_escaped_fragment_=my-request"))); + assertTrue(UrlUtil.isEscapeFragmentUrl(new URL("http://some-request-url/test1?_escaped_fragment_=my-request"))); } @Test public void isEscapedFragmentShouldReturnFalse() throws MalformedURLException { - assertFalse(UrlUtil.isEscapeFragmentUrl(new URL("http://some-request-url/test1?someparamateer=somevalue"))); + assertFalse(UrlUtil.isEscapeFragmentUrl(new URL("http://some-request-url/test1?someparamateer=somevalue"))); } @Test public void toEscapedFragmentUrlShouldReturnFragmentUrl() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1#!my-request"); - final URL fragmentUrl = new URL("http://some-request-url/test1?_escaped_fragment_=my-request"); - assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); + final URL url = new URL("http://some-request-url/test1#!my-request"); + final URL fragmentUrl = new URL("http://some-request-url/test1?_escaped_fragment_=my-request"); + assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); } @Test public void fromEscapedFragmentUrlShouldReturnDefaultUrl() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1#!my-request"); - final URL fragmentUrl = new URL("http://some-request-url/test1?_escaped_fragment_=my-request"); - assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); + final URL url = new URL("http://some-request-url/test1#!my-request"); + final URL fragmentUrl = new URL("http://some-request-url/test1?_escaped_fragment_=my-request"); + assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); } @Test public void toEscapedFragmentUrlShouldReturnFragmentUrl2() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1?test2=test2#!my-request"); - final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&_escaped_fragment_=my-request"); - assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); + final URL url = new URL("http://some-request-url/test1?test2=test2#!my-request"); + final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&_escaped_fragment_=my-request"); + assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); } @Test public void fromEscapedFragmentUrlShouldReturnDefaultUrl2() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1?test2=test2#!my-request"); - final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&_escaped_fragment_=my-request"); - assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); + final URL url = new URL("http://some-request-url/test1?test2=test2#!my-request"); + final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&_escaped_fragment_=my-request"); + assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); } @Test public void toEscapedFragmentUrlShouldReturnFragmentUrl3() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1?test2=test2&test3=test3#!my-request"); - final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&test3=test3&_escaped_fragment_=my-request"); - assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); + final URL url = new URL("http://some-request-url/test1?test2=test2&test3=test3#!my-request"); + final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&test3=test3&_escaped_fragment_=my-request"); + assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); } @Test public void fromEscapedFragmentUrlShouldReturnDefaultUrl3() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1?test2=test2&test3=test3#!my-request"); - final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&test3=test3&_escaped_fragment_=my-request"); - assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); + final URL url = new URL("http://some-request-url/test1?test2=test2&test3=test3#!my-request"); + final URL fragmentUrl = new URL("http://some-request-url/test1?test2=test2&test3=test3&_escaped_fragment_=my-request"); + assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); } @Test public void toEscapedFragmentUrlShouldReturnFragmentUrl4() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1?#!my-request&key=value"); - final URL fragmentUrl = new URL("http://some-request-url/test1?&_escaped_fragment_=my-request%26key=value"); - assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); + final URL url = new URL("http://some-request-url/test1?#!my-request&key=value"); + final URL fragmentUrl = new URL("http://some-request-url/test1?&_escaped_fragment_=my-request%26key=value"); + assertEquals(fragmentUrl, UrlUtil.toEscapedFragmentUrl(url)); } @Test public void fromEscapedFragmentUrlShouldReturnDefaultUrl4() throws MalformedURLException, UnsupportedEncodingException { - final URL url = new URL("http://some-request-url/test1?#!my-request&key=value"); - final URL fragmentUrl = new URL("http://some-request-url/test1?&_escaped_fragment_=my-request%26key=value"); - assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); + final URL url = new URL("http://some-request-url/test1?#!my-request&key=value"); + final URL fragmentUrl = new URL("http://some-request-url/test1?&_escaped_fragment_=my-request%26key=value"); + assertEquals(url, UrlUtil.fromEscapedFragmentUrl(fragmentUrl)); } @Test public void subdomainShouldReturnCorrectValue1() throws MalformedURLException { - final URL url = new URL("http://news.google.co.uk?#!my-request&key=value"); - assertEquals("news", UrlUtil.subDomain(url)); + final URL url = new URL("http://news.google.co.uk?#!my-request&key=value"); + assertEquals("news", UrlUtil.subDomain(url)); } @Test public void subdomainShouldReturnCorrectValue2() throws MalformedURLException { - final URL url = new URL("http://news.germany.google.co.uk?#!my-request&key=value"); - assertEquals("news.germany", UrlUtil.subDomain(url)); + final URL url = new URL("http://news.germany.google.co.uk?#!my-request&key=value"); + assertEquals("news.germany", UrlUtil.subDomain(url)); } @Test public void subdomainShouldReturnCorrectValue3() throws MalformedURLException { - final URL url = new URL("http://slavigru.univie.ac.at/news"); - assertEquals("slavigru", UrlUtil.subDomain(url)); + final URL url = new URL("http://slavigru.univie.ac.at/news"); + assertEquals("slavigru", UrlUtil.subDomain(url)); } @Test public void subdomainShouldReturnCorrectValue4() throws MalformedURLException { - final URL url = new URL("http://www.seminarraum.co.at/info"); - assertEquals("www", UrlUtil.subDomain(url)); + final URL url = new URL("http://www.seminarraum.co.at/info"); + assertEquals("www", UrlUtil.subDomain(url)); } @Test public void subdomainShouldReturnCorrectValue5() throws MalformedURLException { - final URL url = new URL("http://buch.co.at/bar"); - assertEquals("www", UrlUtil.subDomain(url)); + final URL url = new URL("http://buch.co.at/bar"); + assertEquals("www", UrlUtil.subDomain(url)); } @Test public void subdomainShouldReturnCorrectValue6() throws MalformedURLException { - final URL url = new URL("http://127.0.0.1/gugus"); - assertEquals("127.0.0.1", UrlUtil.subDomain(url)); + final URL url = new URL("http://127.0.0.1/gugus"); + assertEquals("127.0.0.1", UrlUtil.subDomain(url)); } @Test public void getParentSiteReturnParentSite() { - assertEquals("org.wikipedia.de", UrlUtil.getParentSite("org.wikipedia.de.wwww")); + assertEquals("org.wikipedia.de", UrlUtil.getParentSite("org.wikipedia.de.wwww")); } @Test public void getParentSiteReturnParentSite2() { - assertEquals("org.wikipedia", UrlUtil.getParentSite("org.wikipedia.de")); + assertEquals("org.wikipedia", UrlUtil.getParentSite("org.wikipedia.de")); } @Test public void getParentSiteReturnSameSite() { - assertEquals("org.wikipedia", UrlUtil.getParentSite("org.wikipedia")); + assertEquals("org.wikipedia", UrlUtil.getParentSite("org.wikipedia")); } @Test public void getParentSiteReturnSameSite2() { - assertEquals("org", UrlUtil.getParentSite("org")); + assertEquals("org", UrlUtil.getParentSite("org")); } @Test public void getParentSiteReturnSameSite3() { - assertEquals("1235", UrlUtil.getParentSite("1235")); + assertEquals("1235", UrlUtil.getParentSite("1235")); } @Test public void getParentSiteReturnSameSite4() { - assertEquals("asdf", UrlUtil.getParentSite("asdf")); + assertEquals("asdf", UrlUtil.getParentSite("asdf")); } @Test public void getParentSiteReturnSameSite5() { - assertEquals("asdf.ok.uk.wikipedia", UrlUtil.getParentSite("asdf.ok.uk.wikipedia")); + assertEquals("asdf.ok.uk.wikipedia", UrlUtil.getParentSite("asdf.ok.uk.wikipedia")); } @Test public void getParentSiteReturnSameSite6() { - assertEquals("1.2.3.4", UrlUtil.getParentSite("1.2.3.4")); + assertEquals("1.2.3.4", UrlUtil.getParentSite("1.2.3.4")); } }