Coverage details for com.topcoder.testframework.Server

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  
8 import java.io.File;
9  
10  
11 /**
12  * This class is a base class for all classes representing an actual server to be used while running tests. Derived
13  * classes should have corresponding XML elements to be used in the Ant build file. <ul><li>Supported attributes:
14  * <ul><li><tt>dir</tt> - required, defines the home directory where the server binaries are located, thus needed to
15  * start up the server</li> <li><tt>port</tt> - non-required, specifies the port to be used when staring the server</li>
16  * <li><tt>server</tt> - non-required, the hostname under which the started server shall be connected, defaults to
17  * <tt>"localhost"</tt></li> <li><tt>startserver</tt> - non-required, specifies whether the server should be started
18  * during test run preparation</li> <li><tt>initserver</tt> - non-required, specifies whether the server should be
19  * initialized with test data during test run preparation</li> </ul></li><li>Nested elements: <ul><li>Can include single
20  * <tt>credentials</tt> element, which is not required. </li></ul></li></ul>
21  * <p/>
22  * This class is mutable, i.e. not thread-safe.
23  *
24  * @author real_vg, TCSDEVELOPER
25  * @version 1.0
26  */
27 public abstract class Server {
28  
29     /**
30      * This constant represents the key used in the credentials property map to pass the user name. It is used to
31      * provide common interface for all derived classes. Derived classes should also define such constants for the keys
32      * they need.
33      */
34     public static final String USER_NAME = "user_name";
35  
36     /**
37      * This constant represents the key used in the credentials property map to pass the password. It is used to provide
38      * common interface for all derived classes. Derived classes should also define such constants for the keys they
39      * need.
40      */
41     public static final String PASSWORD = "password";
42     /**
43      * This field represents the <tt>dir</tt> attribute. This attribute represents the installation directory of the
44      * server. This attribute is required. The field is initialized to <tt>null</tt> and set via the {@link
45      * #setDir(java.io.File)} method.
46      */
47134    private File dir = null;
48  
49     /**
50      * This field represents the <tt>port</tt> attribute. This attribute represents the TCP port that the server should
51      * listen to. This attribute is not required. The default value should be defined by the derived classes. The field
52      * is initialized to <tt>-1</tt> and set via the {@link #setPort(int)} method.
53      */
54134    private int port = -1;
55  
56     /**
57      * This field represents the <tt>server</tt> attribute. This attribute represents the host name or IP address of the
58      * machine running the server. This attribute is not required, default value is <tt>"localhost"</tt>. The field is
59      * initialized to <tt>"localhost"</tt> and set via the {@link #setServer(String)} method.
60      */
61134    private String server = "localhost";
62  
63     /**
64      * This field represents the <tt>startserver</tt> attribute. This attribute specifies if the server should be
65      * started by the Component while running tests. The field is initialized to <tt>true</tt> and set via the {@link
66      * #setStartServer(boolean)} method.
67      */
68134    private boolean startServer = true;
69  
70     /**
71      * This field represents information about the current Ant project, task, etc. The field is initialized to
72      * <tt>null</tt> and set via the {@link #setAntTaskInfo(AntTaskInfo)} method.
73      */
74134    private AntTaskInfo antTaskInfo = null;
75  
76     /**
77      * This field represents the <tt>initserver</tt> attribute. This attribute specifies if the server should be
78      * initialized with data by the Component when running tests. The field is initialized to <tt>true</tt> and set via
79      * the {@link #setInitServer(boolean)} method.
80      */
81134    private boolean initServer = true;
82  
83     /**
84      * Represents the nested <tt>credentials</tt> element, which contains the credentials used to authenticate to the
85      * server. This can include user name, password, certificate, etc. The field is initialized to <tt>null</tt> and set
86      * in the {@link #createCredentials()} method.
87      */
88134    private CredentialsElement credentials = null;
89  
90     /**
91      * Creates a Server instance. The type of server represented by the class is specified. Type is a string like
92      * <tt>"tomcat5x"</tt>, which actually specifies the name and version of the server.
93      *
94      * @param type the type of the server
95      *
96      * @throws BuildException if the type of the server is unrecognized, or any other error happens
97      * @throws IllegalArgumentException if type is <tt>null</tt>
98      */
99134    protected Server(final String type) {
100134        if (type == null) {
1014            throw new IllegalArgumentException("The parameter named [type] was null.");
102         }
103130    }
104  
105     /**
106      * This method starts the server. In the case when <tt>startserver</tt> attribute is set to <tt>false</tt>, the
107      * server should not be started, but some initialization may still be needed. Subclasses must implement this method
108      * to perform the actual task of starting up the server.
109      *
110      * @throws BuildException if any error happens
111      */
112     public abstract void startUp();
113  
114     /**
115      * This method stops the server. The server should not be stopped if it wasn't started by the startUp() method, i.e.
116      * <tt>startserver</tt> attribute is set to <tt>false</tt>. Subclasses must implement this method to perform the
117      * actual task of stopping the server.
118      *
119      * @throws BuildException if any error happens
120      */
121     public abstract void shutDown();
122  
123     /**
124      * This method initializes the server with data. In the case when <tt>initserver</tt> attribute is set to
125      * <tt>false</tt>, does nothing, or can check if the data are properly initialized, the implementation can choose
126      * the preferred way. Subclasses must implement this method to perform the actual task of initializing server with
127      * data.
128      *
129      * @throws BuildException if any error happens
130      */
131     public abstract void initData();
132  
133     /**
134      * This method cleans up the data initialized by the {@link #initData()} method. In the case when
135      * <tt>initserver</tt> attribute is set to <tt>false</tt> or data was not initialized, does nothing. Subclasses must
136      * implement this method to perform the actual task of cleaning-up the data.
137      *
138      * @throws BuildException if any error happens
139      */
140     public abstract void cleanUpData();
141  
142     /**
143      * This method sets the value of the <tt>dir</tt> attribute, which represents the installation directory of the
144      * server. This attribute is required.
145      *
146      * @param dir the value of the <tt>dir</tt> attribute to be set
147      */
148     public void setDir(final File dir) {
1498        this.dir = dir;
1508    }
151  
152     /**
153      * This method returns the value of the <tt>dir</tt> attribute, which represents the installation directory of the
154      * server.
155      *
156      * @return the value of the <tt>dir</tt> attribute
157      */
158     public File getDir() {
15912        return dir;
160     }
161  
162     /**
163      * This method sets the value of the <tt>port</tt> attribute, which represents the TCP port that the server should
164      * listen to. This attribute is not required. The default value should be defined by the derived classes.
165      *
166      * @param port the value of the <tt>port</tt> attribute to be set
167      *
168      * @throws IllegalArgumentException if port is invalid (e.g. is non-positive)
169      */
170     public void setPort(final int port) {
17112        if (port < 1) {
1724            throw new IllegalArgumentException(
173                 "The parameter named [port] was expected to be >0 , but was [" + port + "].");
174         }
175  
1768        this.port = port;
1778    }
178  
179     /**
180      * This method returns the value of the <tt>port</tt> attribute, which represents the TCP port that the server
181      * should listen to.
182      *
183      * @return the value of the <tt>port</tt> attribute
184      */
185     public int getPort() {
18615        return port;
187     }
188  
189     /**
190      * This method sets the value of the <tt>server</tt> attribute, which represents the host name or IP address of the
191      * machine running the server. This attribute is not required, default value is <tt>"localhost"</tt>.
192      *
193      * @param server the value of the <tt>server</tt> attribute to be set
194      *
195      * @throws IllegalArgumentException if server is empty string
196      */
197     public void setServer(final String server) {
1988        if (server != null && server.trim().length() == 0) {
1994            throw new IllegalArgumentException("The parameter named [server] was an empty String.");
200         }
2014        this.server = server;
2024    }
203  
204     /**
205      * This method sets the value of the <tt>server</tt> attribute, which represents the host name or IP address of the
206      * machine running the server.
207      *
208      * @return the value of the <tt>server</tt> attribute
209      */
210     public String getServer() {
21112        return server;
212     }
213  
214     /**
215      * This method sets the value of the <tt>startserver</tt> attribute, which specifies whether the server should be
216      * started by the Component while running tests.
217      *
218      * @param startServer the value of the <tt>startserver</tt> attribute to be set
219      */
220     public void setStartServer(final boolean startServer) {
2214        this.startServer = startServer;
2224    }
223  
224     /**
225      * This method returns the value of the <tt>startserver</tt> attribute, which specifies whether the server should be
226      * started by the Component while running tests.
227      *
228      * @return the value of the <tt>startserver</tt> attribute
229      */
230     public boolean getStartServer() {
2318        return startServer;
232     }
233  
234     /**
235      * This method sets the AntTaskInfo which represents information about the current Ant project, task, etc.
236      *
237      * @param antTaskInfo the AntTaskInfo to be set
238      *
239      * @throws IllegalArgumentException if given antTaskInfo is <tt>null</tt>
240      */
241     public void setAntTaskInfo(final AntTaskInfo antTaskInfo) {
24215        if (antTaskInfo == null) {
2434            throw new IllegalArgumentException("The parameter named [antTaskInfo] was null.");
244         }
245  
24611        this.antTaskInfo = antTaskInfo;
24711    }
248  
249     /**
250      * This method returns the AntTaskInfo which represents information about the current Ant project, task, etc.
251      *
252      * @return the AntTaskInfo describing the current environment
253      */
254     public AntTaskInfo getAntTaskInfo() {
25512        return antTaskInfo;
256     }
257  
258     /**
259      * This method sets the value of the <tt>initserver</tt> attribute, which specifies whether the server should be
260      * initialized with data by the Component when running tests.
261      *
262      * @param initServer the value of the <tt>initserver</tt> attribute to be set
263      */
264     public void setInitServer(final boolean initServer) {
2654        this.initServer = initServer;
2664    }
267  
268     /**
269      * This method returns the value of the <tt>initserver</tt> attribute, which specifies whether the server should be
270      * initialized with data by the Component when running tests.
271      *
272      * @return the value of the <tt>initserver</tt> attribute
273      */
274     public boolean getInitServer() {
2758        return initServer;
276     }
277  
278     /**
279      * This method creates a nested <tt>credentials</tt> element.
280      *
281      * @return the created instance of {@link CredentialsElement}
282      *
283      * @throws BuildException if there are more than one nested elements (i.e. this method gets called more than once),
284      * or when fails to create a nested element
285      */
286     public CredentialsElement createCredentials() {
28712        if (credentials != null) {
2884            throw new BuildException("This Server element already has a nested credentials element.");
289         }
2908        credentials = new CredentialsElement();
2918        return credentials;
292     }
293  
294     /**
295      * This method returns the nested <tt>credentials</tt> element, which represents the credentials used to
296      * authenticate to the server.
297      *
298      * @return the nested <tt>credentials</tt> element, or <tt>null</tt> if no nested credentials element exists
299      */
300     public CredentialsElement getCredentials() {
3018        return credentials;
302     }
303 }

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.