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 junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import net.sf.drftpd.NoAvailableSlaveException;
024: import net.sf.drftpd.ObjectNotFoundException;
025: import net.sf.drftpd.master.SlaveFileException;
026:
027: import org.drftpd.GlobalContext;
028:
029: import org.drftpd.master.RemoteSlave;
030: import org.drftpd.master.RemoteTransfer;
031: import org.drftpd.remotefile.AbstractLinkedRemoteFile;
032: import org.drftpd.remotefile.CaseInsensitiveHashtable;
033: import org.drftpd.slave.Transfer;
034:
035: import org.drftpd.tests.DummyGlobalContext;
036: import org.drftpd.tests.DummyRemoteSlave;
037: import org.drftpd.tests.DummySlaveManager;
038:
039: import java.io.IOException;
040:
041: import java.util.Arrays;
042: import java.util.Properties;
043: import java.util.Set;
044:
045: /**
046: * @author mog
047: * @version $Id: MatchdirFilterTest.java 823 2004-11-29 01:36:22Z mog $
048: */
049: public class MatchdirFilterTest extends TestCase {
050: RemoteSlave[] rslaves = { new DummyRemoteSlave("slave1", null),
051: new DummyRemoteSlave("slave2", null),
052: new DummyRemoteSlave("slave3", null) };
053:
054: public MatchdirFilterTest(String fName) {
055: super (fName);
056: }
057:
058: public static TestSuite suite() {
059: return new TestSuite(MatchdirFilterTest.class);
060: }
061:
062: public void testSimple() throws ObjectNotFoundException,
063: NoAvailableSlaveException {
064: Properties p = new Properties();
065: p.put("1.assign", "slave1+100,slave2-100");
066: p.put("1.match", "/path1/*");
067:
068: Filter f = new MatchdirFilter(new FC(), 1, p);
069: ScoreChart sc = new ScoreChart(Arrays.asList(rslaves));
070:
071: f.process(sc, null, null, Transfer.TRANSFER_SENDING_DOWNLOAD,
072: new LinkedRemoteFilePath("/path2/dir/file.txt"), null);
073: assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore());
074: assertEquals(0, sc.getSlaveScore(rslaves[1]).getScore());
075: assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore());
076:
077: f.process(sc, null, null, Transfer.TRANSFER_SENDING_DOWNLOAD,
078: new LinkedRemoteFilePath("/"), null);
079: assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore());
080: assertEquals(0, sc.getSlaveScore(rslaves[1]).getScore());
081: assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore());
082:
083: f.process(sc, null, null, Transfer.TRANSFER_SENDING_DOWNLOAD,
084: new LinkedRemoteFilePath("/path1/dir/file.txt"), null);
085: assertEquals(100, sc.getSlaveScore(rslaves[0]).getScore());
086: assertEquals(-100, sc.getSlaveScore(rslaves[1]).getScore());
087: assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore());
088: }
089:
090: public void testAll() throws ObjectNotFoundException,
091: NoAvailableSlaveException {
092: Properties p = new Properties();
093: p.put("1.assign", "ALL+100");
094: p.put("1.match", "/path2/*");
095:
096: Filter f = new MatchdirFilter(new FC(), 1, p);
097: ScoreChart sc = new ScoreChart(Arrays.asList(rslaves));
098:
099: f.process(sc, null, null, Transfer.TRANSFER_SENDING_DOWNLOAD,
100: new LinkedRemoteFilePath("/path1/dir/file.txt"), null);
101: assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore());
102: assertEquals(0, sc.getSlaveScore(rslaves[1]).getScore());
103: assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore());
104:
105: f.process(sc, null, null, Transfer.TRANSFER_SENDING_DOWNLOAD,
106: new LinkedRemoteFilePath("/path2/dir/file.txt"), null);
107: assertEquals(100, sc.getSlaveScore(rslaves[0]).getScore());
108: assertEquals(100, sc.getSlaveScore(rslaves[1]).getScore());
109: assertEquals(100, sc.getSlaveScore(rslaves[2]).getScore());
110: }
111:
112: public void testRemove() throws NoAvailableSlaveException,
113: ObjectNotFoundException {
114: Properties p = new Properties();
115: p.put("1.assign", "slave2-remove");
116: p.put("1.match", "/path1/*");
117:
118: ScoreChart sc = new ScoreChart(Arrays.asList(rslaves));
119:
120: Filter f = new MatchdirFilter(new FC(), 1, p);
121: f.process(sc, null, null, Transfer.TRANSFER_SENDING_DOWNLOAD,
122: new LinkedRemoteFilePath("/path1/dir/file.txt"), null);
123:
124: assertEquals(0, sc.getSlaveScore(rslaves[0]).getScore());
125: assertEquals(0, sc.getSlaveScore(rslaves[2]).getScore());
126:
127: try {
128: sc.getSlaveScore(rslaves[1]);
129: fail();
130: } catch (ObjectNotFoundException success) {
131: //success
132: }
133: }
134:
135: public static class LinkedRemoteFilePath extends
136: AbstractLinkedRemoteFile {
137: private String _path;
138:
139: public LinkedRemoteFilePath(String path) {
140: _path = path;
141: }
142:
143: public String getPath() {
144: return _path;
145: }
146:
147: public void deleteOthers(Set destSlaves) {
148: }
149:
150: public void remerge(CaseInsensitiveHashtable lightRemoteFiles,
151: RemoteSlave rslave) throws IOException {
152: }
153: }
154:
155: public class FC extends FilterChain {
156: DummyGlobalContext gc = null;
157:
158: public DummySlaveManager getSlaveManager()
159: throws SlaveFileException {
160: return new SM();
161: }
162:
163: public GlobalContext getGlobalContext() {
164: if (gc == null) {
165: gc = new DummyGlobalContext();
166:
167: try {
168: gc.setSlaveManager(getSlaveManager());
169: } catch (SlaveFileException e) {
170: throw new RuntimeException(e);
171: }
172: }
173:
174: return gc;
175: }
176: }
177:
178: public class SM extends DummySlaveManager {
179: public SM() throws SlaveFileException {
180: super ();
181: }
182:
183: public RemoteSlave getRemoteSlave(String s)
184: throws ObjectNotFoundException {
185: if (s == null) {
186: throw new NullPointerException("s");
187: }
188:
189: if (rslaves[0] == null) {
190: throw new NullPointerException("rslaves[0] == null");
191: }
192:
193: if (s.equals(rslaves[0].getName())) {
194: return rslaves[0];
195: }
196:
197: if (s.equals(rslaves[1].getName())) {
198: return rslaves[1];
199: }
200:
201: throw new ObjectNotFoundException();
202: }
203: }
204: }
|