Coverage details for com.topcoder.testframework.ServerAppTestTask

LineHitsSource
1 /*
2  * Copyright (C) 2006 TopCoder Inc., All Rights Reserved.
3  */
4 package com.topcoder.testframework;
5  
6 import org.apache.tools.ant.BuildException;
7 import org.apache.tools.ant.DynamicElementNS;
8 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTask;
9 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
10  
11 import java.io.File;
12 import java.util.Enumeration;
13 import java.util.Iterator;
14 import java.util.LinkedList;
15 import java.util.List;
16  
17  
18 /**
19  * This class represents an Ant task that extends the optional JUnit task to provide support for testing using this
20  * Component. This is the main Ant task of this component.
21  * <p/>
22  * <ul><li>Supported attributes: <ul><li><tt>reportsdir</tt> - not required, specifies the directory to which the
23  * reports of the test run should be written</li></ul> </li><li>Nested XML elements: <ul><li>One or more dynamic (i.e.
24  * with free element name instead of defined name) elements where the element name specifies the fully qualified class
25  * name to be used for instantiating the child element of type {@link Server}</li></ul></li></ul>
26  * <p/>
27  * This class is mutable, i.e. not thread-safe.
28  *
29  * @author real_vg, TCSDEVELOPER
30  * @version 1.0
31  */
32 public class ServerAppTestTask extends JUnitTask implements DynamicElementNS {
33  
34     /**
35      * This field represents the list of server elements. All elements of the list should be ServerElement instances.
36      * Is modified by {@link #createServer()} method. Then the list will be used in {@link #execute()} method.
37      */
389    private final List servers = new LinkedList();
39  
40     /**
41      * This field represents the <tt>reportsdir</tt> attribute, which represents the directory to which the test reports
42      * should be written. The field is initialized to <tt>null</tt> and set via the {@link #setReportsDir(java.io.File)}
43      * method.
44      */
459    private File reportsDir = null;
46  
47     /**
48      * Creates a ServerAppTestTask instance.
49      *
50      * @throws Exception in case the construction fails
51      */
52     public ServerAppTestTask() throws Exception {
539        super();
549    }
55  
56     /**
57      * Executes the task. First starts the servers if needed. Then runs the testcases. After running the testcases the
58      * servers are stopped.
59      *
60      * @throws BuildException if any error happens
61      */
62     public void execute() {
633        if (servers.isEmpty()) {
641            throw new BuildException("No server child elements have been specified.");
65         }
66         try {
67             //initialize all servers
682            for (Iterator iterator = servers.iterator(); iterator.hasNext();) {
692                final Server server = ((ServerElement) iterator.next()).getServer();
702                server.startUp();
711                server.initData();
72             }
73  
74             //run the tests
751            final Enumeration individualTests = getIndividualTests();
762            while (individualTests.hasMoreElements()) {
771                final JUnitTest jUnitTest = (JUnitTest) individualTests.nextElement();
781                if (jUnitTest.shouldRun(getProject())) {
79                     //set output dir
801                    if (getReportsDir() != null) {
811                        jUnitTest.setTodir(getReportsDir());
82                     }
83                     //execute test
841                    execute(jUnitTest);
85                 }
86             }
87  
881            for (Iterator iterator = servers.iterator(); iterator.hasNext();) {
891                final Server server = ((ServerElement) iterator.next()).getServer();
901                server.cleanUpData();
911                server.shutDown();
92             }
931        } catch (Exception e) {
94             // Catch of Exception is normally discouraged by TC style, but as we want
95             // to 'shield' the caller from the internals of the wrapped implementation
96             // of cactus (which sometimes can throw NPEs) we simply wrap everything
97             // into BuildException to assure normal ant behavior
98             //
99             // This method does not do any 'clever' finally-block cleanup strategies
100             // as this would only complicate the debugging of errors in tests or setup.
101             // see https://software.topcoder.com/forum/c_forum_message.jsp?f=20015788&r=21585282
1021            throw new BuildException("Error while executing the tests.", e);
1031        }
1041    }
105  
106     /**
107      * This method creates an element with the given name. Actually it creates a {@link ServerElement} for each call to
108      * this method and given the argument of this method to the ServerElement as constructor arg. The ServerElement
109      * expects this element to be a fully qualified class name of a {@link Server} subclass.
110      *
111      * @param uri The namespace uri for this element
112      * @param localName The localname of this element
113      * @param qName The qualified name for this element
114      *
115      * @return the element created for this element
116      *
117      * @throws org.apache.tools.ant.BuildException
118      * when any error occurs
119      * @throws IllegalArgumentException in case localName is <tt>null</tt> or an empty (trim'd) String
120      */
121     public Object createDynamicElement(final String uri, final String localName, final String qName) {
1226        if (localName == null) {
1231            throw new IllegalArgumentException("The parameter named [localName] was null.");
124         }
1255        if (localName.trim().length() == 0) {
1261            throw new IllegalArgumentException("The parameter named [localName] was an empty String.");
127         }
128  
1294        final ServerElement serverElement = new ServerElement(localName);
1304        serverElement.setAntTaskInfo(new AntTaskInfo(getProject(), getTaskName(), getLocation(), getOwningTarget()));
1314        servers.add(serverElement);
1324        return serverElement;
133     }
134  
135     /**
136      * This method sets the value of the <tt>reportsdir</tt> attribute, which represents the directory to which the test
137      * reports should be written.
138      *
139      * @param reportsDir the value of the <tt>reportsdir</tt> attribute
140      */
141     public void setReportsDir(final File reportsDir) {
1422        this.reportsDir = reportsDir;
1432    }
144  
145     /**
146      * This method returns the value of the <tt>reportsdir</tt> attribute, which represents the directory to which the
147      * test reports should be written.
148      *
149      * @return the value of the <tt>reportsdir</tt> attribute
150      */
151     public File getReportsDir() {
1525        return reportsDir;
153     }
154  
155 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.