• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test.remote;
2 
3 import org.testng.Assert;
4 import org.testng.annotations.Test;
5 import org.testng.collections.Lists;
6 import org.testng.remote.RemoteTestNG;
7 import org.testng.remote.strprotocol.IMessage;
8 import org.testng.remote.strprotocol.IMessageSender;
9 import org.testng.remote.strprotocol.MessageHub;
10 import org.testng.remote.strprotocol.SerializedMessageSender;
11 import org.testng.remote.strprotocol.StringMessageSender;
12 
13 import test.SimpleBaseTest;
14 
15 import java.net.SocketTimeoutException;
16 import java.util.ArrayList;
17 import java.util.List;
18 
19 /**
20  * A simple client that launches RemoteTestNG and then talks to it via the
21  * two supported protocols, String and Serialized.
22  *
23  * @author Cedric Beust <cedric@beust.com>
24  */
25 public class RemoteTest extends SimpleBaseTest {
26   // Note: don't use the ports used by the plug-in or the RemoteTestNG processes
27   // launched in this test will interfere with the plug-in.
28   private static final int PORT1 = 1243;
29   private static final int PORT2 = 1242;
30   private static final List<String> EXPECTED_MESSAGES = new ArrayList<String>() {{
31     add("GenericMessage"); // method and test counts
32     add("SuiteMessage");  // suite started
33     add("TestMessage");  // test started
34     add("TestResultMessage"); // status: started
35     add("TestResultMessage"); // status: success
36     add("TestResultMessage"); // status: started
37     add("TestResultMessage"); // status: success
38     add("TestMessage"); // test finished
39     add("SuiteMessage"); // suite finished
40   }};
41 
42   @Test
testSerialized()43   public void testSerialized() {
44     runTest("-serport", PORT1, new SerializedMessageSender("localhost", PORT1));
45   }
46 
47   @Test
testString()48   public void testString() {
49     runTest("-port", PORT2, new StringMessageSender("localhost", PORT2));
50   }
51 
launchRemoteTestNG(final String portArg, final int portValue)52   private void launchRemoteTestNG(final String portArg, final int portValue) {
53     new Thread(new Runnable() {
54       @Override
55       public void run() {
56         RemoteTestNG.main(new String[] {
57             portArg, Integer.toString(portValue), "-dontexit",
58             getPathToResource("testng-remote.xml")
59           });
60         }
61       }).start();
62   }
63 
runTest(String arg, int portValue, IMessageSender sms)64   private void runTest(String arg, int portValue, IMessageSender sms) {
65     p("Launching RemoteTestNG on port " + portValue);
66     launchRemoteTestNG(arg, portValue);
67     MessageHub mh = new MessageHub(sms);
68     List<String> received = Lists.newArrayList();
69     try {
70       mh.initReceiver();
71       IMessage message = mh.receiveMessage();
72       while (message != null) {
73         received.add(message.getClass().getSimpleName());
74         message = mh.receiveMessage();
75       }
76 
77       Assert.assertEquals(received, EXPECTED_MESSAGES);
78     }
79     catch(SocketTimeoutException ex) {
80       Assert.fail("Time out");
81     }
82   }
83 
p(String s)84   private static void p(String s) {
85     if (false) {
86       System.out.println("[RemoteTest] " + s);
87     }
88   }
89 }
90