001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.CharArrayReader;
021: import java.io.IOException;
022:
023: public class CharArrayReaderTest extends junit.framework.TestCase {
024:
025: char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
026:
027: CharArrayReader cr;
028:
029: /**
030: * @tests java.io.CharArrayReader#CharArrayReader(char[])
031: */
032: public void test_Constructor$C() throws IOException {
033: cr = new CharArrayReader(hw);
034: assertTrue("Failed to create reader", cr.ready());
035: }
036:
037: /**
038: * @tests java.io.CharArrayReader#CharArrayReader(char[], int, int)
039: */
040: public void test_Constructor$CII() throws IOException {
041: cr = new CharArrayReader(hw, 5, 5);
042: assertTrue("Failed to create reader", cr.ready());
043:
044: int c = cr.read();
045: assertTrue("Created incorrect reader--returned '" + (char) c
046: + "' intsead of 'W'", c == 'W');
047: }
048:
049: /**
050: * @tests java.io.CharArrayReader#close()
051: */
052: public void test_close() {
053: cr = new CharArrayReader(hw);
054: cr.close();
055: try {
056: cr.read();
057: fail("Failed to throw exception on read from closed stream");
058: } catch (IOException e) {
059: // Expected
060: }
061:
062: // No-op
063: cr.close();
064: }
065:
066: /**
067: * @tests java.io.CharArrayReader#mark(int)
068: */
069: public void test_markI() throws IOException {
070: cr = new CharArrayReader(hw);
071: cr.skip(5L);
072: cr.mark(100);
073: cr.read();
074: cr.reset();
075: assertEquals("Failed to mark correct position", 'W', cr.read());
076: }
077:
078: /**
079: * @tests java.io.CharArrayReader#markSupported()
080: */
081: public void test_markSupported() {
082: cr = new CharArrayReader(hw);
083: assertTrue("markSupported returned false", cr.markSupported());
084: }
085:
086: /**
087: * @tests java.io.CharArrayReader#read()
088: */
089: public void test_read() throws IOException {
090: cr = new CharArrayReader(hw);
091: assertEquals("Read returned incorrect char", 'H', cr.read());
092: cr = new CharArrayReader(new char[] { '\u8765' });
093: assertTrue("Incorrect double byte char", cr.read() == '\u8765');
094: }
095:
096: /**
097: * @tests java.io.CharArrayReader#read(char[], int, int)
098: */
099: public void test_read$CII() throws IOException {
100: char[] c = new char[11];
101: cr = new CharArrayReader(hw);
102: cr.read(c, 1, 10);
103: assertTrue("Read returned incorrect chars",
104: new String(c, 1, 10).equals(new String(hw, 0, 10)));
105: }
106:
107: /**
108: * @tests java.io.CharArrayReader#ready()
109: */
110: public void test_ready() throws IOException {
111: cr = new CharArrayReader(hw);
112: assertTrue("ready returned false", cr.ready());
113: cr.skip(1000);
114: assertTrue("ready returned true", !cr.ready());
115: cr.close();
116:
117: try {
118: cr.ready();
119: fail("No exception 1");
120: } catch (IOException e) {
121: // expected
122: }
123: try {
124: cr = new CharArrayReader(hw);
125: cr.close();
126: cr.ready();
127: fail("No exception 2");
128: } catch (IOException e) {
129: // expected
130: }
131: }
132:
133: /**
134: * @tests java.io.CharArrayReader#reset()
135: */
136: public void test_reset() throws IOException {
137: cr = new CharArrayReader(hw);
138: cr.skip(5L);
139: cr.mark(100);
140: cr.read();
141: cr.reset();
142: assertEquals("Reset failed to return to marker position", 'W',
143: cr.read());
144:
145: // Regression for HARMONY-4357
146: String str = "offsetHello world!";
147: char[] data = new char[str.length()];
148: str.getChars(0, str.length(), data, 0);
149: int offsetLength = 6;
150: int length = data.length - offsetLength;
151:
152: CharArrayReader reader = new CharArrayReader(data,
153: offsetLength, length);
154: reader.reset();
155: for (int i = 0; i < length; i++) {
156: assertEquals(data[offsetLength + i], (char) reader.read());
157: }
158: }
159:
160: /**
161: * @tests java.io.CharArrayReader#skip(long)
162: */
163: public void test_skipJ() throws IOException {
164: cr = new CharArrayReader(hw);
165: long skipped = cr.skip(5L);
166:
167: assertEquals("Failed to skip correct number of chars", 5L,
168: skipped);
169: assertEquals("Skip skipped wrong chars", 'W', cr.read());
170: }
171:
172: /**
173: * Tears down the fixture, for example, close a network connection. This
174: * method is called after a test is executed.
175: */
176: protected void tearDown() {
177: if (cr != null)
178: cr.close();
179: }
180: }
|