ByteSinkTester.java revision 7dd252788645e940eada959bdde927426e2531c9
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
17package com.google.common.io;
18
19import static org.junit.Assert.assertArrayEquals;
20
21import com.google.common.base.Charsets;
22import com.google.common.collect.ImmutableList;
23import com.google.common.io.SourceSinkFactory.ByteSinkFactory;
24import com.google.common.io.SourceSinkFactory.CharSinkFactory;
25
26import junit.framework.TestSuite;
27
28import java.io.BufferedOutputStream;
29import java.io.ByteArrayInputStream;
30import java.io.IOException;
31import java.io.OutputStream;
32import java.io.UnsupportedEncodingException;
33import java.lang.reflect.Method;
34import java.util.Map;
35
36/**
37 * A generator of {@code TestSuite} instances for testing {@code ByteSink} implementations.
38 * Generates tests of a all methods on a {@code ByteSink} given various inputs written to it as well
39 * as sub-suites for testing the {@code CharSink} view in the same way.
40 *
41 * @author Colin Decker
42 */
43public class ByteSinkTester extends SourceSinkTester<ByteSink, byte[], ByteSinkFactory> {
44
45  private static final ImmutableList<Method> testMethods
46      = getTestMethods(ByteSinkTester.class);
47
48  static TestSuite tests(String name, ByteSinkFactory factory) {
49    TestSuite suite = new TestSuite(name);
50    for (Map.Entry<String, String> entry : TEST_STRINGS.entrySet()) {
51      String desc = entry.getKey();
52      TestSuite stringSuite = suiteForString(name, factory, entry.getValue(), desc);
53      suite.addTest(stringSuite);
54    }
55    return suite;
56  }
57
58  private static TestSuite suiteForString(String name, ByteSinkFactory factory,
59      String string, String desc) {
60    byte[] bytes;
61    try {
62      bytes = string.getBytes(Charsets.UTF_8.name());
63    } catch (UnsupportedEncodingException e) {
64      throw new AssertionError(e);
65    }
66    TestSuite suite = suiteForBytes(name, factory, desc, bytes);
67    CharSinkFactory charSinkFactory = SourceSinkFactories.asCharSinkFactory(factory);
68    suite.addTest(CharSinkTester.suiteForString(name + ".asCharSink[Charset]", charSinkFactory,
69        string, desc));
70    return suite;
71  }
72
73  private static TestSuite suiteForBytes(String name, ByteSinkFactory factory,
74      String desc, byte[] bytes) {
75    TestSuite suite = new TestSuite(name + " [" + desc + "]");
76    for (final Method method : testMethods) {
77      suite.addTest(new ByteSinkTester(factory, bytes, name, desc, method));
78    }
79    return suite;
80  }
81
82  private ByteSink sink;
83
84  ByteSinkTester(ByteSinkFactory factory, byte[] data, String suiteName,
85      String caseDesc, Method method) {
86    super(factory, data, suiteName, caseDesc, method);
87  }
88
89  @Override
90  protected void setUp() throws Exception {
91    sink = factory.createSink();
92  }
93
94  public void testOpenStream() throws IOException {
95    OutputStream out = sink.openStream();
96    try {
97      ByteStreams.copy(new ByteArrayInputStream(data), out);
98    } finally {
99      out.close();
100    }
101
102    assertContainsExpectedBytes();
103  }
104
105  public void testOpenBufferedStream() throws IOException {
106    BufferedOutputStream out = sink.openBufferedStream();
107    try {
108      ByteStreams.copy(new ByteArrayInputStream(data), out);
109    } finally {
110      out.close();
111    }
112
113    assertContainsExpectedBytes();
114  }
115
116  public void testWrite() throws IOException {
117    sink.write(data);
118
119    assertContainsExpectedBytes();
120  }
121
122  public void testWriteFrom_inputStream() throws IOException {
123    sink.writeFrom(new ByteArrayInputStream(data));
124
125    assertContainsExpectedBytes();
126  }
127
128  private void assertContainsExpectedBytes() throws IOException {
129    assertArrayEquals(expected, factory.getSinkContents());
130  }
131}
132