jaroslav@934: /** jaroslav@934: * Back 2 Browser Bytecode Translator jaroslav@934: * Copyright (C) 2012 Jaroslav Tulach jaroslav@934: * jaroslav@934: * This program is free software: you can redistribute it and/or modify jaroslav@934: * it under the terms of the GNU General Public License as published by jaroslav@934: * the Free Software Foundation, version 2 of the License. jaroslav@934: * jaroslav@934: * This program is distributed in the hope that it will be useful, jaroslav@934: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@934: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@934: * GNU General Public License for more details. jaroslav@934: * jaroslav@934: * You should have received a copy of the GNU General Public License jaroslav@934: * along with this program. Look for COPYING file in the top folder. jaroslav@934: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@934: */ jaroslav@934: package org.apidesign.bck2brwsr.htmlpage; jaroslav@934: jaroslav@934: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@934: import org.apidesign.bck2brwsr.vmtest.BrwsrTest; jaroslav@934: import org.apidesign.bck2brwsr.vmtest.VMTest; jaroslav@1014: import org.json.JSONException; jaroslav@1014: import org.json.JSONObject; jaroslav@934: import org.testng.annotations.Factory; jaroslav@934: jaroslav@934: /** jaroslav@934: * jaroslav@934: * @author Jaroslav Tulach jaroslav@934: */ jaroslav@934: public class ConvertTypesTest { jaroslav@1014: @JavaScriptBody(args = { "includeSex" }, body = "var json = new Object();" jaroslav@934: + "json.firstName = 'son';\n" jaroslav@934: + "json.lastName = 'dj';\n" jaroslav@934: + "json.sex = 'MALE';\n" jaroslav@934: + "return json;" jaroslav@934: ) jaroslav@1014: private static Object createJSON(boolean includeSex) throws JSONException { jaroslav@1014: JSONObject o = new JSONObject(); jaroslav@1014: o.put("firstName", "son"); jaroslav@1014: o.put("lastName", "dj"); jaroslav@1014: if (includeSex) { jaroslav@1014: o.put("sex", "MALE"); jaroslav@1014: } jaroslav@1014: return o; jaroslav@1014: } jaroslav@934: jaroslav@934: @BrwsrTest jaroslav@1014: public void testConvertToPeople() throws JSONException { jaroslav@1014: final Object o = createJSON(true); jaroslav@934: jaroslav@934: Person p = new Person(o); jaroslav@934: jaroslav@934: assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName(); jaroslav@934: assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName(); jaroslav@960: assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex(); jaroslav@934: } jaroslav@1014: jaroslav@1014: @BrwsrTest jaroslav@1014: public void testConvertToPeopleWithoutSex() throws JSONException { jaroslav@1014: final Object o = createJSON(false); jaroslav@1014: jaroslav@1014: Person p = new Person(o); jaroslav@1014: jaroslav@1014: assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName(); jaroslav@1014: assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName(); jaroslav@1014: assert p.getSex() == null : "No sex: " + p.getSex(); jaroslav@1014: } jaroslav@934: jaroslav@934: @Factory public static Object[] create() { jaroslav@934: return VMTest.create(ConvertTypesTest.class); jaroslav@934: } jaroslav@934: }