8080501: javaarrayconversion.js test is flawed

Reviewed-by: attila, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2015-09-15 19:31:24 +05:30
parent cf428a3931
commit 9698127af3
3 changed files with 23 additions and 9 deletions

View File

@ -345,7 +345,8 @@ public final class NativeJava {
/**
* Given a script object and a Java type, converts the script object into the desired Java type. Currently it
* performs shallow creation of Java arrays, as well as wrapping of objects in Lists, Dequeues, Queues,
* and Collections. Example:
* and Collections. If conversion is not possible or fails for some reason, TypeError is thrown.
* Example:
* <pre>
* var anArray = [1, "13", false]
* var javaIntArray = Java.to(anArray, "int[]")
@ -389,7 +390,11 @@ public final class NativeJava {
}
if(targetClass.isArray()) {
return JSType.toJavaArray(obj, targetClass.getComponentType());
try {
return JSType.toJavaArray(obj, targetClass.getComponentType());
} catch (final Exception exp) {
throw typeError(exp, "java.array.conversion.failed", targetClass.getName());
}
}
if (targetClass == List.class || targetClass == Deque.class || targetClass == Queue.class || targetClass == Collection.class) {

View File

@ -150,6 +150,7 @@ type.error.no.constructor.matches.args=Can not create new object with constructo
type.error.method.not.constructor=Java method {0} cannot be used as a constructor.
type.error.env.not.object=$ENV must be an Object.
type.error.unsupported.java.to.type=Unsupported Java.to target type {0}.
type.error.java.array.conversion.failed=Java.to conversion to array type {0} failed
type.error.constructor.requires.new=Constructor {0} requires "new".
type.error.new.on.nonpublic.javatype=new cannot be used with non-public java type {0}.

View File

@ -128,24 +128,32 @@ test({ valueOf: function() { return 42; } }, "java.lang.String", "[object Object
// Converting to string, toString takes precedence over valueOf
test({ valueOf: function() { return "42"; }, toString: function() { return "43"; } }, "java.lang.String", "43")
function assertCanConvert(sourceType, targetType) {
Java.to([new (Java.type(sourceType))()], targetType + "[]")
++testCount;
}
function assertCantConvert(sourceType, targetType) {
try {
Java.to([new Java.type(sourceType)()], targetType + "[]")
Java.to([new (Java.type(sourceType))()], targetType + "[]")
throw "no TypeError encountered"
} catch(e) {
if(!(e instanceof TypeError)) {
if(!(e instanceof TypeError) ||
!e.message.startsWith("Java.to conversion to array type")) {
throw e;
}
++testCount;
}
}
// Arbitrary POJOs to JS Primitive type should work
assertCanConvert("java.util.BitSet", "int")
assertCanConvert("java.util.BitSet", "double")
assertCanConvert("java.util.BitSet", "long")
assertCanConvert("java.util.BitSet", "boolean")
assertCanConvert("java.util.BitSet", "java.lang.String")
// Arbitrary POJOs can't be converted to Java values
assertCantConvert("java.util.BitSet", "int")
assertCantConvert("java.util.BitSet", "double")
assertCantConvert("java.util.BitSet", "long")
assertCantConvert("java.util.BitSet", "boolean")
assertCantConvert("java.util.BitSet", "java.lang.String")
assertCantConvert("java.util.BitSet", "java.lang.Double")
assertCantConvert("java.util.BitSet", "java.lang.Long")