001    /*
002     * Copyright 2005,2009 Ivan SZKIBA
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.ini4j.spi;
017    
018    import org.ini4j.Ini4jCase;
019    
020    import static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertNull;
022    
023    import org.junit.Before;
024    import org.junit.Test;
025    
026    public class EscapeToolTest extends Ini4jCase
027    {
028        private static final String VALUE1 = "simple";
029        private static final String ESCAPE1 = "simple";
030        private static final String VALUE2 = "Iv\ufffdn";
031        private static final String ESCAPE2 = "Iv\\ufffdn";
032        private static final String VALUE3 = "1\t2\n3\f4\b5\r6";
033        private static final String ESCAPE3 = "1\\t2\\n3\\f4\\b5\\r6";
034        private static final String VALUE4 = "Iv\u0017n";
035        private static final String ESCAPE4 = "Iv\\u0017n";
036        private static final String INVALID_UNICODE = "\\u98x";
037        private static final String UNQUOTED1 = "simple";
038        private static final String QUOTED1 = "\"simple\"";
039        private static final String UNQUOTED2 = "no\\csak\"";
040        private static final String QUOTED2 = "\"no\\\\csak\\\"\"";
041        private static final String UNQUOTED3 = "";
042        private static final String QUOTED3 = "";
043        protected EscapeTool instance;
044    
045        @Before @Override public void setUp() throws Exception
046        {
047            super.setUp();
048            instance = EscapeTool.getInstance();
049        }
050    
051        @Test public void testEscape() throws Exception
052        {
053            assertEquals(ESCAPE1, instance.escape(VALUE1));
054            assertEquals(ESCAPE2, instance.escape(VALUE2));
055            assertEquals(ESCAPE3, instance.escape(VALUE3));
056            assertEquals(ESCAPE4, instance.escape(VALUE4));
057        }
058    
059        @Test public void testInvalidUnicode()
060        {
061            try
062            {
063                instance.unescape(INVALID_UNICODE);
064                missing(IllegalArgumentException.class);
065            }
066            catch (IllegalArgumentException x)
067            {
068                //
069            }
070        }
071    
072        @Test public void testQuote() throws Exception
073        {
074            assertEquals(QUOTED1, instance.quote(UNQUOTED1));
075            assertEquals(QUOTED2, instance.quote(UNQUOTED2));
076            assertEquals(QUOTED3, instance.quote(UNQUOTED3));
077            assertNull(instance.quote(null));
078        }
079    
080        @Test public void testSingleton() throws Exception
081        {
082            assertEquals(EscapeTool.class, EscapeTool.getInstance().getClass());
083        }
084    
085        @SuppressWarnings("empty-statement")
086        @Test public void testUnescape() throws Exception
087        {
088            assertEquals(VALUE1, instance.unescape(ESCAPE1));
089            assertEquals(VALUE2, instance.unescape(ESCAPE2));
090            assertEquals(VALUE3, instance.unescape(ESCAPE3));
091            assertEquals(VALUE4, instance.unescape(ESCAPE4));
092            assertEquals("=", instance.unescape("\\="));
093        }
094    }