KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Set JavaDoc;
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 /**
37  * @author mog
38  * @version $Id: MatchdirFilterTest.java,v 1.7 2004/05/31 02:47:20 mog Exp $
39  */

40 public class MatchdirFilterTest extends TestCase {
41
42     public static class LinkedRemoteFilePath extends AbstractLinkedRemoteFile {
43         private String JavaDoc _path;
44         public LinkedRemoteFilePath(String JavaDoc path) {
45             _path = path;
46         }
47         public String JavaDoc getPath() {
48             return _path;
49         }
50         public void deleteOthers(Set JavaDoc destSlaves) {
51         }
52     }
53     public MatchdirFilterTest(String JavaDoc 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 JavaDoc e) {
66                 throw new RuntimeException JavaDoc(e);
67             }
68         }
69     }
70
71     public class SM extends SlaveManagerImpl {
72         public SM() throws RemoteException JavaDoc {
73             super();
74         }
75         public RemoteSlave getSlave(String JavaDoc s) throws ObjectNotFoundException {
76             if(s == null) throw new NullPointerException JavaDoc("s");
77             if(rslaves[0] == null) throw new NullPointerException JavaDoc("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 JavaDoc p = new Properties JavaDoc();
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 JavaDoc p = new Properties JavaDoc();
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 JavaDoc p = new Properties JavaDoc();
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             //success
151
}
152     }
153 }
154
Popular Tags