1 18 package org.drftpd.slaveselection.filter; 19 20 import java.rmi.RemoteException ; 21 import java.util.Arrays ; 22 import java.util.Collections ; 23 import java.util.Properties ; 24 import java.util.Set ; 25 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 import net.sf.drftpd.NoAvailableSlaveException; 29 import net.sf.drftpd.ObjectNotFoundException; 30 import net.sf.drftpd.master.RemoteSlave; 31 import net.sf.drftpd.master.SlaveManagerImpl; 32 import net.sf.drftpd.slave.Transfer; 33 34 import org.drftpd.remotefile.AbstractLinkedRemoteFile; 35 36 40 public class MatchdirFilterTest extends TestCase { 41 42 public static class LinkedRemoteFilePath extends AbstractLinkedRemoteFile { 43 private String _path; 44 public LinkedRemoteFilePath(String path) { 45 _path = path; 46 } 47 public String getPath() { 48 return _path; 49 } 50 public void deleteOthers(Set destSlaves) { 51 } 52 } 53 public MatchdirFilterTest(String fName) { 54 super(fName); 55 } 56 57 public static TestSuite suite() { 58 return new TestSuite(MatchdirFilterTest.class); 59 } 60 61 public class FC extends FilterChain { 62 public SlaveManagerImpl getSlaveManager() { 63 try { 64 return new SM(); 65 } catch (RemoteException e) { 66 throw new RuntimeException (e); 67 } 68 } 69 } 70 71 public class SM extends SlaveManagerImpl { 72 public SM() throws RemoteException { 73 super(); 74 } 75 public RemoteSlave getSlave(String s) throws ObjectNotFoundException { 76 if(s == null) throw new NullPointerException ("s"); 77 if(rslaves[0] == null) throw new NullPointerException ("rslaves[0] == null"); 78 if (s.equals(rslaves[0].getName())) 79 return rslaves[0]; 80 if (s.equals(rslaves[1].getName())) 81 return rslaves[1]; 82 throw new ObjectNotFoundException(); 83 } 84 85 } 86 87 RemoteSlave rslaves[] = 88 { 89 new RemoteSlave("slave1", Collections.EMPTY_LIST), 90 new RemoteSlave("slave2", Collections.EMPTY_LIST), 91 new RemoteSlave("slave3", Collections.EMPTY_LIST)}; 92 93 public void testSimple() throws ObjectNotFoundException, NoAvailableSlaveException { 94 Properties p = new Properties (); 95 p.put("1.assign", "slave1+100,slave2-100"); 96 p.put("1.match", "/path1/*"); 97 98 Filter f = new MatchdirFilter(new FC(), 1, p); 99 ScoreChart sc = new ScoreChart(Arrays.asList(rslaves)); 100 101 f.process(sc, null, null,Transfer.TRANSFER_SENDING_DOWNLOAD, new LinkedRemoteFilePath("/path2/dir/file.txt")); 102 assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore()); 103 assertEquals(0, sc.getSlaveScore(rslaves[1]).getScore()); 104 assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore()); 105 106 f.process(sc, null, null,Transfer.TRANSFER_SENDING_DOWNLOAD, new LinkedRemoteFilePath("/")); 107 assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore()); 108 assertEquals(0, sc.getSlaveScore(rslaves[1]).getScore()); 109 assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore()); 110 111 f.process(sc, null, null,Transfer.TRANSFER_SENDING_DOWNLOAD, new LinkedRemoteFilePath("/path1/dir/file.txt")); 112 assertEquals(100, sc.getSlaveScore(rslaves[0]).getScore()); 113 assertEquals(-100, sc.getSlaveScore(rslaves[1]).getScore()); 114 assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore()); 115 } 116 public void testAll() throws ObjectNotFoundException, NoAvailableSlaveException { 117 Properties p = new Properties (); 118 p.put("1.assign", "ALL+100"); 119 p.put("1.match", "/path2/*"); 120 121 Filter f = new MatchdirFilter(new FC(), 1, p); 122 ScoreChart sc = new ScoreChart(Arrays.asList(rslaves)); 123 124 f.process(sc, null, null,Transfer.TRANSFER_SENDING_DOWNLOAD, new LinkedRemoteFilePath("/path1/dir/file.txt")); 125 assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore()); 126 assertEquals(0, sc.getSlaveScore(rslaves[1]).getScore()); 127 assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore()); 128 129 f.process(sc, null, null,Transfer.TRANSFER_SENDING_DOWNLOAD, new LinkedRemoteFilePath("/path2/dir/file.txt")); 130 assertEquals(100, sc.getSlaveScore(rslaves[0]).getScore()); 131 assertEquals(100, sc.getSlaveScore(rslaves[1]).getScore()); 132 assertEquals(100, sc.getSlaveScore(rslaves[2]).getScore()); 133 } 134 public void testRemove() throws NoAvailableSlaveException, ObjectNotFoundException { 135 Properties p = new Properties (); 136 p.put("1.assign", "slave2-remove"); 137 p.put("1.match", "/path1/*"); 138 139 ScoreChart sc = new ScoreChart(Arrays.asList(rslaves)); 140 141 Filter f = new MatchdirFilter(new FC(), 1, p); 142 f.process(sc, null, null, Transfer.TRANSFER_SENDING_DOWNLOAD, new LinkedRemoteFilePath("/path1/dir/file.txt")); 143 144 assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore()); 145 assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore()); 146 try { 147 sc.getSlaveScore(rslaves[1]); 148 fail(); 149 } catch (ObjectNotFoundException success) { 150 } 152 } 153 } 154 | Popular Tags |