Fix UriComponents.equals() method

Fix HierarchicalUriComponents and OpaqueUriComponents .equals() methods.

Issue: SPR-10313
This commit is contained in:
Phillip Webb 2013-02-19 13:19:42 -08:00
parent 7e2022b9a7
commit 5b7969e726
3 changed files with 45 additions and 36 deletions

View File

@ -39,6 +39,7 @@ import org.springframework.util.StringUtils;
* Extension of {@link UriComponents} for hierarchical URIs.
*
* @author Arjen Poutsma
* @author Phillip Webb
* @since 3.1.3
* @see <a href="http://tools.ietf.org/html/rfc3986#section-1.2.3">Hierarchical URIs</a>
*/
@ -430,28 +431,15 @@ final class HierarchicalUriComponents extends UriComponents {
return false;
}
HierarchicalUriComponents other = (HierarchicalUriComponents) obj;
if (ObjectUtils.nullSafeEquals(getScheme(), other.getScheme())) {
return false;
}
if (ObjectUtils.nullSafeEquals(getUserInfo(), other.getUserInfo())) {
return false;
}
if (ObjectUtils.nullSafeEquals(getHost(), other.getHost())) {
return false;
}
if (this.port != other.port) {
return false;
}
if (!this.path.equals(other.path)) {
return false;
}
if (!this.queryParams.equals(other.queryParams)) {
return false;
}
if (ObjectUtils.nullSafeEquals(getFragment(), other.getFragment())) {
return false;
}
return true;
boolean rtn = true;
rtn &= ObjectUtils.nullSafeEquals(getScheme(), other.getScheme());
rtn &= ObjectUtils.nullSafeEquals(getUserInfo(), other.getUserInfo());
rtn &= ObjectUtils.nullSafeEquals(getHost(), other.getHost());
rtn &= getPort() == other.getPort();
rtn &= this.path.equals(other.path);
rtn &= this.queryParams.equals(other.queryParams);
rtn &= ObjectUtils.nullSafeEquals(getFragment(), other.getFragment());
return rtn;
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import org.springframework.util.ObjectUtils;
* Extension of {@link UriComponents} for opaque URIs.
*
* @author Arjen Poutsma
* @author Phillip Webb
* @since 3.2
* @see <a href="http://tools.ietf.org/html/rfc3986#section-1.2.3">Hierarchical vs Opaque URIs</a>
*/
@ -145,18 +146,11 @@ final class OpaqueUriComponents extends UriComponents {
}
OpaqueUriComponents other = (OpaqueUriComponents) obj;
if (ObjectUtils.nullSafeEquals(getScheme(), other.getScheme())) {
return false;
}
if (ObjectUtils.nullSafeEquals(this.ssp, other.ssp)) {
return false;
}
if (ObjectUtils.nullSafeEquals(getFragment(), other.getFragment())) {
return false;
}
return true;
boolean rtn = true;
rtn &= ObjectUtils.nullSafeEquals(getScheme(), other.getScheme());
rtn &= ObjectUtils.nullSafeEquals(this.ssp, other.ssp);
rtn &= ObjectUtils.nullSafeEquals(getFragment(), other.getFragment());
return rtn;
}
@Override

View File

@ -26,9 +26,14 @@ import java.net.URISyntaxException;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.*;
/** @author Arjen Poutsma */
/**
* @author Arjen Poutsma
* @author Phillip Webb
*/
public class UriComponentsTests {
@Test
@ -92,4 +97,26 @@ public class UriComponentsTests {
assertThat(uriComponents.toString(), equalTo(readObject.toString()));
}
@Test
public void equalsHierarchicalUriComponents() throws Exception {
UriComponents uriComponents1 = UriComponentsBuilder.fromUriString("http://example.com").path("/{foo}").query("bar={baz}").build();
UriComponents uriComponents2 = UriComponentsBuilder.fromUriString("http://example.com").path("/{foo}").query("bar={baz}").build();
UriComponents uriComponents3 = UriComponentsBuilder.fromUriString("http://example.com").path("/{foo}").query("bin={baz}").build();
assertThat(uriComponents1, instanceOf(HierarchicalUriComponents.class));
assertThat(uriComponents1, equalTo(uriComponents1));
assertThat(uriComponents1, equalTo(uriComponents2));
assertThat(uriComponents1, not(equalTo(uriComponents3)));
}
@Test
public void equalsOpaqueUriComponents() throws Exception {
UriComponents uriComponents1 = UriComponentsBuilder.fromUriString("http:example.com/foo/bar").build();
UriComponents uriComponents2 = UriComponentsBuilder.fromUriString("http:example.com/foo/bar").build();
UriComponents uriComponents3 = UriComponentsBuilder.fromUriString("http:example.com/foo/bin").build();
assertThat(uriComponents1, instanceOf(OpaqueUriComponents.class));
assertThat(uriComponents1, equalTo(uriComponents1));
assertThat(uriComponents1, equalTo(uriComponents2));
assertThat(uriComponents1, not(equalTo(uriComponents3)));
}
}