@Test( expected = RuntimeException.class ) public void convertFromJs_TypeNone() throws Exception { JavaScriptUtils.convertFromJs( null, ValueMetaInterface.TYPE_NONE, "qwerty" ); }
public static Object convertFromJs( Object value, int type, String fieldName ) throws KettleValueException { String classType = value.getClass().getName(); switch ( type ) { case ValueMetaInterface.TYPE_NUMBER: return jsToNumber( value, classType ); case ValueMetaInterface.TYPE_INTEGER: return jsToInteger( value, value.getClass() ); case ValueMetaInterface.TYPE_STRING: return jsToString( value, classType ); case ValueMetaInterface.TYPE_DATE: return jsToDate( value, classType ); case ValueMetaInterface.TYPE_BOOLEAN: return value; case ValueMetaInterface.TYPE_BIGNUMBER: return jsToBigNumber( value, classType ); case ValueMetaInterface.TYPE_BINARY: { return Context.jsToJava( value, byte[].class ); } case ValueMetaInterface.TYPE_NONE: { throw new RuntimeException( "No data output data type was specified for new field [" + fieldName + "]" ); } default: return Context.jsToJava( value, Object.class ); } }
@Test( expected = RuntimeException.class ) public void jsToBigNumber_UnknownClass() throws Exception { JavaScriptUtils.jsToBigNumber( "1", "qwerty" ); }
@Test( expected = NumberFormatException.class ) public void jsToInteger_String_Unparseable() throws Exception { JavaScriptUtils.jsToInteger( "q", String.class ); }
@Test( expected = NumberFormatException.class ) public void jsToDate_String_Unparseable() throws Exception { JavaScriptUtils.jsToDate( "qwerty", String.class.getName() ); }
@Test public void jsToNumber_JavaNumber() throws Exception { Number number = JavaScriptUtils.jsToNumber( 1.0, Double.class.getName() ); assertEquals( 1.0, number.doubleValue(), 1e-6 ); }
@Test public void jsToString_String() throws Exception { assertEquals( "qwerty", JavaScriptUtils.jsToString( "qwerty", String.class.getName() ) ); }
@Test public void jsToBigNumber_NaturalNumbers() throws Exception { Number[] naturalNumbers = new Number[] { (byte) 1, (short) 1, 1, (long) 1 }; for ( Number number : naturalNumbers ) { assertEquals( 1.0, JavaScriptUtils.jsToBigNumber( number, number.getClass().getName() ).doubleValue(), 1e-6 ); } }
@Test( expected = NumberFormatException.class ) public void jsToInteger_Other_String() throws Exception { JavaScriptUtils.jsToInteger( "qwerty", getClass() ); }
@Test public void jsToDate_Double() throws Exception { assertEquals( new Date( 1 ), JavaScriptUtils.jsToDate( 1.0, Double.class.getName() ) ); }
@Test public void jsToNumber_Undefined() throws Exception { assertNull( JavaScriptUtils.jsToNumber( null, UNDEFINED ) ); }
@Test public void jsToString_Undefined() throws Exception { assertEquals( "null", JavaScriptUtils.jsToString( null, UNDEFINED ) ); }
@Test public void jsToBigNumber_Double() throws Exception { assertEquals( 1.0, JavaScriptUtils.jsToBigNumber( 1.0, Double.class.getName() ).doubleValue(), 1e-6 ); }
@Test public void jsToInteger_NaturalNumbers() throws Exception { Number[] naturalNumbers = new Number[] { (byte) 1, (short) 1, 1, (long) 1 }; for ( Number number : naturalNumbers ) { assertEquals( Long.valueOf( 1 ), JavaScriptUtils.jsToInteger( number, number.getClass() ) ); } }
@Test public void jsToDate_String() throws Exception { assertEquals( new Date( 1 ), JavaScriptUtils.jsToDate( "1.0", String.class.getName() ) ); }
@Test public void jsToNumber_NativeNumber() throws Exception { Scriptable value = Context.toObject( 1.0, scope ); Number number = JavaScriptUtils.jsToNumber( value, NATIVE_NUMBER ); assertEquals( 1.0, number.doubleValue(), 1e-6 ); }
@Test public void convertFromJs_TypeBoolean() throws Exception { Object o = new Object(); Object o2 = JavaScriptUtils.convertFromJs( o, ValueMetaInterface.TYPE_BOOLEAN, "qwerty" ); assertEquals( o, o2 ); }
@Test public void jsToString_NativeJavaObject_Double() throws Exception { assertEquals( "1.0", JavaScriptUtils.jsToString( getDoubleValue(), JAVA_OBJECT ).trim() ); }
@Test public void jsToBigNumber_String() throws Exception { assertEquals( 1.0, JavaScriptUtils.jsToBigNumber( "1", String.class.getName() ).doubleValue(), 1e-6 ); }
@Test public void jsToInteger_Other_Int() throws Exception { assertEquals( Long.valueOf( 1 ), JavaScriptUtils.jsToInteger( 1, getClass() ) ); }