/* * 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 getValue() method when the value of expression is set by a * previous call to setValue(). */ public void testGetValue_Set() 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); }
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()); }
/** * 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 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]); }
/* * Test the constructor with empty method name. */ public void testConstructor_EmptyMethodName() { Object target = new Object(); Object[] oa = new Object[] {new Object()}; Expression t = new Expression(target, "", oa); assertSame(target, t.getTarget()); assertSame("", t.getMethodName()); assertSame(oa, t.getArguments()); }
/* * 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 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, 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 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 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}); }
/** 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 constructor with null target. */ public void testConstructor_NullTarget() { Object arg = new Object(); Object[] oa = new Object[] {arg}; Expression t = new Expression(null, "method", oa); assertSame(null, t.getTarget()); assertSame("method", t.getMethodName()); assertSame(oa, t.getArguments()); assertSame(arg, t.getArguments()[0]); }
/* * 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 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 } }
/* * Test the constructor with null arguments. */ public void testConstructor_NullArguments() { Object target = new MockParent(); Expression t = new Expression(target, "method", null); assertSame(target, t.getTarget()); assertSame("method", t.getMethodName()); assertEquals(0, t.getArguments().length); assertEquals("<unbound>=ExpressionTest$MockParent.method();", t.toString()); }
/** 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."); } }
/* * 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 } }
/* * Test the constructor with the method name "new". */ public void testConstructor_NewMethodName() { Object target = MockObject.class; Object[] oa = new Object[] {new Object()}; Expression t = new Expression(target, "new", oa); assertSame(target, t.getTarget()); assertSame("new", t.getMethodName()); assertSame(oa, t.getArguments()); assertEquals("<unbound>=Class.new(Object);", t.toString()); }
/** 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 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 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 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); }
/** 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."); } }