1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.io; 18 19 import static com.google.common.io.SourceSinkFactory.CharSinkFactory; 20 21 import com.google.common.base.Joiner; 22 import com.google.common.collect.ImmutableList; 23 import java.io.IOException; 24 import java.io.Writer; 25 import java.lang.reflect.Method; 26 import java.util.Map.Entry; 27 import junit.framework.TestSuite; 28 29 /** 30 * A generator of {@code TestSuite} instances for testing {@code CharSink} implementations. 31 * Generates tests of a all methods on a {@code CharSink} given various inputs written to it. 32 * 33 * @author Colin Decker 34 */ 35 @AndroidIncompatible // Android doesn't understand tests that lack default constructors. 36 public class CharSinkTester extends SourceSinkTester<CharSink, String, CharSinkFactory> { 37 38 private static final ImmutableList<Method> testMethods = getTestMethods(CharSinkTester.class); 39 tests(String name, CharSinkFactory factory)40 static TestSuite tests(String name, CharSinkFactory factory) { 41 TestSuite suite = new TestSuite(name); 42 for (Entry<String, String> entry : TEST_STRINGS.entrySet()) { 43 String desc = entry.getKey(); 44 TestSuite stringSuite = suiteForString(name, factory, entry.getValue(), desc); 45 suite.addTest(stringSuite); 46 } 47 return suite; 48 } 49 suiteForString( String name, CharSinkFactory factory, String string, String desc)50 static TestSuite suiteForString( 51 String name, CharSinkFactory factory, String string, String desc) { 52 TestSuite stringSuite = new TestSuite(name + " [" + desc + "]"); 53 for (final Method method : testMethods) { 54 stringSuite.addTest(new CharSinkTester(factory, string, name, desc, method)); 55 } 56 return stringSuite; 57 } 58 59 private final ImmutableList<String> lines; 60 private final ImmutableList<String> expectedLines; 61 62 private CharSink sink; 63 CharSinkTester( CharSinkFactory factory, String string, String suiteName, String caseDesc, Method method)64 public CharSinkTester( 65 CharSinkFactory factory, String string, String suiteName, String caseDesc, Method method) { 66 super(factory, string, suiteName, caseDesc, method); 67 this.lines = getLines(string); 68 this.expectedLines = getLines(expected); 69 } 70 71 @Override setUp()72 protected void setUp() throws Exception { 73 this.sink = factory.createSink(); 74 } 75 testOpenStream()76 public void testOpenStream() throws IOException { 77 Writer writer = sink.openStream(); 78 try { 79 writer.write(data); 80 } finally { 81 writer.close(); 82 } 83 84 assertContainsExpectedString(); 85 } 86 testOpenBufferedStream()87 public void testOpenBufferedStream() throws IOException { 88 Writer writer = sink.openBufferedStream(); 89 try { 90 writer.write(data); 91 } finally { 92 writer.close(); 93 } 94 95 assertContainsExpectedString(); 96 } 97 testWrite()98 public void testWrite() throws IOException { 99 sink.write(data); 100 101 assertContainsExpectedString(); 102 } 103 testWriteLines_systemDefaultSeparator()104 public void testWriteLines_systemDefaultSeparator() throws IOException { 105 String separator = System.getProperty("line.separator"); 106 sink.writeLines(lines); 107 108 assertContainsExpectedLines(separator); 109 } 110 testWriteLines_specificSeparator()111 public void testWriteLines_specificSeparator() throws IOException { 112 String separator = "\r\n"; 113 sink.writeLines(lines, separator); 114 115 assertContainsExpectedLines(separator); 116 } 117 assertContainsExpectedString()118 private void assertContainsExpectedString() throws IOException { 119 assertEquals(expected, factory.getSinkContents()); 120 } 121 assertContainsExpectedLines(String separator)122 private void assertContainsExpectedLines(String separator) throws IOException { 123 String expected = expectedLines.isEmpty() ? "" : Joiner.on(separator).join(expectedLines); 124 if (!lines.isEmpty()) { 125 // if we wrote any lines in writeLines(), there will be a trailing newline 126 expected += separator; 127 } 128 assertEquals(expected, factory.getSinkContents()); 129 } 130 } 131