json-tck/src/main/java/net/java/html/json/tests/WebSocketTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 563 88fd2972bfdf
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@34
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@34
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@34
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@34
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@34
    42
 */
jaroslav@34
    43
package net.java.html.json.tests;
jaroslav@34
    44
jaroslav@110
    45
import net.java.html.BrwsrCtx;
jaroslav@34
    46
import net.java.html.json.Model;
jaroslav@77
    47
import net.java.html.json.Models;
jaroslav@34
    48
import net.java.html.json.OnReceive;
jaroslav@34
    49
import net.java.html.json.Property;
jaroslav@137
    50
import org.apidesign.html.json.tck.KOTest;
jaroslav@34
    51
jaroslav@247
    52
/** Testing support of WebSocket communication.
jaroslav@34
    53
 *
jaroslav@34
    54
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@34
    55
 */
jaroslav@247
    56
@Model(className = "WebSocketik", properties = {
jaroslav@34
    57
    @Property(name = "fetched", type = Person.class),
jaroslav@34
    58
    @Property(name = "fetchedCount", type = int.class),
jaroslav@250
    59
    @Property(name = "open", type = int.class),
jaroslav@73
    60
    @Property(name = "fetchedResponse", type = String.class),
jaroslav@34
    61
    @Property(name = "fetchedSex", type = Sex.class, array = true)
jaroslav@34
    62
})
jaroslav@247
    63
public final class WebSocketTest {
jaroslav@247
    64
    private WebSocketik js;
jaroslav@138
    65
    private String url;
jaroslav@34
    66
    
jaroslav@247
    67
    @OnReceive(url = "{url}", data = Sex.class, method = "WebSocket", onError = "error")
jaroslav@247
    68
    static void querySex(WebSocketik model, Person data) {
jaroslav@250
    69
        if (data == null) {
jaroslav@250
    70
            model.setOpen(1);
jaroslav@250
    71
        } else {
jaroslav@250
    72
            model.setFetched(data);
jaroslav@250
    73
        }
jaroslav@240
    74
    }
jaroslav@240
    75
    
jaroslav@247
    76
    @KOTest public void connectUsingWebSocket() throws Throwable {
jaroslav@34
    77
        if (js == null) {
jaroslav@138
    78
            url = Utils.prepareURL(
jaroslav@267
    79
                JSONTest.class, "{'firstName': 'Mitar', 'sex': '$0' }", 
jaroslav@247
    80
                "application/javascript",
jaroslav@247
    81
                "protocol:ws"
jaroslav@138
    82
            );
jaroslav@247
    83
            
jaroslav@247
    84
            js = Models.bind(new WebSocketik(), newContext());
jaroslav@34
    85
            js.applyBindings();
jaroslav@34
    86
jaroslav@234
    87
            js.setFetched(null);
jaroslav@250
    88
            
jaroslav@250
    89
            // connects to the server
jaroslav@250
    90
            js.querySex(url, null);
jaroslav@34
    91
        }
jaroslav@34
    92
        
jaroslav@247
    93
        if (bailOutIfNotSupported(js)) {
jaroslav@247
    94
            return;
jaroslav@34
    95
        }
jaroslav@250
    96
        
jaroslav@250
    97
        if (js.getOpen() == 0) {
jaroslav@250
    98
            throw new InterruptedException();
jaroslav@250
    99
        }
jaroslav@250
   100
        if (js.getOpen() == 1) {
jaroslav@250
   101
            // send a query to the server
jaroslav@250
   102
            js.querySex(url, Sex.FEMALE);
jaroslav@250
   103
            js.setOpen(2);
jaroslav@250
   104
        }
jaroslav@34
   105
    
jaroslav@34
   106
        Person p = js.getFetched();
jaroslav@34
   107
        if (p == null) {
jaroslav@34
   108
            throw new InterruptedException();
jaroslav@34
   109
        }
jaroslav@34
   110
        
jaroslav@34
   111
        assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
jaroslav@34
   112
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@250
   113
jaroslav@250
   114
        if (js.getOpen() == 2) {
jaroslav@250
   115
            // close the socket
jaroslav@250
   116
            js.querySex(url, null);
jaroslav@250
   117
            js.setOpen(3);
jaroslav@250
   118
        }
jaroslav@250
   119
        
jaroslav@250
   120
        if (js.getFetchedResponse() == null) {
jaroslav@250
   121
            throw new InterruptedException();
jaroslav@250
   122
        }
jaroslav@250
   123
        assert "null".equals(js.getFetchedResponse()) : "Should be null: " + js.getFetchedResponse();
jaroslav@34
   124
    }
jaroslav@34
   125
    
jaroslav@247
   126
    @KOTest public void errorUsingWebSocket() throws Throwable {
jaroslav@247
   127
        js = Models.bind(new WebSocketik(), newContext());
jaroslav@247
   128
        js.applyBindings();
jaroslav@34
   129
jaroslav@247
   130
        js.setFetched(null);
jaroslav@258
   131
        js.querySex("http://wrong.protocol", null);
jaroslav@247
   132
jaroslav@247
   133
        assert js.getFetchedResponse() != null : "Error reported";
jaroslav@34
   134
    }
jaroslav@258
   135
jaroslav@258
   136
    @KOTest public void haveToOpenTheWebSocket() throws Throwable {
jaroslav@258
   137
        js = Models.bind(new WebSocketik(), newContext());
jaroslav@258
   138
        js.applyBindings();
jaroslav@258
   139
jaroslav@258
   140
        js.setFetched(null);
jaroslav@258
   141
        try {
jaroslav@258
   142
            js.querySex("http://wrong.protocol", Sex.MALE);
jaroslav@258
   143
            assert false : "Should throw an exception";
jaroslav@258
   144
        } catch (IllegalStateException ex) {
jaroslav@258
   145
            assert ex.getMessage().contains("not open") : "Expecting 'not open' msg: " + ex.getMessage();
jaroslav@258
   146
        }
jaroslav@258
   147
    }
jaroslav@34
   148
    
jaroslav@247
   149
    static void error(WebSocketik model, Exception ex) {
jaroslav@250
   150
        if (ex != null) {
jaroslav@250
   151
            model.setFetchedResponse(ex.getClass() + ":" + ex.getMessage());
jaroslav@250
   152
        } else {
jaroslav@250
   153
            model.setFetchedResponse("null");
jaroslav@250
   154
        }
jaroslav@77
   155
    }
jaroslav@240
   156
    
jaroslav@121
   157
    private static BrwsrCtx newContext() {
jaroslav@247
   158
        return Utils.newContext(WebSocketTest.class);
jaroslav@247
   159
    }
jaroslav@247
   160
jaroslav@247
   161
    private boolean bailOutIfNotSupported(WebSocketik js) {
jaroslav@247
   162
        if (js.getFetchedResponse() == null) {
jaroslav@247
   163
            return false;
jaroslav@247
   164
        }
jaroslav@260
   165
        return js.getFetchedResponse().contains("UnsupportedOperationException") &&
jaroslav@260
   166
            Utils.canFailWebSockets(WebSocketTest.class);
jaroslav@121
   167
    }
jaroslav@77
   168
    
jaroslav@34
   169
}