package org.jcodings.specific;

import org.jcodings.Ptr;
import org.jcodings.transcode.EConv;
import org.jcodings.transcode.EConvFlags;
import org.jcodings.transcode.TranscoderDB;
import org.jcodings.transcode.Transcoding;
import org.junit.Assert;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * Created by headius on 2/25/14.
 */
public class TestEConv {
    @Test
    public void testUTF8toUTF16() throws Exception {
        EConv econv = TranscoderDB.open("UTF-8".getBytes(), "UTF-16".getBytes(), 0);

        byte[] src = "foo".getBytes("UTF-8");
        byte[] dest = new byte["foo".getBytes("UTF-16").length];

        econv.convert(src, new Ptr(0), 3, dest, new Ptr(0), dest.length, 0);

        Assert.assertArrayEquals("foo".getBytes("UTF-16"), dest);
    }

    @Test
    public void testUniversalNewline() throws Exception {
        EConv econv = TranscoderDB.open("".getBytes(), "".getBytes(), EConvFlags.UNIVERSAL_NEWLINE_DECORATOR);

        byte[] src = "foo\r\nbar".getBytes();
        byte[] dest = new byte[7];

        econv.convert(src, new Ptr(0), 8, dest, new Ptr(0), dest.length, 0);

        Assert.assertArrayEquals("foo\nbar".getBytes(), dest);
    }

    @Test
    public void testCrlfNewline() throws Exception {
        EConv econv = TranscoderDB.open("".getBytes(), "".getBytes(), EConvFlags.CRLF_NEWLINE_DECORATOR);

        byte[] src = "foo\nbar".getBytes();
        byte[] dest = new byte[8];

        econv.convert(src, new Ptr(0), 7, dest, new Ptr(0), dest.length, 0);

        Assert.assertArrayEquals("foo\r\nbar".getBytes(), dest);
    }

    @Test
    public void testCrNewline() throws Exception {
        EConv econv = TranscoderDB.open("".getBytes(), "".getBytes(), EConvFlags.CR_NEWLINE_DECORATOR);

        byte[] src = "foo\nbar".getBytes();
        byte[] dest = new byte[7];

        econv.convert(src, new Ptr(0), 7, dest, new Ptr(0), dest.length, 0);

        Assert.assertArrayEquals("foo\rbar".getBytes(), dest);
    }

    @Test
    public void testXMLWithCharref() throws Exception {
        EConv econv = TranscoderDB.open("utf-8".getBytes(), "euc-jp".getBytes(), EConvFlags.XML_ATTR_CONTENT_DECORATOR | EConvFlags.XML_ATTR_QUOTE_DECORATOR | EConvFlags.UNDEF_HEX_CHARREF);

        byte[] src = "<\u2665>&\"\u2661\"".getBytes(UTF8);

        byte[] dest = new byte[50];
        Ptr destP = new Ptr(0);

        econv.convert(src, new Ptr(0), src.length, dest, destP, dest.length, 0);

        Assert.assertArrayEquals("\"&lt;&#x2665;&gt;&amp;&quot;&#x2661;&quot;\"".getBytes(), Arrays.copyOf(dest, destP.p));
    }

    private static final Charset UTF8;

    static {
        Charset utf8 = null;
        try {
            utf8 = Charset.forName("UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        UTF8 = utf8;
    }
}

@@ -263,16 +263,50 @@
 it.Test;
+%0Aimport sun.nio.cs.ext.ISO2022_JP;
 %0A%0Aimport
@@ -2648,41 +2648,770 @@
     
-private static final Charset UTF8
+@Test%0A    public void testXMLText() throws Exception %7B%0A//        EConv econv = TranscoderDB.open(%22utf-8%22.getBytes(), %22iso-2022-jp%22.getBytes(), EConvFlags.XML_TEXT_DECORATOR);%0A//%0A//        byte%5B%5D expected = %22&amp;%E3%81%86&#x2665;&amp;%5C%22'%22.getBytes(ISO2022_JP);%0A//%0A//        byte%5B%5D src = %22&%5Cu3046%5Cu2665&%5C%22'%22.getBytes(UTF8);%0A//        byte%5B%5D dest = new byte%5B50%5D;%0A//        Ptr destP = new Ptr(0);%0A//%0A//        econv.convert(src, new Ptr(0), src.length, dest, destP, dest.length, 0);%0A//%0A////        Assert.assertArrayEquals(expected, Arrays.copyOf(dest, destP.p));%0A//        Assert.assertEquals(new String(expected, ISO2022_JP), new String(Arrays.copyOf(dest, destP.p), ISO2022_JP));%0A    %7D%0A%0A    private static final Charset UTF8;%0A    private static final Charset ISO2022_JP
 ;%0A%0A 
@@ -3451,16 +3451,51 @@
 = null;%0A
+        Charset iso2022_jp = null;%0A
         
@@ -3537,32 +3537,89 @@
 rName(%22UTF-8%22);%0A
+            iso2022_jp = Charset.forName(%22ISO-2022-JP%22);%0A
         %7D catch 
@@ -3687,16 +3687,16 @@
       %7D%0A
-
         
@@ -3708,16 +3708,49 @@
 = utf8;%0A
+        ISO2022_JP = iso2022_jp;%0A
     %7D%0A%7D%0A

Add test for XML text (not passing yet).