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