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