Implement StringWriter and PrintWriter

This commit is contained in:
Alexey Andreev 2017-10-26 15:51:01 +03:00
parent c224c57f98
commit f61567dfde
4 changed files with 1078 additions and 0 deletions

View File

@ -0,0 +1,267 @@
/*
* Copyright 2017 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.io;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Formatter;
import java.util.Locale;
import java.util.Objects;
public class TPrintWriter extends Writer {
protected Writer out;
private boolean ioError;
private boolean autoflush;
public TPrintWriter(OutputStream out) {
this(new OutputStreamWriter(out), false);
}
public TPrintWriter(OutputStream out, boolean autoflush) {
this(new OutputStreamWriter(out), autoflush);
}
public TPrintWriter(Writer wr) {
this(wr, false);
}
public TPrintWriter(Writer wr, boolean autoflush) {
super(wr);
this.autoflush = autoflush;
out = wr;
}
public boolean checkError() {
Writer delegate = out;
if (delegate == null) {
return ioError;
}
flush();
return ioError;
}
@Override
public void close() {
if (out != null) {
try {
out.close();
} catch (IOException e) {
setError();
}
out = null;
}
}
@Override
public void flush() {
if (out != null) {
try {
out.flush();
} catch (IOException e) {
setError();
}
} else {
setError();
}
}
public TPrintWriter format(String format, Object... args) {
return format(Locale.getDefault(), format, args);
}
public TPrintWriter format(Locale l, String format, Object... args) {
Objects.requireNonNull(format);
new Formatter(this, l).format(format, args);
if (autoflush) {
flush();
}
return this;
}
public TPrintWriter printf(String format, Object... args) {
return format(format, args);
}
public TPrintWriter printf(Locale l, String format, Object... args) {
return format(l, format, args);
}
public void print(char[] charArray) {
print(new String(charArray, 0, charArray.length));
}
public void print(char ch) {
print(String.valueOf(ch));
}
public void print(double dnum) {
print(String.valueOf(dnum));
}
public void print(float fnum) {
print(String.valueOf(fnum));
}
public void print(int inum) {
print(String.valueOf(inum));
}
public void print(long lnum) {
print(String.valueOf(lnum));
}
public void print(Object obj) {
print(String.valueOf(obj));
}
public void print(String str) {
write(str != null ? str : String.valueOf((Object) null));
}
public void print(boolean bool) {
print(String.valueOf(bool));
}
public void println() {
print("\n");
if (autoflush) {
flush();
}
}
public void println(char[] charArray) {
println(new String(charArray, 0, charArray.length));
}
public void println(char ch) {
println(String.valueOf(ch));
}
public void println(double dnum) {
println(String.valueOf(dnum));
}
public void println(float fnum) {
println(String.valueOf(fnum));
}
public void println(int inum) {
println(String.valueOf(inum));
}
public void println(long lnum) {
println(String.valueOf(lnum));
}
public void println(Object obj) {
println(String.valueOf(obj));
}
public void println(String str) {
print(str);
println();
}
public void println(boolean bool) {
println(String.valueOf(bool));
}
protected void setError() {
ioError = true;
}
@Override
public void write(char[] buf) {
write(buf, 0, buf.length);
}
@Override
public void write(char[] buf, int offset, int count) {
doWrite(buf, offset, count);
}
@Override
public void write(int oneChar) {
doWrite(new char[] { (char) oneChar }, 0, 1);
}
private void doWrite(char[] buf, int offset, int count) {
if (out != null) {
try {
out.write(buf, offset, count);
} catch (IOException e) {
setError();
}
} else {
setError();
}
}
@Override
public void write(String str) {
write(str.toCharArray());
}
@Override
public void write(String str, int offset, int count) {
write(str.substring(offset, offset + count).toCharArray());
}
@Override
public TPrintWriter append(char c) {
write(c);
return this;
}
@Override
public TPrintWriter append(CharSequence csq) {
if (null == csq) {
append("null", 0, 4);
} else {
append(csq, 0, csq.length());
}
return this;
}
@Override
public TPrintWriter append(CharSequence csq, int start, int end) {
if (null == csq) {
csq = "null";
}
String output = csq.subSequence(start, end).toString();
write(output, 0, output.length());
return this;
}
}

View File

@ -0,0 +1,126 @@
/*
* Copyright 2017 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.io;
import java.io.IOException;
import java.io.Writer;
public class TStringWriter extends Writer {
private StringBuffer buf;
public TStringWriter() {
buf = new StringBuffer(16);
lock = buf;
}
public TStringWriter(int initialSize) {
if (initialSize < 0) {
throw new IllegalArgumentException();
}
buf = new StringBuffer(initialSize);
lock = buf;
}
@Override
public void close() throws IOException {
/* empty */
}
@Override
public void flush() {
/* empty */
}
public StringBuffer getBuffer() {
return buf;
}
@Override
public String toString() {
return buf.toString();
}
@Override
public void write(char[] cbuf, int offset, int count) {
if (offset < 0 || offset > cbuf.length || count < 0 || count > cbuf.length - offset) {
throw new IndexOutOfBoundsException();
}
if (count == 0) {
return;
}
buf.append(cbuf, offset, count);
}
@Override
public void write(int oneChar) {
buf.append((char) oneChar);
}
@Override
public void write(String str) {
buf.append(str);
}
@Override
public void write(String str, int offset, int count) {
String sub = str.substring(offset, offset + count);
buf.append(sub);
}
@Override
public TStringWriter append(char c) {
write(c);
return this;
}
@Override
public TStringWriter append(CharSequence csq) {
if (null == csq) {
write("null");
} else {
write(csq.toString());
}
return this;
}
@Override
public TStringWriter append(CharSequence csq, int start, int end) {
if (null == csq) {
csq = "null";
}
String output = csq.subSequence(start, end).toString();
write(output, 0, output.length());
return this;
}
}

View File

@ -0,0 +1,532 @@
/*
* Copyright 2017 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.io;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Locale;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner;
@RunWith(TeaVMTestRunner.class)
public class PrintWriterTest {
static class Bogus {
public String toString() {
return "Bogus";
}
}
PrintWriter pw;
ByteArrayOutputStream bao;
BufferedReader br;
public PrintWriterTest() {
bao = new ByteArrayOutputStream();
pw = new PrintWriter(bao, false);
}
@Test
public void constructorLjava_io_OutputStream() {
String s;
pw.println("Random Chars");
pw.write("Hello World");
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
s = br.readLine();
assertEquals("Incorrect string written/read", "Random Chars", s);
s = br.readLine();
assertTrue("Incorrect string written/read: " + s, s.equals("Hello World"));
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
}
@Test
public void constructorLjava_io_OutputStreamZ() {
String s;
pw = new PrintWriter(bao, true);
pw.println("Random Chars");
pw.write("Hello World");
try {
br = new BufferedReader(new StringReader(bao.toString()));
s = br.readLine();
assertTrue("Incorrect string written/read: " + s, s.equals("Random Chars"));
pw.flush();
br = new BufferedReader(new StringReader(bao.toString()));
s = br.readLine();
assertTrue("Incorrect string written/read: " + s, s.equals("Random Chars"));
s = br.readLine();
assertTrue("Incorrect string written/read: " + s, s.equals("Hello World"));
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
}
@Test
public void constructorLjava_io_Writer() {
StringWriter sw = new StringWriter();
pw = new PrintWriter(sw);
pw.print("Hello");
pw.flush();
assertEquals("Failed to construct proper writer", "Hello", sw.toString());
}
@Test
public void constructorLjava_io_WriterZ() {
StringWriter sw = new StringWriter();
pw = new PrintWriter(sw, true);
pw.print("Hello");
// Auto-flush should have happened
assertEquals("Failed to construct proper writer", "Hello", sw.toString());
}
@Test
public void checkError() {
pw.close();
pw.print(490000000000.08765);
assertTrue("Failed to return error", pw.checkError());
}
@Test
public void close() {
pw.close();
pw.println("l");
assertTrue("Write on closed stream failed to generate error", pw.checkError());
}
@Test
public void flush() {
final double dub = 490000000000.08765;
pw.print(dub);
pw.flush();
assertTrue("Failed to flush", new String(bao.toByteArray()).equals(String.valueOf(dub)));
}
@Test
public void print$C() {
String s = null;
char[] schars = new char[11];
"Hello World".getChars(0, 11, schars, 0);
pw.print(schars);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect char[] string: " + s, s.equals("Hello World"));
}
@Test
public void printC() {
pw.print('c');
pw.flush();
assertEquals("Wrote incorrect char string", "c", new String(bao.toByteArray()));
}
@Test
public void printD() {
final double dub = 490000000000.08765;
pw.print(dub);
pw.flush();
assertTrue("Wrote incorrect double string", new String(bao.toByteArray()).equals(String.valueOf(dub)));
}
@Test
public void printF() {
final float flo = 49.08765f;
pw.print(flo);
pw.flush();
assertTrue("Wrote incorrect float string", new String(bao.toByteArray()).equals(String.valueOf(flo)));
}
@Test
public void printI() {
pw.print(4908765);
pw.flush();
assertEquals("Wrote incorrect int string", "4908765", new String(bao.toByteArray()));
}
@Test
public void printJ() {
pw.print(49087650000L);
pw.flush();
assertEquals("Wrote incorrect long string", "49087650000", new String(bao.toByteArray()));
}
@Test
public void printLjava_lang_Object() {
pw.print((Object) null);
pw.flush();
assertEquals("Did not write null", "null", new String(bao.toByteArray()));
bao.reset();
pw.print(new Bogus());
pw.flush();
assertEquals("Wrote in incorrect Object string", "Bogus", new String(bao.toByteArray()));
}
@Test
public void printLjava_lang_String() {
pw.print((String) null);
pw.flush();
assertEquals("did not write null", "null", new String(bao.toByteArray()));
bao.reset();
pw.print("Hello World");
pw.flush();
assertEquals("Wrote incorrect string", "Hello World", new String(bao.toByteArray()));
}
@Test
public void printZ() {
pw.print(true);
pw.flush();
assertEquals("Wrote in incorrect boolean string", "true", new String(bao.toByteArray()));
}
@Test
public void println() {
String s;
pw.println("Blarg");
pw.println();
pw.println("Bleep");
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
s = br.readLine();
assertTrue("Wrote incorrect line: " + s, s.equals("Blarg"));
s = br.readLine();
assertTrue("Wrote incorrect line: " + s, s.equals(""));
s = br.readLine();
assertTrue("Wrote incorrect line: " + s, s.equals("Bleep"));
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
}
@Test
public void test_println$C() {
String s = null;
char[] schars = new char[11];
"Hello World".getChars(0, 11, schars, 0);
pw.println("Random Chars");
pw.println(schars);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
s = br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect char[] string: " + s, s.equals("Hello World"));
}
@Test
public void printlnC() {
String s = null;
pw.println("Random Chars");
pw.println('c');
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
s = br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect char string: " + s, s.equals("c"));
}
@Test
public void printlnD() {
String s = null;
final double dub = 4000000000000000.657483;
pw.println("Random Chars");
pw.println(dub);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect double string: " + s, s.equals(String.valueOf(dub)));
}
@Test
public void printlnF() {
String s;
final float flo = 40.4646464f;
pw.println("Random Chars");
pw.println(flo);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
assertTrue("Wrote incorrect float string: " + s + " wanted: " + String.valueOf(flo),
s.equals(String.valueOf(flo)));
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
}
@Test
public void printlnI() {
String s = null;
pw.println("Random Chars");
pw.println(400000);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect int string: " + s, s.equals("400000"));
}
@Test
public void printlnJ() {
String s = null;
pw.println("Random Chars");
pw.println(4000000000000L);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect long string: " + s, s.equals("4000000000000"));
}
@Test
public void printlnLjava_lang_Object() {
String s = null;
pw.println("Random Chars");
pw.println(new Bogus());
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect Object string: " + s, s.equals("Bogus"));
}
@Test
public void printlnLjava_lang_String() {
String s = null;
pw.println("Random Chars");
pw.println("Hello World");
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect string: " + s, s.equals("Hello World"));
}
@Test
public void printlnZ() {
String s = null;
pw.println("Random Chars");
pw.println(false);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect boolean string: " + s, s.equals("false"));
}
@Test
public void write$C() {
String s = null;
char[] schars = new char[11];
"Hello World".getChars(0, 11, schars, 0);
pw.println("Random Chars");
pw.write(schars);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test: " + e.getMessage());
}
assertTrue("Wrote incorrect char[] string: " + s, s.equals("Hello World"));
}
@Test
public void write$CII() {
String s = null;
char[] schars = new char[11];
"Hello World".getChars(0, 11, schars, 0);
pw.println("Random Chars");
pw.write(schars, 6, 5);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect char[] string: " + s, s.equals("World"));
}
@Test
public void writeI() throws IOException {
char[] cab = new char[3];
pw.write('a');
pw.write('b');
pw.write('c');
pw.flush();
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(bao.toByteArray()));
cab[0] = (char) isr.read();
cab[1] = (char) isr.read();
cab[2] = (char) isr.read();
assertTrue("Wrote incorrect ints", cab[0] == 'a' && cab[1] == 'b' && cab[2] == 'c');
}
@Test
public void writeLjava_lang_String() {
String s = null;
pw.println("Random Chars");
pw.write("Hello World");
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect char[] string: " + s, s.equals("Hello World"));
}
@Test
public void writeLjava_lang_StringII() {
String s = null;
pw.println("Random Chars");
pw.write("Hello World", 6, 5);
pw.flush();
try {
br = new BufferedReader(new StringReader(bao.toString()));
br.readLine();
s = br.readLine();
} catch (IOException e) {
fail("IOException during test : " + e.getMessage());
}
assertTrue("Wrote incorrect char[] string: " + s, s.equals("World"));
}
@Test
public void appendChar() {
char testChar = ' ';
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintWriter printWriter = new PrintWriter(out);
printWriter.append(testChar);
printWriter.flush();
assertEquals(String.valueOf(testChar), out.toString());
printWriter.close();
}
@Test
public void appendCharSequence() {
String testString = "My Test String";
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintWriter printWriter = new PrintWriter(out);
printWriter.append(testString);
printWriter.flush();
assertEquals(testString, out.toString());
printWriter.close();
}
@Test
public void appendCharSequenceIntInt() {
String testString = "My Test String";
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintWriter printWriter = new PrintWriter(out);
printWriter.append(testString, 1, 3);
printWriter.flush();
assertEquals(testString.substring(1, 3), out.toString());
printWriter.close();
}
@Test
public void printfLjava_lang_String$Ljava_lang_Object() {
pw.printf("%s %s", "Hello", "World");
pw.flush();
assertEquals("Wrote incorrect string", "Hello World", new String(bao.toByteArray()));
}
@Test
public void printfLjava_util_Locale_Ljava_lang_String_$Ljava_lang_Object() {
pw.printf(Locale.US, "%s %s", "Hello", "World");
pw.flush();
assertEquals("Wrote incorrect string", "Hello World", new String(bao.toByteArray()));
}
}

View File

@ -0,0 +1,153 @@
/*
* Copyright 2017 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.io;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.StringWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner;
@RunWith(TeaVMTestRunner.class)
public class StringWriterTest {
StringWriter sw = new StringWriter();
@Test
public void close() {
try {
sw.close();
} catch (IOException e) {
fail("IOException closing StringWriter : " + e.getMessage());
}
}
@Test
public void flush() {
sw.flush();
sw.write('c');
assertEquals("Failed to flush char", "c", sw.toString());
}
@Test
public void getBuffer() {
sw.write("This is a test string");
StringBuffer sb = sw.getBuffer();
assertEquals("Incorrect buffer returned", "This is a test string", sb.toString());
}
@Test
public void toStringWorks() {
sw.write("This is a test string");
assertEquals("Incorrect string returned", "This is a test string", sw.toString());
}
@Test
public void write$CII() {
char[] c = new char[1000];
"This is a test string".getChars(0, 21, c, 0);
sw.write(c, 0, 21);
assertEquals("Chars not written properly", "This is a test string", sw.toString());
}
@Test
public void write$CII_2() {
StringWriter obj;
try {
obj = new StringWriter();
obj.write(new char[0], 0, -1);
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException t) {
assertEquals("IndexOutOfBoundsException rather than a subclass expected",
IndexOutOfBoundsException.class, t.getClass());
}
}
@Test
public void write$CII_3() {
StringWriter obj;
try {
obj = new StringWriter();
obj.write(new char[0], -1, 0);
fail("IndexOutOfBoundsException expected");
} catch (ArrayIndexOutOfBoundsException t) {
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException t) {
// Do nothing
}
}
@Test
public void test_write$CII_4() {
StringWriter obj;
try {
obj = new StringWriter();
obj.write(new char[0], -1, -1);
fail("IndexOutOfBoundsException expected");
} catch (ArrayIndexOutOfBoundsException t) {
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException t) {
// Do nothing
}
}
@Test
public void writeI() {
sw.write('c');
assertEquals("Char not written properly", "c", sw.toString());
}
@Test
public void writeLjava_lang_String() {
sw.write("This is a test string");
assertEquals("String not written properly", "This is a test string", sw.toString());
}
@Test
public void writeLjava_lang_StringII() {
sw.write("This is a test string", 2, 2);
assertEquals("String not written properly", "is", sw.toString());
}
@Test
public void appendChar() throws IOException {
char testChar = ' ';
StringWriter stringWriter = new StringWriter(20);
stringWriter.append(testChar);
assertEquals(String.valueOf(testChar), stringWriter.toString());
stringWriter.close();
}
@Test
public void appendCharSequence() throws IOException {
String testString = "My Test String";
StringWriter stringWriter = new StringWriter(20);
stringWriter.append(testString);
assertEquals(String.valueOf(testString), stringWriter.toString());
stringWriter.close();
}
@Test
public void appendCharSequenceIntInt() throws IOException {
String testString = "My Test String";
StringWriter stringWriter = new StringWriter(20);
stringWriter.append(testString, 1, 3);
assertEquals(testString.substring(1, 3), stringWriter.toString());
stringWriter.close();
}
}