|
26 | 26 | import io.netty.channel.embedded.EmbeddedChannel;
|
27 | 27 | import io.netty.util.CharsetUtil;
|
28 | 28 | import io.netty.util.ReferenceCountUtil;
|
| 29 | +import org.junit.Assert; |
29 | 30 | import org.junit.Test;
|
30 | 31 |
|
31 | 32 | import java.io.ByteArrayInputStream;
|
32 | 33 | import java.io.File;
|
33 | 34 | import java.io.FileOutputStream;
|
34 | 35 | import java.io.IOException;
|
| 36 | +import java.io.RandomAccessFile; |
35 | 37 | import java.nio.channels.Channels;
|
| 38 | +import java.nio.channels.ClosedChannelException; |
| 39 | +import java.nio.channels.FileChannel; |
36 | 40 | import java.util.concurrent.CountDownLatch;
|
37 | 41 | import java.util.concurrent.atomic.AtomicBoolean;
|
38 | 42 | import java.util.concurrent.atomic.AtomicInteger;
|
@@ -102,6 +106,41 @@ public void testChunkedNioFile() throws IOException {
|
102 | 106 | check(new ChunkedNioFile(TMP), new ChunkedNioFile(TMP), new ChunkedNioFile(TMP));
|
103 | 107 | }
|
104 | 108 |
|
| 109 | + @Test |
| 110 | + public void testChunkedNioFileLeftPositionUnchanged() throws IOException { |
| 111 | + FileChannel in = null; |
| 112 | + final long expectedPosition = 10; |
| 113 | + try { |
| 114 | + in = new RandomAccessFile(TMP, "r").getChannel(); |
| 115 | + in.position(expectedPosition); |
| 116 | + check(new ChunkedNioFile(in) { |
| 117 | + @Override |
| 118 | + public void close() throws Exception { |
| 119 | + //no op |
| 120 | + } |
| 121 | + }); |
| 122 | + Assert.assertTrue(in.isOpen()); |
| 123 | + Assert.assertEquals(expectedPosition, in.position()); |
| 124 | + } finally { |
| 125 | + if (in != null) { |
| 126 | + in.close(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test(expected = ClosedChannelException.class) |
| 132 | + public void testChunkedNioFileFailOnClosedFileChannel() throws IOException { |
| 133 | + final FileChannel in = new RandomAccessFile(TMP, "r").getChannel(); |
| 134 | + in.close(); |
| 135 | + check(new ChunkedNioFile(in) { |
| 136 | + @Override |
| 137 | + public void close() throws Exception { |
| 138 | + //no op |
| 139 | + } |
| 140 | + }); |
| 141 | + Assert.fail(); |
| 142 | + } |
| 143 | + |
105 | 144 | @Test
|
106 | 145 | public void testUnchunkedData() throws IOException {
|
107 | 146 | check(Unpooled.wrappedBuffer(BYTES));
|
|
0 commit comments