commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils MethodUtilsTestCase.java TestBean.java
Date Mon, 22 Jul 2002 00:02:02 GMT
craigmcc    2002/07/21 17:02:01

  Modified:    beanutils/src/test/org/apache/commons/beanutils
                        MethodUtilsTestCase.java TestBean.java
  Log:
  Some unit tests for calling static methods directly and indirectly via
  MethodUtils.
  
  Revision  Changes    Path
  1.7       +223 -0    jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java
  
  Index: MethodUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- MethodUtilsTestCase.java	5 Apr 2002 14:58:15 -0000	1.6
  +++ MethodUtilsTestCase.java	22 Jul 2002 00:02:01 -0000	1.7
  @@ -298,4 +298,227 @@
               // this is what we're expecting!
           }
       }
  +
  +
  +    /**
  +     * Simple tests for accessing static methods via invokeMethod().
  +     */
  +    public void testSimpleStatic1() {
  +
  +        TestBean bean = new TestBean();
  +        Object value = null;
  +        int current = TestBean.currentCounter();
  +
  +        try {
  +
  +            // Return initial value of the counter
  +            value = MethodUtils.invokeMethod
  +                (bean, "currentCounter", new Object[0], new Class[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +            // Increment via no-arguments version
  +            MethodUtils.invokeMethod
  +                (bean, "incrementCounter", new Object[0], new Class[0]);
  +
  +            // Validate updated value
  +            current++;
  +            value = MethodUtils.invokeMethod
  +                (bean, "currentCounter", new Object[0], new Class[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +            // Increment via specified-argument version
  +            MethodUtils.invokeMethod
  +                (bean, "incrementCounter",
  +                 new Object[] { new Integer(5) },
  +                 new Class[] { Integer.TYPE });
  +
  +            // Validate updated value
  +            current += 5;
  +            value = MethodUtils.invokeMethod
  +                (bean, "currentCounter", new Object[0], new Class[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +        } catch (Exception e) {
  +            fail("Threw exception" + e);
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Simple tests for accessing static methods via invokeExactMethod().
  +     */
  +    public void testSimpleStatic2() {
  +
  +        TestBean bean = new TestBean();
  +        Object value = null;
  +        int current = TestBean.currentCounter();
  +
  +        try {
  +
  +            // Return initial value of the counter
  +            value = MethodUtils.invokeExactMethod
  +                (bean, "currentCounter", new Object[0], new Class[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +            // Increment via no-arguments version
  +            MethodUtils.invokeExactMethod
  +                (bean, "incrementCounter", new Object[0], new Class[0]);
  +
  +            // Validate updated value
  +            current++;
  +            value = MethodUtils.invokeExactMethod
  +                (bean, "currentCounter", new Object[0], new Class[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +            // Increment via specified-argument version
  +            MethodUtils.invokeExactMethod
  +                (bean, "incrementCounter",
  +                 new Object[] { new Integer(5) },
  +                 new Class[] { Integer.TYPE });
  +
  +            // Validate updated value
  +            current += 5;
  +            value = MethodUtils.invokeExactMethod
  +                (bean, "currentCounter", new Object[0], new Class[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +
  +        } catch (Exception e) {
  +            fail("Threw exception" + e);
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Simple tests for accessing static methods via getAccessibleMethod()
  +     */
  +    public void testSimpleStatic3() {
  +
  +        Object value = null;
  +        int current = TestBean.currentCounter();
  +
  +        try {
  +
  +            // Acquire the methods we need
  +            Method currentCounterMethod = MethodUtils.getAccessibleMethod
  +                (TestBean.class, "currentCounter",
  +                 new Class[0]);
  +            assertNotNull("currentCounterMethod exists",
  +                          currentCounterMethod);
  +            assertEquals("currentCounterMethod name",
  +                         "currentCounter",
  +                         currentCounterMethod.getName());
  +            assertEquals("currentCounterMethod args",
  +                         0,
  +                         currentCounterMethod.getParameterTypes().length);
  +            assertTrue("currentCounterMethod public",
  +                       Modifier.isPublic(currentCounterMethod.getModifiers()));
  +            assertTrue("currentCounterMethod static",
  +                       Modifier.isStatic(currentCounterMethod.getModifiers()));
  +            Method incrementCounterMethod1 = MethodUtils.getAccessibleMethod
  +                (TestBean.class, "incrementCounter",
  +                 new Class[0]);
  +            assertNotNull("incrementCounterMethod1 exists",
  +                          incrementCounterMethod1);
  +            assertEquals("incrementCounterMethod1 name",
  +                         "incrementCounter",
  +                         incrementCounterMethod1.getName());
  +            assertEquals("incrementCounterMethod1 args",
  +                         0,
  +                         incrementCounterMethod1.getParameterTypes().length);
  +            assertTrue("incrementCounterMethod1 public",
  +                       Modifier.isPublic(incrementCounterMethod1.getModifiers()));
  +            assertTrue("incrementCounterMethod1 static",
  +                       Modifier.isStatic(incrementCounterMethod1.getModifiers()));
  +            Method incrementCounterMethod2 = MethodUtils.getAccessibleMethod
  +                (TestBean.class, "incrementCounter",
  +                 new Class[] { Integer.TYPE });
  +            assertNotNull("incrementCounterMethod2 exists",
  +                          incrementCounterMethod2);
  +            assertEquals("incrementCounterMethod2 name",
  +                         "incrementCounter",
  +                         incrementCounterMethod2.getName());
  +            assertEquals("incrementCounterMethod2 args",
  +                         1,
  +                         incrementCounterMethod2.getParameterTypes().length);
  +            assertTrue("incrementCounterMethod2 public",
  +                       Modifier.isPublic(incrementCounterMethod2.getModifiers()));
  +            assertTrue("incrementCounterMethod2 static",
  +                       Modifier.isStatic(incrementCounterMethod2.getModifiers()));
  +
  +            // Return initial value of the counter
  +            value = currentCounterMethod.invoke(null, new Object[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +            // Increment via no-arguments version
  +            incrementCounterMethod1.invoke(null, new Object[0]);
  +
  +            // Validate updated value
  +            current++;
  +            value = currentCounterMethod.invoke(null, new Object[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +            // Increment via specified-argument version
  +            incrementCounterMethod2.invoke(null,
  +                                           new Object[] { new Integer(5) });
  +
  +            // Validate updated value
  +            current += 5;
  +            value = currentCounterMethod.invoke(null, new Object[0]);
  +            assertNotNull("currentCounter exists", value);
  +            assertTrue("currentCounter type",
  +                       value instanceof Integer);
  +            assertEquals("currentCounter value",
  +                         current,
  +                         ((Integer) value).intValue());
  +
  +        } catch (Exception e) {
  +            fail("Threw exception" + e);
  +        }
  +
  +    }
  +
  +
   }
  
  
  
  1.12      +46 -4     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBean.java
  
  Index: TestBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBean.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- TestBean.java	20 Jul 2002 22:36:36 -0000	1.11
  +++ TestBean.java	22 Jul 2002 00:02:01 -0000	1.12
  @@ -443,4 +443,46 @@
       }
   
   
  +    // ------------------------------------------------------- Static Variables
  +
  +
  +    /**
  +     * A static variable that is accessed and updated via static methods
  +     * for MethodUtils testing.
  +     */
  +    private static int counter = 0;
  +
  +
  +    /**
  +     * Return the current value of the counter.
  +     */
  +    public static int currentCounter() {
  +
  +        return (counter);
  +
  +    }
  +
  +
  +    /**
  +     * Increment the current value of the counter by 1.
  +     */
  +    public static void incrementCounter() {
  +
  +        incrementCounter(1);
  +
  +    }
  +
  +
  +    /**
  +     * Increment the current value of the counter by the specified amount.
  +     *
  +     * @param amount Amount to be added to the current counter
  +     */
  +    public static void incrementCounter(int amount) {
  +
  +        counter += amount;
  +
  +    }
  +
  +
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message