KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > remotefile > LinkedRemoteFileTest


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.remotefile;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.net.InetSocketAddress JavaDoc;
24 import java.rmi.RemoteException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29
30 import junit.framework.AssertionFailedError;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33 import net.sf.drftpd.SFVFile;
34 import net.sf.drftpd.master.RemoteSlave;
35 import net.sf.drftpd.master.config.FtpConfig;
36 import net.sf.drftpd.slave.Slave;
37 import net.sf.drftpd.slave.SlaveStatus;
38 import net.sf.drftpd.slave.Transfer;
39
40 import org.apache.log4j.BasicConfigurator;
41
42 /**
43  * @author mog
44  * @version $Id: LinkedRemoteFileTest.java,v 1.2.2.1 2004/06/11 00:59:58 mog Exp $
45  */

46 public class LinkedRemoteFileTest extends TestCase {
47     public static class FC extends FtpConfig {
48         public FC() {
49         }
50     }
51     public static class RS extends RemoteSlave {
52
53         public RS(String JavaDoc string, List JavaDoc list) {
54             super(string, list);
55         }
56         public Slave getSlave() {
57             return new Slave() {
58                 public long checkSum(String JavaDoc path)
59                     throws RemoteException JavaDoc, IOException JavaDoc {
60                     throw new NoSuchMethodError JavaDoc();
61                 }
62
63                 public Transfer listen(boolean encrypted)
64                     throws RemoteException JavaDoc, IOException JavaDoc {
65                     throw new NoSuchMethodError JavaDoc();
66                 }
67
68                 public Transfer connect(
69                     InetSocketAddress JavaDoc addr,
70                     boolean encrypted)
71                     throws RemoteException JavaDoc {
72                     throw new NoSuchMethodError JavaDoc();
73                 }
74
75                 public SlaveStatus getSlaveStatus() throws RemoteException JavaDoc {
76                     throw new NoSuchMethodError JavaDoc();
77                 }
78
79                 public void ping() throws RemoteException JavaDoc {
80                     throw new NoSuchMethodError JavaDoc();
81                 }
82
83                 public SFVFile getSFVFile(String JavaDoc path)
84                     throws RemoteException JavaDoc, IOException JavaDoc {
85                     throw new NoSuchMethodError JavaDoc();
86                 }
87
88                 public void rename(
89                     String JavaDoc from,
90                     String JavaDoc toDirPath,
91                     String JavaDoc toName)
92                     throws RemoteException JavaDoc, IOException JavaDoc {
93                     // pretend we succeeded.
94
}
95
96                 public void delete(String JavaDoc path)
97                     throws RemoteException JavaDoc, IOException JavaDoc {
98                     throw new NoSuchMethodError JavaDoc();
99                 }
100
101                 public LinkedRemoteFile getSlaveRoot() throws IOException JavaDoc {
102                     throw new NoSuchMethodError JavaDoc();
103                 }
104             };
105         }
106     }
107
108     private static void buildRoot(
109         LinkedRemoteFile root,
110         List JavaDoc slaveBothList,
111         List JavaDoc slave1List,
112         List JavaDoc slave2List)
113         throws FileNotFoundException JavaDoc {
114         root.addFile(new StaticRemoteFile(slaveBothList, "ConflictTest", 1000));
115         root.addFile(new StaticRemoteFile(slave1List, "AddSlaveTest", 1000));
116         root.addFile(
117             new StaticRemoteFile(slaveBothList, "RemoteSlaveTest", 1000));
118         root.addFile(new StaticRemoteFile(slave2List, "RemoveFile", 1000));
119         root.addFile(new StaticRemoteFile(null, "DirTest", 0));
120         LinkedRemoteFileInterface masterdir = root.getFile("DirTest");
121         masterdir.addFile(
122             new StaticRemoteFile(slaveBothList, "TestFileInDir", 1000));
123     }
124
125     private static void internalRemergeSlave1(
126         LinkedRemoteFile masterroot,
127         RemoteSlave slave1)
128         throws IOException JavaDoc {
129         LinkedRemoteFile slaveroot = new LinkedRemoteFile(null);
130         slaveroot.addFile(
131             new StaticRemoteFile(Collections.EMPTY_LIST, "ConflictTest", 1000));
132         slaveroot.addFile(
133             new StaticRemoteFile(Collections.EMPTY_LIST, "AddSlaveTest", 1000));
134         slaveroot.addFile(
135             new StaticRemoteFile(
136                 Collections.EMPTY_LIST,
137                 "RemoteSlaveTest",
138                 1000));
139         masterroot.remerge(slaveroot, slave1);
140     }
141
142     private static void internalRemergeSlave2(
143         LinkedRemoteFile root,
144         RemoteSlave slave2)
145         throws IOException JavaDoc {
146         LinkedRemoteFile slaveroot = new LinkedRemoteFile(null);
147         slaveroot.addFile(
148             new StaticRemoteFile(Collections.EMPTY_LIST, "ConflictTest", 1001));
149         slaveroot.addFile(
150             new StaticRemoteFile(Collections.EMPTY_LIST, "AddSlaveTest", 1000));
151         slaveroot.addFile(new StaticRemoteFile(null, "DirTest", 0));
152         LinkedRemoteFileInterface slavedir = slaveroot.getFile("DirTest");
153         slavedir.addFile(
154             new StaticRemoteFile(
155                 Collections.EMPTY_LIST,
156                 "TestFileInDir",
157                 1000));
158         root.remerge(slaveroot, slave2);
159     }
160
161     private LinkedRemoteFile _root;
162
163     private RS _slave1;
164
165     private RS _slave2;
166
167     public LinkedRemoteFileTest(String JavaDoc fName) {
168         super(fName);
169     }
170     private void internalSetUp() {
171         _slave1 = new RS("slave1", Collections.EMPTY_LIST);
172         _slave2 = new RS("slave2", Collections.EMPTY_LIST);
173         FC cfg = new FC();
174         _root = new LinkedRemoteFile(cfg);
175     }
176
177     public void setUp() {
178         BasicConfigurator.configure();
179     }
180
181     public void testAddSlave() throws IOException JavaDoc {
182         internalSetUp();
183         List JavaDoc bothSlaves = Arrays.asList(new RemoteSlave[] { _slave1, _slave2 });
184
185         //file2 = _root.addFile(file);
186
LinkedRemoteFile slaveroot = new LinkedRemoteFile(null);
187         slaveroot.addFile(
188             new StaticRemoteFile(Collections.EMPTY_LIST, "AddSlaveTest", 1000));
189         slaveroot.getFile("AddSlaveTest");
190         _root.remerge(slaveroot, _slave1);
191         _root.getFile("AddSlaveTest");
192         _root.remerge(slaveroot, _slave2);
193         LinkedRemoteFileInterface file2 = _root.getFile("AddSlaveTest");
194         assertEquals(file2, _root.getFile(file2.getName()));
195         assertEquals(bothSlaves, _root.getFile(file2.getName()).getSlaves());
196         System.out.println(file2);
197     }
198     public void testEmptyRoot() throws FileNotFoundException JavaDoc {
199         _root = new LinkedRemoteFile(null);
200         assertEquals(0, _root.length());
201
202         _root.addFile(
203             new StaticRemoteFile(Collections.EMPTY_LIST, "Test1", 1000));
204
205         assertEquals(1000, _root.length());
206
207         _root.addFile(
208             new StaticRemoteFile(Collections.EMPTY_LIST, "Test2", 10000));
209
210         assertEquals(11000, _root.length());
211
212         _root.getFile("Test1").delete();
213
214         assertEquals(10000, _root.length());
215     }
216     public void testRemerge() throws IOException JavaDoc {
217         internalSetUp();
218         // List slaveBothList = new ArrayList();
219
// slaveBothList.add(_slave1);
220
// slaveBothList.add(_slave2);
221
List JavaDoc slaveBothList =
222             Arrays.asList(new RemoteSlave[] { _slave1, _slave2 });
223
224         // build like files.mlst does
225
buildRoot(
226             _root,
227             slaveBothList,
228             Collections.singletonList(_slave1),
229             Collections.singletonList(_slave2));
230
231         // remerge slave 1
232
internalRemergeSlave1(_root, _slave1);
233         assertEquals(
234             Collections.singletonList(_slave1),
235             _root.getFile("AddSlaveTest").getSlaves());
236         // remerge slave 2
237
internalRemergeSlave2(_root, _slave2);
238
239         {
240             assertNotNull(_root.getFile("ConflictTest"));
241             assertNotNull(_root.getFile("ConflictTest.slave2.conflict"));
242             assertEquals(
243                 slaveBothList,
244                 _root.getFile("AddSlaveTest").getSlaves());
245             assertFalse(
246                 _root.getFile("RemoteSlaveTest").getSlaves().contains(_slave2));
247
248             try {
249                 LinkedRemoteFileInterface file = _root.getFile("RemoveFile");
250                 throw new AssertionFailedError(
251                     file.toString() + " should be deleted");
252             } catch (FileNotFoundException JavaDoc success) {
253             }
254
255             LinkedRemoteFileInterface masterdir = _root.getFile("DirTest");
256             assertTrue(
257                 masterdir.getFile("TestFileInDir").getSlaves().contains(
258                     _slave2));
259         }
260     }
261 }
262
Popular Tags