package com.alibaba.fastjson.serializer;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.util.IOUtils;

public class SerializeWriterTest {

    private final Logger logger = Logger.getLogger(SerializeWriterTest.class.getSimpleName());

    private final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    private final SerializeWriter writer = new SerializeWriter(new OutputStreamWriter(baos));

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testWriteLiteBasicStr() throws UnsupportedEncodingException {
        String targetStr = new String(IOUtils.DIGITS);
        this.doTestWrite(targetStr);
    }

    private String doTestWrite(String input) throws UnsupportedEncodingException {
        writer.writeString(input, (char) 0);
        writer.flush();
        String result = this.baos.toString("UTF-8");

        Assert.assertEquals(input, JSON.parse(result));

        logger.info(result);

        return result;
    }

    @Test
    public void testWriteLiteSpecilaStr() throws UnsupportedEncodingException {
        this.doTestWrite(this.makeSpecialChars());
    }

    private String makeSpecialChars() {
        StringBuilder strBuilder = new StringBuilder(128);
        for (char c = 128; c <= 160; c++) {
            strBuilder.append(c);
        }
        return strBuilder.toString();
    }

    @Test
    public void testWriteLargeBasicStr() throws UnsupportedEncodingException {
        String tmp = new String(IOUtils.DIGITS);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 200; i++) {
            builder.append(tmp);
        }
        this.doTestWrite(builder.toString());
    }

    @Test
    public void testWriteLargeSpecialStr() throws UnsupportedEncodingException {

        String tmp = this.makeSpecialChars();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 200; i++) {
            builder.append(tmp);
        }
        this.doTestWrite(builder.toString());
    }

    @Test
    public void test_large() throws Exception {
        SerializeWriter writer = new SerializeWriter();

        for (int i = 0; i < 1024 * 1024; ++i) {
            writer.write(i);
        }

        writer.close();
    }
}

@@ -153,16 +153,48 @@
 eption;%0A
+import java.lang.reflect.Field;%0A
 import j
@@ -214,24 +214,24 @@
 ing.Logger;%0A
-
 %0Aimport org.
@@ -1795,32 +1795,157 @@
 dingException %7B%0A
+        String str = createLargeBasicStr();%0A        this.doTestWrite(str);%0A    %7D%0A%0A    private String createLargeBasicStr() %7B%0A
         String t
@@ -2050,33 +2050,33 @@
 (int i = 0; i %3C 
-2
+4
 00; i++) %7B%0A     
@@ -2121,33 +2121,23 @@
         
-this.doTestWrite(
+return 
 builder.
@@ -2138,33 +2138,32 @@
 ilder.toString()
-)
 ;%0A    %7D%0A%0A    @Te
@@ -2681,24 +2681,24 @@
 %0A        %7D%0A%0A
-
         writ
@@ -2714,11 +2714,842 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testBytesBufLocal() throws Exception %7B%0A        String str = createLargeBasicStr();%0A        SerializeWriter writer = new SerializeWriter();%0A        //%E5%86%99%E5%85%A5%E5%A4%A7%E4%BA%8E12K%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%0A        writer.writeString(str);%0A        writer.writeString(str);%0A        byte%5B%5D bytes = writer.toBytes(%22UTF-8%22);%0A        writer.close();%0A%0A        //%E6%A3%80%E6%9F%A5bytesLocal%E5%A4%A7%E5%B0%8F%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%BC%93%E5%AD%98%E6%88%90%E5%8A%9F%E5%BA%94%E8%AF%A5%E5%A4%A7%E4%BA%8E%E7%AD%89%E4%BA%8E%E8%BE%93%E5%87%BA%E7%9A%84bytes%E9%95%BF%E5%BA%A6%0A        Field bytesBufLocalField = SerializeWriter.class.getDeclaredField(%22bytesBufLocal%22);%0A        bytesBufLocalField.setAccessible(true);%0A        ThreadLocal%3Cbyte%5B%5D%3E bytesBufLocal = (ThreadLocal%3Cbyte%5B%5D%3E) bytesBufLocalField.get(null);%0A        byte%5B%5D bytesLocal = bytesBufLocal.get();%0A        Assert.assertNotNull(%22bytesLocal is null%22, bytesLocal);%0A        Assert.assertTrue(%22bytesLocal is smaller than expected%22, bytesLocal.length %3E= bytes.length);%0A    %7D
 %0A%7D%0A

添加检查SerializeWriter bytesBufLocal的单元测试