1 33 34 package edu.rice.cs.drjava.ui; 35 36 import edu.rice.cs.drjava.model.GlobalModelTestCase; 37 import edu.rice.cs.drjava.model.repl.InteractionsDJDocument; 38 import edu.rice.cs.util.text.ConsoleDocument; 39 import edu.rice.cs.util.text.EditDocumentException; 40 41 49 public final class ConsoleControllerTest extends GlobalModelTestCase { 50 51 protected InteractionsDJDocument _adapter; 52 53 54 protected ConsoleDocument _doc; 55 56 57 protected InteractionsPane _pane; 58 59 60 protected ConsoleController _controller; 61 62 63 protected Object _lock; 64 65 68 public void setUp() throws Exception { 69 super.setUp(); 70 _adapter = _model.getSwingConsoleDocument(); 71 _doc = _model.getConsoleDocument(); 72 _controller = new TestConsoleController(_doc, _adapter); 73 _pane = _controller.getPane(); 74 _model.getInteractionsModel().setInputListener(_controller.getInputListener()); 75 _lock = _controller.getInputWaitObject(); } 77 78 81 public void tearDown() throws Exception { 82 _adapter = null; 83 _doc = null; 84 _controller = null; 85 _pane = null; 86 super.tearDown(); 87 } 88 89 95 public void testBasicConsoleInput() 96 throws EditDocumentException, InterruptedException { 97 Thread inputGenerator = new InputGeneratorThread("a"); 98 String result; 99 synchronized(_lock) { 100 inputGenerator.start(); 101 _lock.wait(); } 104 result = interpret("System.in.read()"); 105 106 String expected = 107 String.valueOf((int) 'a'); 108 assertEquals("read() returns the correct character", expected, result); 109 result = interpret("System.in.read()"); 110 assertEquals("second read() should get the end-of-line character", 111 String.valueOf((int) System.getProperty("line.separator").charAt(0)), result); 112 } 113 114 117 protected class InputGeneratorThread extends Thread { 118 private String _text; 119 122 public InputGeneratorThread(String text) { 123 _text = text; 124 } 125 public void run() { 126 try { 127 synchronized(_lock) { 128 _lock.notify(); _lock.wait(); _doc.insertText(_doc.getLength(), _text, ConsoleDocument.DEFAULT_STYLE); 132 _controller.enterAction.actionPerformed(null); 133 } 134 } 135 catch (Exception e) { 136 listenerFail("InputGenerator failed: " + e); 137 } 138 } 139 } 140 141 145 protected class TestConsoleController extends ConsoleController { 146 public TestConsoleController(ConsoleDocument doc, InteractionsDJDocument adapter) { 147 super(doc, adapter); 148 } 149 150 protected void _waitForInput() { 151 synchronized(_lock) { 152 _lock.notify(); super._waitForInput(); 154 } 155 } 156 } 157 } 158 | Popular Tags |