json/src/test/java/org/netbeans/html/json/impl/OnReceiveTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 650 cbffa9025961
child 838 bdc3d696dd4a
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
jtulach@650
     1
/**
jtulach@650
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@650
     3
 *
jtulach@650
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@650
     5
 *
jtulach@650
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@650
     7
 * Other names may be trademarks of their respective owners.
jtulach@650
     8
 *
jtulach@650
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@650
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@650
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@650
    12
 * "License"). You may not use this file except in compliance with the
jtulach@650
    13
 * License. You can obtain a copy of the License at
jtulach@650
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@650
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@650
    16
 * specific language governing permissions and limitations under the
jtulach@650
    17
 * License.  When distributing the software, include this License Header
jtulach@650
    18
 * Notice in each file and include the License file at
jtulach@650
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@650
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@650
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@650
    22
 * accompanied this code. If applicable, add the following below the
jtulach@650
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@650
    24
 * your own identifying information:
jtulach@650
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@650
    26
 *
jtulach@650
    27
 * Contributor(s):
jtulach@650
    28
 *
jtulach@650
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@650
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@650
    31
 *
jtulach@650
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@650
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@650
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@650
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@650
    36
 * single choice of license, a recipient has the option to distribute
jtulach@650
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@650
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@650
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@650
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@650
    41
 * made subject to such option by the copyright holder.
jtulach@650
    42
 */
jtulach@650
    43
package org.netbeans.html.json.impl;
jtulach@650
    44
jtulach@650
    45
import java.io.IOException;
jtulach@650
    46
import java.io.InputStream;
jtulach@650
    47
import java.util.HashMap;
jtulach@650
    48
import java.util.Map;
jtulach@650
    49
import net.java.html.BrwsrCtx;
jtulach@650
    50
import net.java.html.json.Models;
jtulach@650
    51
import net.java.html.json.Person;
jtulach@650
    52
import org.apidesign.html.context.spi.Contexts;
jtulach@650
    53
import org.apidesign.html.json.spi.JSONCall;
jtulach@650
    54
import org.apidesign.html.json.spi.Transfer;
jtulach@650
    55
import static org.testng.Assert.*;
jtulach@650
    56
import org.testng.annotations.Test;
jtulach@650
    57
jtulach@650
    58
/**
jtulach@650
    59
 *
jtulach@790
    60
 * @author Jaroslav Tulach
jtulach@650
    61
 */
jtulach@650
    62
public class OnReceiveTest {
jtulach@650
    63
    @Test public void performJSONCall() {
jtulach@650
    64
        MockTrans mt = new MockTrans();
jtulach@650
    65
        BrwsrCtx ctx = Contexts.newBuilder().register(Transfer.class, mt, 1).build();
jtulach@650
    66
        
jtulach@650
    67
        Employee e = Models.bind(new Employee(), ctx);
jtulach@650
    68
        e.setCall(null);
jtulach@650
    69
        Person p = new Person();
jtulach@650
    70
        
jtulach@650
    71
        mt.result = new HashMap<String, String>();
jtulach@650
    72
        mt.result.put("firstName", "Jarda");
jtulach@650
    73
        mt.result.put("lastName", "Tulach");
jtulach@650
    74
        e.changePersonalities(1, 2.0, "3", p);
jtulach@650
    75
        final Call c = e.getCall();
jtulach@650
    76
        assertNotNull(c, "A call has been made");
jtulach@650
    77
        assertEquals(c.getI(), 1);
jtulach@650
    78
        assertEquals(c.getD(), 2.0);
jtulach@650
    79
        assertEquals(c.getS(), "3");
jtulach@650
    80
        assertEquals(c.getP(), p);
jtulach@650
    81
        assertEquals(c.getData().size(), 1, "One result sent over wire");
jtulach@650
    82
        assertEquals(c.getData().get(0).getFirstName(), "Jarda");
jtulach@650
    83
        assertEquals(c.getData().get(0).getLastName(), "Tulach");
jtulach@650
    84
    }
jtulach@650
    85
jtulach@650
    86
    
jtulach@650
    87
    public static class MockTrans implements Transfer {
jtulach@650
    88
        Map<String,String> result;
jtulach@650
    89
        
jtulach@650
    90
        @Override
jtulach@650
    91
        public void extract(Object obj, String[] props, Object[] values) {
jtulach@650
    92
            assertTrue(obj instanceof Map, "It is a map: " + obj);
jtulach@650
    93
            Map<?,?> mt = (Map<?,?>) obj;
jtulach@650
    94
            for (int i = 0; i < props.length; i++) {
jtulach@650
    95
                values[i] = mt.get(props[i]);
jtulach@650
    96
            }
jtulach@650
    97
        }
jtulach@650
    98
jtulach@650
    99
        @Override
jtulach@650
   100
        public Object toJSON(InputStream is) throws IOException {
jtulach@650
   101
            throw new IOException();
jtulach@650
   102
        }
jtulach@650
   103
jtulach@650
   104
        @Override
jtulach@650
   105
        public void loadJSON(JSONCall call) {
jtulach@650
   106
            Object r = result;
jtulach@650
   107
            assertNotNull(r, "We need a reply!");
jtulach@650
   108
            result = null;
jtulach@650
   109
            call.notifySuccess(r);
jtulach@650
   110
        }
jtulach@650
   111
    }
jtulach@650
   112
}