You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2010/03/30 19:36:16 UTC
svn commit: r929197 - in
/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src:
main/java/org/apache/myfaces/scripting/core/util/
test/java/org/apache/myfaces/scripting/core/util/
test/java/org/apache/myfaces/scripting/core/utilsTe...
Author: werpu
Date: Tue Mar 30 17:36:16 2010
New Revision: 929197
URL: http://svn.apache.org/viewvc?rev=929197&view=rev
Log:
http://issues.apache.org/jira/browse/EXTSCRIPT-109
Modified:
myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java
myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/util/Probe2.java
myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/utilsTests/ReflectUtilTest.java
Modified: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java?rev=929197&r1=929196&r2=929197&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java Tue Mar 30 17:36:16 2010
@@ -112,7 +112,7 @@ public class ReflectUtil {
public static Collection<Method> getAllMethods(Class clazz, String methodName, int varargLength) {
ArrayList<Method> retVal = new ArrayList<Method>(30);
- while (clazz.equals(java.lang.Object.class)) {
+ while (clazz != null) {
for (Method m : clazz.getDeclaredMethods()) {
if (m.getParameterTypes().length == varargLength && m.getName().equals(methodName)) {
retVal.add(m);
Modified: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/util/Probe2.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/util/Probe2.java?rev=929197&r1=929196&r2=929197&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/util/Probe2.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/util/Probe2.java Tue Mar 30 17:36:16 2010
@@ -26,7 +26,11 @@ package org.apache.myfaces.scripting.cor
public class Probe2 {
- public Probe2(String [] test) {
-
+ public Probe2(String[] test) {
+
+ }
+
+ public static Boolean myHello(String xxx) {
+ return Boolean.TRUE;
}
}
Modified: myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/utilsTests/ReflectUtilTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/utilsTests/ReflectUtilTest.java?rev=929197&r1=929196&r2=929197&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/utilsTests/ReflectUtilTest.java (original)
+++ myfaces/extensions/scripting/trunk/extscript-core-root/extscript-core/src/test/java/org/apache/myfaces/scripting/core/utilsTests/ReflectUtilTest.java Tue Mar 30 17:36:16 2010
@@ -24,6 +24,9 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import java.lang.reflect.Method;
+import java.util.Collection;
+
import static org.junit.Assert.*;
/**
@@ -37,6 +40,9 @@ import static org.junit.Assert.*;
public class ReflectUtilTest {
private static final String HELLO_WORLD = "Hello World";
private static final String JAVA_LANG_STRING = "java.lang.String";
+ private static final String MSG_INSTANTIATED = "String must be instantiated";
+ private static final String MSG_PROBE_INSTANTIATED = "Probe must be instantiated";
+ private static final String MSG_INIT_FAIL = "init failed expected";
@Before
public void setUp() throws Exception {
@@ -49,29 +55,29 @@ public class ReflectUtilTest {
@Test
public void testInstantiate() throws Exception {
String retVal = (String) ReflectUtil.instantiate(JAVA_LANG_STRING);
- assertTrue("String must be instantiated", retVal != null);
+ assertTrue(MSG_INSTANTIATED, retVal != null);
retVal = (String) ReflectUtil.instantiate(JAVA_LANG_STRING, HELLO_WORLD);
- assertTrue("String must be instantiated", retVal != null && retVal.equals(HELLO_WORLD));
+ assertTrue(MSG_INSTANTIATED, retVal != null && retVal.equals(HELLO_WORLD));
Object myHello = HELLO_WORLD;
Object probe = ReflectUtil.instantiate(Probe.class, new Cast(String.class, myHello), HELLO_WORLD);
- assertTrue("Probe must be instantiated", probe != null);
+ assertTrue(MSG_PROBE_INSTANTIATED, probe != null);
try {
probe = ReflectUtil.instantiate(Probe.class, new Cast(Integer.class, myHello), HELLO_WORLD);
fail();
} catch (RuntimeException ex) {
- assertTrue("init failed expected", true);
+ assertTrue(MSG_INIT_FAIL, true);
}
probe = ReflectUtil.instantiate(Probe.class, new Null(String.class), new Null(String.class));
- assertTrue("Probe must be instantiated", probe != null);
+ assertTrue(MSG_PROBE_INSTANTIATED, probe != null);
try {
probe = ReflectUtil.instantiate(Probe.class, new Null(Integer.class), new Null(String.class));
fail();
} catch (RuntimeException ex) {
- assertTrue("init failed expected", true);
+ assertTrue(MSG_INIT_FAIL, true);
}
//TODO test fails, but is not used so we can live with it
@@ -81,21 +87,75 @@ public class ReflectUtilTest {
@Test
public void testNewObject() throws Exception {
+ String retVal = (String) ReflectUtil.newObject(String.class);
+ assertTrue(MSG_INSTANTIATED, retVal != null);
}
@Test
public void testExecuteStaticMethod() throws Exception {
+ Boolean retVal = (Boolean) ReflectUtil.executeStaticMethod(Boolean.class, "valueOf", "true");
+ assertTrue("retval must be true", retVal);
+
+ try {
+ retVal = (Boolean) ReflectUtil.executeStaticMethod(Boolean.class, "xx_valueOf", "true");
+ fail();
+ } catch (RuntimeException ex) {
+ assertTrue("Exception must be thrown", true);
+ }
+
}
@Test
- public void testGetAllMethods() throws Exception {
+ public void testFastExecuteStaticMethod() throws Exception {
+ Boolean retVal = (Boolean) ReflectUtil.fastExecuteStaticMethod(Boolean.class, "valueOf", "true");
+ assertTrue("retval must be true", retVal);
}
@Test
- public void testGetMethods() throws Exception {
+ public void testGetAllMethods() throws Exception {
+ Collection<Method> retVal = ReflectUtil.getAllMethods(Boolean.class, "valueOf", 1);
+ assertTrue(retVal.size() == 2);/*String and boolean*/
+ retVal = ReflectUtil.getAllMethods(Object.class, "toString", 0);
+ assertTrue(retVal.size() == 1);/*String and boolean*/
}
@Test
public void testExecuteMethod() throws Exception {
+
+ Boolean probe = new Boolean(true);
+ Boolean retVal = (Boolean) ReflectUtil.executeMethod(probe, "valueOf", "true");
+ assertTrue(retVal);
+ String sRetVal = (String) ReflectUtil.executeMethod(probe, "toString");
+ assertTrue(sRetVal.equals("true"));
+
+ Object hashVal = ReflectUtil.executeMethod(new Probe(), "hashCode");
+ assertTrue(hashVal != null);
+
+ try {
+ hashVal = ReflectUtil.executeMethod(new Probe(), "xx_hashCode");
+ fail();
+ } catch (RuntimeException ex) {
+ assertTrue("calling must faile with an RE", true);
+ }
+ }
+
+ @Test
+ public void testFastExecuteMethod() throws Exception {
+
+ Boolean probe = new Boolean(true);
+ Boolean retVal = (Boolean) ReflectUtil.fastExecuteMethod(probe, "valueOf", "true");
+ assertTrue(retVal);
+ String sRetVal = (String) ReflectUtil.fastExecuteMethod(probe, "toString");
+ assertTrue(sRetVal.equals("true"));
+
+ Object hashVal = ReflectUtil.fastExecuteMethod(new Probe(), "hashCode");
+ assertTrue(hashVal != null);
+
+ }
+
+ @Test
+ public void testCast() {
+ assertTrue("Cast testing", ReflectUtil.cast(String.class, HELLO_WORLD) instanceof Cast);
+ assertTrue("Cast testing", ReflectUtil.nullCast(String.class) instanceof Null);
}
}