001: /*
002: * Copyright (c) xsocket.org, 2006-2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.spi;
022:
023: import java.nio.ByteBuffer;
024:
025: import org.junit.Assert;
026: import org.junit.Test;
027: import org.xsocket.DataConverter;
028: import org.xsocket.connection.spi.IoQueue;
029:
030: /**
031: *
032: * @author grro@xsocket.org
033: */
034: public final class IoQueueTest {
035:
036: private static final String TEXT_1 = "Yes, ";
037: private static final String TEXT_2 = "it ";
038: private static final String TEXT_3 = "works ";
039: private static final String TEXT_4 = "fine";
040:
041: @Test
042: public void append1() throws Exception {
043: IoQueue ioQueue = new IoQueue();
044: ioQueue.append(DataConverter.toByteBuffer(TEXT_1, "UTF-8"));
045: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
046:
047: Assert.assertEquals(data, TEXT_1);
048: }
049:
050: @Test
051: public void append2() throws Exception {
052: IoQueue ioQueue = new IoQueue();
053:
054: ByteBuffer[] buffers = new ByteBuffer[2];
055: buffers[0] = DataConverter.toByteBuffer(TEXT_1, "UTF-8");
056: buffers[1] = DataConverter.toByteBuffer(TEXT_2, "UTF-8");
057: ioQueue.append(buffers);
058:
059: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
060: Assert.assertEquals(data, TEXT_1 + TEXT_2);
061: }
062:
063: @Test
064: public void append3() throws Exception {
065: IoQueue ioQueue = new IoQueue();
066:
067: ioQueue.append(DataConverter.toByteBuffer(TEXT_1, "UTF-8"));
068:
069: ByteBuffer[] buffers = new ByteBuffer[2];
070: buffers[0] = DataConverter.toByteBuffer(TEXT_2, "UTF-8");
071: buffers[1] = DataConverter.toByteBuffer(TEXT_3, "UTF-8");
072: ioQueue.append(buffers);
073:
074: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
075: Assert.assertEquals(data, TEXT_1 + TEXT_2 + TEXT_3);
076: }
077:
078: @Test
079: public void append4() throws Exception {
080: IoQueue ioQueue = new IoQueue();
081:
082: ByteBuffer[] buffers = new ByteBuffer[2];
083: buffers[0] = DataConverter.toByteBuffer(TEXT_1, "UTF-8");
084: buffers[1] = DataConverter.toByteBuffer(TEXT_2, "UTF-8");
085: ioQueue.append(buffers);
086:
087: ByteBuffer[] buffers2 = new ByteBuffer[2];
088: buffers2[0] = DataConverter.toByteBuffer(TEXT_3, "UTF-8");
089: buffers2[1] = DataConverter.toByteBuffer(TEXT_4, "UTF-8");
090: ioQueue.append(buffers2);
091:
092: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
093: Assert.assertEquals(data, TEXT_1 + TEXT_2 + TEXT_3 + TEXT_4);
094: }
095:
096: @Test
097: public void append5() throws Exception {
098: IoQueue ioQueue = new IoQueue();
099:
100: ByteBuffer[] buffers = new ByteBuffer[2];
101: buffers[0] = DataConverter.toByteBuffer(TEXT_1, "UTF-8");
102: buffers[1] = DataConverter.toByteBuffer(TEXT_2, "UTF-8");
103: ioQueue.append(buffers);
104:
105: ioQueue.append(DataConverter.toByteBuffer(TEXT_3, "UTF-8"));
106:
107: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
108: Assert.assertEquals(data, TEXT_1 + TEXT_2 + TEXT_3);
109: }
110:
111: @Test
112: public void add1() throws Exception {
113: IoQueue ioQueue = new IoQueue();
114:
115: ByteBuffer[] buffers = new ByteBuffer[2];
116: buffers[0] = DataConverter.toByteBuffer(TEXT_1, "UTF-8");
117: buffers[1] = DataConverter.toByteBuffer(TEXT_2, "UTF-8");
118: ioQueue.addFirst(buffers);
119:
120: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
121: Assert.assertEquals(data, TEXT_1 + TEXT_2);
122: }
123:
124: @Test
125: public void add2() throws Exception {
126: IoQueue ioQueue = new IoQueue();
127:
128: ByteBuffer[] buffers = new ByteBuffer[2];
129: buffers[0] = DataConverter.toByteBuffer(TEXT_3, "UTF-8");
130: buffers[1] = DataConverter.toByteBuffer(TEXT_4, "UTF-8");
131: ioQueue.addFirst(buffers);
132:
133: ByteBuffer[] buffers2 = new ByteBuffer[2];
134: buffers2[0] = DataConverter.toByteBuffer(TEXT_1, "UTF-8");
135: buffers2[1] = DataConverter.toByteBuffer(TEXT_2, "UTF-8");
136: ioQueue.addFirst(buffers2);
137:
138: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
139: Assert.assertEquals(data, TEXT_1 + TEXT_2 + TEXT_3 + TEXT_4);
140: }
141:
142: @Test
143: public void add3() throws Exception {
144: IoQueue ioQueue = new IoQueue();
145:
146: ioQueue.append(DataConverter.toByteBuffer(TEXT_3, "UTF-8"));
147:
148: ByteBuffer[] buffers = new ByteBuffer[2];
149: buffers[0] = DataConverter.toByteBuffer(TEXT_1, "UTF-8");
150: buffers[1] = DataConverter.toByteBuffer(TEXT_2, "UTF-8");
151: ioQueue.addFirst(buffers);
152:
153: String data = DataConverter.toString(ioQueue.drain(), "UTF-8");
154: Assert.assertEquals(data, TEXT_1 + TEXT_2 + TEXT_3);
155: }
156:
157: }
|