1 16 package org.apache.commons.io; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.io.Reader ; 22 import java.io.Writer ; 23 import java.util.Arrays ; 24 25 import org.apache.commons.io.output.ByteArrayOutputStream; 26 import org.apache.commons.io.testtools.YellOnCloseInputStream; 27 import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream; 28 import org.apache.commons.io.testtools.FileBasedTestCase; 29 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 import junit.textui.TestRunner; 33 34 43 public class CopyUtilsTest extends FileBasedTestCase { 44 45 51 52 private static final int FILE_SIZE = 1024 * 4 + 1; 53 54 55 private byte[] inData = generateTestData(FILE_SIZE); 56 57 public static void main(String [] args) { 58 TestRunner.run(suite()); 59 } 60 61 public static Test suite() { 62 return new TestSuite(CopyUtilsTest.class); 63 } 64 65 public CopyUtilsTest(String testName) { 66 super(testName); 67 } 68 69 73 public void setUp() throws Exception { 74 } 75 76 public void tearDown() throws Exception { 77 } 78 79 83 public void testCopy_byteArrayToOutputStream() throws Exception { 84 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 85 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 86 87 CopyUtils.copy(inData, out); 88 89 assertEquals("Sizes differ", inData.length, baout.size()); 90 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 91 } 92 93 public void testCopy_byteArrayToWriter() throws Exception { 94 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 95 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 96 Writer writer = new java.io.OutputStreamWriter (baout, "US-ASCII"); 97 98 CopyUtils.copy(inData, writer); 99 writer.flush(); 100 101 assertEquals("Sizes differ", inData.length, baout.size()); 102 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 103 } 104 105 public void testCopy_inputStreamToOutputStream() throws Exception { 106 InputStream in = new ByteArrayInputStream (inData); 107 in = new YellOnCloseInputStream(in); 108 109 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 110 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 111 112 int count = CopyUtils.copy(in, out); 113 114 assertTrue("Not all bytes were read", in.available() == 0); 115 assertEquals("Sizes differ", inData.length, baout.size()); 116 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 117 } 118 119 public void testCopy_inputStreamToWriter() throws Exception { 120 InputStream in = new ByteArrayInputStream (inData); 121 in = new YellOnCloseInputStream(in); 122 123 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 124 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 125 Writer writer = new java.io.OutputStreamWriter (baout, "US-ASCII"); 126 127 CopyUtils.copy(in, writer); 128 writer.flush(); 129 130 assertTrue("Not all bytes were read", in.available() == 0); 131 assertEquals("Sizes differ", inData.length, baout.size()); 132 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 133 } 134 135 public void testCopy_readerToOutputStream() throws Exception { 136 InputStream in = new ByteArrayInputStream (inData); 137 in = new YellOnCloseInputStream(in); 138 Reader reader = new java.io.InputStreamReader (in, "US-ASCII"); 139 140 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 141 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 142 143 CopyUtils.copy(reader, out); 144 150 assertEquals("Sizes differ", inData.length, baout.size()); 152 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 153 } 154 155 public void testCopy_readerToWriter() throws Exception { 156 InputStream in = new ByteArrayInputStream (inData); 157 in = new YellOnCloseInputStream(in); 158 Reader reader = new java.io.InputStreamReader (in, "US-ASCII"); 159 160 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 161 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 162 Writer writer = new java.io.OutputStreamWriter (baout, "US-ASCII"); 163 164 int count = CopyUtils.copy(reader, writer); 165 writer.flush(); 166 assertEquals( 167 "The number of characters returned by copy is wrong", 168 inData.length, 169 count); 170 assertEquals("Sizes differ", inData.length, baout.size()); 171 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 172 } 173 174 public void testCopy_stringToOutputStream() throws Exception { 175 String str = new String (inData, "US-ASCII"); 176 177 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 178 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 179 180 CopyUtils.copy(str, out); 181 188 assertEquals("Sizes differ", inData.length, baout.size()); 189 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 190 } 191 192 public void testCopy_stringToWriter() throws Exception { 193 String str = new String (inData, "US-ASCII"); 194 195 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 196 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); 197 Writer writer = new java.io.OutputStreamWriter (baout, "US-ASCII"); 198 199 CopyUtils.copy(str, writer); 200 writer.flush(); 201 202 assertEquals("Sizes differ", inData.length, baout.size()); 203 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray())); 204 } 205 206 } | Popular Tags |