aboutsummaryrefslogtreecommitdiff
path: root/guava-tests/test/com/google/common/io/CharSinkTester.java
blob: 9ad41758affd7fe48d5662247883725ddf1035b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2012 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.io;

import static com.google.common.io.SourceSinkFactory.CharSinkFactory;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import junit.framework.TestSuite;

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * A generator of {@code TestSuite} instances for testing {@code CharSink} implementations.
 * Generates tests of a all methods on a {@code CharSink} given various inputs written to it.
 *
 * @author Colin Decker
 */
public class CharSinkTester extends SourceSinkTester<CharSink, String, CharSinkFactory> {

  private static final ImmutableList<Method> testMethods
      = getTestMethods(CharSinkTester.class);

  static TestSuite tests(String name, CharSinkFactory factory) {
    TestSuite suite = new TestSuite(name);
    for (Map.Entry<String, String> entry : TEST_STRINGS.entrySet()) {
      String desc = entry.getKey();
      TestSuite stringSuite = suiteForString(name, factory, entry.getValue(), desc);
      suite.addTest(stringSuite);
    }
    return suite;
  }

  static TestSuite suiteForString(String name, CharSinkFactory factory,
      String string, String desc) {
    TestSuite stringSuite = new TestSuite(name + " [" + desc + "]");
    for (final Method method : testMethods) {
      stringSuite.addTest(new CharSinkTester(factory, string, name, desc, method));
    }
    return stringSuite;
  }

  private final ImmutableList<String> lines;
  private final ImmutableList<String> expectedLines;

  private CharSink sink;

  public CharSinkTester(CharSinkFactory factory, String string,
      String suiteName, String caseDesc, Method method) {
    super(factory, string, suiteName, caseDesc, method);
    this.lines = getLines(string);
    this.expectedLines = getLines(expected);
  }

  @Override
  protected void setUp() throws Exception {
    this.sink = factory.createSink();
  }

  public void testOpenStream() throws IOException {
    Writer writer = sink.openStream();
    try {
      writer.write(data);
    } finally {
      writer.close();
    }

    assertContainsExpectedString();
  }

  public void testOpenBufferedStream() throws IOException {
    Writer writer = sink.openBufferedStream();
    try {
      writer.write(data);
    } finally {
      writer.close();
    }

    assertContainsExpectedString();
  }

  public void testWrite() throws IOException {
    sink.write(data);

    assertContainsExpectedString();
  }

  public void testWriteLines_systemDefaultSeparator() throws IOException {
    String separator = System.getProperty("line.separator");
    sink.writeLines(lines);

    assertContainsExpectedLines(separator);
  }

  public void testWriteLines_specificSeparator() throws IOException {
    String separator = "\r\n";
    sink.writeLines(lines, separator);

    assertContainsExpectedLines(separator);
  }

  private void assertContainsExpectedString() throws IOException {
    assertEquals(expected, factory.getSinkContents());
  }

  private void assertContainsExpectedLines(String separator) throws IOException {
    String expected = expectedLines.isEmpty()
        ? ""
        : Joiner.on(separator).join(expectedLines);
    if (!lines.isEmpty()) {
      // if we wrote any lines in writeLines(), there will be a trailing newline
      expected += separator;
    }
    assertEquals(expected, factory.getSinkContents());
  }
}