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 */
016package org.ini4j.spi;
017
018import org.ini4j.Ini4jCase;
019
020import static org.junit.Assert.assertEquals;
021
022import org.junit.Before;
023import org.junit.Test;
024
025public class WinEscapeToolTest extends Ini4jCase
026{
027    public static final String VALUE1 = "simple";
028    public static final String ESCAPE1 = "simple";
029    public static final String VALUE2 = "Iv??n";
030    public static final String ESCAPE2 = "Iv\\xe1n";
031    public static final String VALUE3 = "1\t2\n3\f4\b5\r6";
032    public static final String ESCAPE3 = "1\\t2\\n3\\f4\\b5\\r6";
033    public static final String VALUE4 = "Iv\u0017n";
034    public static final String ESCAPE4 = "Iv\\x17n";
035    public static final String VALUE5 = "??rv??ztrt??k??rf??r??g??p";
036    public static final String ESCAPE5 = "\\xc1rv\\xedztrt\\xfck\\xf6rf\\xfar\\xf3g\\xe9p";
037    private static final String INVALID_HEX = "\\x1_";
038    private static final String INVALID_OCT = "\\o19_";
039    protected WinEscapeTool instance;
040
041    @Before @Override public void setUp() throws Exception
042    {
043        super.setUp();
044        instance = WinEscapeTool.getInstance();
045    }
046
047    @Test public void testEscape() throws Exception
048    {
049        assertEquals(ESCAPE1, instance.escape(VALUE1));
050        assertEquals(ESCAPE2, instance.escape(VALUE2));
051        assertEquals(ESCAPE3, instance.escape(VALUE3));
052        assertEquals(ESCAPE4, instance.escape(VALUE4));
053        assertEquals(ESCAPE5, instance.escape(VALUE5));
054    }
055
056    @Test public void testInvalidHex()
057    {
058        try
059        {
060            instance.unescape(INVALID_HEX);
061            missing(IllegalArgumentException.class);
062        }
063        catch (IllegalArgumentException x)
064        {
065            //
066        }
067    }
068
069    @Test public void testInvalidOctal()
070    {
071        try
072        {
073            instance.unescape(INVALID_OCT);
074            missing(IllegalArgumentException.class);
075        }
076        catch (IllegalArgumentException x)
077        {
078            //
079        }
080    }
081
082    @Test public void testSingleton() throws Exception
083    {
084        assertEquals(WinEscapeTool.class, WinEscapeTool.getInstance().getClass());
085    }
086
087    @Test public void testUnescape() throws Exception
088    {
089        assertEquals(VALUE1, instance.unescape(ESCAPE1));
090        assertEquals(VALUE2, instance.unescape(ESCAPE2));
091        assertEquals(VALUE3, instance.unescape(ESCAPE3));
092        assertEquals(VALUE4, instance.unescape(ESCAPE4));
093        assertEquals(VALUE5, instance.unescape(ESCAPE5));
094        assertEquals("=", instance.unescape("\\="));
095        assertEquals("xAx", instance.unescape("x\\o101x"));
096    }
097}