View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
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.ini4j.spi;
17  
18  import org.ini4j.Ini4jCase;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNull;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  public class EscapeToolTest extends Ini4jCase
27  {
28      private static final String VALUE1 = "simple";
29      private static final String ESCAPE1 = "simple";
30      private static final String VALUE2 = "Iv\ufffdn";
31      private static final String ESCAPE2 = "Iv\\ufffdn";
32      private static final String VALUE3 = "1\t2\n3\f4\b5\r6";
33      private static final String ESCAPE3 = "1\\t2\\n3\\f4\\b5\\r6";
34      private static final String VALUE4 = "Iv\u0017n";
35      private static final String ESCAPE4 = "Iv\\u0017n";
36      private static final String INVALID_UNICODE = "\\u98x";
37      private static final String UNQUOTED1 = "simple";
38      private static final String QUOTED1 = "\"simple\"";
39      private static final String UNQUOTED2 = "no\\csak\"";
40      private static final String QUOTED2 = "\"no\\\\csak\\\"\"";
41      private static final String UNQUOTED3 = "";
42      private static final String QUOTED3 = "";
43      protected EscapeTool instance;
44  
45      @Before @Override public void setUp() throws Exception
46      {
47          super.setUp();
48          instance = EscapeTool.getInstance();
49      }
50  
51      @Test public void testEscape() throws Exception
52      {
53          assertEquals(ESCAPE1, instance.escape(VALUE1));
54          assertEquals(ESCAPE2, instance.escape(VALUE2));
55          assertEquals(ESCAPE3, instance.escape(VALUE3));
56          assertEquals(ESCAPE4, instance.escape(VALUE4));
57      }
58  
59      @Test public void testInvalidUnicode()
60      {
61          try
62          {
63              instance.unescape(INVALID_UNICODE);
64              missing(IllegalArgumentException.class);
65          }
66          catch (IllegalArgumentException x)
67          {
68              //
69          }
70      }
71  
72      @Test public void testQuote() throws Exception
73      {
74          assertEquals(QUOTED1, instance.quote(UNQUOTED1));
75          assertEquals(QUOTED2, instance.quote(UNQUOTED2));
76          assertEquals(QUOTED3, instance.quote(UNQUOTED3));
77          assertNull(instance.quote(null));
78      }
79  
80      @Test public void testSingleton() throws Exception
81      {
82          assertEquals(EscapeTool.class, EscapeTool.getInstance().getClass());
83      }
84  
85      @SuppressWarnings("empty-statement")
86      @Test public void testUnescape() throws Exception
87      {
88          assertEquals(VALUE1, instance.unescape(ESCAPE1));
89          assertEquals(VALUE2, instance.unescape(ESCAPE2));
90          assertEquals(VALUE3, instance.unescape(ESCAPE3));
91          assertEquals(VALUE4, instance.unescape(ESCAPE4));
92          assertEquals("=", instance.unescape("\\="));
93      }
94  }