1 /* JSToJFuncResolTest.java 2 Copyright (C) 2012 Red Hat, Inc. 3 4 This file is part of IcedTea. 5 6 IcedTea is free software; you can redistribute it and/or 7 modify it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, version 2. 9 10 IcedTea is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with IcedTea; see the file COPYING. If not, write to 17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 18 02110-1301 USA. 19 20 Linking this library statically or dynamically with other modules is 21 making a combined work based on this library. Thus, the terms and 22 conditions of the GNU General Public License cover the whole 23 combination. 24 25 As a special exception, the copyright holders of this library give you 26 permission to link this library with independent modules to produce an 27 executable, regardless of the license terms of these independent 28 modules, and to copy and distribute the resulting executable under 29 terms of your choice, provided that you also meet, for each linked 30 independent module, the terms and conditions of the license of that 31 module. An independent module is a module which is not derived from 32 or based on this library. If you modify this library, you may extend 33 this exception to your version of the library, but you are not 34 obligated to do so. If you do not wish to do so, delete this 35 exception statement from your version. 36 */ 37 38 import net.sourceforge.jnlp.ProcessResult; 39 import net.sourceforge.jnlp.ServerAccess; 40 import net.sourceforge.jnlp.browsertesting.BrowserTest; 41 import net.sourceforge.jnlp.browsertesting.Browsers; 42 import net.sourceforge.jnlp.closinglisteners.CountingClosingListener; 43 import net.sourceforge.jnlp.annotations.NeedsDisplay; 44 import net.sourceforge.jnlp.annotations.TestInBrowsers; 45 import net.sourceforge.jnlp.annotations.KnownToFail; 46 import org.junit.Assert; 47 import org.junit.Test; 48 49 public class JSToJFuncResolTest extends BrowserTest { 50 51 private final String initStr = "JSToJFuncResol applet initialized."; 52 private final String afterStr = "afterTests"; 53 54 private class CountingClosingListenerImpl extends CountingClosingListener { 55 56 @Override isAlowedToFinish(String s)57 protected boolean isAlowedToFinish(String s) { 58 59 return (s.contains(initStr) && s.contains(afterStr)); 60 } 61 } 62 evaluateStdoutContents(String expectedStdout, ProcessResult pr)63 private void evaluateStdoutContents(String expectedStdout, ProcessResult pr) { 64 // Assert that the applet was initialized. 65 Assert.assertTrue("JSToJFuncResol: the stdout should contain " + initStr + ", but it didnt.", pr.stdout.contains(initStr)); 66 67 // Assert that the values set by JavaScript are ok 68 Assert.assertTrue("JSToJFuncResol: the output should include: " + expectedStdout + ", but it didnt.", (pr.stderr.contains(expectedStdout) || pr.stdout.contains(expectedStdout))); 69 70 } 71 jsToJavaFuncResolTest(String methodStr, String valueStr, String expectedStdout)72 private void jsToJavaFuncResolTest(String methodStr, String valueStr, String expectedStdout) throws Exception { 73 String strURL = "/JSToJFuncResol.html?" + methodStr + ";" + valueStr; 74 ProcessResult pr = server.executeBrowser(strURL, new CountingClosingListenerImpl(), new CountingClosingListenerImpl()); 75 evaluateStdoutContents(expectedStdout, pr); 76 } 77 78 /****** Primitive (numeric) value resolutions ******/ 79 @Test 80 @TestInBrowsers(testIn = { Browsers.all }) 81 @NeedsDisplay AppletJSToJFuncResol_numeric_Test()82 public void AppletJSToJFuncResol_numeric_Test() throws Exception { 83 jsToJavaFuncResolTest("numeric", "1", "numeric(int) with 1"); 84 } 85 86 @Test 87 @TestInBrowsers(testIn = { Browsers.all }) 88 @NeedsDisplay AppletJSToJFuncResol_numericToDifferentNumeric_Test()89 public void AppletJSToJFuncResol_numericToDifferentNumeric_Test() throws Exception { 90 jsToJavaFuncResolTest("numericToDifferentNumeric", "1.1", "numericToDifferentNumeric(double) with 1.1"); 91 } 92 93 @Test 94 @TestInBrowsers(testIn = { Browsers.all }) 95 @NeedsDisplay AppletJSToJFuncResol_numericToDouble_Test()96 public void AppletJSToJFuncResol_numericToDouble_Test() throws Exception { 97 jsToJavaFuncResolTest("numericToDouble", "1.1", "numericToDouble(double) with 1.1"); 98 } 99 100 /****** Null resolutions ******/ 101 102 @Test 103 @TestInBrowsers(testIn = { Browsers.all }) 104 @NeedsDisplay AppletJSToJFuncResol_nullToInteger_Test()105 public void AppletJSToJFuncResol_nullToInteger_Test() throws Exception { 106 jsToJavaFuncResolTest("nullToInteger", "null", "nullToInteger(Integer) with null"); 107 } 108 109 /****** Java inherited class resolutions ******/ 110 111 @Test 112 @TestInBrowsers(testIn = { Browsers.all }) 113 @NeedsDisplay AppletJSToJFuncResol_inheritedClass_Test()114 public void AppletJSToJFuncResol_inheritedClass_Test() throws Exception { 115 jsToJavaFuncResolTest("inheritedClass", "applet.getNewOverloadTestHelper2()", "inheritedClass(OverloadTestHelper2) with JSToJFuncResol$OverloadTestHelper2@"); 116 } 117 118 @Test 119 @TestInBrowsers(testIn = { Browsers.all }) 120 @NeedsDisplay AppletJSToJFuncResol_inheritedClassToParent1_Test()121 public void AppletJSToJFuncResol_inheritedClassToParent1_Test() throws Exception { 122 jsToJavaFuncResolTest("inheritedClassToParent1", "applet.getNewOverloadTestHelper3()", "inheritedClassToParent1(OverloadTestHelper2) with JSToJFuncResol$OverloadTestHelper3@"); 123 } 124 125 @Test 126 @TestInBrowsers(testIn = { Browsers.all }) 127 @NeedsDisplay AppletJSToJFuncResol_inheritedClassToParent2_Test()128 public void AppletJSToJFuncResol_inheritedClassToParent2_Test() throws Exception { 129 jsToJavaFuncResolTest("inheritedClassToParent2", "applet.getNewOverloadTestHelper2()", "inheritedClassToParent2(OverloadTestHelper1) with JSToJFuncResol$OverloadTestHelper2@"); 130 } 131 132 /****** Java object resolutions ******/ 133 134 @Test 135 @TestInBrowsers(testIn = { Browsers.all }) 136 @NeedsDisplay AppletJSToJFuncResol_javaObjectToString_Test()137 public void AppletJSToJFuncResol_javaObjectToString_Test() throws Exception { 138 jsToJavaFuncResolTest("javaObjectToString", "applet.getNewOverloadTestHelper1()", "javaObjectToString(String) with JSToJFuncResol$OverloadTestHelper1@"); 139 } 140 141 /****** String resolutions ******/ 142 143 @Test 144 @TestInBrowsers(testIn = { Browsers.all }) 145 @NeedsDisplay AppletJSToJFuncResol_javascriptStringToNumeric_Test()146 public void AppletJSToJFuncResol_javascriptStringToNumeric_Test() throws Exception { 147 jsToJavaFuncResolTest("javascriptStringToNumeric", "\"1.1\"", "javascriptStringToNumeric(double) with 1.1"); 148 } 149 150 /****** Javascript object resolutions ******/ 151 152 @Test 153 @TestInBrowsers(testIn = { Browsers.all }) 154 @NeedsDisplay AppletJSToJFuncResol_javascriptObject_Test()155 public void AppletJSToJFuncResol_javascriptObject_Test() throws Exception { 156 jsToJavaFuncResolTest("javascriptObject", "window", "javascriptObject(JSObject) with [object Window]"); 157 } 158 159 @Test 160 @TestInBrowsers(testIn = { Browsers.all }) 161 @NeedsDisplay AppletJSToJFuncResol_javascriptObjectToArray_Test()162 public void AppletJSToJFuncResol_javascriptObjectToArray_Test() throws Exception { 163 jsToJavaFuncResolTest("javascriptObjectToArray", "[10]", "javascriptObjectToArray(int[]) with [I@"); 164 } 165 166 /****** The unsupported resolutions: *****/ 167 168 @Test 169 @TestInBrowsers(testIn = { Browsers.all }) 170 @NeedsDisplay AppletJSToJFuncResol_nullToPrimitive_Test()171 public void AppletJSToJFuncResol_nullToPrimitive_Test() throws Exception { 172 jsToJavaFuncResolTest("nullToPrimitive", "null", "Error on Java side: No suitable method named nullToPrimitive with matching args found"); 173 } 174 175 @Test 176 @TestInBrowsers(testIn = { Browsers.all }) 177 @NeedsDisplay AppletJSToJFuncResol_javascriptObjectToUnrelatedType_Test()178 public void AppletJSToJFuncResol_javascriptObjectToUnrelatedType_Test() throws Exception { 179 jsToJavaFuncResolTest("javascriptObjectToUnrelatedType", "window", "Error on Java side: No suitable method named javascriptObjectToUnrelatedType with matching args found"); 180 } 181 182 @Test 183 @TestInBrowsers(testIn = { Browsers.all }) 184 @NeedsDisplay AppletJSToJFuncResol_unsupported_Test()185 public void AppletJSToJFuncResol_unsupported_Test() throws Exception { 186 jsToJavaFuncResolTest("unsupported", "25", "Error on Java side: No suitable method named unsupported with matching args found"); 187 } 188 189 } 190