Line | Hits | Source |
---|---|---|
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 | */ | |
38 | 9 | 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 | */ | |
45 | 9 | 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 { | |
53 | 9 | super(); |
54 | 9 | } |
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() { | |
63 | 3 | if (servers.isEmpty()) { |
64 | 1 | throw new BuildException("No server child elements have been specified."); |
65 | } | |
66 | try { | |
67 | //initialize all servers | |
68 | 2 | for (Iterator iterator = servers.iterator(); iterator.hasNext();) { |
69 | 2 | final Server server = ((ServerElement) iterator.next()).getServer(); |
70 | 2 | server.startUp(); |
71 | 1 | server.initData(); |
72 | } | |
73 | ||
74 | //run the tests | |
75 | 1 | final Enumeration individualTests = getIndividualTests(); |
76 | 2 | while (individualTests.hasMoreElements()) { |
77 | 1 | final JUnitTest jUnitTest = (JUnitTest) individualTests.nextElement(); |
78 | 1 | if (jUnitTest.shouldRun(getProject())) { |
79 | //set output dir | |
80 | 1 | if (getReportsDir() != null) { |
81 | 1 | jUnitTest.setTodir(getReportsDir()); |
82 | } | |
83 | //execute test | |
84 | 1 | execute(jUnitTest); |
85 | } | |
86 | } | |
87 | ||
88 | 1 | for (Iterator iterator = servers.iterator(); iterator.hasNext();) { |
89 | 1 | final Server server = ((ServerElement) iterator.next()).getServer(); |
90 | 1 | server.cleanUpData(); |
91 | 1 | server.shutDown(); |
92 | } | |
93 | 1 | } 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 | |
102 | 1 | throw new BuildException("Error while executing the tests.", e); |
103 | 1 | } |
104 | 1 | } |
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) { | |
122 | 6 | if (localName == null) { |
123 | 1 | throw new IllegalArgumentException("The parameter named [localName] was null."); |
124 | } | |
125 | 5 | if (localName.trim().length() == 0) { |
126 | 1 | throw new IllegalArgumentException("The parameter named [localName] was an empty String."); |
127 | } | |
128 | ||
129 | 4 | final ServerElement serverElement = new ServerElement(localName); |
130 | 4 | serverElement.setAntTaskInfo(new AntTaskInfo(getProject(), getTaskName(), getLocation(), getOwningTarget())); |
131 | 4 | servers.add(serverElement); |
132 | 4 | 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) { | |
142 | 2 | this.reportsDir = reportsDir; |
143 | 2 | } |
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() { | |
152 | 5 | return reportsDir; |
153 | } | |
154 | ||
155 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |