KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slaveselection > filter > MaxbandwidthFilterTest


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.drftpd.slaveselection.filter;
19
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import net.sf.drftpd.NoAvailableSlaveException;
29 import net.sf.drftpd.ObjectNotFoundException;
30 import net.sf.drftpd.SlaveUnavailableException;
31 import net.sf.drftpd.master.RemoteSlave;
32 import net.sf.drftpd.master.SlaveManagerImpl;
33 import net.sf.drftpd.slave.SlaveStatus;
34 import net.sf.drftpd.slave.Transfer;
35
36 import org.drftpd.remotefile.AbstractLinkedRemoteFile;
37
38 import junit.framework.TestCase;
39
40 /*
41  * @author zubov
42  * @version $Id
43  */

44 public class MaxbandwidthFilterTest extends TestCase {
45     public static class LinkedRemoteFilePath extends AbstractLinkedRemoteFile {
46         private String JavaDoc _path;
47         public LinkedRemoteFilePath(String JavaDoc path) {
48             _path = path;
49         }
50         public String JavaDoc getPath() {
51             return _path;
52         }
53         public void deleteOthers(Set JavaDoc destSlaves) {
54         }
55     }
56     /**
57      * Constructor for MaxbandwidthFilterTest.
58      * @param arg0
59      */

60     public MaxbandwidthFilterTest(String JavaDoc arg0) {
61         super(arg0);
62     }
63
64     public class FC extends FilterChain {
65         public SlaveManagerImpl getSlaveManager() {
66             try {
67                 return new SM();
68             } catch (RemoteException JavaDoc e) {
69                 throw new RuntimeException JavaDoc(e);
70             }
71         }
72     }
73
74     public class SM extends SlaveManagerImpl {
75         public SM() throws RemoteException JavaDoc {
76             super();
77         }
78         public RemoteSlave getSlave(String JavaDoc s) throws ObjectNotFoundException {
79             if(s == null) throw new RuntimeException JavaDoc();
80             if(rslaves[0] == null) throw new RuntimeException JavaDoc();
81             if (s.equals(rslaves[0].getName()))
82                 return rslaves[0];
83             if (s.equals(rslaves[1].getName()))
84                 return rslaves[1];
85             throw new ObjectNotFoundException();
86         }
87
88     }
89     
90     public class RS extends RemoteSlave {
91         public RS(String JavaDoc name,Collection JavaDoc duh) {
92             super(name,duh);
93         }
94
95         public synchronized SlaveStatus getStatus()
96             throws SlaveUnavailableException {
97                 if (getName().equals("slave2"))
98                     return new SlaveStatus(0,0,0,0,0,0,0,0);
99                 //if (getName().equals("slave1"))
100
return new SlaveStatus(0,0,0,0,9999999,1,9999999,1);
101         }
102     }
103
104     RemoteSlave rslaves[] =
105         {
106             new RS("slave1", Collections.EMPTY_LIST),
107             new RS("slave2", Collections.EMPTY_LIST)};
108
109     public static void main(String JavaDoc[] args) {
110         junit.textui.TestRunner.run(MaxbandwidthFilterTest.class);
111     }
112
113     public void testSimple() throws ObjectNotFoundException, NoAvailableSlaveException {
114         Properties JavaDoc p = new Properties JavaDoc();
115         p.put("1.maxbandwidth", "800kb");
116
117         Filter f = new MaxbandwidthFilter(new FC(), 1, p);
118         ScoreChart sc = new ScoreChart(Arrays.asList(rslaves));
119
120         f.process(sc, null, null,Transfer.TRANSFER_SENDING_DOWNLOAD, new LinkedRemoteFilePath("/"));
121         assertEquals(sc.getBestSlave(),rslaves[1]);
122     }
123 }
124
Popular Tags