1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import java.lang.reflect.Method ; 21 22 import org.apache.tools.ant.BuildFileTest; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.TypeAdapter; 27 28 29 31 public class TypeAdapterTest extends BuildFileTest { 32 33 public TypeAdapterTest(String name) { 34 super(name); 35 } 36 37 public void setUp() { 38 configureProject("src/etc/testcases/taskdefs/typeadapter.xml"); 39 } 40 41 public void testTaskAdapter() { 42 expectLogContaining("taskadapter", "MyExec called"); 43 } 44 45 public void testRunAdapter() { 46 expectLogContaining("runadapter", "MyRunnable called"); 47 } 48 49 public void testRunAdapterError() { 50 expectBuildExceptionContaining( 51 "runadaptererror", "xx", "No public run() method in"); 52 } 53 54 public void testDelay() { 55 expectLogContaining("delay", "MyTask called"); 56 } 57 58 public void testOnErrorReport() { 59 expectLogContaining("onerror.report", 60 "MyTaskNotPresent cannot be found"); 61 } 62 63 public void testOnErrorIgnore() { 64 expectLog("onerror.ignore",""); 65 } 66 67 public static class MyTask extends Task { 68 public void execute() { 69 log("MyTask called"); 70 } 71 } 72 73 public static class MyExec { 74 private Project project; 75 public void setProject(Project project) { 76 this.project = project; 77 } 78 79 public void execute() { 80 project.log("MyExec called"); 81 } 82 } 83 84 public static class MyRunnable { 85 private Project project; 86 public void setProject(Project project) { 87 this.project = project; 88 } 89 90 public void run() { 91 project.log("MyRunnable called"); 92 } 93 } 94 95 public static class RunnableAdapter 96 extends Task implements TypeAdapter 97 { 98 private String execMethodName = "run"; 99 private Object proxy; 100 101 public Method getExecuteMethod(Class proxyClass) { 102 try { 103 Method execMethod = proxyClass.getMethod( 104 execMethodName, null); 105 if (!Void.TYPE.equals(execMethod.getReturnType())) { 106 String message = 107 "return type of " + execMethodName + "() should be " 108 + "void but was \"" + execMethod.getReturnType() + 109 "\" in " 110 + proxyClass; 111 log(message, Project.MSG_WARN); 112 } 113 return execMethod; 114 } catch (NoSuchMethodException e) { 115 String message = "No public "+ execMethodName + 116 "() method in " 117 + proxyClass; 118 log(message, Project.MSG_ERR); 119 throw new BuildException(message); 120 } 121 } 122 public void checkProxyClass(Class proxyClass) { 123 getExecuteMethod(proxyClass); 124 } 125 126 public void setProxy(Object o) { 127 getExecuteMethod(o.getClass()); 128 this.proxy = o; 129 } 130 131 public Object getProxy() { 132 return proxy; 133 } 134 135 public void execute() { 136 getProject().setProjectReference(proxy); 137 Method executeMethod = getExecuteMethod(proxy.getClass()); 138 try { 139 executeMethod.invoke(proxy, null); 140 } catch (java.lang.reflect.InvocationTargetException ie) { 141 log("Error in " + proxy.getClass(), Project.MSG_ERR); 142 Throwable t = ie.getTargetException(); 143 if (t instanceof BuildException) { 144 throw ((BuildException) t); 145 } else { 146 throw new BuildException(t); 147 } 148 } catch (Exception ex) { 149 log("Error in " + proxy.getClass(), Project.MSG_ERR); 150 throw new BuildException(ex); 151 } 152 } 153 } 154 } 155 | Popular Tags |