javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 18 Apr 2013 17:28:34 +0200
branchfx
changeset 1014 7a7686e6f875
parent 960 4887e22cb810
permissions -rw-r--r--
Support for null enums
jaroslav@934
     1
/**
jaroslav@934
     2
 * Back 2 Browser Bytecode Translator
jaroslav@934
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@934
     4
 *
jaroslav@934
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@934
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@934
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@934
     8
 *
jaroslav@934
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@934
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@934
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@934
    12
 * GNU General Public License for more details.
jaroslav@934
    13
 *
jaroslav@934
    14
 * You should have received a copy of the GNU General Public License
jaroslav@934
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@934
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@934
    17
 */
jaroslav@934
    18
package org.apidesign.bck2brwsr.htmlpage;
jaroslav@934
    19
jaroslav@934
    20
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@934
    21
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
jaroslav@934
    22
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@1014
    23
import org.json.JSONException;
jaroslav@1014
    24
import org.json.JSONObject;
jaroslav@934
    25
import org.testng.annotations.Factory;
jaroslav@934
    26
jaroslav@934
    27
/**
jaroslav@934
    28
 *
jaroslav@934
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@934
    30
 */
jaroslav@934
    31
public class ConvertTypesTest {
jaroslav@1014
    32
    @JavaScriptBody(args = { "includeSex" }, body = "var json = new Object();"
jaroslav@934
    33
        + "json.firstName = 'son';\n"
jaroslav@934
    34
        + "json.lastName = 'dj';\n"
jaroslav@934
    35
        + "json.sex = 'MALE';\n"
jaroslav@934
    36
        + "return json;"
jaroslav@934
    37
    )
jaroslav@1014
    38
    private static Object createJSON(boolean includeSex) throws JSONException {
jaroslav@1014
    39
        JSONObject o = new JSONObject();
jaroslav@1014
    40
        o.put("firstName", "son");
jaroslav@1014
    41
        o.put("lastName", "dj");
jaroslav@1014
    42
        if (includeSex) {
jaroslav@1014
    43
            o.put("sex", "MALE");
jaroslav@1014
    44
        }
jaroslav@1014
    45
        return o;
jaroslav@1014
    46
    }
jaroslav@934
    47
    
jaroslav@934
    48
    @BrwsrTest
jaroslav@1014
    49
    public void testConvertToPeople() throws JSONException {
jaroslav@1014
    50
        final Object o = createJSON(true);
jaroslav@934
    51
        
jaroslav@934
    52
        Person p = new Person(o);
jaroslav@934
    53
        
jaroslav@934
    54
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jaroslav@934
    55
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jaroslav@960
    56
        assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
jaroslav@934
    57
    }
jaroslav@1014
    58
jaroslav@1014
    59
    @BrwsrTest
jaroslav@1014
    60
    public void testConvertToPeopleWithoutSex() throws JSONException {
jaroslav@1014
    61
        final Object o = createJSON(false);
jaroslav@1014
    62
        
jaroslav@1014
    63
        Person p = new Person(o);
jaroslav@1014
    64
        
jaroslav@1014
    65
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jaroslav@1014
    66
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jaroslav@1014
    67
        assert p.getSex() == null : "No sex: " + p.getSex();
jaroslav@1014
    68
    }
jaroslav@934
    69
    
jaroslav@934
    70
    @Factory public static Object[] create() {
jaroslav@934
    71
        return VMTest.create(ConvertTypesTest.class);
jaroslav@934
    72
    }
jaroslav@934
    73
}