public static Object invokeStaticNoArgumentsMethod(Class type, String methodName) { return invokeStaticMethod(type, methodName, EMPTY_ARGS); }
public static Object invokeStaticMethod(String klass, String methodName, Object arguments) throws ClassNotFoundException { Class type = Class.forName(klass); return invokeStaticMethod(type, methodName, arguments); }
/** * Run the specified class extending TestSuite as a unit test. * This is done through reflection, to avoid adding a dependency to the JUnit framework. * Otherwise, developers embedding Groovy and using GroovyShell to load/parse/compile * groovy scripts and classes would have to add another dependency on their classpath. * * @param scriptClass the class to be run as a unit test * @param loader the class loader */ @Override public Object run(Class<?> scriptClass, GroovyClassLoader loader) { try { Object testSuite = InvokerHelper.invokeStaticMethod(scriptClass, "suite", new Object[]{}); return InvokerHelper.invokeStaticMethod("junit.textui.TestRunner", "run", new Object[]{testSuite}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the unit test. JUnit is not on the Classpath.", e); } } }
public static Object invokeStaticMethodN(Class senderClass, Class receiver, String messageName, Object[] messageArguments) throws Throwable { try { return InvokerHelper.invokeStaticMethod(receiver, messageName, messageArguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
/** * This method is called by the -- operator for enums. It will invoke * Groovy's default previous behaviour for enums that do not have * their own previous method. * * @param self an Enum * @return the previous defined enum from the enum class */ public static Object previous(Enum self) { final Method[] methods = self.getClass().getMethods(); for (Method method : methods) { if (method.getName().equals("previous") && method.getParameterTypes().length == 0) { return InvokerHelper.invokeMethod(self, "previous", NO_ARGS); } } Object[] values = (Object[]) InvokerHelper.invokeStaticMethod(self.getClass(), "values", NO_ARGS); int index = Arrays.asList(values).indexOf(self); return values[index > 0 ? index - 1 : values.length - 1]; }
/** * This method is called by the ++ operator for enums. It will invoke * Groovy's default next behaviour for enums do not have their own * next method. * * @param self an Enum * @return the next defined enum from the enum class */ public static Object next(Enum self) { final Method[] methods = self.getClass().getMethods(); for (Method method : methods) { if (method.getName().equals("next") && method.getParameterTypes().length == 0) { return InvokerHelper.invokeMethod(self, "next", NO_ARGS); } } Object[] values = (Object[]) InvokerHelper.invokeStaticMethod(self.getClass(), "values", NO_ARGS); int index = Arrays.asList(values).indexOf(self); return values[index < values.length - 1 ? index + 1 : 0]; }
return (String) InvokerHelper.invokeStaticMethod("org.codehaus.groovy.util.StringUtil", "tr", new Object[]{self.toString(), sourceSet.toString(), replacementSet.toString()});
for (Iterator iter = list.iterator(); iter.hasNext(); idx++) { Object element = iter.next(); Array.setBoolean(array, idx, (Boolean) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, boolean.class})); Array.setByte(array, idx, (Byte) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, byte.class})); Array.setChar(array, idx, (Character) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, char.class})); Array.setDouble(array, idx, (Double) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, double.class})); Array.setFloat(array, idx, (Float) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, float.class})); Array.setInt(array, idx, (Integer) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, int.class})); Array.setLong(array, idx, (Long) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, long.class})); Array.setShort(array, idx, (Short) InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, short.class})); Array.set(array, idx, InvokerHelper.invokeStaticMethod(DefaultGroovyMethods.class, "asType", new Object[]{element, elementType}));
/** * Run the specified class extending TestCase as a unit test. * This is done through reflection, to avoid adding a dependency to the JUnit framework. * Otherwise, developers embedding Groovy and using GroovyShell to load/parse/compile * groovy scripts and classes would have to add another dependency on their classpath. * * @param scriptClass the class to be run as a unit test * @param loader the class loader */ @Override public Object run(Class<?> scriptClass, GroovyClassLoader loader) { try { Object testSuite = InvokerHelper.invokeConstructorOf("junit.framework.TestSuite", new Object[]{scriptClass}); return InvokerHelper.invokeStaticMethod("junit.textui.TestRunner", "run", new Object[]{testSuite}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the unit test. JUnit is not on the Classpath.", e); } } }
/** * Utility method to run a JUnit4 test. * * @param scriptClass the class we want to run as a test * @return the result of running the test */ static Object realRunJUnit4Test(Class scriptClass, GroovyClassLoader loader) { // invoke through reflection to eliminate mandatory JUnit 4 jar dependency try { Class junitCoreClass = loader.loadClass("org.junit.runner.JUnitCore"); Object result = InvokerHelper.invokeStaticMethod(junitCoreClass, "runClasses", new Object[]{scriptClass}); System.out.print("JUnit 4 Runner, Tests: " + InvokerHelper.getProperty(result, "runCount")); System.out.print(", Failures: " + InvokerHelper.getProperty(result, "failureCount")); System.out.println(", Time: " + InvokerHelper.getProperty(result, "runTime")); List failures = (List) InvokerHelper.getProperty(result, "failures"); for (int i = 0; i < failures.size(); i++) { Object f = failures.get(i); System.out.println("Test Failure: " + InvokerHelper.getProperty(f, "description")); System.out.println(InvokerHelper.getProperty(f, "trace")); } return result; } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Error running JUnit 4 test.", e); } } }
/** * Run the specified class extending TestCase as a unit test. * This is done through reflection, to avoid adding a dependency to the JUnit framework. * Otherwise, developers embedding Groovy and using GroovyShell to load/parse/compile * groovy scripts and classes would have to add another dependency on their classpath. * * @param scriptClass the class to be run as a unit test * @param loader the class loader */ @Override public Object run(Class<?> scriptClass, GroovyClassLoader loader) { try { Class<?> junitCoreClass = loader.loadClass("org.junit.runner.JUnitCore"); Object result = InvokerHelper.invokeStaticMethod(junitCoreClass, "runClasses", new Object[]{scriptClass}); System.out.print("JUnit 4 Runner, Tests: " + InvokerHelper.getProperty(result, "runCount")); System.out.print(", Failures: " + InvokerHelper.getProperty(result, "failureCount")); System.out.println(", Time: " + InvokerHelper.getProperty(result, "runTime")); List<?> failures = (List<?>) InvokerHelper.getProperty(result, "failures"); for (Object f : failures) { System.out.println("Test Failure: " + InvokerHelper.getProperty(f, "description")); System.out.println(InvokerHelper.getProperty(f, "trace")); } return result; } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Error running JUnit 4 test.", e); } }
private Object runTestNgTest(Class scriptClass) { try { return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.TestNgUtils", "realRunTestNgTest", new Object[]{scriptClass, this.loader}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the TestNG test.", e); } }
private Object runTestNgTest(Class scriptClass) { try { return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.TestNgUtils", "realRunTestNgTest", new Object[]{scriptClass}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the TestNG test."); } }
private Object runJUnit4Test(Class scriptClass) { try { return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.JUnit4Utils", "realRunJUnit4Test", new Object[]{scriptClass}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the JUnit 4 test."); } }
private Object runJUnit4Test(Class scriptClass) { try { return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.JUnit4Utils", "realRunJUnit4Test", new Object[]{scriptClass}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the JUnit 4 test."); } }
public static Object invokeStaticMethodN(Class senderClass, Class receiver, String messageName, Object[] messageArguments) throws Throwable { try { return InvokerHelper.invokeStaticMethod(receiver, messageName, messageArguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
public static Object invokeStaticMethodN(Class senderClass, Class receiver, String messageName, Object[] messageArguments) throws Throwable { try { return InvokerHelper.invokeStaticMethod(receiver, messageName, messageArguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
private Object runJUnit4Test(Class scriptClass) { try { return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.JUnit4Utils", "realRunJUnit4Test", new Object[]{scriptClass, this.loader}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the JUnit 4 test.", e); } }
public static Object invokeStaticMethodN(Class senderClass, Class receiver, String messageName, Object[] messageArguments) throws Throwable { try { return InvokerHelper.invokeStaticMethod(receiver, messageName, messageArguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
private Object runTestNgTest(Class scriptClass) { try { return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.TestNgUtils", "realRunTestNgTest", new Object[]{scriptClass}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the TestNG test."); } }