1 18 package org.drftpd.slaveselection.filter; 19 20 import java.io.FileNotFoundException ; 21 import java.rmi.RemoteException ; 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Properties ; 26 27 import org.apache.log4j.BasicConfigurator; 28 import org.drftpd.remotefile.FileUtils; 29 import org.drftpd.sections.SectionInterface; 30 import org.drftpd.sections.SectionManagerInterface; 31 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 import net.sf.drftpd.NoAvailableSlaveException; 35 import net.sf.drftpd.FileExistsException; 36 import net.sf.drftpd.ObjectNotFoundException; 37 import net.sf.drftpd.master.ConnectionManager; 38 import net.sf.drftpd.master.RemoteSlave; 39 import net.sf.drftpd.master.SlaveManagerImpl; 40 import net.sf.drftpd.remotefile.LinkedRemoteFile; 41 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 42 import net.sf.drftpd.remotefile.StaticRemoteFile; 43 import net.sf.drftpd.slave.Transfer; 44 45 49 public class SlavetopFilterTest extends TestCase { 50 51 public class CM extends ConnectionManager { 52 public SectionManagerInterface getSectionManager() { 53 return new SectionManagerInterface() { 54 55 public ConnectionManager getConnectionManager() { 56 throw new UnsupportedOperationException (); 57 } 58 public Collection getSections() { 59 throw new UnsupportedOperationException (); 60 } 61 public SectionInterface lookup(String string) { 62 return new SectionInterface() { 63 64 public LinkedRemoteFileInterface getFile() { 65 return dir1; 66 } 67 68 public Collection getFiles() { 69 throw new UnsupportedOperationException (); 70 } 71 72 public String getName() { 73 throw new UnsupportedOperationException (); 74 } 75 76 public String getPath() { 77 return getFile().getPath(); 78 } 79 80 public LinkedRemoteFileInterface getFirstDirInSection(LinkedRemoteFileInterface dir) { 81 try { 92 return FileUtils.getSubdirOfDirectory( 93 getFile(), 94 dir); 95 } catch (FileNotFoundException e) { 96 return dir; 97 } 98 } 99 100 }; 101 } 102 public SectionInterface getSection(String string) { 103 throw new UnsupportedOperationException (); 104 } 105 public void reload() { 106 } 107 }; 108 } 109 } 110 111 public class FC extends FilterChain { 112 113 public SlaveManagerImpl getSlaveManager() { 114 try { 115 return new SlaveManager(); 116 } catch (RemoteException e) { 117 throw new RuntimeException (e); 118 } 119 } 120 } 121 122 public class SlaveManager extends SlaveManagerImpl { 123 124 protected SlaveManager() throws RemoteException { 125 super(); 126 } 127 128 public ConnectionManager getConnectionManager() { 129 return new CM(); 130 } 131 132 } 133 134 public static TestSuite suite() { 135 return new TestSuite(SlavetopFilterTest.class); 136 } 137 private LinkedRemoteFile dir1; 138 139 private LinkedRemoteFile root; 140 141 public SlavetopFilterTest(String name) { 142 super(name); 143 } 144 145 protected void setUp() throws Exception { 146 BasicConfigurator.configure(); 147 } 148 149 public void testSimple() 150 throws 151 NoAvailableSlaveException, 152 FileExistsException, 153 ObjectNotFoundException { 154 Properties p = new Properties (); 155 p.put("1.topslaves", "2"); 156 p.put("1.assign", "100"); 157 158 RemoteSlave rslaves[] = 159 { 160 new RemoteSlave("slave1", null), 161 new RemoteSlave("slave2", null), 162 new RemoteSlave("slave3", null)}; 163 164 ScoreChart sc = new ScoreChart(Arrays.asList(rslaves)); 165 166 root = new LinkedRemoteFile(null); 167 dir1 = root.createDirectory("dir1"); 168 LinkedRemoteFile dir2 = dir1.createDirectory("dir2"); 169 170 dir2.addFile( 171 new StaticRemoteFile( 172 "file1", 173 Collections.singletonList(rslaves[0]))); 174 dir2.addFile( 175 new StaticRemoteFile( 176 "file2", 177 Collections.singletonList(rslaves[2]))); 178 dir2.addFile( 179 new StaticRemoteFile( 180 "file3", 181 Collections.singletonList(rslaves[0]))); 182 dir2.addFile( 183 new StaticRemoteFile( 184 "file4", 185 Collections.singletonList(rslaves[1]))); 186 dir2.addFile( 187 new StaticRemoteFile( 188 "file5", 189 Collections.singletonList(rslaves[2]))); 190 191 dir1.addFile( 193 new StaticRemoteFile( 194 "file6", 195 Collections.singletonList(rslaves[1]))); 196 197 dir1.addFile( 198 new StaticRemoteFile( 199 "file7", 200 Collections.singletonList(rslaves[1]))); 201 202 dir1.addFile( 203 new StaticRemoteFile( 204 "file8", 205 Collections.singletonList(rslaves[1]))); 206 207 Filter f = new SlavetopFilter(new FC(), 1, p); 208 f.process(sc, null, null, Transfer.TRANSFER_UNKNOWN, dir2); 209 assertEquals(100, sc.getSlaveScore(rslaves[0]).getScore()); 210 assertEquals(0, sc.getSlaveScore(rslaves[1]).getScore()); 211 assertEquals(100, sc.getSlaveScore(rslaves[2]).getScore()); 212 } 213 214 } 215 | Popular Tags |