KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileNotFoundException JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Properties JavaDoc;
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 /**
46  * @author mog
47  * @version $Id: SlavetopFilterTest.java,v 1.5 2004/04/25 17:46:21 mog Exp $
48  */

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 JavaDoc();
57                 }
58                 public Collection JavaDoc getSections() {
59                     throw new UnsupportedOperationException JavaDoc();
60                 }
61                 public SectionInterface lookup(String JavaDoc string) {
62                     return new SectionInterface() {
63
64                         public LinkedRemoteFileInterface getFile() {
65                             return dir1;
66                         }
67
68                         public Collection JavaDoc getFiles() {
69                             throw new UnsupportedOperationException JavaDoc();
70                         }
71
72                         public String JavaDoc getName() {
73                             throw new UnsupportedOperationException JavaDoc();
74                         }
75
76                         public String JavaDoc getPath() {
77                             return getFile().getPath();
78                         }
79
80                         public LinkedRemoteFileInterface getFirstDirInSection(LinkedRemoteFileInterface dir) {
81                             // LinkedRemoteFileInterface dir1 = dir, dir2 = dir;
82
// while(dir1 != getFile()) {
83
// dir2 = dir1;
84
// try {
85
// dir1 = dir1.getParentFile();
86
// } catch (FileNotFoundException e) {
87
// return getFile();
88
// }
89
// }
90
// return dir2;
91
try {
92                                 return FileUtils.getSubdirOfDirectory(
93                                     getFile(),
94                                     dir);
95                             } catch (FileNotFoundException JavaDoc e) {
96                                 return dir;
97                             }
98                         }
99
100                     };
101                 }
102                 public SectionInterface getSection(String JavaDoc string) {
103                     throw new UnsupportedOperationException JavaDoc();
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 JavaDoc e) {
117                 throw new RuntimeException JavaDoc(e);
118             }
119         }
120     }
121
122     public class SlaveManager extends SlaveManagerImpl {
123
124         protected SlaveManager() throws RemoteException JavaDoc {
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 JavaDoc name) {
142         super(name);
143     }
144
145     protected void setUp() throws Exception JavaDoc {
146         BasicConfigurator.configure();
147     }
148
149     public void testSimple()
150         throws
151             NoAvailableSlaveException,
152             FileExistsException,
153             ObjectNotFoundException {
154         Properties JavaDoc p = new Properties JavaDoc();
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         // these 3 shouldn't get included by SlavetopFilter, as they are directly in the section.
192
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