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