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 com.google.common.base.Charsets; 20 import com.google.common.base.Optional; 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.Lists; 23 import com.google.common.io.SourceSinkFactory.ByteSourceFactory; 24 import com.google.common.io.SourceSinkFactory.CharSourceFactory; 25 import java.io.BufferedReader; 26 import java.io.IOException; 27 import java.io.Reader; 28 import java.io.StringWriter; 29 import java.lang.reflect.Method; 30 import java.util.List; 31 import java.util.Map.Entry; 32 import junit.framework.TestSuite; 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 @AndroidIncompatible // Android doesn't understand tests that lack default constructors. 42 public class CharSourceTester extends SourceSinkTester<CharSource, String, CharSourceFactory> { 43 44 private static final ImmutableList<Method> testMethods = getTestMethods(CharSourceTester.class); 45 tests(String name, CharSourceFactory factory, boolean testAsByteSource)46 static TestSuite tests(String name, CharSourceFactory factory, boolean testAsByteSource) { 47 TestSuite suite = new TestSuite(name); 48 for (Entry<String, String> entry : TEST_STRINGS.entrySet()) { 49 if (testAsByteSource) { 50 suite.addTest( 51 suiteForBytes( 52 factory, entry.getValue().getBytes(Charsets.UTF_8), name, entry.getKey(), true)); 53 } else { 54 suite.addTest(suiteForString(factory, entry.getValue(), name, entry.getKey())); 55 } 56 } 57 return suite; 58 } 59 suiteForBytes( CharSourceFactory factory, byte[] bytes, String name, String desc, boolean slice)60 static TestSuite suiteForBytes( 61 CharSourceFactory factory, byte[] bytes, String name, String desc, boolean slice) { 62 TestSuite suite = suiteForString(factory, new String(bytes, Charsets.UTF_8), name, desc); 63 ByteSourceFactory byteSourceFactory = SourceSinkFactories.asByteSourceFactory(factory); 64 suite.addTest( 65 ByteSourceTester.suiteForBytes( 66 byteSourceFactory, bytes, name + ".asByteSource[Charset]", desc, slice)); 67 return suite; 68 } 69 suiteForString( CharSourceFactory factory, String string, String name, String desc)70 static TestSuite suiteForString( 71 CharSourceFactory factory, String string, String name, String desc) { 72 TestSuite suite = new TestSuite(name + " [" + desc + "]"); 73 for (Method method : testMethods) { 74 suite.addTest(new CharSourceTester(factory, string, name, desc, method)); 75 } 76 return suite; 77 } 78 79 private final ImmutableList<String> expectedLines; 80 81 private CharSource source; 82 CharSourceTester( CharSourceFactory factory, String string, String suiteName, String caseDesc, Method method)83 public CharSourceTester( 84 CharSourceFactory factory, String string, String suiteName, String caseDesc, Method method) { 85 super(factory, string, suiteName, caseDesc, method); 86 this.expectedLines = getLines(expected); 87 } 88 89 @Override setUp()90 protected void setUp() throws Exception { 91 this.source = factory.createSource(data); 92 } 93 testOpenStream()94 public void testOpenStream() throws IOException { 95 Reader reader = source.openStream(); 96 97 StringWriter writer = new StringWriter(); 98 char[] buf = new char[64]; 99 int read; 100 while ((read = reader.read(buf)) != -1) { 101 writer.write(buf, 0, read); 102 } 103 reader.close(); 104 writer.close(); 105 106 assertExpectedString(writer.toString()); 107 } 108 testOpenBufferedStream()109 public void testOpenBufferedStream() throws IOException { 110 BufferedReader reader = source.openBufferedStream(); 111 112 StringWriter writer = new StringWriter(); 113 char[] buf = new char[64]; 114 int read; 115 while ((read = reader.read(buf)) != -1) { 116 writer.write(buf, 0, read); 117 } 118 reader.close(); 119 writer.close(); 120 121 assertExpectedString(writer.toString()); 122 } 123 testCopyTo_appendable()124 public void testCopyTo_appendable() throws IOException { 125 StringBuilder builder = new StringBuilder(); 126 127 assertEquals(expected.length(), source.copyTo(builder)); 128 129 assertExpectedString(builder.toString()); 130 } 131 testCopyTo_charSink()132 public void testCopyTo_charSink() throws IOException { 133 TestCharSink sink = new TestCharSink(); 134 135 assertEquals(expected.length(), source.copyTo(sink)); 136 137 assertExpectedString(sink.getString()); 138 } 139 testRead_toString()140 public void testRead_toString() throws IOException { 141 String string = source.read(); 142 assertExpectedString(string); 143 } 144 testReadFirstLine()145 public void testReadFirstLine() throws IOException { 146 if (expectedLines.isEmpty()) { 147 assertNull(source.readFirstLine()); 148 } else { 149 assertEquals(expectedLines.get(0), source.readFirstLine()); 150 } 151 } 152 testReadLines_toList()153 public void testReadLines_toList() throws IOException { 154 assertExpectedLines(source.readLines()); 155 } 156 testIsEmpty()157 public void testIsEmpty() throws IOException { 158 assertEquals(expected.isEmpty(), source.isEmpty()); 159 } 160 testLength()161 public void testLength() throws IOException { 162 assertEquals(expected.length(), source.length()); 163 } 164 testLengthIfKnown()165 public void testLengthIfKnown() throws IOException { 166 Optional<Long> lengthIfKnown = source.lengthIfKnown(); 167 if (lengthIfKnown.isPresent()) { 168 assertEquals(expected.length(), (long) lengthIfKnown.get()); 169 } 170 } 171 testReadLines_withProcessor()172 public void testReadLines_withProcessor() throws IOException { 173 List<String> list = 174 source.readLines( 175 new LineProcessor<List<String>>() { 176 List<String> list = Lists.newArrayList(); 177 178 @Override 179 public boolean processLine(String line) throws IOException { 180 list.add(line); 181 return true; 182 } 183 184 @Override 185 public List<String> getResult() { 186 return list; 187 } 188 }); 189 190 assertExpectedLines(list); 191 } 192 testReadLines_withProcessor_stopsOnFalse()193 public void testReadLines_withProcessor_stopsOnFalse() throws IOException { 194 List<String> list = 195 source.readLines( 196 new LineProcessor<List<String>>() { 197 List<String> list = Lists.newArrayList(); 198 199 @Override 200 public boolean processLine(String line) throws IOException { 201 list.add(line); 202 return false; 203 } 204 205 @Override 206 public List<String> getResult() { 207 return list; 208 } 209 }); 210 211 if (expectedLines.isEmpty()) { 212 assertTrue(list.isEmpty()); 213 } else { 214 assertEquals(expectedLines.subList(0, 1), list); 215 } 216 } 217 assertExpectedString(String string)218 private void assertExpectedString(String string) { 219 assertEquals(expected, string); 220 } 221 assertExpectedLines(List<String> list)222 private void assertExpectedLines(List<String> list) { 223 assertEquals(expectedLines, list); 224 } 225 } 226