| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package org.mozilla.javascript.tests; |
| 9 |
|
| 10 |
import org.junit.Test; |
| 11 |
import static org.junit.Assert.assertEquals; |
| 12 |
import static org.junit.Assert.assertTrue; |
| 13 |
import static org.junit.Assert.fail; |
| 14 |
|
| 15 |
import java.util.Collection; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
import org.mozilla.javascript.Context; |
| 19 |
import org.mozilla.javascript.ContextAction; |
| 20 |
import org.mozilla.javascript.EvaluatorException; |
| 21 |
import org.mozilla.javascript.Scriptable; |
| 22 |
|
| 23 |
public class OverloadTest { |
| 24 |
|
| 25 |
public static String x(Collection<String> x) { |
| 26 |
return "collection"; |
| 27 |
} |
| 28 |
public static String x(Map<String, String> x) { |
| 29 |
return "map"; |
| 30 |
} |
| 31 |
public static String x(Runnable r) { |
| 32 |
return "runnable"; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
@Test |
| 37 |
public void testJSObjectToMap() { |
| 38 |
assertEvaluates("map", "String(org.mozilla.javascript.tests.OverloadTest.x({}));"); |
| 39 |
assertEvaluates("map", "String(org.mozilla.javascript.tests.OverloadTest.x({ run: function() {} }));"); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void testJSArrayToCollection() { |
| 44 |
assertEvaluates("collection", "String(org.mozilla.javascript.tests.OverloadTest.x([]));"); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void testJSFunctionToInterface() { |
| 49 |
assertThrows(EvaluatorException.class, "String(org.mozilla.javascript.tests.OverloadTest.x(function() {}));"); |
| 50 |
} |
| 51 |
|
| 52 |
private void assertEvaluates(final Object expected, final String source) { |
| 53 |
final ContextAction action = new ContextAction() { |
| 54 |
public Object run(Context cx) { |
| 55 |
final Scriptable scope = cx.initStandardObjects(); |
| 56 |
final Object rep = cx.evaluateString(scope, source, "test.js", |
| 57 |
0, null); |
| 58 |
assertEquals(expected, rep); |
| 59 |
return null; |
| 60 |
} |
| 61 |
}; |
| 62 |
Utils.runWithAllOptimizationLevels(action); |
| 63 |
} |
| 64 |
|
| 65 |
private void assertThrows(final Class<? extends Exception> exceptionClass, final String source) { |
| 66 |
final ContextAction action = new ContextAction() { |
| 67 |
public Object run(Context cx) { |
| 68 |
final Scriptable scope = cx.initStandardObjects(); |
| 69 |
try { |
| 70 |
cx.evaluateString(scope, source, "test.js", 0, null); |
| 71 |
fail("Did not throw exception"); |
| 72 |
} catch (Exception e) { |
| 73 |
assertTrue(exceptionClass.isInstance(e)); |
| 74 |
} |
| 75 |
return null; |
| 76 |
} |
| 77 |
}; |
| 78 |
Utils.runWithAllOptimizationLevels(action); |
| 79 |
} |
| 80 |
} |