1 18 package net.sf.drftpd.remotefile; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.net.InetSocketAddress ; 24 import java.rmi.RemoteException ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.List ; 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 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 string, List list) { 54 super(string, list); 55 } 56 public Slave getSlave() { 57 return new Slave() { 58 public long checkSum(String path) 59 throws RemoteException , IOException { 60 throw new NoSuchMethodError (); 61 } 62 63 public Transfer listen(boolean encrypted) 64 throws RemoteException , IOException { 65 throw new NoSuchMethodError (); 66 } 67 68 public Transfer connect( 69 InetSocketAddress addr, 70 boolean encrypted) 71 throws RemoteException { 72 throw new NoSuchMethodError (); 73 } 74 75 public SlaveStatus getSlaveStatus() throws RemoteException { 76 throw new NoSuchMethodError (); 77 } 78 79 public void ping() throws RemoteException { 80 throw new NoSuchMethodError (); 81 } 82 83 public SFVFile getSFVFile(String path) 84 throws RemoteException , IOException { 85 throw new NoSuchMethodError (); 86 } 87 88 public void rename( 89 String from, 90 String toDirPath, 91 String toName) 92 throws RemoteException , IOException { 93 } 95 96 public void delete(String path) 97 throws RemoteException , IOException { 98 throw new NoSuchMethodError (); 99 } 100 101 public LinkedRemoteFile getSlaveRoot() throws IOException { 102 throw new NoSuchMethodError (); 103 } 104 }; 105 } 106 } 107 108 private static void buildRoot( 109 LinkedRemoteFile root, 110 List slaveBothList, 111 List slave1List, 112 List slave2List) 113 throws FileNotFoundException { 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 { 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 { 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 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 { 182 internalSetUp(); 183 List bothSlaves = Arrays.asList(new RemoteSlave[] { _slave1, _slave2 }); 184 185 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 { 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 { 217 internalSetUp(); 218 List slaveBothList = 222 Arrays.asList(new RemoteSlave[] { _slave1, _slave2 }); 223 224 buildRoot( 226 _root, 227 slaveBothList, 228 Collections.singletonList(_slave1), 229 Collections.singletonList(_slave2)); 230 231 internalRemergeSlave1(_root, _slave1); 233 assertEquals( 234 Collections.singletonList(_slave1), 235 _root.getFile("AddSlaveTest").getSlaves()); 236 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 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 |