1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.io;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24import dalvik.annotation.TestTargetNew;
25
26import java.io.OutputStream;
27import java.io.IOException;
28
29@TestTargetClass(
30        value = OutputStream.class,
31        untestedMethods = {
32            @TestTargetNew(
33                    method = "OutputStream",
34                    args = {},
35                    level = TestLevel.NOT_NECESSARY,
36                    notes = "Constructor just calls super()."
37            ),
38            @TestTargetNew(
39                    method = "close",
40                    args = {},
41                    level = TestLevel.NOT_NECESSARY,
42                    notes = "According to specification, the implementation " +
43                            "does nothing."
44            ),
45            @TestTargetNew(
46                    method = "flush",
47                    args = {},
48                    level = TestLevel.NOT_NECESSARY,
49                    notes = "According to specification, the implementation " +
50                            "does nothing."
51            )
52        }
53)
54public class OutputStreamTest extends junit.framework.TestCase {
55
56    class BasicOutputStream extends OutputStream {
57
58        private static final int BUFFER_SIZE = 20;
59        private byte[] buffer;
60        private int position;
61
62        public BasicOutputStream() {
63            buffer = new byte[BUFFER_SIZE];
64            position = 0;
65        }
66
67        public void write(int oneByte) throws IOException {
68            if (position < BUFFER_SIZE) {
69                buffer[position] = (byte) (oneByte & 255);
70                position++;
71            } else {
72                throw new IOException("Internal buffer overflow.");
73            }
74        }
75
76        public byte[] getBuffer() {
77            return buffer;
78        }
79    }
80
81    private final byte[] shortByteArray = "Lorem ipsum...".getBytes();
82    private final byte[] longByteArray = "Lorem ipsum dolor sit amet...".getBytes();
83
84    @TestTargetNew(
85        level = TestLevel.COMPLETE,
86        notes = "Verifies write(byte[]).",
87        method = "write",
88        args = {byte[].class}
89    )
90    public void test_write$B() {
91        BasicOutputStream bos = new BasicOutputStream();
92        boolean expected;
93        byte[] buffer;
94
95        try {
96            bos.write(shortByteArray);
97        } catch (IOException e) {
98            fail("Test 1: Unexpected IOException encountered.");
99        }
100
101        expected = true;
102        buffer = bos.getBuffer();
103        for (int i = 0; i < (shortByteArray.length) && expected; i++) {
104            expected = (shortByteArray[i] == buffer[i]);
105        }
106        assertTrue("Test 1: Test byte array has not been written correctly.",
107                   expected);
108
109        try {
110            bos.write(longByteArray);
111            fail("Test 2: IOException expected.");
112        } catch (IOException e) {}
113    }
114
115    @TestTargetNew(
116        level = TestLevel.COMPLETE,
117        notes = "Verifies write(byte[], int, int).",
118        method = "write",
119        args = {byte[].class, int.class, int.class}
120    )
121    public void test_write$BII() {
122        BasicOutputStream bos = new BasicOutputStream();
123        boolean expected;
124        byte[] buffer;
125
126        try {
127            bos.write(shortByteArray, 6, 5);
128        } catch (IOException e) {
129            fail("Test 1: Unexpected IOException encountered.");
130        }
131
132        expected = true;
133        buffer = bos.getBuffer();
134        for (int i = 6, j = 0; j < 5 && expected; i++, j++) {
135            expected = (shortByteArray[i] == buffer[j]);
136        }
137        assertTrue("Test 1: Test byte array has not been written correctly.",
138                   expected);
139
140        try {
141            bos.write(longByteArray, 5, 20);
142            fail("Test 2: IOException expected.");
143        } catch (IOException e) {}
144
145        try {
146            bos.write(longByteArray, -1, 10);
147            fail("Test 3: IndexOutOfBoundsException expected.");
148        } catch (IndexOutOfBoundsException e) {
149            // Expected
150        } catch (IOException e) {
151            fail("Test 3: Unexpected IOException encountered.");
152        }
153
154        try {
155            bos.write(longByteArray, 10, -1);
156            fail("Test 4: IndexOutOfBoundsException expected.");
157        } catch (IndexOutOfBoundsException e) {
158            // Expected
159        } catch (IOException e) {
160            fail("Test 4: Unexpected IOException encountered.");
161        }
162
163        try {
164            bos.write(longByteArray, 20, 10);
165            fail("Test 5: IndexOutOfBoundsException expected.");
166        } catch (IndexOutOfBoundsException e) {
167            // Expected
168        } catch (IOException e) {
169            fail("Test 5: Unexpected IOException encountered.");
170        }
171    }
172}
173