1 16 17 package org.springframework.util; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.StringReader ; 23 import java.io.StringWriter ; 24 import java.util.Arrays ; 25 26 import junit.framework.TestCase; 27 28 32 public class FileCopyUtilTests extends TestCase { 33 34 public void testCopyFromInputStream() throws IOException { 35 byte[] content = "content".getBytes(); 36 ByteArrayInputStream in = new ByteArrayInputStream (content); 37 ByteArrayOutputStream out = new ByteArrayOutputStream (content.length); 38 int count = FileCopyUtils.copy(in, out); 39 assertEquals(content.length, count); 40 assertTrue(Arrays.equals(content, out.toByteArray())); 41 } 42 43 public void testCopyFromByteArray() throws IOException { 44 byte[] content = "content".getBytes(); 45 ByteArrayOutputStream out = new ByteArrayOutputStream (content.length); 46 FileCopyUtils.copy(content, out); 47 assertTrue(Arrays.equals(content, out.toByteArray())); 48 } 49 50 public void testCopyToByteArray() throws IOException { 51 byte[] content = "content".getBytes(); 52 ByteArrayInputStream in = new ByteArrayInputStream (content); 53 byte[] result = FileCopyUtils.copyToByteArray(in); 54 assertTrue(Arrays.equals(content, result)); 55 } 56 57 public void testCopyFromReader() throws IOException { 58 String content = "content"; 59 StringReader in = new StringReader (content); 60 StringWriter out = new StringWriter (); 61 int count = FileCopyUtils.copy(in, out); 62 assertEquals(content.length(), count); 63 assertEquals(content, out.toString()); 64 } 65 66 public void testCopyFromString() throws IOException { 67 String content = "content"; 68 StringWriter out = new StringWriter (); 69 FileCopyUtils.copy(content, out); 70 assertEquals(content, out.toString()); 71 } 72 73 public void testCopyToString() throws IOException { 74 String content = "content"; 75 StringReader in = new StringReader (content); 76 String result = FileCopyUtils.copyToString(in); 77 assertEquals(content, result); 78 } 79 80 } 81 | Popular Tags |