Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.skynetcomputing.skynetclient; import com.skynetcomputing.connection.IConnectionMgr; import com.skynetcomputing.serializable.SrzData; import com.skynetcomputing.serializable.SrzTask; import com.skynetcomputing.skynetclient.persistence.PersistenceManager; import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; /** * * @author Kargathia */ public class WorkManagerTest { private static final String TEST_ROOT_DIR = "..\\SkynetTest\\" + WorkManagerTest.class.getName() + File.separator; private static final String MANDELTASK_DIR = "..\\SkynetTest\\MandelTask\\"; private static final String LOCAL_INPUT_DIR = TEST_ROOT_DIR + "Input" + File.separator; private static final String ROOT_DIR = TEST_ROOT_DIR + "PersistRoot" + File.separator; private static final AtomicBoolean isRunning = new AtomicBoolean(true); public WorkManagerTest() { } @BeforeClass public static void setUpClass() throws IOException { FileUtils.deleteDirectory(new File(ROOT_DIR)); } @AfterClass public static void tearDownClass() throws IOException { } /** * Test of start method, of class WorkManager. * * @throws java.io.IOException * @throws java.lang.InterruptedException */ @Test public void testStart() throws IOException, InterruptedException { IConnectionMgr conn = new IConnectionMgr() { @Override public void sendFile(File aFile) { try { Files.copy(aFile.toPath(), new File(LOCAL_INPUT_DIR + aFile.getName()).toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { Logger.getLogger(WorkManagerTest.class.getName()).log(Level.SEVERE, "Error sending file", ex); } } @Override public void notifyJarOKNOK(boolean isOK) throws IOException { assertTrue("Checking Jar presence", isOK); } @Override public void notifyTaskCompleted() throws IOException { Logger.getLogger(WorkManagerTest.class.getName()).log(Level.INFO, "Task Completed"); synchronized (isRunning) { isRunning.set(false); isRunning.notifyAll(); } } @Override public void start(InetSocketAddress serverAddress) { System.out.println("Fake start listening.."); } @Override public boolean isSendingFiles() { throw new UnsupportedOperationException("Not supported yet."); } @Override public void notifyTaskStarting() throws IOException { throw new UnsupportedOperationException("Not supported yet."); } }; WorkManager workMgr = new WorkManager(conn, ROOT_DIR); // Read only, should not be used for saving files PersistenceManager persistMgr = new PersistenceManager(ROOT_DIR); File testDir = new File(MANDELTASK_DIR); File jobJar = FileUtils.listFiles(testDir, new String[] { "jar" }, false).iterator().next(); workMgr.onJarReceived(jobJar); File localDir = new File(LOCAL_INPUT_DIR); FileUtils.deleteDirectory(localDir); localDir.mkdirs(); for (File f : testDir.listFiles()) { Files.copy(f.toPath(), new File(LOCAL_INPUT_DIR + f.getName()).toPath(), StandardCopyOption.REPLACE_EXISTING); } for (int i = 1; i <= 4; i++) { isRunning.getAndSet(true); File taskJson = new File(localDir, i + ".task"); File jobData = new File(localDir, i + ".data"); workMgr.onTaskReceived(taskJson); workMgr.onDataReceived(jobData); synchronized (isRunning) { while (isRunning.get()) { isRunning.wait(); } } } isRunning.getAndSet(true); File combineTaskFile = new File(localDir, "20" + SrzTask.EXT); workMgr.onTaskReceived(combineTaskFile); SrzTask combineTask = persistMgr.readTaskFile(combineTaskFile); for (int id : combineTask.getDependencies()) { workMgr.onDataReceived(new File(localDir, id + SrzData.EXT)); } synchronized (isRunning) { while (isRunning.get()) { isRunning.wait(); } } Logger.getLogger(WorkManagerTest.class.getName()).log(Level.INFO, "Test Finished"); } }