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.master;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import net.sf.drftpd.SlaveUnavailableException;
024: import net.sf.drftpd.event.Event;
025: import net.sf.drftpd.master.SlaveFileException;
026:
027: import org.drftpd.GlobalContext;
028:
029: import org.drftpd.remotefile.AbstractLinkedRemoteFile;
030: import org.drftpd.remotefile.CaseInsensitiveHashtable;
031: import org.drftpd.remotefile.LinkedRemoteFileInterface;
032:
033: import org.drftpd.slave.async.AsyncResponse;
034:
035: import org.drftpd.tests.DummyRemoteSlave;
036: import org.drftpd.tests.DummySlaveManager;
037:
038: import java.io.IOException;
039:
040: import java.net.SocketException;
041:
042: import java.util.HashSet;
043: import java.util.List;
044: import java.util.Set;
045:
046: /**
047: * @author mog
048: * @version $Id: RemoteSlaveTest.java 1513 2006-10-13 22:41:08Z tdsoul $
049: */
050: public class RemoteSlaveTest extends TestCase {
051: public RemoteSlaveTest(String fName) {
052: super (fName);
053: }
054:
055: public static TestSuite suite() {
056: return new TestSuite(RemoteSlaveTest.class);
057: }
058:
059: public void testEquals() throws SlaveFileException {
060: DummySlaveManager sm = new DummySlaveManager();
061: GlobalContext gc = new GC();
062: sm.setGlobalContext(gc);
063: ((GC) gc).setSlaveManager(sm);
064:
065: RemoteSlave rslave1 = new DummyRemoteSlave("test1", gc);
066: RemoteSlave rslave2 = new DummyRemoteSlave("test1", gc);
067: RemoteSlave rslave3 = new DummyRemoteSlave("test2", gc);
068: assertTrue(rslave1.equals(rslave1));
069: assertTrue(rslave1.equals(rslave2));
070: assertFalse(rslave1.equals(rslave3));
071: }
072:
073: public void testProcessQueue() throws SlaveFileException,
074: IOException, SlaveUnavailableException {
075: DummySlaveManager sm = new DummySlaveManager();
076: GC gc = new GC();
077: sm.setGlobalContext(gc);
078: gc.setSlaveManager(sm);
079:
080: RemergeRemoteSlave rslave = new RemergeRemoteSlave("test", gc);
081: sm.addSlave(rslave);
082:
083: rslave.simpleDelete("/deleteme");
084: rslave.simpleRename("/renameme", "/indir", "tofile");
085:
086: HashSet filelist = new HashSet();
087: filelist.add("/deleteme");
088: filelist.add("/renameme");
089: filelist.add("/indir");
090:
091: rslave.setFileList(filelist);
092: rslave.processQueue();
093:
094: assertFalse(filelist.contains("/deleteme"));
095: assertFalse(filelist.contains("/renameme"));
096: assertTrue(filelist.contains("/indir"));
097: assertTrue(filelist.contains("/indir/tofile"));
098: }
099:
100: public void testAddNetworkError() throws InterruptedException,
101: SlaveFileException {
102: DummySlaveManager sm = new DummySlaveManager();
103: GC gc = new GC();
104: sm.setGlobalContext(gc);
105: gc.setSlaveManager(sm);
106:
107: DummyRemoteSlave rslave = new DummyRemoteSlave("test", gc);
108: sm.addSlave(rslave);
109: rslave.setProperty("errortimeout", "100");
110: rslave.setProperty("maxerrors", "2");
111: rslave.fakeConnect();
112: rslave.setAvailable(true);
113: assertTrue(rslave.isAvailable());
114: rslave.addNetworkError(new SocketException());
115: assertTrue(rslave.isAvailable());
116: rslave.addNetworkError(new SocketException());
117: assertTrue(rslave.isAvailable());
118: Thread.sleep(100);
119: rslave.addNetworkError(new SocketException());
120: assertTrue(rslave.isAvailable());
121: rslave.addNetworkError(new SocketException());
122: assertFalse(rslave.isAvailable());
123: }
124:
125: public class LRF extends AbstractLinkedRemoteFile {
126: public void cleanSlaveFromMerging(RemoteSlave slave) {
127: }
128:
129: public void resetSlaveForMerging(RemoteSlave slave) {
130: }
131:
132: public void setSlaveForMerging(RemoteSlave rslave) {
133: }
134:
135: public void deleteOthers(Set destSlaves) {
136: }
137:
138: /* (non-Javadoc)
139: * @see net.sf.drftpd.remotefile.LinkedRemoteFileInterface#remerge(net.sf.drftpd.remotefile.LinkedRemoteFile.CaseInsensitiveHashtable, net.sf.drftpd.master.RemoteSlave)
140: */
141: public void remerge(CaseInsensitiveHashtable lightRemoteFiles,
142: RemoteSlave rslave) throws IOException {
143: // TODO Auto-generated method stub
144: }
145:
146: public boolean isValid() {
147: return true;
148: }
149: }
150:
151: public class GC extends GlobalContext {
152: public SlaveManager getSlaveManager() {
153: return super .getSlaveManager();
154: }
155:
156: public void dispatchFtpEvent(Event event) {
157: }
158:
159: public void setSlaveManager(SlaveManager sm) {
160: _slaveManager = sm;
161: }
162:
163: public LinkedRemoteFileInterface getRoot() {
164: System.out.println("new lrf");
165:
166: return new LRF();
167: }
168: }
169:
170: public class RemergeRemoteSlave extends RemoteSlave {
171: private HashSet _filelist = null;
172:
173: public RemergeRemoteSlave(String name, GlobalContext gctx) {
174: super (name, gctx);
175: }
176:
177: /**
178: * @param filelist
179: */
180: public void setFileList(HashSet filelist) {
181: _filelist = filelist;
182: }
183:
184: public String issueDeleteToSlave(String sourceFile)
185: throws SlaveUnavailableException {
186: _filelist.remove(sourceFile);
187:
188: return null;
189: }
190:
191: public String issueRenameToSlave(String from, String toDirPath,
192: String toName) throws SlaveUnavailableException {
193: _filelist.remove(from);
194: _filelist.add(new String(toDirPath + "/" + toName));
195:
196: return null;
197: }
198:
199: public void simpleDelete(String path) {
200: addQueueDelete(path);
201: }
202:
203: public void simpleRename(String from, String toDirPath,
204: String toName) {
205: addQueueRename(from, toDirPath + "/" + toName);
206: }
207:
208: public AsyncResponse fetchResponse(String index)
209: throws SlaveUnavailableException {
210: return null;
211: }
212: }
213: }
|