001: package org.apache.mina.transport;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.net.InetSocketAddress;
007: import java.nio.ByteBuffer;
008: import java.nio.channels.FileChannel;
009: import java.util.concurrent.CountDownLatch;
010:
011: import junit.framework.TestCase;
012:
013: import org.apache.mina.common.ConnectFuture;
014: import org.apache.mina.common.IoAcceptor;
015: import org.apache.mina.common.IoBuffer;
016: import org.apache.mina.common.IoConnector;
017: import org.apache.mina.common.IoHandlerAdapter;
018: import org.apache.mina.common.IoSession;
019: import org.apache.mina.util.AvailablePortFinder;
020:
021: public abstract class AbstractFileRegionTest extends TestCase {
022:
023: private static final int FILE_SIZE = 1 * 1024 * 1024; // 1MB file
024:
025: protected abstract IoAcceptor createAcceptor();
026:
027: protected abstract IoConnector createConnector();
028:
029: public void testSendLargeFile() throws Throwable {
030: File file = createLargeFile();
031: assertEquals("Test file not as big as specified", FILE_SIZE,
032: file.length());
033:
034: final CountDownLatch latch = new CountDownLatch(1);
035: final boolean[] success = { false };
036: final Throwable[] exception = { null };
037:
038: int port = AvailablePortFinder.getNextAvailable(1025);
039: IoAcceptor acceptor = createAcceptor();
040: acceptor.setHandler(new IoHandlerAdapter() {
041: private int index = 0;
042:
043: @Override
044: public void exceptionCaught(IoSession session,
045: Throwable cause) throws Exception {
046: exception[0] = cause;
047: session.close();
048: }
049:
050: @Override
051: public void sessionClosed(IoSession session)
052: throws Exception {
053: latch.countDown();
054: }
055:
056: @Override
057: public void messageReceived(IoSession session,
058: Object message) throws Exception {
059: IoBuffer buffer = (IoBuffer) message;
060: while (buffer.hasRemaining()) {
061: int x = buffer.getInt();
062: if (x != index) {
063: throw new Exception(
064: String
065: .format(
066: "Integer at %d was %d but should have been %d",
067: index, x, index));
068: }
069: index++;
070: }
071: if (index > FILE_SIZE / 4) {
072: throw new Exception("Read too much data");
073: }
074: if (index == FILE_SIZE / 4) {
075: success[0] = true;
076: session.close();
077: }
078: }
079: });
080: acceptor.bind(new InetSocketAddress(port));
081:
082: IoConnector connector = createConnector();
083: connector.setHandler(new IoHandlerAdapter() {
084: @Override
085: public void exceptionCaught(IoSession session,
086: Throwable cause) throws Exception {
087: exception[0] = cause;
088: latch.countDown();
089: }
090: });
091: ConnectFuture future = connector.connect(new InetSocketAddress(
092: "localhost", port));
093: future.awaitUninterruptibly();
094:
095: IoSession session = future.getSession();
096: session.write(file);
097:
098: latch.await();
099:
100: if (exception[0] != null) {
101: throw exception[0];
102: }
103: assertTrue("Did not complete file transfer successfully",
104: success[0]);
105:
106: assertEquals(
107: "Written messages should be 1 (we wrote one file)", 1,
108: session.getWrittenMessages());
109: assertEquals("Written bytes should match file size", FILE_SIZE,
110: session.getWrittenBytes());
111:
112: connector.dispose();
113: acceptor.dispose();
114: }
115:
116: private File createLargeFile() throws IOException {
117: File largeFile = File.createTempFile("mina-test", "largefile");
118: FileChannel channel = new FileOutputStream(largeFile)
119: .getChannel();
120: ByteBuffer buffer = createBuffer();
121: channel.write(buffer);
122: channel.close();
123: return largeFile;
124: }
125:
126: private ByteBuffer createBuffer() {
127: ByteBuffer buffer = ByteBuffer.allocate(FILE_SIZE);
128: for (int i = 0; i < FILE_SIZE / 4; i++) {
129: buffer.putInt(i);
130: }
131: buffer.flip();
132: return buffer;
133: }
134:
135: }
|