KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > TypeAdapterTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 import java.lang.reflect.Method JavaDoc;
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 /**
30  */

31 public class TypeAdapterTest extends BuildFileTest {
32
33     public TypeAdapterTest(String JavaDoc 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 JavaDoc execMethodName = "run";
99         private Object JavaDoc proxy;
100
101         public Method JavaDoc getExecuteMethod(Class JavaDoc proxyClass) {
102             try {
103                 Method JavaDoc execMethod = proxyClass.getMethod(
104                     execMethodName, null);
105                 if (!Void.TYPE.equals(execMethod.getReturnType())) {
106                     String JavaDoc 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 JavaDoc e) {
115                 String JavaDoc 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 JavaDoc proxyClass) {
123             getExecuteMethod(proxyClass);
124         }
125
126         public void setProxy(Object JavaDoc o) {
127             getExecuteMethod(o.getClass());
128             this.proxy = o;
129         }
130
131         public Object JavaDoc getProxy() {
132             return proxy;
133         }
134
135         public void execute() {
136             getProject().setProjectReference(proxy);
137             Method JavaDoc executeMethod = getExecuteMethod(proxy.getClass());
138             try {
139                 executeMethod.invoke(proxy, null);
140             } catch (java.lang.reflect.InvocationTargetException JavaDoc ie) {
141                 log("Error in " + proxy.getClass(), Project.MSG_ERR);
142                 Throwable JavaDoc t = ie.getTargetException();
143                 if (t instanceof BuildException) {
144                     throw ((BuildException) t);
145                 } else {
146                     throw new BuildException(t);
147                 }
148             } catch (Exception JavaDoc ex) {
149                 log("Error in " + proxy.getClass(), Project.MSG_ERR);
150                 throw new BuildException(ex);
151             }
152         }
153     }
154 }
155
Popular Tags