1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.incubator.http.internal.websocket;
  24 
  25 import org.testng.annotations.Test;
  26 
  27 import jdk.incubator.http.internal.websocket.TestSupport.AssertionFailedException;
  28 import java.util.concurrent.TimeUnit;
  29 
  30 import static jdk.incubator.http.internal.websocket.TestSupport.assertThrows;
  31 import static jdk.incubator.http.internal.websocket.TestSupport.checkExpectations;
  32 
  33 public class MockListenerTest {
  34 
  35     @Test
  36     public void testPass01() {
  37         MockListener l = new MockListener.Builder().build();
  38         checkExpectations(1, TimeUnit.SECONDS, l);
  39     }
  40 
  41     @Test
  42     public void testPass02() {
  43         MockListener l = new MockListener.Builder()
  44                 .expectOnOpen(ws -> ws == null)
  45                 .build();
  46         l.onOpen(null);
  47         checkExpectations(1, TimeUnit.SECONDS, l);
  48     }
  49 
  50     @Test
  51     public void testPass03() {
  52         MockListener l = new MockListener.Builder()
  53                 .expectOnOpen(ws -> ws == null)
  54                 .expectOnClose((ws, code, reason) ->
  55                         ws == null && code == 1002 && "blah".equals(reason))
  56                 .build();
  57         l.onOpen(null);
  58         l.onClose(null, 1002, "blah");
  59         checkExpectations(1, TimeUnit.SECONDS, l);
  60     }
  61 
  62     @Test
  63     public void testFail01() {
  64         MockListener l = new MockListener.Builder()
  65                 .expectOnOpen(ws -> ws != null)
  66                 .build();
  67         l.onOpen(null);
  68         assertThrows(AssertionFailedException.class,
  69                 () -> checkExpectations(1, TimeUnit.SECONDS, l));
  70     }
  71 
  72     @Test
  73     public void testFail02() {
  74         MockListener l = new MockListener.Builder()
  75                 .expectOnOpen(ws -> true)
  76                 .build();
  77         assertThrows(AssertionFailedException.class,
  78                 () -> checkExpectations(1, TimeUnit.SECONDS, l));
  79     }
  80 
  81     @Test
  82     public void testFail03() {
  83         MockListener l = new MockListener.Builder()
  84                 .expectOnOpen(ws -> true)
  85                 .build();
  86         l.onOpen(null);
  87         l.onClose(null, 1002, "");
  88         assertThrows(AssertionFailedException.class,
  89                 () -> checkExpectations(1, TimeUnit.SECONDS, l));
  90     }
  91 
  92     @Test
  93     public void testFail04() {
  94         MockListener l = new MockListener.Builder().build();
  95         l.onClose(null, 1002, "");
  96         assertThrows(AssertionFailedException.class,
  97                 () -> checkExpectations(1, TimeUnit.SECONDS, l));
  98     }
  99 }