/* * Test the getValue() method when the value of the expression is evaluated * by a previous call to getValue(). */ public void testGetValue_Evaluated() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, "method", new Object[0]); assertEquals("method1", t.getValue()); MockObject.assertCalled("method1", new Object[0]); assertEquals("method1", t.getValue()); MockObject.assertNotCalled(); }
/* * Test the method getValue() with a normal object, the method name "new" * and valid arguments. */ public void testGetValue_UnboundedNormalConstructor() throws Exception { Expression t = new Expression(MockObject.class, "new", new Object[0]); t.getValue(); MockObject.assertCalled("new0", new Object[0]); t = new Expression(MockObject.class, "new", null); assertTrue(t.getValue() instanceof MockObject); MockObject.assertCalled("new0", new Object[0]); }
public void testGetValue_returnNull() throws Exception { MockTarget target = new MockTarget(); Expression e = new Expression(target, "aMethod", new Object[] {}); Object got = e.getValue(); assertTrue(MockTarget.isCalled()); got = e.getValue(); assertFalse(MockTarget.isCalled()); }
/* * Test the method getValue() with a normal object, a valid method name and * valid arguments. */ public void testGetValue_UnboundedNormalInstanceMethod() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, "method", new Object[0]); assertEquals("method1", t.getValue()); MockObject.assertCalled("method1", new Object[0]); t = new Expression(mo, "method", null); assertEquals("method1", t.getValue()); MockObject.assertCalled("method1", new Object[0]); }
/* * Test the method getValue() with a Class object of a normal class that has * a method of the same signature as Class.forName(String), a static method * name "forName" and valid argument "string". */ public void testGetValue_UnboundedAmbitiousStaticMethod() throws Exception { Object[] arguments = new Object[] {"test"}; Expression t = new Expression(MockObject.class, "forName", arguments); assertNull(t.getValue()); MockObject.assertCalled("forName", arguments); t = new Expression(String.class, "forName", new Object[] {"java.lang.String"}); assertSame(String.class, t.getValue()); }
/* * Test the setValue() method when the value of the expression is set by the * constructor. */ public void testSetValue_Constructor() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, mo, "method", new Object[0]); assertSame(mo, t.getValue()); MockObject.assertNotCalled(); t.setValue(null); assertSame(null, t.getValue()); MockObject.assertNotCalled(); }
/* * Test the method getValue() with a normal object with overloaded methods * (primitive type VS wrapper class), a valid method name and valid * arguments. * * Note: decided by definition position! */ public void testGetValue_UnboundedPrimitiveVSWrapper() throws Exception { MockObject mo = new MockObject(false); Object[] arguments = new Object[] {new Integer(1)}; Expression t = new Expression(mo, "methodB", arguments); assertEquals("methodB2", t.getValue()); MockObject.assertCalled("methodB2", arguments); arguments = new Object[] {Boolean.FALSE}; t = new Expression(mo, "methodB", arguments); assertEquals("methodB3", t.getValue()); MockObject.assertCalled("methodB3", arguments); }
/* * Test the method getValue() with a normal object, an overloaded method and * valid arguments. */ public void testGetValue_UnboundedOverloadedMethods() throws Exception { MockObject mo = new MockObject(false); Object[] arguments = new Object[] {new Object()}; Expression t = new Expression(mo, "method", arguments); assertEquals("method2", t.getValue()); MockObject.assertCalled("method2", arguments); arguments = new Object[] {"test"}; t = new Expression(mo, "method", arguments); assertEquals("method3", t.getValue()); MockObject.assertCalled("method3", arguments); }
/* * Test the method getValue() with an object, a static method name and valid * arguments. */ public void testGetValue_UnboundedNormalStaticMethodViaObject() throws Exception { MockObject mo = new MockObject(false); Object[] arguments = new Object[] {new Object()}; Expression t = new Expression(mo, "staticMethod", arguments); assertEquals("staticMethod", t.getValue()); MockObject.assertCalled("staticMethod", arguments); }
/* * Test the setValue() method with a non-null value when the value of the * expression is still unbounded. */ public void testSetValue_UnboundNormal() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, "method", new Object[0]); t.setValue(mo); assertSame(mo, t.getValue()); MockObject.assertNotCalled(); }
/* * Test the method getValue() with a normal object with void method name and * valid arguments. */ public void testGetValue_UnboundedVoidMethod() throws Exception { MockObject mo = new MockObject(false); Object[] arguments = new Object[] {new Integer(1)}; Expression t = new Expression(mo, "voidMethod", arguments); assertNull(t.getValue()); MockObject.assertCalled("voidMethod2", arguments); }
/** * Execute method use reflection * * @see java.beans.Expression * @param target target object * @param methodName the method for execute * @param arguments the arguments to this method * @return execute result */ public static Object executeMethod(Object target, String methodName, Object[] arguments) { Expression exp = new Expression(target, methodName, arguments); try { return exp.getValue(); } catch (Exception e) { throw new RuntimeException(e); } }
/* * Test the method getValue() with the special method Class.forName(). */ public void testGetValue_UnboundedClassForName() throws Exception { Object[] arguments = new String[] {Expression.class.getName()}; Expression t = new Expression(Class.class, "forName", arguments); assertSame(Expression.class, t.getValue()); // t = new Expression(String.class, "forName", arguments); // assertSame(this.getClass(), t.getValue()); }
/* * Test the method getValue() with a normal object, the method name "new" * and invalid arguments (in terms of type, numbers, etc.). */ public void testGetValue_UnboundedNonExistingConstructor() throws Exception { Expression t = new Expression(MockObject.class, "new", new Object[] {null, null, null}); try { t.getValue(); fail("Should throw NoSuchMethodException!"); } catch (NoSuchMethodException ex) { // expected } }
/* * Test the method getValue() with a null object. */ public void testGetValue_UnboundedNullTarget() throws Exception { Expression t = new Expression(null, "method_not_existing", new Object[] {null, null}); try { t.getValue(); fail("Should throw NullPointerException!"); } catch (NullPointerException ex) { // expected } }
/* * Test the method getValue() with a normal object with overloaded * constructors, the method name "new" and valid arguments. See Java * Language Specification (15.11) for reference. * * Note: decided by definition position. */ public void testGetValue_UnboundedOverloadedConstructors() throws Exception { Object[] arguments = new Object[] {new Object()}; Expression t = new Expression(MockObject.class, "new", arguments); t.getValue(); MockObject.assertCalled("new2", arguments); // FIXME: the following 2 commented assert cannot pass neither in RI nor in Harmony // (HARMONY-4392), // waiting for dev-list approval to fix Harmony implementation following spec arguments = new Object[] {"test"}; t = new Expression(MockObject.class, "new", arguments); assertTrue(t.getValue() instanceof MockObject); // MockObject.assertCalled("new3", arguments); arguments = new Object[] {new Integer(1)}; t = new Expression(MockObject.class, "new", arguments); assertTrue(t.getValue() instanceof MockObject); // MockObject.assertCalled("new1-2", arguments); }
/* * Test the method getValue() with a normal object, the method name "new" * that throws an exception and valid arguments. */ public void testGetValue_UnboundedExceptionalConstructor() throws Exception { Expression t = new Expression(MockObject.class, "new", new Object[] {null, null}); try { t.getValue(); fail("Should throw NullPointerException!"); } catch (NullPointerException ex) { // expected } MockObject.assertCalled("new4", new Object[] {null, null}); }
/* * Test the method getValue() with a normal object and a non-existing method * name. */ public void testGetValue_UnboundedNonExistingMethod() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, "method_not_existing", new Object[] {null, null}); try { t.getValue(); fail("Should throw NoSuchMethodException!"); } catch (NoSuchMethodException ex) { // expected } }
/** The test checks the correct constructor is initialized */ public void testConstructor() throws Exception { Expression expr = new Expression(SampleBean.class, "new", new Object[] {"hello"}); Object result = expr.getValue(); if (result != null && result instanceof SampleBean) { SampleBean bean = (SampleBean) result; assertEquals("hello", bean.getText()); } else { fail("Cannot instantiate an instance of Bean class."); } }
/** The test checks the correct getter is initialized */ public void testGetter() throws Exception { Expression expr = new Expression(new SampleBean("hello"), "getText", new Object[] {}); Object result = expr.getValue(); if (result != null && result instanceof String) { assertEquals("hello", result); } else { fail("Result of SampleBean.getText() call is not " + "of String type."); } }
/* * Test the method getValue() with a null method name. */ public void testGetValue_UnboundedNullMethodName() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, null, new Object[] {null, null}); try { t.getValue(); fail("Should throw NullPointerException!"); } catch (NullPointerException ex) { // expected } }
/* * Test the method getValue() with a normal array object, the method name * "gets". */ public void testGetValue_UnboundedArrayInvalidName() throws Exception { Object[] array = new Object[] {"test"}; Expression t = new Expression(array, "gets", new Object[] {new Integer(0), new Object()}); try { t.getValue(); fail("Should throw NoSuchMethodException!"); } catch (NoSuchMethodException ex) { // expected } }
/* * Test the method getValue() with a protected method but within java.beans * package. */ public void testGetValue_ProtectedMethodWithPackage() throws Exception { DefaultPersistenceDelegate dpd = new DefaultPersistenceDelegate(); Object[] arguments = new Object[] {"test", "test"}; Expression t = new Expression(dpd, "mutatesTo", arguments); try { t.getValue(); fail("Should throw NoSuchMethodException!"); } catch (NoSuchMethodException e) { // expected } }
/** The test checks the correct array getter is initialized */ public void testArrayGetter() throws Exception { int[] a = {1, 2, 3}; Expression expr = new Expression(a, "get", new Object[] {new Integer(1)}); Object result = expr.getValue(); if (result != null && result instanceof Integer) { assertEquals(new Integer(2), result); } else { fail("Result of array getter is not of Integer type."); } }
/* * Test the method getValue() with a normal object, a valid method that * throws an exception and valid arguments. */ public void testGetValue_UnboundedExceptionalMethod() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, "method", new Object[] {null, null}); try { t.getValue(); fail("Should throw NullPointerException!"); } catch (NullPointerException ex) { // expected } MockObject.assertCalled("method4", new Object[] {null, null}); }
/* * Test the method getValue() with a normal object, a valid method and * invalid arguments (in terms of type, numbers, etc.). */ public void testGetValue_UnboundedInvalidArguments() throws Exception { MockObject mo = new MockObject(false); Expression t = new Expression(mo, "method", new Object[] {new Object(), new Object(), new Object()}); try { t.getValue(); fail("Should throw NoSuchMethodException!"); } catch (NoSuchMethodException ex) { // expected } }
/* * Test the method getValue() with a method that is applicable via type * conversion. */ public void testGetValue_ApplicableViaTypeConversion() throws Exception { MockObject mo = new MockObject(false); // mo.methodB('c'); Object[] arguments = new Object[] {new Character((char) 1)}; Expression t = new Expression(mo, "methodB", arguments); try { t.getValue(); fail("Should throw NoSuchMethodException!"); } catch (NoSuchMethodException e) { // expected } }
/* * Test the constructor(value, ...) with null arguments. */ public void testConstructor_Value_NullArguments() throws Exception { Object val = new Object(); Object target = new Object(); Expression t = new Expression(val, target, "method", null); assertSame(val, t.getValue()); assertSame(target, t.getTarget()); assertSame("method", t.getMethodName()); assertEquals(0, t.getArguments().length); assertEquals("Object=Object.method();", t.toString()); }
/** The test checks the correct static method is initialized */ public void testStatic() throws Exception { SampleBean theBean = new SampleBean(); Expression expr = new Expression(SampleBean.class, "create", new Object[] {"hello", theBean}); Object result = expr.getValue(); if (result != null && result instanceof SampleBean) { SampleBean bean = (SampleBean) result; assertEquals("hello", bean.getText()); assertEquals(theBean, bean.getObject()); } else { fail("Cannot instantiate an instance of Bean class by " + "static method."); } }
/* * Test the constructor(value, ...) with a null value. */ public void testConstructor_Value_NullValue() throws Exception { Object target = new Object(); Object[] oa = new Object[] {null}; Expression t = new Expression(null, target, "method", oa); assertSame(null, t.getValue()); assertSame(target, t.getTarget()); assertSame("method", t.getMethodName()); assertSame(oa, t.getArguments()); assertNull(t.getArguments()[0]); assertEquals("null=Object.method(null);", t.toString()); }