View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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  
17  package org.yaml.snakeyaml.util;
18  
19  import java.nio.ByteBuffer;
20  import java.nio.charset.CharacterCodingException;
21  
22  import junit.framework.TestCase;
23  
24  public class UriEncoderTest extends TestCase {
25  
26      public void testEncode() {
27          assertEquals("Acad%C3%A9mico", UriEncoder.encode("Académico"));
28          assertEquals("Check http://yaml.org/spec/1.1/#escaping%20in%20URI/", "[]",
29                  UriEncoder.encode("[]"));
30      }
31  
32      public void testDecode() throws CharacterCodingException {
33          ByteBuffer buff = ByteBuffer.allocate(10);
34          buff.put((byte) 0x34);
35          buff.put((byte) 0x35);
36          buff.flip();
37          assertEquals("45", UriEncoder.decode(buff));
38      }
39  
40      public void testFailDecode() throws CharacterCodingException {
41          ByteBuffer buff = ByteBuffer.allocate(10);
42          buff.put((byte) 0x34);
43          buff.put((byte) 0xC1);
44          buff.flip();
45          try {
46              UriEncoder.decode(buff);
47              fail("Invalid UTF-8 must not be accepted.");
48          } catch (Exception e) {
49              assertEquals("Input length = 1", e.getMessage());
50          }
51      }
52  }