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 DeleteJobTest extends TestCase { 17 18 File dir = new File ("work/io/delete"); 19 20 public void setUp() throws Exception { 21 if (dir.exists()) { 22 FileUtils.forceDelete(dir); 23 } 24 } 25 26 public void testDeleteFile() throws Exception { 27 FileUtils.forceMkdir(dir); 28 29 FileUtils.touch(new File (dir, "a")); 30 31 WildcardSpec wild = new WildcardSpec(new File (dir, "a")); 32 File [] found = wild.findFiles(); 33 assertEquals(1, found.length); 34 35 String xml = "<oddjob><delete files='work/io/delete/a'/></oddjob>"; 36 37 Oddjob oj = new Oddjob(); 38 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 39 oj.run(); 40 41 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 42 43 found = wild.findFiles(); 44 assertEquals(0, found.length); 45 } 46 47 public void testDeleteFiles() throws Exception { 48 FileUtils.forceMkdir(dir); 49 50 FileUtils.touch(new File (dir, "a")); 51 FileUtils.touch(new File (dir, "b")); 52 FileUtils.touch(new File (dir, "c")); 53 54 WildcardSpec wild = new WildcardSpec(new File (dir, "*")); 55 File [] found = wild.findFiles(); 56 assertEquals(3, found.length); 57 58 String xml = "<oddjob><delete files='work/io/delete/*'/></oddjob>"; 59 60 Oddjob oj = new Oddjob(); 61 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 62 oj.run(); 63 64 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 65 66 found = wild.findFiles(); 67 assertEquals(0, found.length); 68 } 69 70 public void testDeleteDir() throws Exception { 71 FileUtils.forceMkdir(dir); 72 73 String xml = "<oddjob><delete files='work/io/delete'/></oddjob>"; 74 75 Oddjob oj = new Oddjob(); 76 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 77 78 oj.run(); 79 80 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 81 assertFalse(dir.exists()); 82 } 83 84 public void testDeleteFullDir() throws Exception { 85 FileUtils.forceMkdir(dir); 86 87 FileUtils.touch(new File (dir, "a")); 88 FileUtils.touch(new File (dir, "b")); 89 FileUtils.touch(new File (dir, "c")); 90 91 92 String xml = "<oddjob><delete files='work/io/delete'/></oddjob>"; 93 94 Oddjob oj = new Oddjob(); 95 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 96 97 oj.run(); 98 assertEquals(JobState.EXCEPTION, oj.lastJobStateEvent().getJobState()); 99 100 assertTrue(dir.exists()); 101 102 xml = "<oddjob><delete force='true' files='work/io/delete'/></oddjob>"; 103 oj.hardReset(); 104 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 105 106 oj.run(); 107 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 108 109 assertFalse(dir.exists()); 110 } 111 112 public void testSerialize() throws Exception { 113 FileUtils.forceMkdir(dir); 114 115 assertTrue(dir.exists()); 116 DeleteJob test = new DeleteJob(); 117 test.setFiles(new File [] { dir } ); 118 119 Runnable copy = (Runnable ) Helper.copy(test); 120 copy.run(); 121 122 assertFalse(dir.exists()); 123 } 124 } 125 | Popular Tags |