KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > domain > F_JonasAdminDomain


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_JonasAdminDomain.java,v 1.4 2005/07/12 13:20:02 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jonasadmin.test.domain;
27
28
29 import junit.framework.TestSuite;
30
31 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
32 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
33 import org.xml.sax.SAXException JavaDoc;
34
35 import com.meterware.httpunit.HttpUnitOptions;
36 import com.meterware.httpunit.HttpUnitUtils;
37 import com.meterware.httpunit.TableCell;
38 import com.meterware.httpunit.WebConversation;
39 import com.meterware.httpunit.WebLink;
40 import com.meterware.httpunit.WebResponse;
41 import com.meterware.httpunit.WebTable;
42
43 /**
44  * Define a class to add useful methods for test the examples
45  * - Execute the EditDomainAction
46  * @author Paul Kemler
47  */

48 public class F_JonasAdminDomain extends JonasAdminTestCase {
49
50     /**
51      * TITLE of 'OTHER SERVERS' TABLE
52      */

53     private static final String JavaDoc TITLE_OTHERSERVERS = "Other servers in domain";
54
55     /**
56      * Main method
57      * @param args the arguments
58      */

59     public static void main(String JavaDoc[] args) {
60
61         String JavaDoc testtorun = null;
62         // Get args
63
for (int argn = 0; argn < args.length; argn++) {
64             String JavaDoc sArg = args[argn];
65             if (sArg.equals("-n")) {
66                 testtorun = args[++argn];
67             }
68         }
69         if (testtorun == null) {
70             junit.textui.TestRunner.run(suite());
71         } else {
72             junit.textui.TestRunner.run(new F_JonasAdminDomain(testtorun));
73         }
74     }
75
76     /**
77      * Get a new TestSuite for this class
78      * @return a new TestSuite for this class
79      */

80     public static TestSuite suite() {
81         return new TestSuite(F_JonasAdminDomain.class);
82     }
83
84     /**
85      * Get a new TestSuite for this class
86      * @param wc the WebConversation
87      * @return a new TestSuite for this class with the WebConversation instance
88      */

89     public static TestSuite suite(WebConversation wc) {
90         TestSuite suite = new TestSuite();
91         suite.addTest(new F_JonasAdminDomain(wc, "testSuccessfulSelect"));
92         suite.addTest(new F_JonasAdminDomain(wc, "testSuccesfulServerLink"));
93         suite.addTest(new F_JonasAdminDomain(wc, "testOtherServers"));
94         return suite;
95     }
96
97     /**
98      * Setup need for these tests
99      * jonasAdmin is required
100      * @throws Exception if it fails
101      */

102     protected void setUp() throws Exception JavaDoc {
103         super.setUp();
104
105         if (wc.getCurrentPage().getURL() == null) {
106             useWar("jonasAdmin");
107             // login to jonas admin
108
try {
109                 JonasAdminAuth.doValidAuth(wc, url);
110             } catch (Exception JavaDoc e) {
111                 fail("authentification failed : " + e);
112             }
113         } else {
114             // if there was an error, the connection must be restablished
115
try {
116                 wc.getFrameContents(FRAME_TREE);
117             } catch (Exception JavaDoc e) {
118                 wc.getResponse(urlLogOut);
119                 // login to jonas admin
120
try {
121                     JonasAdminAuth.doValidAuth(wc, url);
122                 } catch (Exception JavaDoc auth) {
123                     fail("authentification failed : " + auth);
124                 }
125             }
126         }
127     }
128
129
130     /**
131      * Constructor with a specified name
132      * @param s name
133      */

134     public F_JonasAdminDomain(String JavaDoc s) {
135         super(s, URL_JONASADMIN);
136     }
137
138     /**
139      * Constructor with a specified name
140      * @param wc the WebConversation of the suite test
141      * @param s name
142      */

143     public F_JonasAdminDomain(WebConversation wc, String JavaDoc s) {
144         super(wc, s, URL_JONASADMIN);
145     }
146
147     /**
148      * Verify if it is the good link of the tree which is selected
149      * @param wr The WebResponse of the 'tree' frame
150      * @param name A string with the name of the link
151      * @return a boolean. True if the name is found in the link, else false.
152      */

153     private boolean treeControlSelected (WebResponse wr, String JavaDoc name) {
154         boolean isSelected = false; // the result
155
int nbSelectedLink = 0; // number of selected link
156
String JavaDoc attribut; // value of class attribut
157

158         // Get all links
159
WebLink[] links;
160         try {
161             links = wr.getLinks();
162
163             // Search links with the 'tree-control-selected' class attribut
164
// and increase nbSelectedLink
165
for (int i = 0; i < links.length; i++) {
166                 attribut = links[i].getAttribute("class");
167                 if (attribut.indexOf("tree-control-selected") != -1) {
168                     nbSelectedLink++;
169                 }
170             }
171
172             if (nbSelectedLink == 1) {
173                 WebLink link = wr.getLinkWith(name);
174                 String JavaDoc content = link.getAttribute("class");
175                 if (content.indexOf("tree-control-selected") != -1) {
176                     isSelected = true;
177                 }
178             }
179         } catch (SAXException JavaDoc e) {
180             fail("No link was found. " + e);
181         }
182
183         return isSelected;
184     }
185
186
187     /**
188      * Test Succesful Select
189      * @throws Exception if it fails
190      *
191      */

192     public void testSuccessfulSelect() throws Exception JavaDoc {
193
194         // Disable errors of javascript
195
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
196
197         // Get the frame "tree"
198
WebResponse treeFrame = wc.getFrameContents(FRAME_TREE);
199
200         // Create a request
201
WebLink link = treeFrame.getLinkWith("Domain");
202         link.click();
203
204         // Get the frame "tree"
205
treeFrame = wc.getFrameContents(FRAME_TREE);
206
207         // Verify if the attribut class that values "tree-control-selected"
208
// it is in a link with "Domain"
209
if (!treeControlSelected(treeFrame, "Domain")) {
210             fail("Domain is not selected in the tree.");
211         }
212
213         // Create a new request for going to another page
214
link = treeFrame.getLinkWith("Services");
215         link.click();
216
217         // Get the frame "tree"
218
treeFrame = wc.getFrameContents(FRAME_TREE);
219
220         // Verify if the attribut class that values "tree-control-selected"
221
// it is NOT in a link with "Domain"
222
if (treeControlSelected(treeFrame, "Domain")) {
223             fail("Domain is selected in the tree.");
224         }
225
226
227         // Create a new request for going to another page
228
link = treeFrame.getLinkWith("Domain");
229         link.click();
230
231         // Get the frame "tree"
232
treeFrame = wc.getFrameContents(FRAME_TREE);
233
234         // Verify if the attribut class that values "tree-control-selected"
235
// it is in a link with "Domain"
236
if (!treeControlSelected(treeFrame, "Domain")) {
237             fail("Domain is not selected in the tree.");
238         }
239
240     }
241
242     /**
243      * Test Succesful Server Link
244      * @throws Exception if it fails
245      *
246      */

247     public void testSuccesfulServerLink() throws Exception JavaDoc {
248
249         // The expected link
250
String JavaDoc expectedLink = URL_JONASADMIN + "EditJonasServer.do?select=jonas:j2eeType=J2EEServer,name=jonas";
251
252         // Disable errors of javascript
253
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
254
255         // Get the frame "tree"
256
WebResponse treeFrame = wc.getFrameContents(FRAME_TREE);
257
258         // Create a request
259
WebLink link = treeFrame.getLinkWith("Domain");
260         link.click();
261
262         // Get the response
263
WebResponse wr = wc.getFrameContents(FRAME_CONTENT);
264
265         // Verify if the link of the JonasAdmin's server
266
link = wr.getLinks()[0];
267         String JavaDoc actualLink = link.getAttribute("href");
268         actualLink = HttpUnitUtils.decode(actualLink);
269         assertEquals("The link of the JonasAdmin's server is wrong.", expectedLink, actualLink);
270     }
271
272     /**
273      * Test if there is not any other server in domain
274      * @throws Exception if it fails
275      *
276      */

277     public void testOtherServers() throws Exception JavaDoc {
278
279         // Disable errors of javascript
280
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
281
282         // Get the frame "tree"
283
WebResponse treeFrame = wc.getFrameContents(FRAME_TREE);
284
285         // Create a request
286
WebLink link = treeFrame.getLinkWith("Domain");
287         link.click();
288
289         // Get the response
290
WebResponse wr = wc.getFrameContents(FRAME_CONTENT);
291
292         // Verify if there is the message "No other server in this domain!"
293
WebTable wt = wr.getTableStartingWith(TITLE_OTHERSERVERS);
294         TableCell cell = wt.getTableCell(1, 0);
295         String JavaDoc content = cell.getText();
296         if (content.indexOf("No other server in this domain!") == -1) {
297             // Verify if there are a link for other servers
298
WebLink[] wl = wr.getLinks();
299             if (wl.length < 2) {
300                 fail("No links are found for other servers in domain.");
301             } else {
302                 for (int i = 1; i < wt.getRowCount(); i++) {
303                     cell = wt.getTableCell(i, 0);
304                     if (!(cell.getLinks().length > 0)) {
305                         fail("A link doesn't exist for a server");
306                     }
307                 }
308             }
309         }
310     }
311
312 }
Popular Tags