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: JonasAdminUtils.java 7196 2005-08-10 14:36:20Z kemlerp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jonasadmin.test.util;
025:
026: import org.xml.sax.SAXException;
027:
028: import com.meterware.httpunit.TableCell;
029: import com.meterware.httpunit.WebResponse;
030: import com.meterware.httpunit.WebTable;
031:
032: /**
033: * Utils
034: * @author kemlerp
035: *
036: */
037: public class JonasAdminUtils {
038:
039: /**
040: * A class which represents the row and the column of a cell
041: * @author kemlerp
042: *
043: */
044: public class CoordCell {
045:
046: /**
047: * Row of the cell
048: */
049: private Integer row = null;
050:
051: /**
052: * Column of the cell
053: */
054: private Integer column = null;
055:
056: /**
057: * Constructor
058: * @param row a positive integer
059: * @param column a positive integer
060: */
061: public CoordCell(Integer row, Integer column) {
062: this .row = row;
063: this .column = column;
064: }
065:
066: /**
067: * Get column
068: * @return column number
069: */
070: public Integer getColumn() {
071: return column;
072: }
073:
074: /**
075: * Set column
076: * @param column a positive integer
077: */
078: public void setColumn(Integer column) {
079: this .column = column;
080: }
081:
082: /**
083: * Get row
084: * @return row number
085: */
086: public Integer getRow() {
087: return row;
088: }
089:
090: /**
091: * Set row
092: * @param row a positive integer
093: */
094: public void setRow(Integer row) {
095: this .row = row;
096: }
097: }
098:
099: /**
100: * Get the last row of table where text is found in the first column
101: * @param text title of the row
102: * @param table the table which contains text
103: * @return -1 if the text was not found in the first column else an integer
104: */
105: public Integer getRow(String text, WebTable table) {
106: Integer row = new Integer(-1);
107: TableCell cell;
108: for (int i = 0; i < table.getRowCount(); i++) {
109: cell = table.getTableCell(i, 0);
110: if (cell.getText().indexOf(text) != -1) {
111: row = new Integer(i);
112: }
113: }
114: return row;
115: }
116:
117: /**
118: * Get the first row of table where text is found in the given column
119: * @param text title of the row
120: * @param table the table which contains text
121: * @param column the column
122: * @return -1 if the text was not found in the first column else an integer
123: */
124: public Integer getFirstRow(String text, WebTable table, int column) {
125: boolean found = false;
126: Integer row = new Integer(-1);
127: TableCell cell;
128: int i = 0;
129: while (i < table.getRowCount() && !found) {
130: cell = table.getTableCell(i, column);
131: if (cell != null && cell.getText().indexOf(text) != -1) {
132: row = new Integer(i);
133: found = true;
134: }
135: i++;
136: }
137: return row;
138: }
139:
140: /**
141: * Get the last row of table where text is found in the given column
142: * @param text title of the row
143: * @param table the table which contains text
144: * @param column the column
145: * @return -1 if the text was not found in the first column else an integer
146: */
147: public Integer getRow(String text, WebTable table, int column) {
148: Integer row = new Integer(-1);
149: TableCell cell;
150: for (int i = 0; i < table.getRowCount(); i++) {
151: cell = table.getTableCell(i, column);
152: if (cell.getText().indexOf(text) != -1) {
153: row = new Integer(i);
154: }
155: }
156: return row;
157: }
158:
159: /**
160: * Get the last column of table where text is found in the first row
161: * @param text title of the column
162: * @param table the table which contains text
163: * @return -1 if the text was not found in the first row else an integer
164: */
165: public Integer getColumn(String text, WebTable table) {
166: Integer column = new Integer(-1);
167: TableCell cell;
168: for (int i = 0; i < table.getColumnCount(); i++) {
169: cell = table.getTableCell(0, i);
170: if (cell.getText().indexOf(text) != -1) {
171: column = new Integer(i);
172: }
173: }
174: return column;
175: }
176:
177: /**
178: * Get the row and the column of the cell which contains selected item
179: * @param table tree Table
180: * @return Null if no item is selected, else the row and the column
181: */
182: public CoordCell getSelectedItemRow(WebTable table) {
183: Integer row = null;
184: Integer column = null;
185: TableCell cell = null;
186: CoordCell coord = null;
187: String attribut;
188: boolean found = false;
189: int i = 0;
190: int j = 0;
191:
192: while (i < table.getRowCount() && !found) {
193: j = 0;
194: while (j < table.getColumnCount() && !found) {
195: cell = table.getTableCell(i, j);
196: if (cell.getElementsWithAttribute("class",
197: "tree-control-selected").length == 1) {
198: found = true;
199: row = new Integer(i);
200: column = new Integer(j);
201: coord = new CoordCell(row, column);
202: }
203: j++;
204: }
205: i++;
206: }
207: return coord;
208: }
209:
210: /**
211: * Get table num
212: * @param contentFrame the content frame
213: * @param num integer between 0 and number of tables in the cell
214: * @return table
215: * @throws SAXException if a table or a cell doesn't match.
216: */
217: public WebTable getTable(WebResponse contentFrame, int num)
218: throws SAXException {
219: WebTable table = contentFrame.getTables()[1];
220: TableCell cell = table.getTableCell(1, 0);
221: table = cell.getTables()[0];
222: cell = table.getTableCell(0, 0);
223: table = cell.getTables()[0];
224: cell = table.getTableCell(0, 0);
225: // Get the (num+1)th table
226: table = cell.getTables()[num];
227: return table;
228: }
229:
230: /**
231: * Get table of tabs
232: * @param contentFrame the content frame
233: * @return table of tabs
234: * @throws SAXException if a table or a cell doesn't match.
235: */
236: public WebTable getTabTable(WebResponse contentFrame)
237: throws SAXException {
238: WebTable table = contentFrame.getTables()[1];
239: TableCell cell = table.getTableCell(0, 0);
240: table = cell.getTables()[0];
241: return table;
242: }
243: }
|