Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include charset in EncodedResource.equals()
Prior to this commit, the implementation of equals() in EncodedResource was based solely on the resource and encoding. Thus, if a Charset were specified instead of an encoding, invocations of equals() would not work as expected. This commit addresses this issue by including the charset in the implementation of equals() and introducing corresponding tests in a new EncodedResourceTests class. Furthermore, this commit makes EncodedResource immutable and updates all Javadoc to reflect support for the encoding and charset properties. Issue: SPR-12767 (cherry picked from commit 93c70b7)
- Loading branch information
Showing
2 changed files
with
126 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
spring-core/src/test/java/org/springframework/core/io/support/EncodedResourceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2002-2015 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core.io.support; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
import org.junit.Test; | ||
import org.springframework.core.io.DescriptiveResource; | ||
import org.springframework.core.io.Resource; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Unit tests for {@link EncodedResource}. | ||
* | ||
* @author Sam Brannen | ||
* @since 3.2.14 | ||
*/ | ||
public class EncodedResourceTests { | ||
|
||
private static final String UTF8 = "UTF-8"; | ||
private static final String UTF16 = "UTF-16"; | ||
private static final Charset UTF8_CS = Charset.forName(UTF8); | ||
private static final Charset UTF16_CS = Charset.forName(UTF16); | ||
|
||
private final Resource resource = new DescriptiveResource("test"); | ||
|
||
|
||
@Test | ||
public void equalsWithNullOtherObject() { | ||
assertFalse(new EncodedResource(resource).equals(null)); | ||
} | ||
|
||
@Test | ||
public void equalsWithSameEncoding() { | ||
EncodedResource er1 = new EncodedResource(resource, UTF8); | ||
EncodedResource er2 = new EncodedResource(resource, UTF8); | ||
assertEquals(er1, er2); | ||
} | ||
|
||
@Test | ||
public void equalsWithDifferentEncoding() { | ||
EncodedResource er1 = new EncodedResource(resource, UTF8); | ||
EncodedResource er2 = new EncodedResource(resource, UTF16); | ||
assertNotEquals(er1, er2); | ||
} | ||
|
||
@Test | ||
public void equalsWithSameCharset() { | ||
EncodedResource er1 = new EncodedResource(resource, UTF8_CS); | ||
EncodedResource er2 = new EncodedResource(resource, UTF8_CS); | ||
assertEquals(er1, er2); | ||
} | ||
|
||
@Test | ||
public void equalsWithDifferentCharset() { | ||
EncodedResource er1 = new EncodedResource(resource, UTF8_CS); | ||
EncodedResource er2 = new EncodedResource(resource, UTF16_CS); | ||
assertNotEquals(er1, er2); | ||
} | ||
|
||
@Test | ||
public void equalsWithEncodingAndCharset() { | ||
EncodedResource er1 = new EncodedResource(resource, UTF8); | ||
EncodedResource er2 = new EncodedResource(resource, UTF8_CS); | ||
assertNotEquals(er1, er2); | ||
} | ||
|
||
} |