1 4 package org.oddjob.monitor.action; 5 6 import junit.framework.TestCase; 7 8 import org.apache.log4j.Logger; 9 import org.oddjob.Stateful; 10 import org.oddjob.monitor.model.ExplorerContext; 11 import org.oddjob.monitor.model.ExplorerModel; 12 import org.oddjob.monitor.model.JobAction; 13 import org.oddjob.state.JobState; 14 import org.oddjob.state.JobStateEvent; 15 import org.oddjob.state.JobStateListener; 16 import org.oddjob.util.ThreadManager; 17 18 public class ExecuteActionTest extends TestCase { 19 private static final Logger logger = Logger.getLogger(ExecuteActionTest.class); 20 21 25 public void testPerform() throws Exception { 26 class MyR implements Runnable { 27 boolean ran = false; 28 public void run() { 29 ran = true; 30 } 31 } 32 MyR sample = new MyR(); 33 34 ExplorerModel em = new ExplorerModel(); 35 em.setRoot(sample); 36 ThreadManager tm = new ThreadManager(); 37 em.setThreadManager(tm); 38 ExplorerContext ec = new ExplorerContext(em); 39 40 JobAction test = new ExecuteAction(); 41 test.optionSelect(sample, ec); 42 43 test.action(); 44 45 while (tm.activeDescriptions().length > 0) { 46 logger.debug("Waiting for ThreadManager."); 47 Thread.yield(); 48 } 49 assertTrue(sample.ran); 50 } 51 52 56 public void testEnabled() { 57 class MySR implements Stateful, Runnable { 58 boolean removed; 59 public void run() {} 60 public void addJobStateListener(JobStateListener listener) { 61 listener.jobStateChange( 62 new JobStateEvent(this, JobState.READY)); 63 } 64 public void removeJobStateListener(JobStateListener listener) { 65 removed = true; 66 } 67 } 68 MySR sample = new MySR(); 69 70 ExplorerModel em = new ExplorerModel(); 71 em.setRoot(sample); 72 ExplorerContext ec = new ExplorerContext(em); 73 74 JobAction test = new ExecuteAction(); 75 76 test.optionSelect(sample, ec); 77 assertTrue(test.enabled()); 78 79 test.optionSelect(null, ec); 80 assertTrue(sample.removed); 81 } 82 83 87 public void testDisabled() { 88 class MySR implements Stateful, Runnable { 89 boolean removed; 90 public void run() {} 91 public void addJobStateListener(JobStateListener listener) { 92 listener.jobStateChange( 93 new JobStateEvent(this, JobState.COMPLETE)); 94 } 95 public void removeJobStateListener(JobStateListener listener) { 96 removed = true; 97 } 98 } 99 MySR sample = new MySR(); 100 101 ExplorerModel em = new ExplorerModel(); 102 em.setRoot(sample); 103 ExplorerContext ec = new ExplorerContext(em); 104 105 JobAction test = new ExecuteAction(); 106 107 test.optionSelect(sample, ec); 108 assertFalse(test.enabled()); 109 110 test.optionSelect(null, ec); 111 assertTrue(sample.removed); 112 113 } 114 115 119 public void testWithObject() { 120 JobAction test = new ExecuteAction(); 121 test.optionSelect(new Object (), null); 122 assertFalse(test.enabled()); 123 } 124 } 125 | Popular Tags |