3131import org .junit .Test ;
3232
3333import java .util .ArrayList ;
34+ import java .util .Arrays ;
3435import java .util .Collections ;
3536import java .util .Comparator ;
3637import java .util .HashMap ;
4647import static org .junit .Assert .assertNull ;
4748import static org .junit .Assert .assertSame ;
4849import static org .junit .Assert .assertThat ;
50+ import static org .junit .Assert .assertThrows ;
4951import static org .junit .Assert .assertTrue ;
5052import static org .junit .Assert .fail ;
5153
@@ -150,7 +152,7 @@ public void attach_detach() {
150152 }
151153 }
152154
153- /* @Test*/
155+ @ Test
154156 public void attach_with_channel_params_channels_get () {
155157 String channelName = "attach_with_channel_params_channels_get_" + testParams .name ;
156158 AblyRealtime ably = null ;
@@ -163,7 +165,7 @@ public void attach_with_channel_params_channels_get() {
163165 assertEquals ("Verify connected state reached" , ConnectionState .connected , ably .connection .state );
164166
165167 ChannelOptions options = new ChannelOptions ();
166- options .params = new HashMap <String , String >();
168+ options .params = new HashMap <>();
167169 options .params .put ("modes" , "subscribe" );
168170 options .params .put ("delta" , "vcdiff" );
169171
@@ -172,7 +174,7 @@ public void attach_with_channel_params_channels_get() {
172174 channel .attach ();
173175 (new ChannelWaiter (channel )).waitFor (ChannelState .attached );
174176 assertEquals ("Verify attached state reached" , ChannelState .attached , channel .state );
175- assertEquals ("Verify channel params" , channel .getParams (), options . params );
177+ assertEquals ("Verify channel params" , options . params , channel .getParams ());
176178 assertArrayEquals ("Verify channel modes" , new ChannelMode [] { ChannelMode .subscribe }, channel .getModes ());
177179 } catch (AblyException e ) {
178180 e .printStackTrace ();
@@ -183,7 +185,7 @@ public void attach_with_channel_params_channels_get() {
183185 }
184186 }
185187
186- /* @Test*/
188+ @ Test
187189 public void attach_with_channel_params_set_options () {
188190 String channelName = "attach_with_channel_params_set_options_" + testParams .name ;
189191 AblyRealtime ably = null ;
@@ -196,6 +198,7 @@ public void attach_with_channel_params_set_options() {
196198 assertEquals ("Verify connected state reached" , ConnectionState .connected , ably .connection .state );
197199
198200 ChannelOptions options = new ChannelOptions ();
201+ options .params = new HashMap <>();
199202 options .params .put ("modes" , "subscribe" );
200203 options .params .put ("delta" , "vcdiff" );
201204
@@ -205,7 +208,7 @@ public void attach_with_channel_params_set_options() {
205208 channel .attach ();
206209 (new ChannelWaiter (channel )).waitFor (ChannelState .attached );
207210 assertEquals ("Verify attached state reached" , ChannelState .attached , channel .state );
208- assertEquals ("Verify channel params" , channel .getParams (), options . params );
211+ assertEquals ("Verify channel params" , options . params , channel .getParams ());
209212 assertArrayEquals ("Verify channel modes" , new ChannelMode [] { ChannelMode .subscribe }, channel .getModes ());
210213 } catch (AblyException e ) {
211214 e .printStackTrace ();
@@ -216,7 +219,7 @@ public void attach_with_channel_params_set_options() {
216219 }
217220 }
218221
219- /* @Test*/
222+ @ Test
220223 public void channels_get_should_throw_when_would_cause_reattach () {
221224 String channelName = "channels_get_should_throw_when_would_cause_reattach_" + testParams .name ;
222225 AblyRealtime ably = null ;
@@ -228,22 +231,25 @@ public void channels_get_should_throw_when_would_cause_reattach() {
228231 (new ConnectionWaiter (ably .connection )).waitFor (ConnectionState .connected );
229232 assertEquals ("Verify connected state reached" , ConnectionState .connected , ably .connection .state );
230233
231- ChannelOptions options = new ChannelOptions ();
232- options .params .put ("modes" , "subscribe" );
233- options .params .put ("delta" , "vcdiff" );
234+ ChannelOptions options = new ChannelOptions () {{
235+ params = new HashMap <>();
236+ params .put ("modes" , "subscribe" );
237+ params .put ("delta" , "vcdiff" );
238+ }};
234239
235240 /* create a channel and attach */
236241 final Channel channel = ably .channels .get (channelName , options );
237242 channel .attach ();
238243 (new ChannelWaiter (channel )).waitFor (ChannelState .attached );
239244
240- try {
241- ably .channels .get (channelName , options );
242- } catch (AblyException e ) {
243- assertEquals ("Verify error code" , 400 , e .errorInfo .code );
244- assertEquals ("Verify error status code" , 40000 , e .errorInfo .statusCode );
245- assertTrue ("Verify error message" , e .errorInfo .message .contains ("setOptions" ));
246- }
245+ AblyRealtime finalAbly = ably ;
246+ AblyException exception = assertThrows (AblyException .class , () ->
247+ finalAbly .channels .get (channelName , options ));
248+
249+ assertEquals ("Verify error code" , 400 , exception .errorInfo .code );
250+ assertEquals ("Verify error status code" , 40000 , exception .errorInfo .statusCode );
251+ assertTrue ("Verify error message" , exception .errorInfo .message .contains ("use Channel.setOptions()" ));
252+
247253 } catch (AblyException e ) {
248254 e .printStackTrace ();
249255 fail ("init0: Unexpected exception instantiating library" );
@@ -253,7 +259,7 @@ public void channels_get_should_throw_when_would_cause_reattach() {
253259 }
254260 }
255261
256- /* @Test*/
262+ @ Test
257263 public void attach_with_channel_params_modes_and_channel_modes () {
258264 String channelName = "attach_with_channel_params_modes_and_channel_modes_" + testParams .name ;
259265 AblyRealtime ably = null ;
@@ -279,7 +285,10 @@ public void attach_with_channel_params_modes_and_channel_modes() {
279285 (new ChannelWaiter (channel )).waitFor (ChannelState .attached );
280286 assertEquals ("Verify attached state reached" , ChannelState .attached , channel .state );
281287 assertEquals ("Verify channel params" , channel .getParams (), options .params );
282- assertArrayEquals ("Verify channel modes" , new ChannelMode [] { ChannelMode .subscribe , ChannelMode .presence }, channel .getModes ());
288+ // modes in params overrides individual modes in options
289+ assertThat ("Verify channel modes" , Arrays .asList (channel .getModes ()),
290+ Matchers .containsInAnyOrder (ChannelMode .presence , ChannelMode .subscribe ));
291+
283292 } catch (AblyException e ) {
284293 e .printStackTrace ();
285294 fail ("init0: Unexpected exception instantiating library" );
@@ -289,7 +298,7 @@ public void attach_with_channel_params_modes_and_channel_modes() {
289298 }
290299 }
291300
292- /* @Test*/
301+ @ Test
293302 public void attach_with_channel_modes () {
294303 String channelName = "attach_with_channel_modes_" + testParams .name ;
295304 AblyRealtime ably = null ;
@@ -312,7 +321,11 @@ public void attach_with_channel_modes() {
312321 channel .attach ();
313322 (new ChannelWaiter (channel )).waitFor (ChannelState .attached );
314323 assertEquals ("Verify attached state reached" , ChannelState .attached , channel .state );
315- assertEquals ("Verify channel modes" , channel .getModes (), options .modes );
324+ assertEquals (2 , channel .getModes ().length );
325+
326+ assertThat ("Verify channel modes" ,
327+ Arrays .asList (channel .getModes ()),
328+ Matchers .containsInAnyOrder (ChannelMode .publish , ChannelMode .presence_subscribe ));
316329 } catch (AblyException e ) {
317330 e .printStackTrace ();
318331 fail ("init0: Unexpected exception instantiating library" );
@@ -322,7 +335,7 @@ public void attach_with_channel_modes() {
322335 }
323336 }
324337
325- /* @Test*/
338+ @ Test
326339 public void attach_with_params_delta_and_channel_modes () {
327340 String channelName = "attach_with_params_delta_and_channel_modes_" + testParams .name ;
328341 AblyRealtime ably = null ;
@@ -335,7 +348,7 @@ public void attach_with_params_delta_and_channel_modes() {
335348 assertEquals ("Verify connected state reached" , ConnectionState .connected , ably .connection .state );
336349
337350 ChannelOptions options = new ChannelOptions ();
338- options .params = new HashMap <String , String >();
351+ options .params = new HashMap <>();
339352 options .params .put ("delta" , "vcdiff" );
340353 options .modes = new ChannelMode [] {
341354 ChannelMode .publish ,
@@ -348,9 +361,11 @@ public void attach_with_params_delta_and_channel_modes() {
348361 channel .attach ();
349362 (new ChannelWaiter (channel )).waitFor (ChannelState .attached );
350363 assertEquals ("Verify attached state reached" , ChannelState .attached , channel .state );
351- options .params .put ("modes" , "publish,subscribe,presence_subscribe" );
352- assertEquals ("Verify channel params" , channel .getParams (), options .params );
353- assertEquals ("Verify channel modes" , channel .getModes (), options .modes );
364+
365+ assertEquals ("Verify channel params" , options .params , channel .getParams ());
366+ assertThat ("Verify channel modes" ,
367+ Arrays .asList (channel .getModes ()),
368+ Matchers .containsInAnyOrder (ChannelMode .publish , ChannelMode .subscribe , ChannelMode .presence_subscribe ));
354369 } catch (AblyException e ) {
355370 e .printStackTrace ();
356371 fail ("init0: Unexpected exception instantiating library" );
@@ -2546,15 +2561,15 @@ public void connect_on_closing_client_should_reinitialize_channels() throws Ably
25462561 new ConnectionWaiter (ably .connection ).waitFor (ConnectionState .closing );
25472562 new ConnectionWaiter (ably .connection ).waitFor (ConnectionState .connected );
25482563
2549- assertEquals (List . of (ConnectionState .closing , ConnectionState .connecting , ConnectionState .connected ), observedConnectionStates );
2564+ assertEquals (Arrays . asList (ConnectionState .closing , ConnectionState .connecting , ConnectionState .connected ), observedConnectionStates );
25502565 assertEquals (ChannelState .initialized , channel .state );
25512566
25522567 channel .attach ();
25532568 new ChannelWaiter (channel ).waitFor (ChannelState .attached );
25542569
25552570 assertNull (channel .reason );
25562571 assertEquals (0 , ably .connection .connectionManager .msgSerial );
2557- assertEquals (List . of (ChannelState .detached , ChannelState .initialized , ChannelState .attaching , ChannelState .attached ), observedChannelStates );
2572+ assertEquals (Arrays . asList (ChannelState .detached , ChannelState .initialized , ChannelState .attaching , ChannelState .attached ), observedChannelStates );
25582573 }
25592574 }
25602575
0 commit comments