Path: blob/master/test/jdk/java/lang/Appendable/Basic.java
41149 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 506740525* @summary Basic test for classes which implement Appendable.26*/2728import java.io.BufferedReader;29import java.io.BufferedWriter;30import java.io.ByteArrayOutputStream;31import java.io.CharArrayWriter;32import java.io.File;33import java.io.FileReader;34import java.io.FileWriter;35import java.io.IOException;36import java.io.OutputStreamWriter;37import java.io.PrintStream;38import java.io.PrintWriter;39import java.io.StringWriter;40import java.io.Writer;41import java.nio.ByteBuffer;42import java.nio.CharBuffer;4344interface BasicRunnable extends Runnable {45void init(Appendable a, String csq, String exp);46Appendable reset(Appendable csq);47}4849public class Basic {5051private static final String s = "Beware the Jabberwock, my son!";52private static CharArrayWriter gw = new CharArrayWriter();53private static ByteArrayOutputStream gos = new ByteArrayOutputStream();5455private static File newFile() {56File f = null;57try {58f = File.createTempFile("append", ".txt");59f.deleteOnExit();60} catch (IOException x) {61fail(x);62}63return f;64}65private static File gf = newFile();6667private static int fail = 0;68private static int pass = 0;6970private static Throwable first;7172static void pass() {73pass++;74}7576static void fail(Throwable ex) {77if (first == null)78first = ex;79System.err.println("FAILED: unexpected exception");80fail++;81}8283static void fail(String fs, Throwable ex) {84String s = "'" + fs + "': " + ex.getClass().getName() + " not thrown";85if (first == null)86first = ex;87System.err.println("FAILED: " + s);88fail++;89}9091static void fail(String fs, String exp, String got) {92String s = "'" + fs + "': Expected '" + exp + "', got '" + got + "'";93if (first == null)94first = new RuntimeException(s);95System.err.println("FAILED: " + s);96fail++;97}9899static void ck(String s, String exp, String got) {100if (!exp.equals(got))101fail(s, exp, got);102else103pass();104}105106private static BasicRunnable testBufferedWriter =107new BasicRunnable() {108private String csn, exp;109public void init(Appendable bw, String csn, String exp) {110try {111((BufferedWriter)bw).flush();112} catch (IOException x) {113fail(x);114}115this.csn = csn;116this.exp = exp;117}118public void run() {119ck("BufferedWriter.append(" + csn + ")", exp, gw.toString());120}121public Appendable reset(Appendable bw) {122gw.reset();123return bw;124}};125126private static BasicRunnable testCharArrayWriter =127new BasicRunnable() {128private String csn, exp;129private CharArrayWriter cw;130public void init(Appendable cw, String csn, String exp) {131this.cw = (CharArrayWriter)cw;132this.csn = csn;133this.exp = exp;134}135public void run() {136ck("CharArrayWriter.append(" + csn + ")", exp, cw.toString());137}138public Appendable reset(Appendable cw) {139((CharArrayWriter)cw).reset();140return cw;141}};142143private static BasicRunnable testFileWriter =144new BasicRunnable() {145private String csn, exp;146public void init(Appendable fw, String csn, String exp) {147try {148((FileWriter)fw).flush();149} catch (IOException x) {150fail(x);151}152this.csn = csn;153this.exp = exp;154}155public void run() {156StringBuilder sb = new StringBuilder();157try {158BufferedReader in = new BufferedReader(new FileReader(gf));159String line;160while (true) {161if ((line = in.readLine()) == null)162break;163sb.append(line);164}165} catch (IOException x) {166fail(x);167}168ck("FileWriter.append(" + csn + ")", exp, sb.toString());169}170public Appendable reset(Appendable fw) {171try {172fw = new FileWriter(gf);173} catch (IOException x) {174fail(x);175}176return fw;177}};178179private static BasicRunnable testOutputStreamWriter =180new BasicRunnable() {181private String csn, exp;182public void init(Appendable osw, String csn, String exp) {183try {184((OutputStreamWriter)osw).flush();185} catch (IOException x) {186fail(x);187}188this.csn = csn;189this.exp = exp;190}191public void run() {192ck("OutputStreamWriter.append(" + csn + ")", exp, gos.toString());193}194public Appendable reset(Appendable osw) {195gos.reset();196return osw;197}};198199private static BasicRunnable testPrintWriter =200new BasicRunnable() {201private String csn, exp;202public void init(Appendable pw, String csn, String exp) {203((PrintWriter)pw).flush();204this.csn = csn;205this.exp = exp;206}207public void run() {208ck("PrintWriter.append(" + csn + ")", exp, gw.toString());209}210public Appendable reset(Appendable pw) {211gw.reset();212return pw;213}};214215private static BasicRunnable testStringWriter =216new BasicRunnable() {217private String csn, exp;218private StringWriter sw;219public void init(Appendable sw, String csn, String exp) {220this.sw = (StringWriter)sw;221this.csn = csn;222this.exp = exp;223}224public void run() {225ck("StringWriter.append(" + csn + ")", exp, sw.toString());226}227public Appendable reset(Appendable sw) {228return new StringWriter();229}};230231private static BasicRunnable testPrintStream =232new BasicRunnable() {233private String csn, exp;234public void init(Appendable ps, String csn, String exp) {235((PrintStream)ps).flush();236this.csn = csn;237this.exp = exp;238}239public void run() {240ck("PrintStream.append(" + csn + ")", exp, gos.toString());241}242public Appendable reset(Appendable ps) {243gos.reset();244return ps;245}};246247private static BasicRunnable testCharBuffer =248new BasicRunnable() {249private String csn, exp;250private CharBuffer cb;251public void init(Appendable cb, String csn, String exp) {252this.cb = (CharBuffer)cb;253this.csn = csn;254this.exp = exp;255}256public void run() {257cb.limit(cb.position()).rewind();258ck("CharBuffer.append(" + csn + ")", exp, cb.toString());259}260public Appendable reset(Appendable cb) {261((CharBuffer)cb).clear();262return cb;263}};264265private static BasicRunnable testStringBuffer =266new BasicRunnable() {267private String csn, exp;268private StringBuffer sb;269public void init(Appendable sb, String csn, String exp) {270this.sb = (StringBuffer)sb;271this.csn = csn;272this.exp = exp;273}274public void run() {275ck("StringBuffer.append(" + csn + ")", exp, sb.toString());276}277public Appendable reset(Appendable sb) {278return new StringBuffer();279}};280281private static BasicRunnable testStringBuilder =282new BasicRunnable() {283private String csn, exp;284private StringBuilder sb;285public void init(Appendable sb, String csn, String exp) {286this.sb = (StringBuilder)sb;287this.csn = csn;288this.exp = exp;289}290public void run() {291ck("StringBuilder.append(" + csn + ")", exp, sb.toString());292}293public Appendable reset(Appendable sb) {294return new StringBuilder();295}};296297private static void test(Appendable a, CharSequence csq, BasicRunnable thunk) {298// appends that should always work299int [][] sp = { { 0, 0 }, { 11, 11 }, { 11, 21 }, { 0, 7 },300{ 0, s.length() }, { s.length(), s.length() },301};302for (int j = 0; j < sp.length; j++) {303int start = sp[j][0];304int end = sp[j][1];305try {306thunk.init(a.append(csq, start, end),307csq.getClass().getName(),308s.subSequence(start, end).toString());309thunk.run();310a = thunk.reset(a);311} catch (IOException x) {312fail(x);313}314}315316// appends that should always throw IndexOutOfBoundsException317int [][] sf = { { -1, 0 }, { 0, -1 }, { 11, 10 },318{ 0, s.length() + 1},319};320for (int j = 0; j < sf.length; j++) {321int start = sf[j][0];322int end = sf[j][1];323try {324a.append(csq, start, end);325fail("start = " + start + ", end = " + end,326new IndexOutOfBoundsException());327a = thunk.reset(a);328} catch (IndexOutOfBoundsException x) {329pass();330} catch (IOException x) {331fail(x);332}333}334335// appends of null336int start = 1;337int end = 2;338try {339thunk.init(a.append(null, start, end), "null",340"null".subSequence(start, end).toString());341thunk.run();342a = thunk.reset(a);343} catch (IOException x) {344fail(x);345}346}347348public static void main(String [] args) throws Exception {349// CharSequences350CharBuffer cb = CharBuffer.allocate(128).put(s);351cb.limit(s.length()).rewind();352CharBuffer dcb = ByteBuffer.allocateDirect(128).asCharBuffer().put(s);353dcb.limit(s.length()).rewind();354CharSequence [] ca = { s,355new StringBuffer(s),356new StringBuilder(s),357cb,358dcb,359};360361// Appendables/Writers362Object [][] wa = { { new CharArrayWriter(), testCharArrayWriter },363{ new BufferedWriter(gw), testBufferedWriter },364// abstract, no implementing classes in jdk365// { new FilterWriter(), testFilterWriter },366{ new FileWriter(gf), testFileWriter },367{ new OutputStreamWriter(gos), testOutputStreamWriter },368// covered by previous two test cases369// { new PipedWriter(gw), testPipedWriter },370{ new PrintWriter(gw), testPrintWriter },371{ new StringWriter(), testStringWriter },372};373374for (int i = 0; i < ca.length; i++) {375CharSequence a = ca[i];376for (int j = 0; j < wa.length; j++)377test((Writer)wa[j][0], a, (BasicRunnable)wa[j][1]);378379// other Appendables380test(new PrintStream(gos), a, testPrintStream);381test(CharBuffer.allocate(128), a, testCharBuffer);382test(ByteBuffer.allocateDirect(128).asCharBuffer(), a, testCharBuffer);383test(new StringBuffer(), a, testStringBuffer);384test(new StringBuilder(), a, testStringBuilder);385}386387if (fail != 0)388throw new RuntimeException((fail + pass) + " tests: "389+ fail + " failure(s), first", first);390else391System.out.println("all " + (fail + pass) + " tests passed");392}393}394395396