1 18 package net.sf.drftpd.mirroring; 19 20 import java.io.IOException ; 21 import java.rmi.RemoteException ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 29 import junit.framework.TestCase; 30 import net.sf.drftpd.FatalException; 31 import net.sf.drftpd.NoAvailableSlaveException; 32 import net.sf.drftpd.master.ConnectionManager; 33 import net.sf.drftpd.master.RemoteSlave; 34 import net.sf.drftpd.master.SlaveManagerImpl; 35 36 import org.drftpd.remotefile.AbstractLinkedRemoteFile; 37 38 42 public class JobManagerTest extends TestCase { 43 44 private Properties p; 45 LinkedRemoteFilePath file2; 46 LinkedRemoteFilePath file; 47 static RemoteSlave rslave1 = new RemoteSlave("slave1", new ArrayList ()); 48 static RemoteSlave rslave2 = new RemoteSlave("slave2", new ArrayList ()); 49 static RemoteSlave rslave3 = new RemoteSlave("slave3", new ArrayList ()); 50 static ArrayList slaveList = new ArrayList (); 51 static { 52 slaveList.add(rslave1); 53 slaveList.add(rslave2); 54 slaveList.add(rslave3); 55 } 56 60 ConnectionManager cm; 61 JobManager jm; 62 class CM extends ConnectionManager { 63 CM(Properties p) { 64 p.put("master.bindname", "slavemanager"); 65 p.put("master.bindport", "1099"); 66 p.put("master.port", "2121"); 67 p.put("master.localslave", "false"); 68 p.put( 69 "master.usermanager", 70 "net.sf.drftpd.master.usermanager.jsx.JSXUserManager"); 71 p.put( 72 "slaveselection", 73 "org.drftpd.slaveselection.def.SlaveSelectionManager"); 74 p.put("sectionmanager", "org.drftpd.sections.conf.SectionManager"); 75 p.put("use.ident", "true"); 76 try { 77 sm = new SM(); 78 } catch (RemoteException e) { 79 } 80 } 81 SM sm; 82 public SlaveManagerImpl getSlaveManager() { 83 return sm; 84 } 85 public void loadJobManager() { 86 if (_jm != null) 87 return; Properties p = new Properties (); 89 p.put("sleepSeconds", "1000"); 90 _jm = new JobManager(this, p); 91 _jm.startJobs(); 92 } 93 } 94 95 class SM extends SlaveManagerImpl { 96 protected SM() throws RemoteException { 97 } 98 99 public Collection getAvailableSlaves() 100 throws NoAvailableSlaveException { 101 return slaveList; 102 } 103 104 public Collection getSlaves() { 105 return slaveList; 106 } 107 108 } 109 public JobManagerTest(String arg0) throws IOException { 110 super(arg0); 111 } 112 public void setUp() { 113 p = new Properties (); 114 cm = new CM(p); 115 cm.loadJobManager(); 116 jm = cm.getJobManager(); 117 file = new LinkedRemoteFilePath("/path/file1.txt"); 118 file.addSlave(rslave1); 119 file2 = new LinkedRemoteFilePath("/path/file2.txt"); 120 file2.addSlave(rslave2); 121 } 122 123 public static void main(String [] args) { 124 junit.textui.TestRunner.run(JobManagerTest.class); 125 } 126 127 public static class LinkedRemoteFilePath extends AbstractLinkedRemoteFile { 128 private String _path; 129 private boolean isDeleted = false; 130 private ArrayList slaves = new ArrayList (); 131 public LinkedRemoteFilePath(String path) { 132 _path = path; 133 } 134 public String getPath() { 135 return _path; 136 } 137 138 public void addSlave(RemoteSlave slave) { 139 slaves.add(slave); 140 } 141 142 public void delete() { 143 isDeleted = true; 144 } 145 146 public Collection getAvailableSlaves() 147 throws NoAvailableSlaveException { 148 if (getSlaves().isEmpty()) 149 throw new NoAvailableSlaveException(); 150 return getSlaves(); 151 } 152 153 public Collection getSlaves() { 154 return slaves; 155 } 156 157 public boolean isAvailable() { 158 return true; 159 } 160 161 public boolean isDeleted() { 162 return isDeleted; 163 } 164 165 public boolean removeSlave(RemoteSlave slave) { 166 return slaves.remove(slave); 167 } 168 169 public String toString() { 170 String string = "[file=" + getPath() + "][availableSlaves["; 171 for (Iterator iter = this.getSlaves().iterator(); 172 iter.hasNext(); 173 ) { 174 RemoteSlave rslave = (RemoteSlave) iter.next(); 175 string = string + rslave + ","; 176 } 177 return string + "]]"; 178 } 179 180 public String getName() { 181 return _path; 182 } 183 184 public void deleteOthers(Set destSlaves) { 185 } 187 } 188 189 192 public void testGetNextJobList() { 193 HashSet slaveSet = new HashSet (); 194 slaveSet.add(rslave1); 195 slaveSet.add(rslave2); 196 slaveSet.add(rslave3); 197 Job job = new Job(file, slaveSet, null, null, 0,slaveSet.size()); 198 jm.addJob(job); 199 Set usedSlaveList = new HashSet (); 200 Set skipJobs = new HashSet (); 201 assertSame(job, jm.getNextJob(usedSlaveList, skipJobs)); 202 skipJobs.add(job); 203 assertNull(jm.getNextJob(usedSlaveList, skipJobs)); 204 Job job2 = new Job(file2, slaveSet, null, null, 5, 2); 205 jm.addJob(job2); 206 assertSame(job2, jm.getNextJob(usedSlaveList, skipJobs)); 207 skipJobs.add(job2); 208 assertNull(jm.getNextJob(usedSlaveList, skipJobs)); 209 skipJobs.clear(); 210 usedSlaveList.add(rslave1); 211 usedSlaveList.add(rslave2); 212 usedSlaveList.add(rslave3); 213 assertNull(jm.getNextJob(usedSlaveList, skipJobs)); 214 } 215 } 216 | Popular Tags |