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.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestLevel;
24
25import java.io.StringBufferInputStream;
26
27@TestTargetClass(StringBufferInputStream.class)
28public class StringBufferInputStreamTest extends junit.framework.TestCase {
29
30    StringBufferInputStream sbis;
31
32    /**
33     * @tests java.io.StringBufferInputStream#StringBufferInputStream(java.lang.String)
34     */
35    @TestTargetNew(
36        level = TestLevel.COMPLETE,
37        notes = "",
38        method = "StringBufferInputStream",
39        args = {java.lang.String.class}
40    )
41    public void test_ConstructorLjava_lang_String() {
42        try {
43            new StringBufferInputStream("");
44        } catch (Exception ee) {
45            fail("Exception " + ee.getMessage() + " does not expected in this case");
46        }
47        // Test for method java.io.StringBufferInputStream(java.lang.String)
48    }
49
50    /**
51     * @tests java.io.StringBufferInputStream#available()
52     */
53    @TestTargetNew(
54        level = TestLevel.COMPLETE,
55        notes = "",
56        method = "available",
57        args = {}
58    )
59    public void test_available() {
60        // Test for method int java.io.StringBufferInputStream.available()
61        assertEquals("Returned incorrect number of available bytes", 11, sbis
62                .available());
63    }
64
65    /**
66     * @tests java.io.StringBufferInputStream#read()
67     */
68    @TestTargetNew(
69        level = TestLevel.PARTIAL_COMPLETE,
70        notes = "",
71        method = "read",
72        args = {byte[].class, int.class, int.class}
73    )
74    public void test_read$BII() {
75        // Test for method int java.io.StringBufferInputStream.read()
76        byte[] buf = new byte[5];
77        sbis.skip(6);
78        sbis.read(buf, 0, 5);
79        assertEquals("Returned incorrect chars", "World", new String(buf));
80    }
81
82    /**
83     * @tests java.io.StringBufferInputStream#read()
84     */
85    @TestTargetNew(
86        level = TestLevel.PARTIAL_COMPLETE,
87        notes = "",
88        method = "read",
89        args = {byte[].class, int.class, int.class}
90    )
91    public void test_read$BII_Exception() {
92        // Test for method int java.io.StringBufferInputStream.read()
93        byte[] buf = new byte[10];
94        try {
95            sbis.read(buf, 0, -1);
96            fail("IndexOutOfBoundsException was not thrown");
97        } catch (IndexOutOfBoundsException e) {
98            // Expected
99        }
100        try {
101            sbis.read(buf, -1, 1);
102            fail("IndexOutOfBoundsException was not thrown");
103        } catch (IndexOutOfBoundsException e) {
104            // Expected
105        }
106        try {
107            sbis.read(buf, 10, 1);
108            fail("IndexOutOfBoundsException was not thrown");
109        } catch (IndexOutOfBoundsException e) {
110            // Expected
111        }
112    }
113
114    /**
115     * @tests java.io.StringBufferInputStream#read(byte[], int, int)
116     */
117    @TestTargetNew(
118        level = TestLevel.COMPLETE,
119        notes = "",
120        method = "read",
121        args = {}
122    )
123    public void test_read() {
124        // Test for method int java.io.StringBufferInputStream.read(byte [],
125        // int, int)
126        assertEquals("Read returned incorrect char", 'H', sbis.read());
127    }
128
129    /**
130     * @tests java.io.StringBufferInputStream#reset()
131     */
132    @TestTargetNew(
133        level = TestLevel.COMPLETE,
134        notes = "",
135        method = "reset",
136        args = {}
137    )
138    public void test_reset() {
139        // Test for method void java.io.StringBufferInputStream.reset()
140        long s = sbis.skip(6);
141        assertEquals("Unable to skip correct umber of chars", 6, s);
142        sbis.reset();
143        assertEquals("Failed to reset", 'H', sbis.read());
144    }
145
146    /**
147     * @tests java.io.StringBufferInputStream#skip(long)
148     */
149    @TestTargetNew(
150        level = TestLevel.COMPLETE,
151        notes = "",
152        method = "skip",
153        args = {long.class}
154    )
155    public void test_skipJ() {
156        // Test for method long java.io.StringBufferInputStream.skip(long)
157        long s = sbis.skip(6);
158        assertEquals("Unable to skip correct umber of chars", 6, s);
159        assertEquals("Skip positioned at incorrect char", 'W', sbis.read());
160    }
161
162    /**
163     * Sets up the fixture, for example, open a network connection. This method
164     * is called before a test is executed.
165     */
166    protected void setUp() {
167        sbis = new StringBufferInputStream("Hello World");
168    }
169
170    /**
171     * Tears down the fixture, for example, close a network connection. This
172     * method is called after a test is executed.
173     */
174    protected void tearDown() {
175    }
176}
177