KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > mirroring > JobManagerTest


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 net.sf.drftpd.mirroring;
19
20 import java.io.IOException JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import junit.framework.TestCase;
30 import net.sf.drftpd.FatalException;
31 import net.sf.drftpd.NoAvailableSlaveException;
32 import net.sf.drftpd.master.ConnectionManager;
33 import net.sf.drftpd.master.RemoteSlave;
34 import net.sf.drftpd.master.SlaveManagerImpl;
35
36 import org.drftpd.remotefile.AbstractLinkedRemoteFile;
37
38 /**
39  * @author zubov
40  * @version $Id: JobManagerTest.java,v 1.7.2.1 2004/06/11 00:59:58 mog Exp $
41  */

42 public class JobManagerTest extends TestCase {
43
44     private Properties JavaDoc p;
45     LinkedRemoteFilePath file2;
46     LinkedRemoteFilePath file;
47     static RemoteSlave rslave1 = new RemoteSlave("slave1", new ArrayList JavaDoc());
48     static RemoteSlave rslave2 = new RemoteSlave("slave2", new ArrayList JavaDoc());
49     static RemoteSlave rslave3 = new RemoteSlave("slave3", new ArrayList JavaDoc());
50     static ArrayList JavaDoc slaveList = new ArrayList JavaDoc();
51     static {
52         slaveList.add(rslave1);
53         slaveList.add(rslave2);
54         slaveList.add(rslave3);
55     }
56     /**
57      * Constructor for JobManagerTest.
58      * @param arg0
59      */

60     ConnectionManager cm;
61     JobManager jm;
62     class CM extends ConnectionManager {
63         CM(Properties JavaDoc p) {
64             p.put("master.bindname", "slavemanager");
65             p.put("master.bindport", "1099");
66             p.put("master.port", "2121");
67             p.put("master.localslave", "false");
68             p.put(
69                 "master.usermanager",
70                 "net.sf.drftpd.master.usermanager.jsx.JSXUserManager");
71             p.put(
72                 "slaveselection",
73                 "org.drftpd.slaveselection.def.SlaveSelectionManager");
74             p.put("sectionmanager", "org.drftpd.sections.conf.SectionManager");
75             p.put("use.ident", "true");
76             try {
77                 sm = new SM();
78             } catch (RemoteException JavaDoc e) {
79             }
80         }
81         SM sm;
82         public SlaveManagerImpl getSlaveManager() {
83             return sm;
84         }
85     public void loadJobManager() {
86         if (_jm != null)
87             return; // already loaded
88
Properties JavaDoc p = new Properties JavaDoc();
89         p.put("sleepSeconds", "1000");
90         _jm = new JobManager(this, p);
91         _jm.startJobs();
92     }
93     }
94
95     class SM extends SlaveManagerImpl {
96         protected SM() throws RemoteException JavaDoc {
97         }
98
99         public Collection JavaDoc getAvailableSlaves()
100             throws NoAvailableSlaveException {
101             return slaveList;
102         }
103
104         public Collection JavaDoc getSlaves() {
105             return slaveList;
106         }
107
108     }
109     public JobManagerTest(String JavaDoc arg0) throws IOException JavaDoc {
110         super(arg0);
111     }
112     public void setUp() {
113         p = new Properties JavaDoc();
114         cm = new CM(p);
115         cm.loadJobManager();
116         jm = cm.getJobManager();
117         file = new LinkedRemoteFilePath("/path/file1.txt");
118         file.addSlave(rslave1);
119         file2 = new LinkedRemoteFilePath("/path/file2.txt");
120         file2.addSlave(rslave2);
121     }
122
123     public static void main(String JavaDoc[] args) {
124         junit.textui.TestRunner.run(JobManagerTest.class);
125     }
126
127     public static class LinkedRemoteFilePath extends AbstractLinkedRemoteFile {
128         private String JavaDoc _path;
129         private boolean isDeleted = false;
130         private ArrayList JavaDoc slaves = new ArrayList JavaDoc();
131         public LinkedRemoteFilePath(String JavaDoc path) {
132             _path = path;
133         }
134         public String JavaDoc getPath() {
135             return _path;
136         }
137
138         public void addSlave(RemoteSlave slave) {
139             slaves.add(slave);
140         }
141
142         public void delete() {
143             isDeleted = true;
144         }
145
146         public Collection JavaDoc getAvailableSlaves()
147             throws NoAvailableSlaveException {
148             if (getSlaves().isEmpty())
149                 throw new NoAvailableSlaveException();
150             return getSlaves();
151         }
152
153         public Collection JavaDoc getSlaves() {
154             return slaves;
155         }
156
157         public boolean isAvailable() {
158             return true;
159         }
160
161         public boolean isDeleted() {
162             return isDeleted;
163         }
164
165         public boolean removeSlave(RemoteSlave slave) {
166             return slaves.remove(slave);
167         }
168
169         public String JavaDoc toString() {
170             String JavaDoc string = "[file=" + getPath() + "][availableSlaves[";
171             for (Iterator JavaDoc iter = this.getSlaves().iterator();
172                 iter.hasNext();
173                 ) {
174                 RemoteSlave rslave = (RemoteSlave) iter.next();
175                 string = string + rslave + ",";
176             }
177             return string + "]]";
178         }
179
180         public String JavaDoc getName() {
181             return _path;
182         }
183
184         public void deleteOthers(Set JavaDoc destSlaves) {
185             // TODO Auto-generated method stub
186
}
187     }
188
189     /*
190      * Test for Job getNextJob(List)
191      */

192     public void testGetNextJobList() {
193         HashSet JavaDoc slaveSet = new HashSet JavaDoc();
194         slaveSet.add(rslave1);
195         slaveSet.add(rslave2);
196         slaveSet.add(rslave3);
197         Job job = new Job(file, slaveSet, null, null, 0,slaveSet.size());
198         jm.addJob(job);
199         Set JavaDoc usedSlaveList = new HashSet JavaDoc();
200         Set JavaDoc skipJobs = new HashSet JavaDoc();
201         assertSame(job, jm.getNextJob(usedSlaveList, skipJobs));
202         skipJobs.add(job);
203         assertNull(jm.getNextJob(usedSlaveList, skipJobs));
204         Job job2 = new Job(file2, slaveSet, null, null, 5, 2);
205         jm.addJob(job2);
206         assertSame(job2, jm.getNextJob(usedSlaveList, skipJobs));
207         skipJobs.add(job2);
208         assertNull(jm.getNextJob(usedSlaveList, skipJobs));
209         skipJobs.clear();
210         usedSlaveList.add(rslave1);
211         usedSlaveList.add(rslave2);
212         usedSlaveList.add(rslave3);
213         assertNull(jm.getNextJob(usedSlaveList, skipJobs));
214     }
215 }
216
Popular Tags