Esempio n. 1
0
 /*
  * 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);
 }
Esempio n. 2
0
 /*
  * 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();
 }
Esempio n. 3
0
 /*
  * 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);
 }
Esempio n. 4
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());
 }
Esempio n. 5
0
 /**
  * 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);
   }
 }
Esempio n. 6
0
  /*
   * 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());
  }
Esempio n. 7
0
 /*
  * 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();
 }
Esempio n. 8
0
 /*
  * 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]);
 }
Esempio n. 9
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());
  }
Esempio n. 10
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());
  }
Esempio n. 11
0
 /*
  * 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
   }
 }
Esempio n. 12
0
 /*
  * 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]);
 }
Esempio n. 13
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
   }
 }
Esempio n. 14
0
 /*
  * 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});
 }
Esempio n. 15
0
  /** 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.");
    }
  }
Esempio n. 16
0
  /*
   * 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]);
  }
Esempio n. 17
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
   }
 }
Esempio n. 18
0
 /*
  * 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
   }
 }
Esempio n. 19
0
  /*
   * 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());
  }
Esempio n. 20
0
 /** 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.");
   }
 }
Esempio n. 21
0
 /*
  * 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
   }
 }
Esempio n. 22
0
 /*
  * 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
   }
 }
Esempio n. 23
0
  /*
   * 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());
  }
Esempio n. 24
0
  /** 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.");
    }
  }
Esempio n. 25
0
 /*
  * 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});
 }
Esempio n. 26
0
 /*
  * 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
   }
 }
Esempio n. 27
0
  /*
   * 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);
  }
Esempio n. 28
0
 /*
  * 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
   }
 }
Esempio n. 29
0
  /*
   * 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);
  }
Esempio n. 30
0
  /** 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.");
    }
  }