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_JonasAdminServiceSecurity.java 7381 2005-09-14 16:00:02Z kemlerp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jonasadmin.test.service.security;
025:
026: import java.io.File;
027:
028: import junit.framework.TestSuite;
029:
030: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
031: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
032: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminUtils;
033:
034: import com.meterware.httpunit.HttpUnitOptions;
035: import com.meterware.httpunit.WebLink;
036: import com.meterware.httpunit.WebResponse;
037: import com.meterware.httpunit.WebTable;
038:
039: /**
040: * Class for testing security service
041: * @author Paul Kemler
042: *
043: */
044: public class F_JonasAdminServiceSecurity extends JonasAdminTestCase {
045:
046: /**
047: * URL of security service
048: */
049: private static final String URL_JONASADMIN_SECURITY = "EditServiceSecurity.do";
050:
051: /**
052: * number of tabs when you are in security
053: */
054: private static final int NUMBER_OF_TABS_FOR_SECURITY = 1;
055:
056: /**
057: * Constructor with a specified name
058: * @param s name
059: */
060: public F_JonasAdminServiceSecurity(String s) {
061: super (s, URL_JONASADMIN);
062: }
063:
064: /**
065: * Main method
066: * @param args the arguments
067: */
068: public static void main(String[] args) {
069:
070: String testtorun = null;
071: // Get args
072: for (int argn = 0; argn < args.length; argn++) {
073: String sArg = args[argn];
074: if (sArg.equals("-n")) {
075: testtorun = args[++argn];
076: }
077: }
078: if (testtorun == null) {
079: junit.textui.TestRunner.run(suite());
080: } else {
081: junit.textui.TestRunner
082: .run(new F_JonasAdminServiceSecurity(testtorun));
083: }
084: }
085:
086: /**
087: * Get a new TestSuite for this class
088: * @return a new TestSuite for this class
089: */
090: public static TestSuite suite() {
091: return new TestSuite(F_JonasAdminServiceSecurity.class);
092: }
093:
094: /**
095: * Setup need for these tests
096: * jonasAdmin is required
097: * @throws Exception if it fails
098: */
099: protected void setUp() throws Exception {
100: super .setUp();
101:
102: if (wc.getCurrentPage().getURL() == null) {
103: useWar("jonasAdmin");
104: // login to jonas admin
105: try {
106: JonasAdminAuth.doValidAuth(wc, url);
107: } catch (Exception e) {
108: fail("authentification failed : " + e);
109: }
110: } else {
111: // if there was an error, the connection must be restablished
112: try {
113: wc.getFrameContents(FRAME_TREE);
114: } catch (Exception e) {
115: wc.getResponse(urlLogOut);
116: // login to jonas admin
117: try {
118: JonasAdminAuth.doValidAuth(wc, url);
119: } catch (Exception auth) {
120: fail("authentification failed : " + auth);
121: }
122: }
123: }
124: }
125:
126: /**
127: * Test realms table
128: * @throws Exception if error occurs
129: *
130: */
131: public void testRealms() throws Exception {
132:
133: WebResponse wr;
134: WebLink link;
135: WebTable table;
136: WebTable tabTable;
137: int selectedTab;
138: String name;
139: String type;
140: String path;
141: boolean used;
142: JonasAdminUtils utils = new JonasAdminUtils();
143:
144: // Disable errors of javascript
145: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
146: // Disable exception thrown on error status
147: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
148:
149: // Test only if the security service is enabled
150: if (jProp.isSecurity()) {
151:
152: // Go to security service
153: wr = wc.getFrameContents(FRAME_TREE);
154: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
155: URL_JONASADMIN_SECURITY);
156: link.click();
157: wr = wc.getFrameContents(FRAME_CONTENT);
158: selectedTab = 1;
159:
160: // Verify 'Realms'
161: path = File.separator + "jonasAdmin";
162: name = "memrlm_1";
163: type = "memory";
164: used = true;
165: try {
166: table = utils.getTable(wr, 0);
167: int rowJonasAdmin = utils.getFirstRow(path, table, 2)
168: .intValue();
169: if (rowJonasAdmin != -1) {
170: testRealmRow(table, rowJonasAdmin, name, type,
171: path, used);
172: }
173: } catch (Exception e) {
174: if (jProp.isCatalina()) {
175: fail("The realm security table is not found. ");
176: } else {
177: throw new IllegalStateException(
178: "Not implemented: JETTY. ");
179: }
180: }
181:
182: // "Olstore": type=datasource and not used
183: if (jProp.isEar()) {
184: useEar("olstore");
185:
186: path = File.separator + "olstore";
187: name = "olstorerlm_1";
188: type = "datasource";
189: used = false;
190: wr = wc.getFrameContents(FRAME_TREE);
191: link = wr.getFirstMatchingLink(
192: WebLink.MATCH_URL_STRING,
193: URL_JONASADMIN_SECURITY);
194: link.click();
195: wr = wc.getFrameContents(FRAME_CONTENT);
196: table = utils.getTable(wr, 0);
197: int rowDatasourceType = utils.getFirstRow(path, table,
198: 2).intValue();
199: if (rowDatasourceType != -1) {
200: testRealmRow(table, rowDatasourceType, name, type,
201: path, used);
202: } else {
203: if (jProp.isCatalina()) {
204: fail("The realm security for the context path '"
205: + path + "' is not found. ");
206: } else {
207: throw new IllegalStateException(
208: "Not implemented: JETTY. ");
209: }
210: }
211: }
212:
213: // Verify there is 1 tab
214: tabTable = utils.getTabTable(wr);
215: testTabs(tabTable, NUMBER_OF_TABS_FOR_SECURITY,
216: selectedTab, "Problem in 'Realms' tab.");
217: }
218: }
219:
220: /**
221: * Test infos of a realm row
222: * @param table the table of realms
223: * @param row the row to test
224: * @param name the value of the name
225: * @param type the value of the type
226: * @param path the value of the path
227: * @param used the value of 'used'
228: */
229: private void testRealmRow(WebTable table, int row, String name,
230: String type, String path, boolean used) {
231: // Verify Name
232: assertEquals("It is not the name for the context path '" + path
233: + "' . ", name, table.getTableCell(row, 0).getText());
234: // Verify Type
235: assertEquals("It is not the type for the context path '" + path
236: + "' . ", type, table.getTableCell(row, 1).getText());
237: // Verify Used
238: if (used) {
239: assertEquals(
240: "It is not the value of 'used' for the context path '"
241: + path + "' . ", used, table.getTableCell(
242: row, 3).getImages()[0].getSource()
243: .endsWith("images/icon/tag_blue.gif"));
244: } else {
245: assertEquals(
246: "It is not the value of 'used' for the context path '"
247: + path + "' . ", 0, table.getTableCell(row,
248: 3).getImages().length);
249: }
250: }
251:
252: /**
253: * Tear Down cleanUp action
254: * @throws Exception if an error occurs
255: */
256: public void tearDown() throws Exception {
257: super.tearDown();
258: }
259:
260: }
|