• 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 com.google.common.base.Joiner;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.io.SourceSinkFactory.CharSinkFactory;
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.lang.reflect.Method;
25 import java.util.Map.Entry;
26 import junit.framework.TestSuite;
27 
28 /**
29  * A generator of {@code TestSuite} instances for testing {@code CharSink} implementations.
30  * Generates tests of a all methods on a {@code CharSink} given various inputs written to it.
31  *
32  * @author Colin Decker
33  */
34 @AndroidIncompatible // Android doesn't understand tests that lack default constructors.
35 public class CharSinkTester extends SourceSinkTester<CharSink, String, CharSinkFactory> {
36 
37   private static final ImmutableList<Method> testMethods = getTestMethods(CharSinkTester.class);
38 
tests(String name, CharSinkFactory factory)39   static TestSuite tests(String name, CharSinkFactory factory) {
40     TestSuite suite = new TestSuite(name);
41     for (Entry<String, String> entry : TEST_STRINGS.entrySet()) {
42       String desc = entry.getKey();
43       TestSuite stringSuite = suiteForString(name, factory, entry.getValue(), desc);
44       suite.addTest(stringSuite);
45     }
46     return suite;
47   }
48 
suiteForString( String name, CharSinkFactory factory, String string, String desc)49   static TestSuite suiteForString(
50       String name, CharSinkFactory factory, String string, String desc) {
51     TestSuite stringSuite = new TestSuite(name + " [" + desc + "]");
52     for (final Method method : testMethods) {
53       stringSuite.addTest(new CharSinkTester(factory, string, name, desc, method));
54     }
55     return stringSuite;
56   }
57 
58   private final ImmutableList<String> lines;
59   private final ImmutableList<String> expectedLines;
60 
61   private CharSink sink;
62 
CharSinkTester( CharSinkFactory factory, String string, String suiteName, String caseDesc, Method method)63   public CharSinkTester(
64       CharSinkFactory factory, String string, String suiteName, String caseDesc, Method method) {
65     super(factory, string, suiteName, caseDesc, method);
66     this.lines = getLines(string);
67     this.expectedLines = getLines(expected);
68   }
69 
70   @Override
setUp()71   protected void setUp() throws Exception {
72     this.sink = factory.createSink();
73   }
74 
testOpenStream()75   public void testOpenStream() throws IOException {
76     Writer writer = sink.openStream();
77     try {
78       writer.write(data);
79     } finally {
80       writer.close();
81     }
82 
83     assertContainsExpectedString();
84   }
85 
testOpenBufferedStream()86   public void testOpenBufferedStream() throws IOException {
87     Writer writer = sink.openBufferedStream();
88     try {
89       writer.write(data);
90     } finally {
91       writer.close();
92     }
93 
94     assertContainsExpectedString();
95   }
96 
testWrite()97   public void testWrite() throws IOException {
98     sink.write(data);
99 
100     assertContainsExpectedString();
101   }
102 
testWriteLines_systemDefaultSeparator()103   public void testWriteLines_systemDefaultSeparator() throws IOException {
104     String separator = System.getProperty("line.separator");
105     sink.writeLines(lines);
106 
107     assertContainsExpectedLines(separator);
108   }
109 
testWriteLines_specificSeparator()110   public void testWriteLines_specificSeparator() throws IOException {
111     String separator = "\r\n";
112     sink.writeLines(lines, separator);
113 
114     assertContainsExpectedLines(separator);
115   }
116 
testWriteLinesStream_systemDefaultSeparator()117   public void testWriteLinesStream_systemDefaultSeparator() throws IOException {
118     String separator = System.getProperty("line.separator");
119     sink.writeLines(lines.stream());
120 
121     assertContainsExpectedLines(separator);
122   }
123 
testWriteLinesStream_specificSeparator()124   public void testWriteLinesStream_specificSeparator() throws IOException {
125     String separator = "\r\n";
126     sink.writeLines(lines.stream(), separator);
127 
128     assertContainsExpectedLines(separator);
129   }
130 
assertContainsExpectedString()131   private void assertContainsExpectedString() throws IOException {
132     assertEquals(expected, factory.getSinkContents());
133   }
134 
assertContainsExpectedLines(String separator)135   private void assertContainsExpectedLines(String separator) throws IOException {
136     String expected = expectedLines.isEmpty() ? "" : Joiner.on(separator).join(expectedLines);
137     if (!lines.isEmpty()) {
138       // if we wrote any lines in writeLines(), there will be a trailing newline
139       expected += separator;
140     }
141     assertEquals(expected, factory.getSinkContents());
142   }
143 }
144