diff --git a/gclc/src/main/java/fr/bigeon/gclc/ApplicationAttachement.java b/gclc/src/main/java/fr/bigeon/gclc/ApplicationAttachement.java index 5a3cc38..e64ccd6 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/ApplicationAttachement.java +++ b/gclc/src/main/java/fr/bigeon/gclc/ApplicationAttachement.java @@ -40,13 +40,9 @@ package fr.bigeon.gclc; import fr.bigeon.gclc.exception.InvalidCommandName; -/** - *

- * TODO +/** Represent a functionnality set that can be added to a console application. * - * @author Emmanuel Bigeon - * - */ + * @author Emmanuel Bigeon */ public interface ApplicationAttachement { /** Attach this object to a console application. *

diff --git a/gclc/src/main/java/fr/bigeon/gclc/proc/TaskPool.java b/gclc/src/main/java/fr/bigeon/gclc/proc/TaskPool.java index cd14e83..dfe2865 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/proc/TaskPool.java +++ b/gclc/src/main/java/fr/bigeon/gclc/proc/TaskPool.java @@ -56,8 +56,12 @@ public class TaskPool { /** Add a process in the pool * - * @param cmd the process */ - public void add(final Task cmd) { + * @param cmd the process + * @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(); synchronized (lock) { running.put(pid, cmd); @@ -74,6 +78,7 @@ public class TaskPool { cmd.rmInterruptionListener(this); } }); + return pid; } /** @return the process id */ diff --git a/gclc/src/test/java/fr/bigeon/gclc/proc/ProcessListTest.java b/gclc/src/test/java/fr/bigeon/gclc/proc/ProcessListTest.java new file mode 100644 index 0000000..6d8b401 --- /dev/null +++ b/gclc/src/test/java/fr/bigeon/gclc/proc/ProcessListTest.java @@ -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; + +/** + *

+ * 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(); + } + + } +} diff --git a/gclc/src/test/java/fr/bigeon/gclc/proc/TaskPoolTest.java b/gclc/src/test/java/fr/bigeon/gclc/proc/TaskPoolTest.java new file mode 100644 index 0000000..0533230 --- /dev/null +++ b/gclc/src/test/java/fr/bigeon/gclc/proc/TaskPoolTest.java @@ -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; + +/** + *

+ * 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()); + } +}