Package com.alibaba.citrus.service.requestcontext.rundata

Source Code of com.alibaba.citrus.service.requestcontext.rundata.BufferedRunDataTests

/*
* Copyright 2010 Alibaba Group Holding Limited.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.alibaba.citrus.service.requestcontext.rundata;

import static org.junit.Assert.*;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.alibaba.citrus.service.requestcontext.AbstractRequestContextsTests;
import com.alibaba.citrus.util.io.ByteArray;

/**
* ����<code>RunData</code>��buffer���ܡ�
*
* @author Michael Zhou
*/
public class BufferedRunDataTests extends AbstractRequestContextsTests<RunData> {
    @BeforeClass
    public static void initFactory() {
        createBeanFactory("services-rundata.xml");
    }

    @Before
    public void init() throws Exception {
        invokeReadFileServlet("form.html");
        initRequestContext();
    }

    @Override
    protected String getDefaultBeanName() {
        return "rundata";
    }

    @Test
    public void byteBuffer() throws Exception {
        OutputStream ostream = newResponse.getOutputStream();

        // write��pop
        ostream.write("abc".getBytes());
        ostream.flush();

        ByteArray bytes = requestContext.popByteBuffer();

        assertEquals("abc", new String(bytes.toByteArray()));

        // write, push, write, push, write, pop, pop, pop
        ostream.write("abc".getBytes());
        ostream.flush();

        requestContext.pushBuffer();
        ostream.write("def".getBytes());
        ostream.flush();

        requestContext.pushBuffer();
        ostream.write("ghi".getBytes());
        ostream.flush();

        bytes = requestContext.popByteBuffer();
        assertEquals("ghi", new String(bytes.toByteArray()));

        bytes = requestContext.popByteBuffer();
        assertEquals("def", new String(bytes.toByteArray()));

        bytes = requestContext.popByteBuffer();
        assertEquals("abc", new String(bytes.toByteArray()));
    }

    @Test
    public void charBuffer() throws Exception {
        PrintWriter writer = newResponse.getWriter();

        // write��pop
        writer.write("abc");
        writer.flush();

        String chars = requestContext.popCharBuffer();

        assertEquals("abc", chars);

        // write, push, write, push, write, pop, pop, pop
        writer.write("abc");
        writer.flush();

        requestContext.pushBuffer();
        writer.write("def");
        writer.flush();

        requestContext.pushBuffer();
        writer.write("ghi");
        writer.flush();

        chars = requestContext.popCharBuffer();
        assertEquals("ghi", chars);

        chars = requestContext.popCharBuffer();
        assertEquals("def", chars);

        chars = requestContext.popCharBuffer();
        assertEquals("abc", chars);
    }

    /**
     * ��getWriter����getOutputStream����ʱ������һ����������
     */
    @Test
    public void writeBytesAndReadChars() throws Exception {
        @SuppressWarnings("unused")
        Writer writer = newResponse.getWriter();
        OutputStream ostream = newResponse.getOutputStream();

        // write��pop
        ostream.write("abc".getBytes());
        ostream.flush();

        String chars = requestContext.popCharBuffer();

        assertEquals("abc", chars);

        // write, push, write, push, write, pop, pop, pop
        ostream.write("abc".getBytes());
        ostream.flush();

        requestContext.pushBuffer();
        ostream.write("def".getBytes());
        ostream.flush();

        requestContext.pushBuffer();
        ostream.write("ghi".getBytes());
        ostream.flush();

        chars = requestContext.popCharBuffer();
        assertEquals("ghi", chars);

        chars = requestContext.popCharBuffer();
        assertEquals("def", chars);

        chars = requestContext.popCharBuffer();
        assertEquals("abc", chars);
    }

    /**
     * ��getOutputStream����getWriter����ʱ������һ����������
     */
    @Test
    public void writeCharsAndReadBytes() throws Exception {
        @SuppressWarnings("unused")
        OutputStream ostream = newResponse.getOutputStream();
        Writer writer = newResponse.getWriter();

        // write��pop
        writer.write("abc");
        writer.flush();

        ByteArray bytes = requestContext.popByteBuffer();

        assertEquals("abc", new String(bytes.toByteArray()));

        // write, push, write, push, write, pop, pop, pop
        writer.write("abc");
        writer.flush();

        requestContext.pushBuffer();
        writer.write("def");
        writer.flush();

        requestContext.pushBuffer();
        writer.write("ghi");
        writer.flush();

        bytes = requestContext.popByteBuffer();
        assertEquals("ghi", new String(bytes.toByteArray()));

        bytes = requestContext.popByteBuffer();
        assertEquals("def", new String(bytes.toByteArray()));

        bytes = requestContext.popByteBuffer();
        assertEquals("abc", new String(bytes.toByteArray()));
    }

    @Test
    public void illegalStateExceptionForBytes() throws Exception {
        // getOutputStream֮����ͼpopCharBuffer
        newResponse.getOutputStream();

        try {
            requestContext.popCharBuffer();
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        // getOutputStream֮����ͼ�ı�bufferģʽ
        initRequestContext();

        newResponse.getOutputStream();
        requestContext.setBuffering(true);

        try {
            requestContext.setBuffering(false);
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        // ��bufferingģʽ�²���byte buffer��
        initRequestContext();

        requestContext.setBuffering(false);
        newResponse.getOutputStream();

        try {
            requestContext.pushBuffer();
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        try {
            requestContext.popByteBuffer();
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        requestContexts.commitRequestContext(requestContext);
    }

    @Test
    public void illegalStateExceptionForWriter() throws Exception {
        // getWriter֮����ͼpopByteBuffer
        newResponse.getWriter();

        try {
            requestContext.popByteBuffer();
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        // getWriter֮����ͼ�ı�bufferģʽ
        initRequestContext();

        newResponse.getWriter();
        requestContext.setBuffering(true);

        try {
            requestContext.setBuffering(false);
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        // ��bufferingģʽ�²���char buffer��
        initRequestContext();

        requestContext.setBuffering(false);
        newResponse.getWriter();

        try {
            requestContext.pushBuffer();
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        try {
            requestContext.popCharBuffer();
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        }

        requestContexts.commitRequestContext(requestContext);
    }
}
TOP

Related Classes of com.alibaba.citrus.service.requestcontext.rundata.BufferedRunDataTests

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.