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