| 1 4 package org.oddjob.io; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.File ; 8 9 import org.apache.commons.io.FileUtils; 10 import org.oddjob.Helper; 11 import org.oddjob.Oddjob; 12 import org.oddjob.state.JobState; 13 14 import junit.framework.TestCase; 15 16 public class CopyJobTest extends TestCase { 17 18 File reference = new File ("test/io/reference"); 19 File dir = new File ("work/io/copy"); 20 21 public void setUp() throws Exception { 22 if (dir.exists()) { 23 FileUtils.forceDelete(dir); 24 } 25 } 26 27 public void testCopyFile() throws Exception { 28 FileUtils.forceMkdir(dir); 29 30 String xml = "<oddjob>" + 31 "<copy from='test/io/reference/test1.txt'" + 32 " to='work/io/copy' />" + 33 "</oddjob>"; 34 35 Oddjob oj = new Oddjob(); 36 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 37 oj.run(); 38 39 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 40 41 assertTrue(new File (dir, "test1.txt").exists()); 42 } 43 44 public void testCopyFiles() throws Exception { 45 FileUtils.forceMkdir(dir); 46 47 String xml = "<oddjob>" + 48 "<copy from='test/io/reference/*.txt'" + 49 " to='work/io/copy' />" + 50 "</oddjob>"; 51 52 Oddjob oj = new Oddjob(); 53 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 54 oj.run(); 55 56 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 57 58 assertEquals(2, new WildcardSpec(new File (dir, "*.txt")).findFiles().length); 59 } 60 61 public void testCopyDirectory() throws Exception { 62 FileUtils.forceMkdir(dir); 63 64 String xml = "<oddjob>" + 65 "<copy from='test/io/reference/a'" + 66 " to='work/io/copy' />" + 67 "</oddjob>"; 68 69 Oddjob oj = new Oddjob(); 70 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 71 oj.run(); 72 73 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 74 75 assertTrue(new File (dir, "a/x/test3.txt").exists()); 76 } 77 78 public void testCopyDirectory2() throws Exception { 79 82 String xml = "<oddjob>" + 83 "<copy from='test/io/reference/a'" + 84 " to='work/io/copy' />" + 85 "</oddjob>"; 86 87 Oddjob oj = new Oddjob(); 88 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 89 oj.run(); 90 91 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 92 93 assertTrue(new File (dir, "x/test3.txt").exists()); 94 } 95 96 public void testSerialize() throws Exception { 97 dir.mkdir(); 98 99 CopyJob test = new CopyJob(); 100 test.setFrom(new File [] { 101 new File ("test/io/reference/test1.txt") }); 102 test.setTo(new File ("work/io/copy")); 103 104 Runnable copy = (Runnable ) Helper.copy(test); 105 copy.run(); 106 107 assertTrue(new File (dir, "test1.txt").exists()); 108 } 109 110 } 111 | Popular Tags |