1 32 package net.sf.retrotranslator.runtime.java.lang; 33 34 import java.io.*; 35 import java.nio.CharBuffer ; 36 import java.util.*; 37 import junit.framework.TestCase; 38 39 42 public class _AppendableTestCase extends TestCase { 43 44 private static class AppendableWrapper implements Appendable { 45 46 private Appendable appendable; 47 48 public AppendableWrapper(Appendable appendable) { 49 this.appendable = appendable; 50 } 51 52 public Appendable append(CharSequence csq) throws IOException { 53 appendable.append(csq); 54 return this; 55 } 56 57 public Appendable append(CharSequence csq, int start, int end) throws IOException { 58 appendable.append(csq, start, end); 59 return this; 60 } 61 62 public Appendable append(char c) throws IOException { 63 appendable.append(c); 64 return this; 65 } 66 67 public String toString() { 68 return appendable.toString(); 69 } 70 } 71 72 public void testAppend() throws Exception { 73 ByteArrayOutputStream out = new ByteArrayOutputStream(); 74 List<Appendable > list = Arrays.asList(new Appendable [] { 75 new StringBuffer (), 76 new PrintStream(out), 77 new StringWriter(), 78 CharBuffer.wrap(new char[10]), 79 new AppendableWrapper(new StringBuffer ()) 80 }); 81 for (Appendable appendable : list) { 82 assertSame(appendable, appendable.append("abc").append("xyz", 1, 2).append('0')); 83 String s; 84 if (appendable instanceof PrintStream) { 85 s = out.toString(); 86 } else if (appendable instanceof CharBuffer ) { 87 CharBuffer buffer = ((CharBuffer ) appendable); 88 buffer.limit(buffer.position()); 89 buffer.position(0); 90 s = buffer.toString(); 91 } else { 92 s = appendable.toString(); 93 } 94 assertEquals("abcy0", s); 95 } 96 } 97 } | Popular Tags |