001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.components;
004:
005: import java.util.*;
006: import fitnesse.testutil.*;
007: import fitnesse.responders.run.SocketDealer;
008: import fit.Counts;
009:
010: public class FitClientTest extends AbstractRegex implements
011: FitClientListener {
012: private List outputs = new ArrayList();
013:
014: private List counts = new ArrayList();
015:
016: public CommandRunningFitClient client;
017:
018: private boolean exceptionOccurred = false;
019:
020: private int port = 9080;
021:
022: private FitSocketReceiver receiver;
023:
024: SimpleSocketDoner doner;
025:
026: public void setUp() throws Exception {
027: CommandRunningFitClient.TIMEOUT = 5000;
028: client = new CommandRunningFitClient(this ,
029: "java -cp classes fit.FitServer -v", port,
030: new SocketDealer());
031: receiver = new CustomFitSocketReceiver(port);
032: }
033:
034: private class CustomFitSocketReceiver extends FitSocketReceiver {
035: public CustomFitSocketReceiver(int port) {
036: super (port, null);
037: }
038:
039: protected void dealSocket(int ticket) throws Exception {
040: doner = new SimpleSocketDoner(socket);
041: client.acceptSocketFrom(doner);
042: }
043: }
044:
045: public void tearDown() throws Exception {
046: receiver.close();
047: }
048:
049: public void acceptOutput(String output) {
050: outputs.add(output);
051: }
052:
053: public void acceptResults(Counts counts) {
054: this .counts.add(counts);
055: }
056:
057: public void exceptionOccurred(Exception e) {
058: exceptionOccurred = true;
059: try {
060: client.kill();
061: } catch (Exception e1) {
062: e1.printStackTrace();
063: }
064: }
065:
066: public void XtestOneRunUsage() throws Exception {
067: // FIXME is an integration test and expects something to be running
068: // already
069:
070: doSimpleRun();
071: assertFalse(exceptionOccurred);
072: assertEquals(1, outputs.size());
073: assertEquals(1, counts.size());
074: assertSubString("class", (String) outputs.get(0));
075: assertEquals(1, ((Counts) counts.get(0)).right);
076: }
077:
078: private void doSimpleRun() throws Exception {
079: receiver.receiveSocket();
080: client.start();
081: client
082: .send("<html><table><tr><td>fitnesse.testutil.PassFixture</td></tr></table></html>");
083: client.done();
084: client.join();
085: }
086:
087: public void testStandardError() throws Exception {
088: client = new CommandRunningFitClient(this , "java blah", port,
089: new SocketDealer());
090: client.start();
091: client.join();
092: assertTrue(exceptionOccurred);
093: // I have commented this out because the exception being thrown is "java.lang.NoClassDefFoundError: blah", and not an Exception.
094: // This may be because I'm building on JRockit jre1.5.0_06 instead of whatever the other developers are building against.
095: // assertSubString("Exception", client.commandRunner.getError());
096: }
097:
098: public void testDosntwaitForTimeoutOnBadCommand() throws Exception {
099: CommandRunningFitClient.TIMEOUT = 5000;
100: long startTime = System.currentTimeMillis();
101: client = new CommandRunningFitClient(this , "java blah", port,
102: new SocketDealer());
103: client.start();
104: client.join();
105: assertTrue(exceptionOccurred);
106: assertTrue(System.currentTimeMillis() - startTime < CommandRunningFitClient.TIMEOUT);
107:
108: }
109:
110: public void XtestOneRunWithManyTables() throws Exception {
111: // FIXME is an integration test and expects something to be running
112: // already
113:
114: receiver.receiveSocket();
115: client.start();
116: client
117: .send("<html><table><tr><td>fitnesse.testutil.PassFixture</td></tr></table>"
118: + "<table><tr><td>fitnesse.testutil.FailFixture</td></tr></table>"
119: + "<table><tr><td>fitnesse.testutil.ErrorFixture</td></tr></table></html>");
120: client.done();
121: client.join();
122: assertFalse(exceptionOccurred);
123: assertEquals(3, outputs.size());
124: assertEquals(1, counts.size());
125: Counts count = (Counts) counts.get(0);
126: assertEquals(1, count.right);
127: assertEquals(1, count.wrong);
128: assertEquals(1, count.exceptions);
129: }
130:
131: public void XtestManyRuns() throws Exception {
132: // FIXME is an integration test and expects something to be running
133: // already
134:
135: receiver.receiveSocket();
136: client.start();
137: client
138: .send("<html><table><tr><td>fitnesse.testutil.PassFixture</td></tr></table></html>");
139: client
140: .send("<html><table><tr><td>fitnesse.testutil.FailFixture</td></tr></table></html>");
141: client
142: .send("<html><table><tr><td>fitnesse.testutil.ErrorFixture</td></tr></table></html>");
143: client.done();
144: client.join();
145:
146: assertFalse(exceptionOccurred);
147: assertEquals(3, outputs.size());
148: assertEquals(3, counts.size());
149: assertEquals(1, ((Counts) counts.get(0)).right);
150: assertEquals(1, ((Counts) counts.get(1)).wrong);
151: assertEquals(1, ((Counts) counts.get(2)).exceptions);
152: }
153:
154: public void XtestDonerIsNotifiedWhenFinished_success()
155: throws Exception {
156: // FIXME is an integration test and expects something to be running
157: // already
158:
159: doSimpleRun();
160: assertTrue(doner.finished);
161: }
162:
163: public void XtestReadyForSending() throws Exception {
164: // FIXME is an integration test and expects something to be running
165: // already
166:
167: CommandRunningFitClient.TIMEOUT = 5000;
168: Thread startThread = new Thread() {
169: public void run() {
170: try {
171: client.start();
172: } catch (InterruptedException ie) {
173: } catch (Exception e) {
174: e.printStackTrace();
175: }
176: }
177: };
178: startThread.start();
179: Thread.sleep(100);
180: assertFalse(client.isSuccessfullyStarted());
181:
182: client.acceptSocketFrom(new SimpleSocketDoner(
183: new MockSocket("")));
184: Thread.sleep(100);
185: assertTrue(client.isSuccessfullyStarted());
186:
187: startThread.interrupt();
188: }
189:
190: public void XtestUnicodeCharacters() throws Exception {
191: // FIXME is an integration test and expects something to be running
192: // already
193:
194: receiver.receiveSocket();
195: client.start();
196: client
197: .send("<html><table><tr><td>fitnesse.testutil.EchoFixture</td><td>\uba80\uba81\uba82\uba83</td></tr></table></html>");
198: client.done();
199: client.join();
200:
201: assertFalse(exceptionOccurred);
202: StringBuffer buffer = new StringBuffer();
203: for (Iterator iterator = outputs.iterator(); iterator.hasNext();)
204: buffer.append(iterator.next());
205:
206: assertSubString("\uba80\uba81\uba82\uba83", buffer.toString());
207: }
208: }
|