0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 5067405
0N/A * @summary Basic test for classes which implement Appendable.
0N/A */
0N/A
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.BufferedWriter;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.CharArrayWriter;
0N/Aimport java.io.File;
0N/Aimport java.io.FileReader;
0N/Aimport java.io.FileWriter;
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.PrintWriter;
0N/Aimport java.io.StringWriter;
0N/Aimport java.io.Writer;
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.CharBuffer;
0N/A
0N/Ainterface BasicRunnable extends Runnable {
0N/A void init(Appendable a, String csq, String exp);
0N/A Appendable reset(Appendable csq);
0N/A}
0N/A
0N/Apublic class Basic {
0N/A
0N/A private static final String s = "Beware the Jabberwock, my son!";
0N/A private static CharArrayWriter gw = new CharArrayWriter();
0N/A private static ByteArrayOutputStream gos = new ByteArrayOutputStream();
0N/A
0N/A private static File newFile() {
0N/A File f = null;
0N/A try {
0N/A f = File.createTempFile("append", ".txt");
0N/A f.deleteOnExit();
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A return f;
0N/A }
0N/A private static File gf = newFile();
0N/A
0N/A private static int fail = 0;
0N/A private static int pass = 0;
0N/A
0N/A private static Throwable first;
0N/A
0N/A static void pass() {
0N/A pass++;
0N/A }
0N/A
0N/A static void fail(Throwable ex) {
0N/A if (first == null)
0N/A first = ex;
0N/A System.err.println("FAILED: unexpected exception");
0N/A fail++;
0N/A }
0N/A
0N/A static void fail(String fs, Throwable ex) {
0N/A String s = "'" + fs + "': " + ex.getClass().getName() + " not thrown";
0N/A if (first == null)
0N/A first = ex;
0N/A System.err.println("FAILED: " + s);
0N/A fail++;
0N/A }
0N/A
0N/A static void fail(String fs, String exp, String got) {
0N/A String s = "'" + fs + "': Expected '" + exp + "', got '" + got + "'";
0N/A if (first == null)
0N/A first = new RuntimeException(s);
0N/A System.err.println("FAILED: " + s);
0N/A fail++;
0N/A }
0N/A
0N/A static void ck(String s, String exp, String got) {
0N/A if (!exp.equals(got))
0N/A fail(s, exp, got);
0N/A else
0N/A pass();
0N/A }
0N/A
0N/A private static BasicRunnable testBufferedWriter =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A public void init(Appendable bw, String csn, String exp) {
0N/A try {
0N/A ((BufferedWriter)bw).flush();
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("BufferedWriter.append(" + csn + ")", exp, gw.toString());
0N/A }
0N/A public Appendable reset(Appendable bw) {
0N/A gw.reset();
0N/A return bw;
0N/A }};
0N/A
0N/A private static BasicRunnable testCharArrayWriter =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A private CharArrayWriter cw;
0N/A public void init(Appendable cw, String csn, String exp) {
0N/A this.cw = (CharArrayWriter)cw;
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("CharArrayWriter.append(" + csn + ")", exp, cw.toString());
0N/A }
0N/A public Appendable reset(Appendable cw) {
0N/A ((CharArrayWriter)cw).reset();
0N/A return cw;
0N/A }};
0N/A
0N/A private static BasicRunnable testFileWriter =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A public void init(Appendable fw, String csn, String exp) {
0N/A try {
0N/A ((FileWriter)fw).flush();
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A StringBuilder sb = new StringBuilder();
0N/A try {
0N/A BufferedReader in = new BufferedReader(new FileReader(gf));
0N/A String line;
0N/A while (true) {
0N/A if ((line = in.readLine()) == null)
0N/A break;
0N/A sb.append(line);
0N/A }
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A ck("FileWriter.append(" + csn + ")", exp, sb.toString());
0N/A }
0N/A public Appendable reset(Appendable fw) {
0N/A try {
0N/A fw = new FileWriter(gf);
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A return fw;
0N/A }};
0N/A
0N/A private static BasicRunnable testOutputStreamWriter =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A public void init(Appendable osw, String csn, String exp) {
0N/A try {
0N/A ((OutputStreamWriter)osw).flush();
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("OutputStreamWriter.append(" + csn + ")", exp, gos.toString());
0N/A }
0N/A public Appendable reset(Appendable osw) {
0N/A gos.reset();
0N/A return osw;
0N/A }};
0N/A
0N/A private static BasicRunnable testPrintWriter =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A public void init(Appendable pw, String csn, String exp) {
0N/A ((PrintWriter)pw).flush();
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("PrintWriter.append(" + csn + ")", exp, gw.toString());
0N/A }
0N/A public Appendable reset(Appendable pw) {
0N/A gw.reset();
0N/A return pw;
0N/A }};
0N/A
0N/A private static BasicRunnable testStringWriter =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A private StringWriter sw;
0N/A public void init(Appendable sw, String csn, String exp) {
0N/A this.sw = (StringWriter)sw;
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("StringWriter.append(" + csn + ")", exp, sw.toString());
0N/A }
0N/A public Appendable reset(Appendable sw) {
0N/A return new StringWriter();
0N/A }};
0N/A
0N/A private static BasicRunnable testPrintStream =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A public void init(Appendable ps, String csn, String exp) {
0N/A ((PrintStream)ps).flush();
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("PrintStream.append(" + csn + ")", exp, gos.toString());
0N/A }
0N/A public Appendable reset(Appendable ps) {
0N/A gos.reset();
0N/A return ps;
0N/A }};
0N/A
0N/A private static BasicRunnable testCharBuffer =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A private CharBuffer cb;
0N/A public void init(Appendable cb, String csn, String exp) {
0N/A this.cb = (CharBuffer)cb;
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A cb.limit(cb.position()).rewind();
0N/A ck("CharBuffer.append(" + csn + ")", exp, cb.toString());
0N/A }
0N/A public Appendable reset(Appendable cb) {
0N/A ((CharBuffer)cb).clear();
0N/A return cb;
0N/A }};
0N/A
0N/A private static BasicRunnable testStringBuffer =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A private StringBuffer sb;
0N/A public void init(Appendable sb, String csn, String exp) {
0N/A this.sb = (StringBuffer)sb;
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("StringBuffer.append(" + csn + ")", exp, sb.toString());
0N/A }
0N/A public Appendable reset(Appendable sb) {
0N/A return new StringBuffer();
0N/A }};
0N/A
0N/A private static BasicRunnable testStringBuilder =
0N/A new BasicRunnable() {
0N/A private String csn, exp;
0N/A private StringBuilder sb;
0N/A public void init(Appendable sb, String csn, String exp) {
0N/A this.sb = (StringBuilder)sb;
0N/A this.csn = csn;
0N/A this.exp = exp;
0N/A }
0N/A public void run() {
0N/A ck("StringBuilder.append(" + csn + ")", exp, sb.toString());
0N/A }
0N/A public Appendable reset(Appendable sb) {
0N/A return new StringBuilder();
0N/A }};
0N/A
0N/A private static void test(Appendable a, CharSequence csq, BasicRunnable thunk) {
0N/A // appends that should always work
0N/A int [][] sp = { { 0, 0 }, { 11, 11 }, { 11, 21 }, { 0, 7 },
0N/A { 0, s.length() }, { s.length(), s.length() },
0N/A };
0N/A for (int j = 0; j < sp.length; j++) {
0N/A int start = sp[j][0];
0N/A int end = sp[j][1];
0N/A try {
0N/A thunk.init(a.append(csq, start, end),
0N/A csq.getClass().getName(),
0N/A s.subSequence(start, end).toString());
0N/A thunk.run();
0N/A a = thunk.reset(a);
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A }
0N/A
0N/A // appends that should always throw IndexOutOfBoundsException
0N/A int [][] sf = { { -1, 0 }, { 0, -1 }, { 11, 10 },
0N/A { 0, s.length() + 1},
0N/A };
0N/A for (int j = 0; j < sf.length; j++) {
0N/A int start = sf[j][0];
0N/A int end = sf[j][1];
0N/A try {
0N/A a.append(csq, start, end);
0N/A fail("start = " + start + ", end = " + end,
0N/A new IndexOutOfBoundsException());
0N/A a = thunk.reset(a);
0N/A } catch (IndexOutOfBoundsException x) {
0N/A pass();
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A }
0N/A
0N/A // appends of null
0N/A int start = 1;
0N/A int end = 2;
0N/A try {
0N/A thunk.init(a.append(null, start, end), "null",
0N/A "null".subSequence(start, end).toString());
0N/A thunk.run();
0N/A a = thunk.reset(a);
0N/A } catch (IOException x) {
0N/A fail(x);
0N/A }
0N/A }
0N/A
0N/A public static void main(String [] args) throws Exception {
0N/A // CharSequences
0N/A CharBuffer cb = CharBuffer.allocate(128).put(s);
0N/A cb.limit(s.length()).rewind();
0N/A CharBuffer dcb = ByteBuffer.allocateDirect(128).asCharBuffer().put(s);
0N/A dcb.limit(s.length()).rewind();
0N/A CharSequence [] ca = { s,
0N/A new StringBuffer(s),
0N/A new StringBuilder(s),
0N/A cb,
0N/A dcb,
0N/A };
0N/A
0N/A // Appendables/Writers
0N/A Object [][] wa = { { new CharArrayWriter(), testCharArrayWriter },
0N/A { new BufferedWriter(gw), testBufferedWriter },
0N/A // abstract, no implementing classes in jdk
0N/A // { new FilterWriter(), testFilterWriter },
0N/A { new FileWriter(gf), testFileWriter },
0N/A { new OutputStreamWriter(gos), testOutputStreamWriter },
0N/A // covered by previous two test cases
0N/A // { new PipedWriter(gw), testPipedWriter },
0N/A { new PrintWriter(gw), testPrintWriter },
0N/A { new StringWriter(), testStringWriter },
0N/A };
0N/A
0N/A for (int i = 0; i < ca.length; i++) {
0N/A CharSequence a = ca[i];
0N/A for (int j = 0; j < wa.length; j++)
0N/A test((Writer)wa[j][0], a, (BasicRunnable)wa[j][1]);
0N/A
0N/A // other Appendables
0N/A test(new PrintStream(gos), a, testPrintStream);
0N/A test(CharBuffer.allocate(128), a, testCharBuffer);
0N/A test(ByteBuffer.allocateDirect(128).asCharBuffer(), a, testCharBuffer);
0N/A test(new StringBuffer(), a, testStringBuffer);
0N/A test(new StringBuilder(), a, testStringBuilder);
0N/A }
0N/A
0N/A if (fail != 0)
0N/A throw new RuntimeException((fail + pass) + " tests: "
0N/A + fail + " failure(s), first", first);
0N/A else
0N/A System.out.println("all " + (fail + pass) + " tests passed");
0N/A }
0N/A}