⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 byteaccesstest.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one *  or more contributor license agreements.  See the NOTICE file *  distributed with this work for additional information *  regarding copyright ownership.  The ASF licenses this file *  to you 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 org.apache.mina.util.byteaccess;import static org.easymock.EasyMock.createStrictControl;import java.nio.ByteOrder;import java.util.ArrayList;import java.util.List;import junit.framework.TestCase;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.util.byteaccess.ByteArray.Cursor;import org.apache.mina.util.byteaccess.CompositeByteArray.CursorListener;import org.apache.mina.util.byteaccess.CompositeByteArrayRelativeWriter.ChunkedExpander;import org.apache.mina.util.byteaccess.CompositeByteArrayRelativeWriter.Flusher;import org.easymock.IMocksControl;/** * Tests classes in the <code>byteaccess</code> package. *  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> * @version $Rev$, $Date$ */public class ByteAccessTest extends TestCase {    private List<String> operations = new ArrayList<String>();    private void resetOperations() {        operations.clear();    }    private void assertOperationCountEquals(int expectedCount) {        assertEquals("Operations: " + operations, expectedCount, operations.size());    }    private void addOperation(String description) {        operations.add(description);    }    public void testBufferByteArray() throws Exception {        ByteArray ba = getByteArrayFactory().create(1000);        testAbsoluteReaderAndWriter(0, 1000, ba, ba);        testAbsoluteReaderAndWriter(0, 1000, ba, ba);        Cursor readCursor = ba.cursor();        Cursor writeCursor = ba.cursor();        testRelativeReaderAndWriter(1000, readCursor, writeCursor);    }    public void testCompositeAddAndRemove() throws Exception {        CompositeByteArray cba = new CompositeByteArray();        assertEquals(0, cba.first());        assertEquals(0, cba.last());        cba.addFirst(getByteArrayFactory().create(100));        assertEquals(-100, cba.first());        assertEquals(0, cba.last());        cba.addFirst(getByteArrayFactory().create(100));        assertEquals(-200, cba.first());        assertEquals(0, cba.last());        cba.addLast(getByteArrayFactory().create(100));        assertEquals(-200, cba.first());        assertEquals(100, cba.last());        cba.removeFirst();        assertEquals(-100, cba.first());        assertEquals(100, cba.last());        cba.addLast(getByteArrayFactory().create(100));        assertEquals(-100, cba.first());        assertEquals(200, cba.last());        cba.removeLast();        assertEquals(-100, cba.first());        assertEquals(100, cba.last());        cba.removeFirst();        assertEquals(0, cba.first());        assertEquals(100, cba.last());        cba.removeFirst();        assertEquals(100, cba.first());        assertEquals(100, cba.last());        cba.addLast(getByteArrayFactory().create(100));        assertEquals(100, cba.first());        assertEquals(200, cba.last());    }    private BufferByteArray wrapString(String string) {        byte[] bytes = string.getBytes();        IoBuffer bb = IoBuffer.wrap(bytes);        BufferByteArray ba = new BufferByteArray(bb) {            @Override            public void free() {                addOperation(this + ".free()");                // Nothing to do.            }        };        return ba;    }    private String toString(ByteArray ba) {        IoBuffer bb = IoBuffer.allocate(ba.length());        ba.get(0, bb);        byte[] bytes = bb.array();        String string = new String(bytes);        return string;    }    public void testCompositeStringJoin() throws Exception {        ByteArray ba1 = wrapString("Hello");        ByteArray ba2 = wrapString("MINA");        ByteArray ba3 = wrapString("World");        CompositeByteArray cba = new CompositeByteArray();        cba.addLast(ba1);        cba.addLast(ba2);        cba.addLast(ba3);        assertEquals("HelloMINAWorld", toString(cba));    }    public void testCompositeCursor() throws Exception {        IMocksControl mc = createStrictControl();        ByteArray ba1 = getByteArrayFactory().create(10);        ByteArray ba2 = getByteArrayFactory().create(10);        ByteArray ba3 = getByteArrayFactory().create(10);        CompositeByteArray cba = new CompositeByteArray();        cba.addLast(ba1);        cba.addLast(ba2);        cba.addLast(ba3);        CursorListener cl = mc.createMock(CursorListener.class);        mc.reset();        mc.replay();        Cursor cursor = cba.cursor(cl);        mc.verify();        mc.reset();        cl.enteredFirstComponent(0, ba1);        mc.replay();        cursor.get();        mc.verify();        mc.reset();        mc.replay();        cursor.setIndex(10);        mc.verify();        mc.reset();        cl.enteredNextComponent(10, ba2);        mc.replay();        cursor.put((byte) 55);        mc.verify();        mc.reset();        mc.replay();        cursor.setIndex(9);        mc.verify();        mc.reset();        cl.enteredPreviousComponent(0, ba1);        cl.enteredNextComponent(10, ba2);        mc.replay();        cursor.putInt(66);        mc.verify();        mc.reset();        cl.enteredNextComponent(20, ba3);        mc.replay();        cursor.setIndex(29);        cursor.get();        mc.verify();        cba.removeLast(); // Force cursor to relocate itself.        mc.reset();        cl.enteredLastComponent(10, ba2);        mc.replay();        cursor.setIndex(15);        cursor.get();        mc.verify();        mc.reset();        cl.enteredPreviousComponent(0, ba1);        mc.replay();        cursor.setIndex(0);        cursor.get();        mc.verify();    }    public void testCompositeByteArray() throws Exception {        CompositeByteArray ba = new CompositeByteArray();        for (int i = 0; i < 1000; i += 100) {            ba.addLast(getByteArrayFactory().create(100));        }        resetOperations();        testAbsoluteReaderAndWriter(0, 1000, ba, ba);        testAbsoluteReaderAndWriter(0, 1000, ba, ba);        assertOperationCountEquals(0);        Cursor readCursor = ba.cursor();        Cursor writeCursor = ba.cursor();        testRelativeReaderAndWriter(1000, readCursor, writeCursor);        assertOperationCountEquals(0);    }    public void testCompositeByteArrayRelativeReaderAndWriter() throws Exception {        CompositeByteArray cba = new CompositeByteArray();        CompositeByteArrayRelativeReader cbarr = new CompositeByteArrayRelativeReader(cba, true);        CompositeByteArrayRelativeWriter cbarw = new CompositeByteArrayRelativeWriter(cba, getExpander(100), getFlusher(), false);        resetOperations();        testRelativeReaderAndWriter(10, cbarr, cbarw);        assertOperationCountEquals(2);        resetOperations();        testRelativeReaderAndWriter(100, cbarr, cbarw);        assertOperationCountEquals(3);        resetOperations();        testRelativeReaderAndWriter(1000, cbarr, cbarw);        assertOperationCountEquals(30);        resetOperations();        testRelativeReaderAndWriter(10000, cbarr, cbarw);        assertOperationCountEquals(300);        resetOperations();        testRelativeReaderAndWriter(90, cbarr, cbarw);        assertOperationCountEquals(0); // Last free doesn't occur, since cursor only moves lazily.    }    public void testCompositeByteArrayRelativeReaderAndWriterWithFlush() throws Exception {        CompositeByteArray cba = new CompositeByteArray();        CompositeByteArrayRelativeReader cbarr = new CompositeByteArrayRelativeReader(cba, true);        CompositeByteArrayRelativeWriter cbarw = new CompositeByteArrayRelativeWriter(cba, getExpander(100), getFlusher(), true);        resetOperations();        testRelativeReaderAndWriter(10, cbarr, cbarw);        assertOperationCountEquals(2);        resetOperations();        testRelativeReaderAndWriter(100, cbarr, cbarw);        assertOperationCountEquals(4);        resetOperations();        testRelativeReaderAndWriter(1000, cbarr, cbarw);        assertOperationCountEquals(40);        resetOperations();        testRelativeReaderAndWriter(10000, cbarr, cbarw);        assertOperationCountEquals(400);        resetOperations();        testRelativeReaderAndWriter(90, cbarr, cbarw);        assertOperationCountEquals(0); // Last free doesn't occur, since cursor only moves lazily.    }    public void testCompositeRemoveTo() throws Exception {        CompositeByteArray cba = new CompositeByteArray();        {            // Remove nothing.            resetOperations();            ByteArray removed = cba.removeTo(0);            assertEquals(0, removed.first());            assertEquals(0, removed.last());            assertEquals(0, cba.first());            assertEquals(0, cba.last());            removed.free();            assertOperationCountEquals(0);        }        cba.addLast(getByteArrayFactory().create(100));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -