001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: F_JonasAdminJoramUser.java 7381 2005-09-14 16:00:02Z kemlerp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jonasadmin.test.joram;
025:
026: import javax.management.MBeanServerConnection;
027: import javax.management.ObjectName;
028: import javax.management.Query;
029: import javax.management.QueryExp;
030:
031: import junit.framework.TestSuite;
032:
033: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
034: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
035: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminUtils;
036:
037: import com.meterware.httpunit.HttpUnitOptions;
038: import com.meterware.httpunit.SubmitButton;
039: import com.meterware.httpunit.WebForm;
040: import com.meterware.httpunit.WebLink;
041: import com.meterware.httpunit.WebResponse;
042: import com.meterware.httpunit.WebTable;
043:
044: /**
045: * Class for testing Joram User
046: * @author Paul Kemler
047: *
048: */
049: public class F_JonasAdminJoramUser extends JonasAdminTestCase {
050:
051: /**
052: * URL of the Joram Platform
053: */
054: private static final String URL_JONASADMIN_JORAM = "EditJoramPlatform.do";
055:
056: /**
057: * URL of the Joram Servers
058: */
059: private static final String URL_JONASADMIN_JORAM_SERVERS = "joramplatform/joramservers.jsp";
060:
061: /**
062: * URL of the Joram Users
063: */
064: private static final String URL_JONASADMIN_JORAM_USERS = "joramplatform/joramusers.jsp";
065:
066: /**
067: * URL of "Create Jms User"
068: */
069: private static final String URL_JONASADMIN_JORAM_CREATE_USER = "CreateJoramUser.do";
070:
071: /**
072: * Constructor with a specified name
073: * @param s name
074: */
075: public F_JonasAdminJoramUser(String s) {
076: super (s, URL_JONASADMIN);
077: }
078:
079: /**
080: * Main method
081: * @param args the arguments
082: */
083: public static void main(String[] args) {
084:
085: String testtorun = null;
086: // Get args
087: for (int argn = 0; argn < args.length; argn++) {
088: String sArg = args[argn];
089: if (sArg.equals("-n")) {
090: testtorun = args[++argn];
091: }
092: }
093: if (testtorun == null) {
094: junit.textui.TestRunner.run(suite());
095: } else {
096: junit.textui.TestRunner.run(new F_JonasAdminJoramUser(
097: testtorun));
098: }
099: }
100:
101: /**
102: * Get a new TestSuite for this class
103: * @return a new TestSuite for this class
104: */
105: public static TestSuite suite() {
106: return new TestSuite(F_JonasAdminJoramUser.class);
107: }
108:
109: /**
110: * Setup need for these tests
111: * jonasAdmin is required
112: * @throws Exception if it fails
113: */
114: protected void setUp() throws Exception {
115: super .setUp();
116:
117: if (wc.getCurrentPage().getURL() == null) {
118: useWar("jonasAdmin");
119: // login to jonas admin
120: try {
121: JonasAdminAuth.doValidAuth(wc, url);
122: } catch (Exception e) {
123: fail("authentification failed : " + e);
124: }
125: } else {
126: // if there was an error, the connection must be restablished
127: try {
128: wc.getFrameContents(FRAME_TREE);
129: } catch (Exception e) {
130: wc.getResponse(urlLogOut);
131: // login to jonas admin
132: try {
133: JonasAdminAuth.doValidAuth(wc, url);
134: } catch (Exception auth) {
135: fail("authentification failed : " + auth);
136: }
137: }
138: }
139: }
140:
141: /**
142: * Test to create a local user
143: * @throws Exception if error occurs
144: *
145: */
146: public void testSuccessfulCreateLocalUser() throws Exception {
147:
148: WebResponse wr;
149: WebLink link;
150: WebTable table;
151: String createUserPage;
152: /*
153: * To get some utils for WebTable
154: */
155: JonasAdminUtils utils = new JonasAdminUtils();
156:
157: // Disable errors of javascript
158: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
159: // Disable exception thrown on error status
160: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
161:
162: if (jProp.isResource()) {
163: // Joram platform is in jonas
164: if (isRarLoaded("joram_for_jonas_ra")) {
165: // Go to Joram Platform
166: wr = wc.getFrameContents(FRAME_TREE);
167: link = wr.getFirstMatchingLink(
168: WebLink.MATCH_URL_STRING, URL_JONASADMIN_JORAM);
169: link.click();
170: wr = wc.getFrameContents(FRAME_CONTENT);
171:
172: // Go to Joram Servers
173: link = wr.getFirstMatchingLink(
174: WebLink.MATCH_URL_STRING,
175: URL_JONASADMIN_JORAM_SERVERS);
176: link.click();
177: wr = wc.getFrameContents(FRAME_CONTENT);
178:
179: // Go to Local Server
180: WebTable localTable = utils.getTable(wr, 1);
181: link = localTable.getTableCell(0, 0).getLinks()[0];
182: if (link == null) {
183: throw new IllegalStateException(
184: "No link was found to a local server in the 'Joram servers' page. ");
185: }
186: link.click();
187: wr = wc.getFrameContents(FRAME_CONTENT);
188:
189: // Go to Joram Users
190: link = wr.getFirstMatchingLink(
191: WebLink.MATCH_URL_STRING,
192: URL_JONASADMIN_JORAM_USERS);
193: link.click();
194: wr = wc.getFrameContents(FRAME_CONTENT);
195:
196: // Go to Create JMS User
197: link = wr.getFirstMatchingLink(
198: WebLink.MATCH_URL_STRING,
199: URL_JONASADMIN_JORAM_CREATE_USER);
200: link.click();
201: wr = wc.getFrameContents(FRAME_CONTENT);
202:
203: createUserPage = wr.getText(); // For comparing with the forwarded page after the creation
204: // Find a name
205: String userName = "new_user_";
206: int userId = 0;
207: boolean found = false;
208: while (!found) {
209: if (!isUser(userName + userId)) {
210: userName = userName + userId;
211: found = true;
212: }
213: userId++;
214: }
215:
216: // Create a new user
217: createUser(wr, userName, userName);
218:
219: wr = wc.getFrameContents(FRAME_CONTENT);
220:
221: // Verify user is created
222: assertTrue("The '" + userName
223: + "' topic was not created. ", isUser(userName));
224:
225: // Verify the forwarded page
226: assertEquals("The 'New User' page has changed. ",
227: createUserPage, wr.getText());
228:
229: }
230: }
231:
232: }
233:
234: /**
235: * Test to create a local user that already exists
236: * @throws Exception if error occurs
237: *
238: */
239: public void testCreateUserAlreadyExists() throws Exception {
240:
241: WebResponse wr;
242: WebLink link;
243: WebTable table;
244: /*
245: * To get some utils for WebTable
246: */
247: JonasAdminUtils utils = new JonasAdminUtils();
248:
249: // Disable errors of javascript
250: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
251: // Disable exception thrown on error status
252: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
253:
254: if (jProp.isResource()) {
255: // Joram platform is in jonas
256: if (isRarLoaded("joram_for_jonas_ra")) {
257:
258: // Find a name
259: String userName = "new_user_";
260: int userId = 0;
261: boolean found = false;
262: while (!found) {
263: if (!isUser(userName + userId)) {
264: userName = userName + userId;
265: found = true;
266: }
267: userId++;
268: }
269:
270: // Create Jms User
271: createLocalJmsUser(userName, userName);
272:
273: // Go to Joram Platform
274: wr = wc.getFrameContents(FRAME_TREE);
275: link = wr.getFirstMatchingLink(
276: WebLink.MATCH_URL_STRING, URL_JONASADMIN_JORAM);
277: link.click();
278: wr = wc.getFrameContents(FRAME_CONTENT);
279:
280: // Go to Joram Servers
281: link = wr.getFirstMatchingLink(
282: WebLink.MATCH_URL_STRING,
283: URL_JONASADMIN_JORAM_SERVERS);
284: link.click();
285: wr = wc.getFrameContents(FRAME_CONTENT);
286:
287: // Go to Local Server
288: WebTable localTable = utils.getTable(wr, 1);
289: link = localTable.getTableCell(0, 0).getLinks()[0];
290: if (link == null) {
291: throw new IllegalStateException(
292: "No link was found to a local server in the 'Joram servers' page. ");
293: }
294: link.click();
295: wr = wc.getFrameContents(FRAME_CONTENT);
296:
297: // Go to Joram Users
298: link = wr.getFirstMatchingLink(
299: WebLink.MATCH_URL_STRING,
300: URL_JONASADMIN_JORAM_USERS);
301: link.click();
302: wr = wc.getFrameContents(FRAME_CONTENT);
303:
304: // Go to Create JMS User
305: link = wr.getFirstMatchingLink(
306: WebLink.MATCH_URL_STRING,
307: URL_JONASADMIN_JORAM_CREATE_USER);
308: link.click();
309: wr = wc.getFrameContents(FRAME_CONTENT);
310:
311: // Create a new user
312: createUser(wr, userName, "password");
313:
314: wr = wc.getFrameContents(FRAME_CONTENT);
315: String errorPage = wr.getText();
316: // Verify error because user already exists
317: assertTrue(
318: "The value of the body class is not 'errors'. ",
319: errorPage.indexOf("<body class=\"errors\">") != -1);
320: assertTrue(
321: "There is no the message: 'User ["
322: + userName
323: + "] already exists but with a different password' in the error page. ",
324: errorPage
325: .indexOf("User ["
326: + userName
327: + "] already exists but with a different password.") != -1);
328:
329: }
330: }
331:
332: }
333:
334: /**
335: * Test the user list :
336: * Create a local user and verify if the user is added to the local user list
337: * and verify the id of the user.
338: * @throws Exception if error occurs
339: *
340: */
341: public void testUserList() throws Exception {
342:
343: WebResponse wr;
344: WebLink link;
345: WebTable table;
346: /*
347: * To get some utils for WebTable
348: */
349: JonasAdminUtils utils = new JonasAdminUtils();
350:
351: // Disable errors of javascript
352: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
353: // Disable exception thrown on error status
354: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
355:
356: if (jProp.isResource()) {
357: // Joram platform is in jonas
358: if (isRarLoaded("joram_for_jonas_ra")) {
359:
360: // Find a name
361: String userName = "new_user_";
362: int userId = 0;
363: boolean found = false;
364: while (!found) {
365: if (!isUser(userName + userId)) {
366: userName = userName + userId;
367: found = true;
368: }
369: userId++;
370: }
371:
372: // Create Jms User
373: createLocalJmsUser(userName, userName);
374:
375: // Go to Joram Platform
376: wr = wc.getFrameContents(FRAME_TREE);
377: link = wr.getFirstMatchingLink(
378: WebLink.MATCH_URL_STRING, URL_JONASADMIN_JORAM);
379: link.click();
380: wr = wc.getFrameContents(FRAME_CONTENT);
381:
382: // Go to Joram Servers
383: link = wr.getFirstMatchingLink(
384: WebLink.MATCH_URL_STRING,
385: URL_JONASADMIN_JORAM_SERVERS);
386: link.click();
387: wr = wc.getFrameContents(FRAME_CONTENT);
388:
389: // Go to Local Server
390: WebTable localTable = utils.getTable(wr, 1);
391: link = localTable.getTableCell(0, 0).getLinks()[0];
392: if (link == null) {
393: throw new IllegalStateException(
394: "No link was found to a local server in the 'Joram servers' page. ");
395: }
396: link.click();
397: wr = wc.getFrameContents(FRAME_CONTENT);
398:
399: // Go to Joram Users
400: link = wr.getFirstMatchingLink(
401: WebLink.MATCH_URL_STRING,
402: URL_JONASADMIN_JORAM_USERS);
403: link.click();
404: wr = wc.getFrameContents(FRAME_CONTENT);
405:
406: // Verify user is in the list
407: table = utils.getTable(wr, 3);
408: int userRow = utils.getRow(userName, table).intValue();
409: if (userRow == -1) {
410: fail("'"
411: + userName
412: + "' was not found in the local user list. ");
413: }
414: String jmsUserId = getJmsUserId(userName);
415: assertEquals("", jmsUserId, table.getTableCell(userRow,
416: 1).getText());
417: }
418: }
419:
420: }
421:
422: /**
423: * User exists
424: * @param name the name of the user
425: * @return true if the user exists, false else
426: * @throws Exception if an error occurs
427: */
428: private boolean isUser(String name) throws Exception {
429: MBeanServerConnection server = getMBeanServer();
430: ObjectName on = ObjectName
431: .getInstance("joramClient:type=User,*");
432: QueryExp exp = Query.match(Query.attr("Name"), Query
433: .value(name));
434:
435: return !server.queryNames(on, exp).isEmpty();
436: }
437:
438: /**
439: * Get the id of the Jms User
440: * @param name the name of the user
441: * @return the id
442: * @throws Exception if an error occurs
443: */
444: private String getJmsUserId(String name) throws Exception {
445: MBeanServerConnection server = getMBeanServer();
446: ObjectName on = ObjectName
447: .getInstance("joramClient:type=User,*");
448: QueryExp exp = Query.match(Query.attr("Name"), Query
449: .value(name));
450:
451: ObjectName joramUserON = (ObjectName) (server.queryNames(on,
452: exp).iterator().next());
453: return (String) server.getAttribute(joramUserON, "ProxyId");
454: }
455:
456: /**
457: * Create a jms user
458: * @param wr the content frame in the 'New User' page
459: * @param name the name of the user
460: * @param password the password of the user
461: * @throws Exception if an error occurs
462: */
463: private void createUser(WebResponse wr, String name, String password)
464: throws Exception {
465: SubmitButton button;
466: WebForm form = wr.getForms()[0];
467: form.setParameter("password", password);
468: form.setParameter("name", name);
469:
470: // Submit
471: button = form.getSubmitButtons()[0];
472: button.click();
473: }
474:
475: /**
476: * Tear Down cleanUp action
477: * @throws Exception
478: */
479: public void tearDown() throws Exception {
480: super.tearDown();
481: }
482:
483: }
|