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