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