KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > AntTaskTest


1 /*
2  * Copyright 1999-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 /* $Id: AntTaskTest.java 42625 2004-03-04 15:45:03Z egli $ */
19
20 package org.apache.lenya.cms.task;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.GregorianCalendar JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 import junit.textui.TestRunner;
31
32 import org.apache.avalon.framework.configuration.ConfigurationException;
33 import org.apache.avalon.framework.parameters.ParameterException;
34 import org.apache.avalon.framework.parameters.Parameters;
35 import org.apache.lenya.cms.PublicationHelper;
36 import org.apache.lenya.cms.publication.Publication;
37 import org.xml.sax.SAXException JavaDoc;
38
39
40 /**
41  * Class for testing AntTasks.
42  * Extend this class to test your own AntTask.
43  * The target can be passed as a command-line argument.
44  * If the argument is omitted, the target is "test".
45  * Override {@link #getTarget()} to provide a hard-coded target.
46  * Override {@link #evaluateTest()} to add your evaluation code.
47  */

48 public class AntTaskTest extends TestCase {
49     private static String JavaDoc target = "test";
50
51     /**
52      * Creates a new AntTaskTest object.
53      *
54      * @param test the test
55      */

56     public AntTaskTest(String JavaDoc test) {
57         super(test);
58     }
59
60     /**
61      * The main program for the AntTaskTest class
62      *
63      * @param args The command line arguments
64      */

65     public static void main(String JavaDoc[] args) {
66         initialize(args);
67         TestRunner.run(getSuite());
68     }
69
70     /**
71      * Initializes the parameters. Call this method from your subclass
72      * {@link main(String[])} method.
73      * @param args The command-line arguments.
74      */

75     public static void initialize(String JavaDoc[] args) {
76         args = PublicationHelper.extractPublicationArguments(args);
77
78         if (args.length > 0) {
79             target = args[0];
80         }
81     }
82
83     /**
84      * Creates a test suite.
85      *
86      * @return the test suite
87      */

88     public static Test getSuite() {
89         return new TestSuite(AntTaskTest.class);
90     }
91
92     /**
93      * Tests the AntTask class.
94      * Template method, please override {@link #evaluateTest()} and {@link #getTarget()} and {@link #prepareTest()}.
95      *
96      * @throws Exception if an error occurs
97      */

98     public final void testAntTask() throws Exception JavaDoc {
99         prepareTest();
100         doTest(getTarget());
101         evaluateTest();
102     }
103
104     /**
105      * Returns the target to test.
106      * @return The target.
107      */

108     protected String JavaDoc getTarget() {
109         return target;
110     }
111
112     /**
113      * Tests an AntTask.
114      * @param target the target of the task to test.
115      *
116      * @throws ExecutionException if an error occurs
117      * @throws IOException if an error occurs
118      * @throws ParameterException if an error occurs
119      * @throws SAXException if an error occurs
120      * @throws ConfigurationException if an error occurs
121      */

122     protected void doTest(String JavaDoc target) throws ExecutionException, ParameterException, ConfigurationException, SAXException JavaDoc, IOException JavaDoc {
123         System.out.println("Testing target [" + target + "]:");
124
125         Publication publication = PublicationHelper.getPublication();
126
127         TaskManager manager = new TaskManager(publication.getDirectory().getCanonicalPath());
128         AntTask task = (AntTask) manager.getTask(TaskManager.ANT_TASK);
129
130         Parameters parameters = getTaskParameters();
131         parameters.setParameter(AntTask.PARAMETER_PUBLICATION_ID, publication.getId());
132         parameters.setParameter(AntTask.PARAMETER_CONTEXT_PREFIX, "/lenya");
133         parameters.setParameter(AntTask.PARAMETER_SERVLET_CONTEXT,
134             publication.getServletContext().getCanonicalPath());
135         parameters.setParameter(AntTask.TARGET, getTarget());
136         task.parameterize(parameters);
137
138         final GregorianCalendar JavaDoc beforeExecution = new GregorianCalendar JavaDoc();
139
140         task.execute(publication.getServletContext().getCanonicalPath());
141
142         File JavaDoc logDirectory = new File JavaDoc(publication.getDirectory(), AntTask.LOG_PATH);
143         File JavaDoc[] logFiles = logDirectory.listFiles(new FileFilter JavaDoc() {
144                     public boolean accept(File JavaDoc file) {
145                         return file.lastModified() > beforeExecution.getTimeInMillis();
146                     }
147                 });
148
149         assertTrue(logFiles.length == 1);
150
151         File JavaDoc logFile = logFiles[0];
152     }
153
154     /**
155      * Returns the task parameters.
156      * You don't need to specify the publication-id.
157      * @return The task parameters.
158      */

159     protected Parameters getTaskParameters() {
160         return new Parameters();
161     }
162
163     /**
164      * Override this method to prepare your test.
165     * @throws Exception if an error occurs
166      */

167     protected void prepareTest() throws Exception JavaDoc {
168         System.out.println("prepare");
169     }
170
171     /**
172      * Override this method to add your test evaluation code.
173      * @throws Exception if an error occurs
174      */

175     protected void evaluateTest() throws Exception JavaDoc {
176         System.out.println("evaluate");
177     }
178
179     /** (non-Javadoc)
180      * @see junit.framework.TestCase#setUp()
181      */

182     protected void setUp() throws Exception JavaDoc {
183         if (PublicationHelper.getPublication() == null) {
184             String JavaDoc[] args = {
185                 "/home/egli/build/jakarta-tomcat-4.1.21-LE-jdk14/webapps/lenya", "test"
186             };
187             PublicationHelper.extractPublicationArguments(args);
188         }
189     }
190 }
191
Popular Tags