View Javadoc

1   /**
2    * Copyright (c) 2008-2012, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.yaml.snakeyaml.util;
17  
18  import java.nio.ByteBuffer;
19  import java.nio.charset.CharacterCodingException;
20  
21  import junit.framework.TestCase;
22  
23  public class UriEncoderTest extends TestCase {
24  
25      public void testEncode() {
26          assertEquals("Acad%C3%A9mico", UriEncoder.encode("Académico"));
27          assertEquals("Check http://yaml.org/spec/1.1/#escaping%20in%20URI/", "[]",
28                  UriEncoder.encode("[]"));
29      }
30  
31      public void testDecode() throws CharacterCodingException {
32          ByteBuffer buff = ByteBuffer.allocate(10);
33          buff.put((byte) 0x34);
34          buff.put((byte) 0x35);
35          buff.flip();
36          assertEquals("45", UriEncoder.decode(buff));
37      }
38  
39      public void testFailDecode() throws CharacterCodingException {
40          ByteBuffer buff = ByteBuffer.allocate(10);
41          buff.put((byte) 0x34);
42          buff.put((byte) 0xC1);
43          buff.flip();
44          try {
45              UriEncoder.decode(buff);
46              fail("Invalid UTF-8 must not be accepted.");
47          } catch (Exception e) {
48              assertEquals("Input length = 1", e.getMessage());
49          }
50      }
51  }