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"));
}
}