/
Expect.java
312 lines (294 loc) · 12.7 KB
/
Expect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package net.sf.expectit;
/*
* #%L
* ExpectIt
* %%
* Copyright (C) 2014 Alexey Gavrilov and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import net.sf.expectit.interact.InteractBuilder;
import net.sf.expectit.matcher.Matcher;
/**
* The main interface providing access to the expect operations.
* <p/>
* Input data for the operations is stored in the internal buffer growing while new data is being
* received from
* the underlying input stream.
* A successful match operation updates the buffer by removing a part from the begging until the
* end position of the
* match.
* <p/>
* The send methods are thread safe as long as the underlying output streams
* are thread safe. In other words it is safe to send data from one thread and expect the
* results in another.
*/
public interface Expect extends Closeable {
/**
* Sends the string to the output stream and flushes the output.
* <p/>
* The string is converted to bytes using {@code charset} set in the {@link net.sf.expectit
* .ExpectBuilder}.
*
* @param string the string to be sent
* @return this
* @throws IOException if I/O error occurs
*/
Expect send(String string) throws IOException;
/**
* Appends the line separator character to the string and calls {@link #send(String)} method.
*
* @param string the string to be sent
* @return this
* @throws IOException if I/O error occurs
* @see #send(String)
* @see net.sf.expectit.ExpectBuilder#withLineSeparator(String)
*/
Expect sendLine(String string) throws IOException;
/**
* Sends a new line to the output. Simply calls the {@link #sendLine(String)} method with
* empty string as a
* parameter.
*
* @return this
* @throws IOException if I/O error occurs
*/
Expect sendLine() throws IOException;
/**
* Sends the given byte array and flushes the output.
*
* @param bytes the bytes to be sent
* @return this
* @throws IOException if I/O error occurs
*/
Expect sendBytes(byte[] bytes) throws IOException;
/**
* Blocks until the given matcher matches against the first input stream using the default
* timeout.
* <p/>
* The method throws an {@link java.lang.AssertionError} if
* {@link net.sf.expectit.ExpectBuilder#errorOnTimeout} is set. Deprecated.
* <p/>
* An instance of {@link java.io.EOFException} will be thrown if the underlying input steam,
* the internal buffer
* is empty and the match operation is not successful.
*
* @param <R> the matcher type
* @param matcher the mather
* @return the match result
* @throws ExpectIOException if the matcher does not match and the
* the {@link net.sf.expectit.ExpectBuilder#withExceptionOnFailure()} flag was set
* @throws java.nio.channels.ClosedByInterruptException if the thread calling this method has
* been interrupted
* @throws java.io.IOException if an I/O error occurs
*/
<R extends Result> R expect(Matcher<R> matcher) throws IOException;
/**
* Blocks until all the given matchers match against the first input stream using the default
* timeout.
* <p/>
* The method throws an {@link java.lang.AssertionError} if
* {@link net.sf.expectit.ExpectBuilder#errorOnTimeout} is set. Deprecated.
* <p/>
* An instance of {@link java.io.EOFException} will be thrown if the underlying input steam,
* the internal buffer
* is empty and the match operation is not successful.
*
* @param matchers the matchers
* @return the match result from all the matchers
* @throws ExpectIOException if the matcher does not match and the
* the {@link net.sf.expectit.ExpectBuilder#withExceptionOnFailure()} flag was set
* @throws java.nio.channels.ClosedByInterruptException if the thread calling this method has
* been interrupted
* @throws java.io.IOException if an I/O error occurs
*/
MultiResult expect(Matcher<?>... matchers) throws IOException;
/**
* Blocks until the given matcher matches the first input stream using the given timeout.
* <p/>
* The method throws an {@link java.lang.AssertionError} if
* {@link net.sf.expectit.ExpectBuilder#errorOnTimeout} is set. Deprecated.
* <p/>
* An instance of {@link java.io.EOFException} will be thrown if the underlying input stream,
* the internal buffer
* is empty and the match operation is not successful.
*
* @param <R> the matcher type
* @param timeoutMs the timeout for the expect operation in milliseconds,
* infinite if -1 is passed
* @param matcher the mather
* @return the match result
* @throws ExpectIOException if the matcher does not match and the
* the {@link ExpectBuilder#withExceptionOnFailure()} flag was set
* @throws java.nio.channels.ClosedByInterruptException if the thread calling this method has
* been interrupted
* @throws IOException if an I/O error occurs
* @deprecated This method is deprecated and will be removed. Use
* {@link #withTimeout(long, java.util.concurrent.TimeUnit)} to change the timeout for the
* expect operation.
*/
@Deprecated
<R extends Result> R expect(long timeoutMs, Matcher<R> matcher) throws IOException;
/**
* Blocks until all the given matchers match the first input stream using the given timeout.
* <p/>
* The method throws an {@link java.lang.AssertionError} if
* {@link net.sf.expectit.ExpectBuilder#errorOnTimeout} is set. Deprecated.
* <p/>
* An instance of {@link java.io.EOFException} will be thrown if the underlying input steam,
* the internal buffer
* is empty and the match operation is not successful.
*
* @param timeoutMs the timeout for the expect operation in milliseconds,
* infinite if -1 is passed
* @param matcher the matchers
* @return the match result from all the matchers
* @throws ExpectIOException if the matcher does not match and the
* the {@link net.sf.expectit.ExpectBuilder#withExceptionOnFailure()} flag was set
* @throws java.nio.channels.ClosedByInterruptException if the thread calling this method has
* been interrupted
* @throws java.io.IOException if an I/O error occurs
* @deprecated This method is deprecated and will be removed. Use
* {@link #withTimeout(long, java.util.concurrent.TimeUnit)} to change the timeout for the
* expect operation.
*/
@Deprecated
MultiResult expect(long timeoutMs, Matcher<?>... matcher) throws IOException;
/**
* Blocks until the given matcher matches against the given input stream using the default
* timeout.
* <p/>
* The method throws an {@link java.lang.AssertionError} if
* {@link net.sf.expectit.ExpectBuilder#errorOnTimeout} is set. Deprecated.
* <p/>
* An instance of {@link java.io.EOFException} will be thrown if the underlying input steam,
* the internal buffer
* is empty and the match operation is not successful.
*
* @param <R> the matcher type
* @param input the index of the input. if the index is outside of the boundaries,
* a runtime exception will be
* thrown
* @param matcher the matcher
* @return the match result
* @throws ExpectIOException if the matcher does not match and the
* the {@link net.sf.expectit.ExpectBuilder#withExceptionOnFailure()} flag was set
* @throws java.nio.channels.ClosedByInterruptException if the thread calling this method has
* been interrupted
* @throws java.io.IOException if an I/O error occurs
*/
<R extends Result> R expectIn(int input, Matcher<R> matcher) throws IOException;
/**
* Blocks until the given matcher matches against the given input stream using the given
* timeout.
* <p/>
* The method throws an {@link java.lang.AssertionError} if
* {@link net.sf.expectit.ExpectBuilder#errorOnTimeout} is set. Deprecated.
*
* @param <R> the matcher type
* @param input the index of the input. if the index is outside of the boundaries,
* a runtime exception will be
* thrown
* @param timeoutMs the timeout for the expect operation in milliseconds,
* infinite if -1 is passed
* @param matcher the mather
* @return the match result
* @throws ExpectIOException if the matcher does not match and the
* the {@link net.sf.expectit.ExpectBuilder#withExceptionOnFailure()} flag was set
* @throws java.nio.channels.ClosedByInterruptException if the thread calling this method has
* been interrupted
* @throws java.io.IOException if an I/O error occurs
* @deprecated This method is deprecated and will be removed. Use
* {@link #withTimeout(long, java.util.concurrent.TimeUnit)} to change the timeout for the
* expect operation.
*/
@Deprecated
<R extends Result> R expectIn(int input, long timeoutMs, Matcher<R> matcher) throws IOException;
/**
* Blocks until all the given matchers matche against the given input stream using the given
* timeout.
* <p/>
* The method throws an {@link java.lang.AssertionError} if
* {@link net.sf.expectit.ExpectBuilder#errorOnTimeout} is set. Deprecated.
* <p/>
* An instance of {@link java.io.EOFException} will be thrown if the underlying input steam,
* the internal buffer
* is empty and the match operation is not successful.
*
* @param input the index of the input. if the index is outside of the boundaries,
* a runtime exception will be
* thrown
* @param timeoutMs the timeout for the expect operation in milliseconds,
* infinite if -1 is passed
* @param matchers the matchers
* @return the match result from all the matchers
* @throws ExpectIOException if the matcher does not match and the
* the {@link net.sf.expectit.ExpectBuilder#withExceptionOnFailure()} flag was set
* @throws java.nio.channels.ClosedByInterruptException if the thread calling this method has
* been interrupted
* @throws java.io.IOException if an I/O error occurs
* @deprecated This method is deprecated and will be removed. Use
* {@link #withTimeout(long, java.util.concurrent.TimeUnit)} to change the timeout for the
* expect operation.
*/
@Deprecated
MultiResult expectIn(int input, long timeoutMs, Matcher<?>... matchers) throws IOException;
/**
* Sets the default timeout in the given unit for the expect operations for the returned
* instance. The timeout for
* this instance will remain unchanged. Both instances, this and the returned one,
* will share the same configuration
* except the default timeout value.
*
* @param duration the timeout value
* @param unit the time unit
* @return an Expect instance with a new default timeout.
* @throws java.lang.IllegalArgumentException if the timeout {@code <= 0}
*/
Expect withTimeout(long duration, TimeUnit unit);
/**
* Sets the default timeout to infinity. The timeout for this instance will remain unchanged.
* Both instances, this
* and the returned one, will share the same configuration except the default timeout value.
*
* @return an Expect instance with a new default timeout.
* @deprecated use {@link #withTimeout(long, TimeUnit)} with Long.MAX_VALUE, TimeUnit.DAYS
* instead.
*/
@Deprecated
Expect withInfiniteTimeout();
/**
* Closes all resources associated with this instance.
*
* @throws IOException if I/O error occurs
*/
@Override
void close() throws IOException;
/**
* Starts the interactive loop for the first input.
*
* @return the interact builder.
*/
InteractBuilder interact();
/**
* Starts the interactive loop for the given input.
*
* @param input the index of the input stream to use for the interact loop.
* @return the interact builder.
*/
InteractBuilder interactWith(int input);
}