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.IOException;
021: import java.io.StringReader;
022:
023: public class StringReaderTest extends junit.framework.TestCase {
024:
025: String testString = "This is a test string";
026:
027: StringReader sr;
028:
029: /**
030: * @tests java.io.StringReader#StringReader(java.lang.String)
031: */
032: public void test_ConstructorLjava_lang_String() {
033: // Test for method java.io.StringReader(java.lang.String)
034: assertTrue("Used in tests", true);
035: }
036:
037: /**
038: * @tests java.io.StringReader#close()
039: */
040: public void test_close() throws Exception {
041: // Test for method void java.io.StringReader.close()
042: try {
043: sr = new StringReader(testString);
044: sr.close();
045: char[] buf = new char[10];
046: sr.read(buf, 0, 2);
047: fail("Close failed");
048: } catch (java.io.IOException e) {
049: return;
050: }
051: }
052:
053: /**
054: * @tests java.io.StringReader#mark(int)
055: */
056: public void test_markI() throws Exception {
057: // Test for method void java.io.StringReader.mark(int)
058: sr = new StringReader(testString);
059: sr.skip(5);
060: sr.mark(0);
061: sr.skip(5);
062: sr.reset();
063: char[] buf = new char[10];
064: sr.read(buf, 0, 2);
065: assertTrue("Failed to return to mark", new String(buf, 0, 2)
066: .equals(testString.substring(5, 7)));
067: }
068:
069: /**
070: * @tests java.io.StringReader#markSupported()
071: */
072: public void test_markSupported() {
073: // Test for method boolean java.io.StringReader.markSupported()
074:
075: sr = new StringReader(testString);
076: assertTrue("markSupported returned false", sr.markSupported());
077: }
078:
079: /**
080: * @tests java.io.StringReader#read()
081: */
082: public void test_read() throws Exception {
083: // Test for method int java.io.StringReader.read()
084: sr = new StringReader(testString);
085: int r = sr.read();
086: assertEquals("Failed to read char", 'T', r);
087: sr = new StringReader(new String(new char[] { '\u8765' }));
088: assertTrue("Wrong double byte char", sr.read() == '\u8765');
089: }
090:
091: /**
092: * @tests java.io.StringReader#read(char[], int, int)
093: */
094: public void test_read$CII() throws Exception {
095: // Test for method int java.io.StringReader.read(char [], int, int)
096: sr = new StringReader(testString);
097: char[] buf = new char[testString.length()];
098: int r = sr.read(buf, 0, testString.length());
099: assertTrue("Failed to read chars", r == testString.length());
100: assertTrue("Read chars incorrectly", new String(buf, 0, r)
101: .equals(testString));
102: }
103:
104: /**
105: * @tests java.io.StringReader#ready()
106: */
107: public void test_ready() throws Exception {
108: // Test for method boolean java.io.StringReader.ready()
109: sr = new StringReader(testString);
110: assertTrue("Steam not ready", sr.ready());
111: sr.close();
112: int r = 0;
113: try {
114: sr.ready();
115: } catch (IOException e) {
116: r = 1;
117: }
118: assertEquals("Expected IOException not thrown in read()", 1, r);
119: }
120:
121: /**
122: * @tests java.io.StringReader#reset()
123: */
124: public void test_reset() throws Exception {
125: // Test for method void java.io.StringReader.reset()
126: sr = new StringReader(testString);
127: sr.skip(5);
128: sr.mark(0);
129: sr.skip(5);
130: sr.reset();
131: char[] buf = new char[10];
132: sr.read(buf, 0, 2);
133: assertTrue("Failed to reset properly", new String(buf, 0, 2)
134: .equals(testString.substring(5, 7)));
135: }
136:
137: /**
138: * @tests java.io.StringReader#skip(long)
139: */
140: public void test_skipJ() throws Exception {
141: // Test for method long java.io.StringReader.skip(long)
142: sr = new StringReader(testString);
143: sr.skip(5);
144: char[] buf = new char[10];
145: sr.read(buf, 0, 2);
146: assertTrue("Failed to skip properly", new String(buf, 0, 2)
147: .equals(testString.substring(5, 7)));
148: }
149:
150: // Regression test for HARMONY-5077
151: static boolean finish = false;
152:
153: public void test_synchronization() {
154: String anything = "Hello world";
155: final StringReader sr = new StringReader(anything);
156: Thread other = new Thread(new Runnable() {
157: public void run() {
158: sr.close();
159: finish = true;
160: };
161: });
162:
163: synchronized (anything) {
164: other.start();
165: while (!finish) {
166: Thread.yield();
167: }
168: }
169: }
170: }
|