File tree Expand file tree Collapse file tree
core-java-8/src/test/java/com/baeldung/encoderdecoder Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .encoderdecoder ;
2+
3+ import org .hamcrest .CoreMatchers ;
4+ import org .junit .Assert ;
5+ import org .junit .Test ;
6+
7+ import java .io .UnsupportedEncodingException ;
8+ import java .net .URLDecoder ;
9+ import java .net .URLEncoder ;
10+ import java .nio .charset .StandardCharsets ;
11+
12+ public class EncoderDecoder {
13+
14+ @ Test
15+ public void givenPlainURL_whenUsingUTF8EncodingScheme_thenEncodeURL () throws UnsupportedEncodingException {
16+ String plainURL = "http://www.baeldung.com" ;
17+ String encodedURL = URLEncoder .encode (plainURL , StandardCharsets .UTF_8 .toString ());
18+
19+ Assert .assertThat ("http%3A%2F%2Fwww.baeldung.com" , CoreMatchers .is (encodedURL ));
20+ }
21+
22+ @ Test
23+ public void givenEncodedURL_whenUsingUTF8EncodingScheme_thenDecodeURL () throws UnsupportedEncodingException {
24+ String encodedURL = "http%3A%2F%2Fwww.baeldung.com" ;
25+ String decodedURL = URLDecoder .decode (encodedURL , StandardCharsets .UTF_8 .toString ());
26+
27+ Assert .assertThat ("http://www.baeldung.com" , CoreMatchers .is (decodedURL ));
28+ }
29+
30+ @ Test
31+ public void givenEncodedURL_whenUsingWrongEncodingScheme_thenDecodeInvalidURL () throws UnsupportedEncodingException {
32+ String encodedURL = "http%3A%2F%2Fwww.baeldung.com" ;
33+
34+ String decodedURL = URLDecoder .decode (encodedURL , StandardCharsets .UTF_16 .toString ());
35+
36+ Assert .assertFalse ("http://www.baeldung.com" .equals (decodedURL ));
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments