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.CharSourceFactory; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.Lists; 23 24 import junit.framework.TestSuite; 25 26 import java.io.BufferedReader; 27 import java.io.IOException; 28 import java.io.Reader; 29 import java.io.StringWriter; 30 import java.lang.reflect.Method; 31 import java.util.List; 32 import java.util.Map; 33 34 /** 35 * A generator of {@code TestSuite} instances for testing {@code CharSource} implementations. 36 * Generates tests of a all methods on a {@code CharSource} given various inputs the source is 37 * expected to contain. 38 * 39 * @author Colin Decker 40 */ 41 public class CharSourceTester extends SourceSinkTester<CharSource, String, CharSourceFactory> { 42 43 private static final ImmutableList<Method> testMethods 44 = getTestMethods(CharSourceTester.class); 45 tests(String name, CharSourceFactory factory)46 static TestSuite tests(String name, CharSourceFactory factory) { 47 TestSuite suite = new TestSuite(name); 48 for (Map.Entry<String, String> entry : TEST_STRINGS.entrySet()) { 49 suite.addTest(suiteForString(factory, entry.getValue(), name, entry.getKey())); 50 } 51 return suite; 52 } 53 suiteForString(CharSourceFactory factory, String string, String name, String desc)54 static TestSuite suiteForString(CharSourceFactory factory, String string, 55 String name, String desc) { 56 TestSuite suite = new TestSuite(name + " [" + desc + "]"); 57 for (Method method : testMethods) { 58 suite.addTest(new CharSourceTester(factory, string, name, desc, method)); 59 } 60 return suite; 61 } 62 63 private final ImmutableList<String> expectedLines; 64 65 private CharSource source; 66 CharSourceTester(CharSourceFactory factory, String string, String suiteName, String caseDesc, Method method)67 public CharSourceTester(CharSourceFactory factory, String string, 68 String suiteName, String caseDesc, Method method) { 69 super(factory, string, suiteName, caseDesc, method); 70 this.expectedLines = getLines(expected); 71 } 72 73 @Override setUp()74 protected void setUp() throws Exception { 75 this.source = factory.createSource(data); 76 } 77 testOpenStream()78 public void testOpenStream() throws IOException { 79 Reader reader = source.openStream(); 80 81 StringWriter writer = new StringWriter(); 82 char[] buf = new char[64]; 83 int read; 84 while ((read = reader.read(buf)) != -1) { 85 writer.write(buf, 0, read); 86 } 87 reader.close(); 88 writer.close(); 89 90 assertExpectedString(writer.toString()); 91 } 92 testOpenBufferedStream()93 public void testOpenBufferedStream() throws IOException { 94 BufferedReader reader = source.openBufferedStream(); 95 96 StringWriter writer = new StringWriter(); 97 char[] buf = new char[64]; 98 int read; 99 while ((read = reader.read(buf)) != -1) { 100 writer.write(buf, 0, read); 101 } 102 reader.close(); 103 writer.close(); 104 105 assertExpectedString(writer.toString()); 106 } 107 testCopyTo_appendable()108 public void testCopyTo_appendable() throws IOException { 109 StringBuilder builder = new StringBuilder(); 110 111 assertEquals(expected.length(), source.copyTo(builder)); 112 113 assertExpectedString(builder.toString()); 114 } 115 testCopyTo_charSink()116 public void testCopyTo_charSink() throws IOException { 117 TestCharSink sink = new TestCharSink(); 118 119 assertEquals(expected.length(), source.copyTo(sink)); 120 121 assertExpectedString(sink.getString()); 122 } 123 testRead_toString()124 public void testRead_toString() throws IOException { 125 String string = source.read(); 126 assertExpectedString(string); 127 } 128 testReadFirstLine()129 public void testReadFirstLine() throws IOException { 130 if (expectedLines.isEmpty()) { 131 assertNull(source.readFirstLine()); 132 } else { 133 assertEquals(expectedLines.get(0), source.readFirstLine()); 134 } 135 } 136 testReadLines_toList()137 public void testReadLines_toList() throws IOException { 138 assertExpectedLines(source.readLines()); 139 } 140 testIsEmpty()141 public void testIsEmpty() throws IOException { 142 assertEquals(expected.isEmpty(), source.isEmpty()); 143 } 144 testReadLines_withProcessor()145 public void testReadLines_withProcessor() throws IOException { 146 List<String> list = source.readLines(new LineProcessor<List<String>>() { 147 List<String> list = Lists.newArrayList(); 148 149 @Override 150 public boolean processLine(String line) throws IOException { 151 list.add(line); 152 return true; 153 } 154 155 @Override 156 public List<String> getResult() { 157 return list; 158 } 159 }); 160 161 assertExpectedLines(list); 162 } 163 testReadLines_withProcessor_stopsOnFalse()164 public void testReadLines_withProcessor_stopsOnFalse() throws IOException { 165 List<String> list = source.readLines(new LineProcessor<List<String>>() { 166 List<String> list = Lists.newArrayList(); 167 168 @Override 169 public boolean processLine(String line) throws IOException { 170 list.add(line); 171 return false; 172 } 173 174 @Override 175 public List<String> getResult() { 176 return list; 177 } 178 }); 179 180 if (expectedLines.isEmpty()) { 181 assertTrue(list.isEmpty()); 182 } else { 183 assertEquals(expectedLines.subList(0, 1), list); 184 } 185 } 186 assertExpectedString(String string)187 private void assertExpectedString(String string) { 188 assertEquals(expected, string); 189 } 190 assertExpectedLines(List<String> list)191 private void assertExpectedLines(List<String> list) { 192 assertEquals(expectedLines, list); 193 } 194 } 195