1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.communication;
23
24 import static junit.framework.Assert.assertEquals;
25 import static junit.framework.Assert.fail;
26 import static net.grinder.testutility.AssertUtilities.assertContains;
27 import static net.grinder.testutility.AssertUtilities.assertNotEquals;
28
29 import java.io.InputStream;
30 import java.io.ObjectOutputStream;
31 import java.io.PipedInputStream;
32 import java.io.PipedOutputStream;
33 import java.net.Socket;
34
35 import net.grinder.testutility.IsolatedObjectFactory;
36
37 import org.junit.Test;
38
39
40
41
42
43
44
45 public class TestConnector {
46
47 @Test public void testConnnect() throws Exception {
48 final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create();
49
50 final Connector connector =
51 new Connector(socketAcceptor.getHostName(), socketAcceptor.getPort(),
52 ConnectionType.WORKER);
53
54 final Socket localSocket = connector.connect();
55
56 socketAcceptor.join();
57
58 final Socket serverSocket = socketAcceptor.getAcceptedSocket();
59 final InputStream inputStream = serverSocket.getInputStream();
60
61 assertEquals(ConnectionType.WORKER,
62 Connector.read(inputStream).getConnectionType());
63
64 final byte[] text = "Hello".getBytes();
65
66 localSocket.getOutputStream().write(text);
67
68 for (int i=0; i<text.length; ++i) {
69 assertEquals(text[i], inputStream.read());
70 }
71
72 socketAcceptor.close();
73
74 try {
75 connector.connect();
76 fail("Expected CommunicationException");
77 }
78 catch (CommunicationException e) {
79 }
80
81
82 final Connector badConnector =
83 new Connector("this is not a host name", 1234, ConnectionType.AGENT);
84
85 try {
86 badConnector.connect();
87 fail("Expected CommunicationException");
88 }
89 catch (CommunicationException e) {
90 }
91 }
92
93 @Test public void testBadRead() throws Exception {
94 final PipedOutputStream out = new PipedOutputStream();
95 final PipedInputStream in = new PipedInputStream(out);
96
97 for (int x = 0; x < 100; ++x) {
98 out.write(99);
99 }
100
101 try {
102 Connector.read(in);
103 fail("Expected CommunicationException");
104 }
105 catch (CommunicationException e) {
106 }
107
108 final ObjectOutputStream objectStream = new ObjectOutputStream(out);
109 objectStream.writeObject(ConnectionType.WORKER);
110 objectStream.write(99);
111 objectStream.writeObject(null);
112
113 try {
114 Connector.read(in);
115 fail("Expected CommunicationException");
116 }
117 catch (CommunicationException e) {
118 }
119
120 while (in.available() > 0) {
121 in.read();
122 }
123
124 objectStream.writeObject(ConnectionType.WORKER);
125 objectStream.writeObject(IsolatedObjectFactory.getIsolatedObject());
126
127 try {
128 Connector.read(in);
129 fail("Expected CommunicationException");
130 }
131 catch (CommunicationException e) {
132 }
133 }
134
135 @Test public void testEquality() throws Exception {
136 final Connector connector =
137 new Connector("a", 1234, ConnectionType.WORKER);
138
139 assertEquals(connector.hashCode(), connector.hashCode());
140 assertEquals(connector, connector);
141 assertNotEquals(connector, null);
142 assertNotEquals(connector, this);
143
144 final Connector[] equal = {
145 new Connector("a", 1234, ConnectionType.WORKER),
146 };
147
148 final Connector[] notEqual = {
149 new Connector("a", 6423, ConnectionType.WORKER),
150 new Connector("b", 1234, ConnectionType.WORKER),
151 new Connector("a", 1234, ConnectionType.AGENT),
152 };
153
154 for (int i = 0; i < equal.length; ++i) {
155 assertEquals(connector.hashCode(), equal[i].hashCode());
156 assertEquals(connector, equal[i]);
157 }
158
159 for (int i = 0; i < notEqual.length; ++i) {
160 assertNotEquals(connector, notEqual[i]);
161 }
162 }
163
164 @Test public void testGetEndpointAsString() throws Exception {
165 assertEquals(
166 "a:1234",
167 new Connector("a", 1234, ConnectionType.WORKER).getEndpointAsString());
168
169 final String description =
170 new Connector("", 1234, ConnectionType.WORKER).getEndpointAsString();
171
172 assertContains(description, "localhost");
173 assertContains(description, "1234");
174 }
175 }