Added test on processes
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
0feb069878
commit
b3224ad689
@ -40,13 +40,9 @@ package fr.bigeon.gclc;
|
|||||||
|
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
|
|
||||||
/**
|
/** Represent a functionnality set that can be added to a console application.
|
||||||
* <p>
|
|
||||||
* TODO
|
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon
|
* @author Emmanuel Bigeon */
|
||||||
*
|
|
||||||
*/
|
|
||||||
public interface ApplicationAttachement {
|
public interface ApplicationAttachement {
|
||||||
/** Attach this object to a console application.
|
/** Attach this object to a console application.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -56,8 +56,12 @@ public class TaskPool {
|
|||||||
|
|
||||||
/** Add a process in the pool
|
/** Add a process in the pool
|
||||||
*
|
*
|
||||||
* @param cmd the process */
|
* @param cmd the process
|
||||||
public void add(final Task cmd) {
|
* @return the pid */
|
||||||
|
public String add(final Task cmd) {
|
||||||
|
if (cmd == null) {
|
||||||
|
throw new NullPointerException("Task cannot be null"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
final String pid = getPID();
|
final String pid = getPID();
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
running.put(pid, cmd);
|
running.put(pid, cmd);
|
||||||
@ -74,6 +78,7 @@ public class TaskPool {
|
|||||||
cmd.rmInterruptionListener(this);
|
cmd.rmInterruptionListener(this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return the process id */
|
/** @return the process id */
|
||||||
|
82
gclc/src/test/java/fr/bigeon/gclc/proc/ProcessListTest.java
Normal file
82
gclc/src/test/java/fr/bigeon/gclc/proc/ProcessListTest.java
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.proc.ProcessListTest.java
|
||||||
|
* Created on: Aug 19, 2017
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.proc;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ProcessListTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.proc.ProcessList#execute(java.lang.String[])}.
|
||||||
|
*
|
||||||
|
* @throws CommandRunException if an error occured in the execution
|
||||||
|
* @throws IOException if the manager could not be created */
|
||||||
|
@Test
|
||||||
|
public final void testExecute() throws CommandRunException, IOException {
|
||||||
|
TaskPool pool = new TaskPool();
|
||||||
|
ProcessList pl = new ProcessList("list", pool,
|
||||||
|
new PipedConsoleManager());
|
||||||
|
pl.execute();
|
||||||
|
pool.add(new Task() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
//
|
||||||
|
throw new RuntimeException("Not implemented yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRunning(boolean running) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rmInterruptionListener(InterruptionListener listener) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRunning() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "name";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterruptionListener(InterruptionListener listener) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
});
|
||||||
|
pl.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTip() {
|
||||||
|
try {
|
||||||
|
new ProcessList("list", new TaskPool(), new PipedConsoleManager())
|
||||||
|
.tip();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
98
gclc/src/test/java/fr/bigeon/gclc/proc/TaskPoolTest.java
Normal file
98
gclc/src/test/java/fr/bigeon/gclc/proc/TaskPoolTest.java
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.proc.TaskPoolTest.java
|
||||||
|
* Created on: Aug 19, 2017
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.proc;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class TaskPoolTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.proc.TaskPool#add(fr.bigeon.gclc.proc.Task)}. */
|
||||||
|
@Test
|
||||||
|
public final void testAdd() {
|
||||||
|
TaskPool pool = new TaskPool();
|
||||||
|
Task task = null;
|
||||||
|
try {
|
||||||
|
pool.add(task);
|
||||||
|
fail("Expected a null pointer exception");
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
task = new Task() {
|
||||||
|
private final Object lock = new Object();
|
||||||
|
private boolean running;
|
||||||
|
private InterruptionListener listener;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (lock) {
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
lock.wait(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
listener.interrupted();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRunning(boolean running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
this.running = running;
|
||||||
|
}
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rmInterruptionListener(InterruptionListener listener) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRunning() {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Test";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterruptionListener(InterruptionListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
pool.add(task);
|
||||||
|
assertEquals(1, pool.getPIDs().size());
|
||||||
|
|
||||||
|
for (String pid : pool.getPIDs()) {
|
||||||
|
assertEquals(task, pool.get(pid));
|
||||||
|
}
|
||||||
|
Thread th = new Thread(task);
|
||||||
|
th.start();
|
||||||
|
task.setRunning(false);
|
||||||
|
try {
|
||||||
|
th.join(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(0, pool.getPIDs().size());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user