KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > tests > ManagerTaskTest


1 /**
2  * $Id: ManagerTaskTest.java 186 2007-03-16 13:42:35Z ssmc $
3  * Copyright 2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.tests;
30
31 import java.util.Map JavaDoc;
32
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.Project;
35
36 import junit.framework.TestSuite;
37
38 import com.idaremedia.antx.AntXFixture;
39 import com.idaremedia.antx.helpers.Tk;
40 import com.idaremedia.antx.starters.ManagerTask;
41 import com.idaremedia.antx.ut.HTC;
42 import com.idaremedia.antx.ut.HTCUtils;
43
44 /**
45  * Unit test for {@linkplain com.idaremedia.antx.starters.ManagerTask}.
46  *
47  * @since JWare/AntX 0.5
48  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
49  * @version 0.5
50  * @.safety single
51  * @.group api,test
52  **/

53
54 public final class ManagerTaskTest extends HTC
55 {
56     /** <i>PET</i> Test Category. **/
57     public static final String JavaDoc TEST_CATEGORY="CLASS";
58
59
60     /**
61      * Create new ManagerTask testcase.
62      **/

63     public ManagerTaskTest(String JavaDoc methodName)
64     {
65         super("ManagerTask::",methodName);
66     }
67
68
69     /**
70      * Create full test suite for ManagerTask.
71      **/

72     public static TestSuite suite()
73     {
74         return new TestSuite(ManagerTaskTest.class);
75     }
76
77
78     /**
79      * Create baseline test suite for ManagerTask (same as full).
80      **/

81     public static TestSuite baseline()
82     {
83         return suite();
84     }
85
86
87     /**
88      * Make this test (standalone) self-running.
89      **/

90     public static void main(String JavaDoc[] argv)
91     {
92         HTCUtils.quickCheck(suite());
93     }
94
95
96 // ---------------------------------------------------------------------------------------------------------
97
// ---------------------------------------- [ Misc Setup Methods ] -----------------------------------------
98
// ---------------------------------------------------------------------------------------------------------
99

100     public static class EmptyManager extends ManagerTask {
101         EmptyManager() {
102             super("EmptyManagerTest:");
103         }
104     }
105
106     static class IllegalManager extends ManagerTask {
107         IllegalManager() {
108             super("IllegalManagerTest:");
109         }
110     }
111
112     public static class TypicalManager extends ManagerTask {
113         TypicalManager() {
114             super("TypicalManagerTest:");
115         }
116         TypicalManager(Project project) {
117             super("TypicalManagerTest:");
118             setProject(project);
119         }
120         public void setAction(String JavaDoc actionName) {
121             m_actionName = Tk.lowercaseFrom(actionName);
122         }
123         public void setParam0(String JavaDoc methodName) {
124             require_(methodName!=null,"setParam- nonzro param value");
125             m_customParams.put("method",methodName);
126         }
127         public void execute() {
128             verifyCanExecute_("exec");
129             doAction(m_actionName,m_customParams,null);
130         }
131         public void doInstall(Map JavaDoc args) {
132             System.out.println("INSTALL");
133             installs++;
134         }
135         public void doUnInstall(Map JavaDoc args) {
136             System.out.println("UNINSTALL");
137             uninstalls++;
138         }
139         public void doIt(Map JavaDoc args) {
140             String JavaDoc method = (String JavaDoc)args.get("method");
141             if (method!=null) {
142                 doAction(method,args,null);
143             } else {
144                 doAction("die",args,null);
145             }
146         }
147         protected void doDIE(Map JavaDoc args) {
148             throw new BuildException("Drats! I'm dead!");
149         }
150         private String JavaDoc m_actionName="DIE";
151         int installs, uninstalls;
152         private Map JavaDoc m_customParams = AntXFixture.newMap();
153     }
154
155
156
157     protected void setUp() throws Exception JavaDoc
158     {
159         configureProjectFromResource("empty.xml");
160     }
161
162 // ---------------------------------------------------------------------------------------------------------
163
// ------------------------------------------- [ The Test Cases ] ------------------------------------------
164
// ---------------------------------------------------------------------------------------------------------
165

166     public void checkBaseline()
167     {
168          //--Ensures setUp() works and can find our xml file!
169
}
170
171     public void testBaseline()
172     {
173         checkBaseline();
174     }
175
176
177     public void testNoActionIntrospection_AntX05()
178     {
179         EmptyManager task = new EmptyManager();
180         Project myproject = getProject();
181         task.setProject(myproject);
182         task.init();
183         task.init();//NB:intentional twice!
184
assertEqual(task.actionMethodCount(),0,"ActionMethodCount");
185         assertEqual(task.validActionNames(null),"","ActionNameList");
186     }
187
188
189     public void testNoActionMethodsExecute_AntX05()
190     {
191         EmptyManager task = new EmptyManager();
192         Project myproject = getProject();
193         task.setProject(myproject);
194         task.init();
195         task.execute();
196     }
197
198
199
200     public void testIntrospectionFindsPublicMethodsOnly_AntX05()
201     {
202         TypicalManager task = new TypicalManager(getProject());
203         task.init();
204         assertEqual(task.actionMethodCount(),3,"Valid Action Count");
205         String JavaDoc list = task.validActionNames(",");
206         println("Valid Action Names:",list);
207         assertTrue(list.indexOf("install")>=0,"Found 'install' action");
208         assertTrue(list.indexOf("uninstall")>=0,"Found 'uninstall' action");
209         assertFalse(list.indexOf("die")>=0,"Did not find 'die' action");
210     }
211
212
213
214     public void testIllegalActionMethodFails_AntX05()
215     {
216         TypicalManager task = new TypicalManager(getProject());
217         try {
218             task.execute();//NB:should look fer actions
219
fail("Should not be able to execute 'die' action successfully!");
220         } catch(BuildException bX) {
221             String JavaDoc msg = bX.getMessage();
222             println(msg);
223             assertTrue(msg.indexOf("\"DIE\"")>0, "Unknown Action message");
224         }
225     }
226
227
228     public void testWillIgnoreErrorIfNoHalt_AntX05()
229     {
230         TypicalManager task = new TypicalManager(getProject());
231         task.setAction("IT");
232         task.setHaltIfError(false);
233         task.execute();
234     }
235
236
237
238     public void testActionsCalled_AntX05()
239     {
240         TypicalManager task = new TypicalManager(getProject());
241         task.setAction("InstaLL");
242         task.execute();
243         assertEqual(task.installs,1,"Install Calls");
244         task.setAction("uninsTAll");
245         task.execute();
246         assertEqual(task.installs,1,"Install Calls");
247         assertEqual(task.uninstalls,1,"Uninstall Calls");
248     }
249     
250     
251     public void testCustomParamsHandled_AntX05()
252     {
253         TypicalManager task = new TypicalManager(getProject());
254         task.setAction("it");
255         task.setParam0("install");
256         assertEqual(task.installs,0,"Install Calls Before");
257         task.execute();
258         assertEqual(task.installs,1,"Install Calls After");
259     }
260
261
262     public void testOnlyPublicManagerClassAllowed_AntX05()
263     {
264         IllegalManager task = new IllegalManager();
265         try {
266             task.init();
267             fail("Should not be able to initialize a non-public manager task");
268         } catch(IllegalStateException JavaDoc malformX) {/*burp*/}
269     }
270 }
271
272 /* end-of-ManagerTaskTest.java */
Popular Tags