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 org.drftpd.slaveselection.filter;
019:
020: import java.rmi.RemoteException;
021: import java.util.Arrays;
022: import java.util.Collections;
023: import java.util.Properties;
024:
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import net.sf.drftpd.FileExistsException;
028: import net.sf.drftpd.NoAvailableSlaveException;
029: import net.sf.drftpd.ObjectNotFoundException;
030: import net.sf.drftpd.master.SlaveFileException;
031:
032: import org.apache.log4j.BasicConfigurator;
033: import org.drftpd.GlobalContext;
034: import org.drftpd.master.RemoteSlave;
035: import org.drftpd.master.SlaveManager;
036: import org.drftpd.remotefile.LinkedRemoteFile;
037: import org.drftpd.remotefile.StaticRemoteFile;
038: import org.drftpd.sections.def.SectionManager;
039: import org.drftpd.slave.Transfer;
040: import org.drftpd.tests.DummyConnectionManager;
041: import org.drftpd.tests.DummyGlobalContext;
042: import org.drftpd.tests.DummyRemoteSlave;
043: import org.drftpd.tests.DummySlaveManager;
044:
045: /**
046: * @author mog
047: * @version $Id: SlavetopFilterTest.java 847 2004-12-02 03:32:41Z mog $
048: */
049: public class SlavetopFilterTest extends TestCase {
050: private LinkedRemoteFile _dir1;
051: private LinkedRemoteFile _root;
052: private RemoteSlave[] _rslaves;
053: private ScoreChart _sc;
054: private DummyFilterChain _fc;
055: private LinkedRemoteFile _dir2;
056:
057: public SlavetopFilterTest(String name) {
058: super (name);
059: }
060:
061: public static TestSuite suite() {
062: return new TestSuite(SlavetopFilterTest.class);
063: }
064:
065: protected void setUp() throws Exception {
066: BasicConfigurator.configure();
067: _rslaves = new RemoteSlave[] {
068: new DummyRemoteSlave("slave1", null),
069: new DummyRemoteSlave("slave2", null),
070: new DummyRemoteSlave("slave3", null) };
071:
072: _sc = new ScoreChart(Arrays.asList(_rslaves));
073:
074: _root = new LinkedRemoteFile(null);
075: _dir1 = _root.createDirectory("dir1");
076:
077: _dir2 = _dir1.createDirectory("dir2");
078:
079: _dir2.addFile(new StaticRemoteFile("file1", Collections
080: .singletonList(_rslaves[0])));
081: _dir2.addFile(new StaticRemoteFile("file2", Collections
082: .singletonList(_rslaves[2])));
083: _dir2.addFile(new StaticRemoteFile("file3", Collections
084: .singletonList(_rslaves[0])));
085: _dir2.addFile(new StaticRemoteFile("file4", Collections
086: .singletonList(_rslaves[1])));
087: _dir2.addFile(new StaticRemoteFile("file5", Collections
088: .singletonList(_rslaves[2])));
089:
090: // these 3 shouldn't get included by SlavetopFilter, as they are directly in the section.
091: _dir1.addFile(new StaticRemoteFile("file6", Collections
092: .singletonList(_rslaves[1])));
093:
094: _dir1.addFile(new StaticRemoteFile("file7", Collections
095: .singletonList(_rslaves[1])));
096:
097: _dir1.addFile(new StaticRemoteFile("file8", Collections
098: .singletonList(_rslaves[1])));
099:
100: _fc = new DummyFilterChain();
101: DummyConnectionManager cm = new DummyConnectionManager();
102:
103: DummyGlobalContext gctx = new DummyGlobalContext();
104: _fc.setGlobalContext(gctx);
105: gctx.setSectionManager(new SectionManager(cm));
106: gctx.setConnectionManager(cm);
107: gctx.setRoot(_root);
108: cm.setGlobalContext(gctx);
109:
110: DummySlaveManager sm = new DummySlaveManager();
111: sm.setGlobalContext(gctx);
112: _fc.setSlaveManager(sm);
113: }
114:
115: public void testAssign() throws NoAvailableSlaveException,
116: FileExistsException, ObjectNotFoundException,
117: RemoteException, SlaveFileException {
118: Properties p = new Properties();
119: p.put("1.topslaves", "2");
120: p.put("1.assign", "100");
121:
122: SlavetopFilter f = new SlavetopFilter(_fc, 1, p);
123: f.process(_sc, null, null, Transfer.TRANSFER_UNKNOWN, _dir2,
124: null);
125: assertEquals(100, _sc.getSlaveScore(_rslaves[0]).getScore());
126: assertEquals(0, _sc.getSlaveScore(_rslaves[1]).getScore());
127: assertEquals(100, _sc.getSlaveScore(_rslaves[2]).getScore());
128: }
129:
130: public void testRemove() throws NoAvailableSlaveException,
131: ObjectNotFoundException {
132: Properties p = new Properties();
133: p.put("1.topslaves", "2");
134: p.put("1.assign", "0");
135:
136: SlavetopFilter f = new SlavetopFilter(_fc, 1, p);
137: f.process(_sc, null, null, Transfer.TRANSFER_UNKNOWN, _dir2,
138: null);
139: assertEquals(0, _sc.getSlaveScore(_rslaves[0]).getScore());
140: try {
141: _sc.getSlaveScore(_rslaves[1]).getScore();
142: fail();
143: } catch (ObjectNotFoundException e) {
144: }
145: assertEquals(0, _sc.getSlaveScore(_rslaves[2]).getScore());
146: }
147:
148: public class DummyFilterChain extends FilterChain {
149: private DummySlaveManager _slavem;
150: private DummyGlobalContext _dgctx;
151:
152: public SlaveManager getSlaveManager() {
153: return _slavem;
154: }
155:
156: public void setSlaveManager(DummySlaveManager sm) {
157: _slavem = sm;
158: }
159:
160: public void setGlobalContext(DummyGlobalContext dgctx) {
161: _dgctx = dgctx;
162: }
163:
164: public GlobalContext getGlobalContext() {
165: return _dgctx;
166: }
167: }
168: }
|