Skip to content

Commit 231500b

Browse files
khatwaniNikhilpivovarit
authored andcommitted
Chnages for BAEL-527 (eugenp#2621)
1 parent 1132943 commit 231500b

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

core-java/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@
211211
<artifactId>jmh-generator-annprocess</artifactId>
212212
<version>1.19</version>
213213
</dependency>
214+
<dependency>
215+
<groupId>org.springframework</groupId>
216+
<artifactId>spring-web</artifactId>
217+
<version>4.3.4.RELEASE</version>
218+
</dependency>
214219
</dependencies>
215220

216221
<build>
@@ -464,7 +469,7 @@
464469
<protonpack.version>1.13</protonpack.version>
465470
<streamex.version>0.6.5</streamex.version>
466471
<vavr.version>0.9.0</vavr.version>
467-
472+
468473
<!-- testing -->
469474
<org.hamcrest.version>1.3</org.hamcrest.version>
470475
<junit.version>4.12</junit.version>

core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package com.baeldung.encoderdecoder;
22

3-
import org.hamcrest.CoreMatchers;
4-
import org.junit.Assert;
5-
import org.junit.Test;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
3+
import static java.util.stream.Collectors.joining;
4+
import static org.hamcrest.CoreMatchers.is;
85

96
import java.io.UnsupportedEncodingException;
10-
import java.net.*;
7+
import java.net.URL;
8+
import java.net.URLDecoder;
9+
import java.net.URLEncoder;
1110
import java.nio.charset.StandardCharsets;
1211
import java.util.Arrays;
1312
import java.util.HashMap;
1413
import java.util.Map;
1514

16-
import static java.util.stream.Collectors.joining;
17-
import static org.hamcrest.CoreMatchers.*;
15+
import org.hamcrest.CoreMatchers;
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.web.util.UriUtils;
1821

1922
public class EncoderDecoderUnitTest {
2023

@@ -76,19 +79,20 @@ public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Ex
7679

7780
private String encodePath(String path) {
7881
try {
79-
path = new URI(null, null, path, null).getPath();
80-
} catch (URISyntaxException e) {
82+
path = UriUtils.encodePath(path, "UTF-8");
83+
} catch (UnsupportedEncodingException e) {
8184
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
8285
}
8386
return path;
8487
}
8588

8689
@Test
87-
public void givenPath_thenEncodeDecodePath() throws URISyntaxException {
88-
URI uri = new URI(null, null, "/Path 1/Path+2", null);
89-
90-
Assert.assertEquals("/Path 1/Path+2", uri.getPath());
91-
Assert.assertEquals("/Path%201/Path+2", uri.getRawPath());
90+
public void givenPathSegment_thenEncodeDecode() throws UnsupportedEncodingException {
91+
String pathSegment = "/Path 1/Path+2";
92+
String encodedPathSegment = encodePath(pathSegment);
93+
String decodedPathSegment = UriUtils.decode(encodedPathSegment, "UTF-8");
94+
Assert.assertEquals("/Path%201/Path+2", encodedPathSegment);
95+
Assert.assertEquals("/Path 1/Path+2", decodedPathSegment);
9296
}
9397

9498
@Test

0 commit comments

Comments
 (0)