1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.nio.channels;
18
19import java.io.IOException;
20import java.io.InputStream;
21import java.io.OutputStream;
22import java.nio.ByteBuffer;
23import java.nio.channels.AsynchronousByteChannel;
24import java.nio.channels.Channels;
25import java.nio.channels.IllegalBlockingModeException;
26import java.nio.channels.Pipe;
27import java.nio.channels.WritableByteChannel;
28import java.util.Arrays;
29import java.util.concurrent.Future;
30import junit.framework.TestCase;
31import org.mockito.ArgumentCaptor;
32import org.mockito.Mockito;
33import org.mockito.invocation.InvocationOnMock;
34import org.mockito.stubbing.Answer;
35import static org.mockito.Mockito.isA;
36import static org.mockito.Mockito.mock;
37import static org.mockito.Mockito.when;
38
39
40public final class ChannelsTest extends TestCase {
41
42    public void testStreamNonBlocking() throws IOException {
43        Pipe.SourceChannel sourceChannel = createNonBlockingChannel("abc".getBytes("UTF-8"));
44        try {
45            Channels.newInputStream(sourceChannel).read();
46            fail();
47        } catch (IllegalBlockingModeException expected) {
48        }
49    }
50
51    /**
52     * This fails on the RI which violates its own promise to throw when
53     * read in non-blocking mode.
54     */
55    public void testReaderNonBlocking() throws IOException {
56        Pipe.SourceChannel sourceChannel = createNonBlockingChannel("abc".getBytes("UTF-8"));
57        try {
58            Channels.newReader(sourceChannel, "UTF-8").read();
59            fail();
60        } catch (IllegalBlockingModeException expected) {
61        }
62    }
63
64    private Pipe.SourceChannel createNonBlockingChannel(byte[] content) throws IOException {
65        Pipe pipe = Pipe.open();
66        WritableByteChannel sinkChannel = pipe.sink();
67        sinkChannel.write(ByteBuffer.wrap(content));
68        Pipe.SourceChannel sourceChannel = pipe.source();
69        sourceChannel.configureBlocking(false);
70        return sourceChannel;
71    }
72
73    public void testInputStreamAsynchronousByteChannel() throws Exception {
74        AsynchronousByteChannel abc = mock(AsynchronousByteChannel.class);
75        InputStream is = Channels.newInputStream(abc);
76        Future<Integer> result = mock(Future.class);
77        ArgumentCaptor<ByteBuffer> bbCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
78        final byte[] bytesRead = new byte[10];
79
80        when(abc.read(bbCaptor.capture())).thenReturn(result);
81        when(result.get()).thenAnswer(
82            new Answer<Integer>() {
83                public Integer answer(InvocationOnMock invocation) {
84                    ByteBuffer bb = bbCaptor.getValue();
85                    assertEquals(bytesRead.length, bb.remaining());
86                    // Write '7' bytes
87                    bb.put(new byte[] {0, 1, 2, 3, 4, 5, 6});
88                    return 7;
89                }
90            });
91
92        assertEquals(7, is.read(bytesRead));
93        // Only 7 bytes of data should be written into the buffer
94        byte[] bytesExpected = new byte[] { 0, 1, 2, 3, 4, 5, 6, 0, 0, 0 };
95        assertTrue(Arrays.equals(bytesExpected, bytesRead));
96
97        Mockito.verify(abc).read(isA(ByteBuffer.class));
98        Mockito.verify(result).get();
99    }
100
101    public void testOutputStreamAsynchronousByteChannel() throws Exception {
102        AsynchronousByteChannel abc = mock(AsynchronousByteChannel.class);
103        OutputStream os = Channels.newOutputStream(abc);
104        Future<Integer> result = mock(Future.class);
105        ArgumentCaptor<ByteBuffer> bbCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
106        final byte[] data = "world".getBytes();
107
108        when(abc.write(bbCaptor.capture())).thenReturn(result);
109        when(result.get()).thenAnswer(
110            new Answer<Integer>() {
111                public Integer answer(InvocationOnMock invocation) {
112                    ByteBuffer bb = bbCaptor.getValue();
113                    assertEquals(data.length, bb.remaining());
114                    byte[] readData = new byte[data.length];
115                    // Read the whole thing
116                    bb.get(readData);
117                    assertTrue(Arrays.equals(data, readData));
118                    return data.length;
119                }
120            });
121
122        os.write(data);
123
124        Mockito.verify(abc).write(isA(ByteBuffer.class));
125        Mockito.verify(result).get();
126  }
127
128}
129