Browse Source

Polishing contribution

Closes gh-35477
pull/35843/head
Brian Clozel 1 month ago
parent
commit
87d95dc30a
  1. 42
      spring-web/src/test/java/org/springframework/web/util/HtmlCharacterEntityDecoderTest.java
  2. 53
      spring-web/src/test/java/org/springframework/web/util/HtmlCharacterEntityDecoderTests.java

42
spring-web/src/test/java/org/springframework/web/util/HtmlCharacterEntityDecoderTest.java

@ -1,42 +0,0 @@ @@ -1,42 +0,0 @@
package org.springframework.web.util;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class HtmlCharacterEntityDecoderTest {
@Test
@DisplayName("Should correctly unescape Unicode supplementary characters")
void unescapeHandlesSupplementaryCharactersCorrectly() {
// Arrange: Prepare test cases with the 'grinning face' emoji (😀, U+1F600).
String expectedCharacter = "😀";
String decimalEntity = "😀";
String hexEntity = "😀";
// Act: Call the HtmlUtils.htmlUnescape method to get the actual results.
String actualResultFromDecimal = HtmlUtils.htmlUnescape(decimalEntity);
String actualResultFromHex = HtmlUtils.htmlUnescape(hexEntity);
// Assert: Verify that the actual results match the expected character.
assertEquals(expectedCharacter, actualResultFromDecimal, "Decimal entity was not converted correctly.");
assertEquals(expectedCharacter, actualResultFromHex, "Hexadecimal entity was not converted correctly.");
}
@Test
@DisplayName("Should correctly unescape basic and named HTML entities")
void unescapeHandlesBasicEntities() {
// Arrange
String input = "<p>Tom & Jerry's "Show"</p>";
String expectedOutput = "<p>Tom & Jerry's \"Show\"</p>";
// Act
String actualOutput = HtmlUtils.htmlUnescape(input);
// Assert
assertEquals(expectedOutput, actualOutput, "Basic HTML entities were not unescaped correctly.");
}
}

53
spring-web/src/test/java/org/springframework/web/util/HtmlCharacterEntityDecoderTests.java

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
/*
* Copyright 2002-present 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
*
* https://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.web.util;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link HtmlCharacterEntityDecoder}.
*/
class HtmlCharacterEntityDecoderTests {
@Test
void unescapeHandlesSupplementaryCharactersAsDecimal() {
String expectedCharacter = "😀";
String decimalEntity = "&#128512;";
String actualResultFromDecimal = HtmlUtils.htmlUnescape(decimalEntity);
assertThat(actualResultFromDecimal).as("Decimal entity was not converted correctly.").isEqualTo(expectedCharacter);
}
@Test
void unescapeHandlesSupplementaryCharactersAsHexadecimal() {
String expectedCharacter = "😀";
String hexEntity = "&#x1F600;";
String actualResultFromHex = HtmlUtils.htmlUnescape(hexEntity);
assertThat(actualResultFromHex).as("Hexadecimal entity was not converted correctly.").isEqualTo(expectedCharacter);
}
@Test
void unescapeHandlesBasicEntities() {
String input = "&lt;p&gt;Tom &amp; Jerry&#39;s &quot;Show&quot;&lt;/p&gt;";
String expectedOutput = "<p>Tom & Jerry's \"Show\"</p>";
String actualOutput = HtmlUtils.htmlUnescape(input);
assertThat(actualOutput).as("Basic HTML entities were not unescaped correctly.").isEqualTo(expectedOutput);
}
}
Loading…
Cancel
Save