001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package net.sf.drftpd.mirroring;
019:
020: import java.io.IOException;
021: import java.rmi.RemoteException;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.Properties;
027: import java.util.Set;
028:
029: import junit.framework.TestCase;
030: import net.sf.drftpd.NoAvailableSlaveException;
031: import net.sf.drftpd.master.SlaveFileException;
032:
033: import org.drftpd.master.ConnectionManager;
034: import org.drftpd.master.RemoteSlave;
035: import org.drftpd.remotefile.AbstractLinkedRemoteFile;
036: import org.drftpd.remotefile.CaseInsensitiveHashtable;
037: import org.drftpd.tests.DummyGlobalContext;
038: import org.drftpd.tests.DummyRemoteSlave;
039: import org.drftpd.tests.DummySlaveManager;
040: import org.drftpd.tests.DummySlaveSelectionManager;
041:
042: /**
043: * @author zubov
044: * @version $Id: JobManagerTest.java 1312 2005-10-14 18:38:37Z zubov $
045: */
046: public class JobManagerTest extends TestCase {
047: private Properties p;
048: LinkedRemoteFilePath file2;
049: LinkedRemoteFilePath file;
050:
051: /**
052: * Constructor for JobManagerTest.
053: * @param arg0
054: */
055: ConnectionManager _cm;
056: JobManager _jm;
057: private ArrayList<RemoteSlave> _slaveList;
058:
059: public JobManagerTest(String arg0) throws IOException {
060: super (arg0);
061: }
062:
063: public void setUp() throws RemoteException, SlaveFileException {
064: DummyGlobalContext dgc = new DummyGlobalContext();
065: //dgc.setSlaveManager(new DummySlaveManager(dgc));
066:
067: DummyRemoteSlave rslave1 = new DummyRemoteSlave("slave1", dgc);
068: DummyRemoteSlave rslave2 = new DummyRemoteSlave("slave2", dgc);
069: DummyRemoteSlave rslave3 = new DummyRemoteSlave("slave3", dgc);
070: _slaveList = new ArrayList<RemoteSlave>();
071: _slaveList.add(rslave1);
072: _slaveList.add(rslave2);
073: _slaveList.add(rslave3);
074: p = new Properties();
075: _cm = new CM(p);
076:
077: _cm.setGlobalContext(dgc);
078: dgc.setConnectionManager(_cm);
079:
080: DummySlaveManager dsm = null;
081:
082: dsm = new DummySlaveManager(dgc);
083:
084: dgc.setSlaveManager(dsm);
085:
086: DummySlaveSelectionManager dssm = new DummySlaveSelectionManager();
087: dgc.setSlaveSelectionManager(dssm);
088: _jm = _cm.getGlobalContext().getJobManager();
089: file = new LinkedRemoteFilePath("/path/file1.txt");
090: file.addSlave(rslave1);
091: file2 = new LinkedRemoteFilePath("/path/file2.txt");
092: file2.addSlave(rslave2);
093: }
094:
095: public static void main(String[] args) {
096: junit.textui.TestRunner.run(JobManagerTest.class);
097: }
098:
099: /*
100: * Test for Job getNextJob(List)
101: */
102: public void testGetNextJobList() {
103: HashSet slaveSet = new HashSet(_slaveList);
104: Job job = new Job(file, slaveSet, 0, slaveSet.size());
105: _jm.addJobToQueue(job);
106:
107: Set usedSlaveList = new HashSet();
108: Set skipJobs = new HashSet();
109: assertSame(job, _jm.getNextJob(usedSlaveList, skipJobs));
110: skipJobs.add(job);
111: assertNull(_jm.getNextJob(usedSlaveList, skipJobs));
112:
113: Job job2 = new Job(file2, slaveSet, 5, 2);
114: _jm.addJobToQueue(job2);
115: assertSame(job2, _jm.getNextJob(usedSlaveList, skipJobs));
116: skipJobs.add(job2);
117: assertNull(_jm.getNextJob(usedSlaveList, skipJobs));
118: skipJobs.clear();
119: usedSlaveList.addAll(_slaveList);
120: assertNull(_jm.getNextJob(usedSlaveList, skipJobs));
121: }
122:
123: class CM extends ConnectionManager {
124: CM(Properties p) {
125: p.put("master.bindname", "slavemanager");
126: p.put("master.bindport", "1099");
127: p.put("master.port", "2121");
128: p.put("master.localslave", "false");
129: p
130: .put("master.usermanager",
131: "net.sf.drftpd.master.usermanager.jsx.JSXUserManager");
132: p
133: .put("slaveselection",
134: "org.drftpd.slaveselection.def.SlaveSelectionManager");
135: p.put("sectionmanager",
136: "org.drftpd.sections.conf.SectionManager");
137: p.put("use.ident", "true");
138: }
139: }
140:
141: public static class LinkedRemoteFilePath extends
142: AbstractLinkedRemoteFile {
143: private String _path;
144: private boolean isDeleted = false;
145: private ArrayList slaves = new ArrayList();
146:
147: public LinkedRemoteFilePath(String path) {
148: _path = path;
149: }
150:
151: public String getPath() {
152: return _path;
153: }
154:
155: public void addSlave(DummyRemoteSlave slave) {
156: slaves.add(slave);
157: }
158:
159: public void delete() {
160: isDeleted = true;
161: }
162:
163: public Collection getAvailableSlaves()
164: throws NoAvailableSlaveException {
165: if (getSlaves().isEmpty()) {
166: throw new NoAvailableSlaveException();
167: }
168:
169: return getSlaves();
170: }
171:
172: public Collection getSlaves() {
173: return slaves;
174: }
175:
176: public boolean isAvailable() {
177: return true;
178: }
179:
180: public boolean isDeleted() {
181: return isDeleted;
182: }
183:
184: public boolean removeSlave(DummyRemoteSlave slave) {
185: return slaves.remove(slave);
186: }
187:
188: public String toString() {
189: String string = "[file=" + getPath() + "][availableSlaves[";
190:
191: for (Iterator iter = this .getSlaves().iterator(); iter
192: .hasNext();) {
193: DummyRemoteSlave rslave = (DummyRemoteSlave) iter
194: .next();
195: string = string + rslave + ",";
196: }
197:
198: return string + "]]";
199: }
200:
201: public String getName() {
202: return _path;
203: }
204:
205: public void deleteOthers(Set destSlaves) {
206: // TODO Auto-generated method stub
207: }
208:
209: public void remerge(CaseInsensitiveHashtable lightRemoteFiles,
210: RemoteSlave rslave) throws IOException {
211: // TODO Auto-generated method stub
212: }
213:
214: public boolean isValid() {
215: return true;
216: }
217: }
218: }
|