Author: konstantin.mishin
Date: 2009-02-24 11:50:16 -0500 (Tue, 24 Feb 2009)
New Revision: 12729
Modified:
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/A4JRepeatTestBean.java
trunk/test-applications/seleniumTest/richfaces/src/main/webapp/pages/ajaxRepeat/ajaxRepeatTest.xhtml
trunk/test-applications/seleniumTest/richfaces/src/test/java/org/richfaces/testng/AjaxRepeatTest.java
Log:
RF-6178
Modified:
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/A4JRepeatTestBean.java
===================================================================
---
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/A4JRepeatTestBean.java 2009-02-24
14:51:06 UTC (rev 12728)
+++
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/A4JRepeatTestBean.java 2009-02-24
16:50:16 UTC (rev 12729)
@@ -25,174 +25,145 @@
import java.util.List;
import java.util.Set;
-import org.ajax4jsf.component.UIRepeat;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.event.ActionEvent;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
/**
* a4j:repeat component test bean
* @author Alexandr Levkovsky
*
*/
-public class A4JRepeatTestBean {
+public class A4JRepeatTestBean implements Validator{
- private Integer first = 0;
+
+ public class Row {
+
+ private String cell1;
+ private String cell2;
+ private String cell3;
- private Integer rows = 5;
-
- private Integer visible = 0;
+ public Row(String index) {
+ this.cell1 = index + "A";
+ this.cell2 = index + "B";
+ this.cell3 = index + "C";
+ }
- private List<A4JRepeatItemTestBean> items;
+ public String getCell1() {
+ return cell1;
+ }
- private UIRepeat repeater;
+ public void setCell1(String cell1) {
+ this.cell1 = cell1;
+ }
- private Boolean rendered = true;
-
- private Set<Integer> keys = null;
+ public String getCell2() {
+ return cell2;
+ }
- /**
- * @return the first
- */
- public Integer getFirst() {
- return first;
- }
+ public void setCell2(String cell2) {
+ this.cell2 = cell2;
+ }
- /**
- * @param first the first to set
- */
- public void setFirst(Integer first) {
- this.first = first;
- }
+ public String getCell3() {
+ return cell3;
+ }
- /**
- * @return the rows
- */
- public Integer getRows() {
- return rows;
- }
+ public void setCell3(String cell3) {
+ this.cell3 = cell3;
+ }
+ }
- /**
- * @param rows the rows to set
- */
- public void setRows(Integer rows) {
- this.rows = rows;
- }
+ private List<Row> model;
+ private int rows;
+ private String trace;
+ private Set<Integer> ajaxKeys;
+
+ public A4JRepeatTestBean() {
+ init();
+ }
- /**
- * @return the items
- */
- public List<A4JRepeatItemTestBean> getItems() {
- if (items == null) {
- generateItems(getRows());
+ public void init() {
+ model = new ArrayList<Row>(10);
+ for (int i = 0; i < 10; i++) {
+ model.add(new Row(Integer.toString(i)));
+ }
+ rows = 0;
+ trace = "";
+ ajaxKeys = new HashSet<Integer>(10);
+ for (int i = 0; i < 10; i++) {
+ if (i != 2 && i != 6) {
+ ajaxKeys.add(i);
+ }
+ }
}
- return items;
- }
- /**
- * @param items the items to set
- */
- public void setItems(List<A4JRepeatItemTestBean> items) {
- this.items = items;
- }
+ public void submit(ActionEvent event) {
+ trace = event.getComponent().getClientId(FacesContext.getCurrentInstance());
+ }
- /**
- * @return the repeater
- */
- public UIRepeat getRepeater() {
- return repeater;
- }
+ public void reRender(ActionEvent event) {
+ for (Row element : model) {
+ element.setCell2("XXX");
+ }
+ }
- /**
- * @param repeater the repeater to set
- */
- public void setRepeater(UIRepeat repeater) {
- this.repeater = repeater;
- }
+ public void setModel(List<Row> model) {
+ this.model = model;
+ }
- /**
- * @return the rendered
- */
- public Boolean getRendered() {
- return rendered;
- }
+ public List<Row> getModel() {
+ return model;
+ }
- /**
- * @param rendered the rendered to set
- */
- public void setRendered(Boolean rendered) {
- this.rendered = rendered;
- }
-
-
-
- /**
- * @return the visible
- */
- public Integer getVisible() {
- return visible;
- }
+ public void setRows(int rows) {
+ this.rows = rows;
+ }
- /**
- * @param visible the visible to set
- */
- public void setVisible(Integer visible) {
- this.visible = visible;
- }
+ public int getRows() {
+ return rows;
+ }
- private void generateItems(Integer rows) {
- items = new ArrayList<A4JRepeatItemTestBean>(rows);
- for (int i = 0; i < rows; i++) {
- A4JRepeatItemTestBean item = new A4JRepeatItemTestBean(i);
- items.add(item);
+ public void setTrace(String trace) {
+ this.trace = trace;
+ }
+ public String getTrace() {
+ return trace;
}
- }
-
- public String init(){
- generateItems(getRows());
- return null;
- }
-
- public String reset(){
- setRows(5);
- setRendered(true);
- setVisible(0);
- setFirst(0);
- generateItems(getRows());
- return null;
- }
- /**
- * @return the keys
- */
- public Set<Integer> getKeys() {
- return keys;
- }
+ public void validate(FacesContext context, UIComponent component,
+ Object value) throws ValidatorException {
+ if ("fail".equals(value)) {
+ throw new ValidatorException(new FacesMessage("validation failure"));
+ }
+ }
- /**
- * @param keys the keys to set
- */
- public void setKeys(Set<Integer> keys) {
- this.keys = keys;
- }
-
- public String change() {
- Set<Integer> keys = new HashSet<Integer>();
- int rowKey = getRepeater().getRowIndex();
- keys.add(rowKey);
- setKeys(keys);
- for (A4JRepeatItemTestBean item : items) {
- item.setNotRerenderedChangeCounter(item.getNotRerenderedChangeCounter() + 1);
- item.setRerenderedChangeCounter(item.getRerenderedChangeCounter() + 1);
+ public void setAjaxKeys(Set<Integer> ajaxKeys) {
+ this.ajaxKeys = ajaxKeys;
}
- return null;
- }
- public String changeWhithoutAjaxKeys() {
- setKeys(null);
- for (A4JRepeatItemTestBean item : items) {
- item.setNotRerenderedChangeCounter(item.getNotRerenderedChangeCounter() + 1);
- item.setRerenderedChangeCounter(item.getRerenderedChangeCounter() + 1);
+ public Set<Integer> getAjaxKeys() {
+ return ajaxKeys;
}
- return null;
- }
+ public Converter getRowKeyConverter() {
+ return new Converter(){
+
+ public Object getAsObject(FacesContext context,
+ UIComponent component, String value) {
+ return new Integer(value.substring(2));
+ }
+
+ public String getAsString(FacesContext context,
+ UIComponent component, Object value) {
+ return "c_" + value.toString();
+ }
+
+ };
+ }
}
Modified:
trunk/test-applications/seleniumTest/richfaces/src/main/webapp/pages/ajaxRepeat/ajaxRepeatTest.xhtml
===================================================================
(Binary files differ)
Modified:
trunk/test-applications/seleniumTest/richfaces/src/test/java/org/richfaces/testng/AjaxRepeatTest.java
===================================================================
---
trunk/test-applications/seleniumTest/richfaces/src/test/java/org/richfaces/testng/AjaxRepeatTest.java 2009-02-24
14:51:06 UTC (rev 12728)
+++
trunk/test-applications/seleniumTest/richfaces/src/test/java/org/richfaces/testng/AjaxRepeatTest.java 2009-02-24
16:50:16 UTC (rev 12729)
@@ -20,6 +20,8 @@
*/
package org.richfaces.testng;
+import java.util.Arrays;
+
import org.ajax4jsf.template.Template;
import org.richfaces.SeleniumTestBase;
import org.testng.Assert;
@@ -33,261 +35,135 @@
*/
public class AjaxRepeatTest extends SeleniumTestBase {
- private final static Integer DEFAULT_ROWS_COUNT = 5;
+ private String rows;
- private final static String FORM1_ID = "form1:";
+ private String panelGroup;
- private final static String FORM2_ID = "form2:";
+ private String repeat;
- private final static String FIRST_INPUT_ID = "first_input";
+ private String reRender;
- private final static String ROWS_INPUT_ID = "rows_input";
+ private String outputText;
+
+ private String dataTable;
- private final static String RESET_BUTTON_ID = "reset";
+ private void init(Template template) {
+ renderPage(null, template, "#{a4jRepeatBean.init}");
+ String attrForm = getParentId() + "attrForm";
+ rows = attrForm + ":rows";
+ String mainForm = getParentId() + "mainForm";
+ panelGroup = mainForm + ":panelGroup";
+ repeat = mainForm + ":repeat";
+ reRender = mainForm + ":reRender";
+ outputText = getParentId() + "outputText";
+ dataTable = getParentId() + "dataTable";
+ }
- private final static String ROWS_COUNT_INPUT_ID = "rows_count_input";
+ /**
+ * items from collection defined as value attribute are output to the client;
+ * number of items is limited using elements attribute and not
+ */
+ @Test
+ public void testStructure(Template template) {
+ init(template);
+ chekStructure();
+ selenium.type(rows, "3");
+ waitForAjaxCompletion();
+ Assert.assertEquals(selenium.getXpathCount("id('"+ panelGroup +
"')/div"), 3);
+ chekStructure();
+ }
+
+ /**
+ * nested input and command components work correctly
+ * for the cases of validation failure or not
+ */
+ @Test
+ public void testComponentsProcessingWithValidation(Template template) {
+ init(template);
+ String liLocator = "xpath=id('"+ panelGroup + "')/div[";
+ String inputLocator = liLocator + "6]/input[";
+ Assert.assertEquals(selenium.getText(outputText), "");
+ selenium.click(inputLocator + "2]");
+ waitForPageToLoad();
+ Assert.assertTrue(selenium.getText(outputText).endsWith("5:submit"));
+ selenium.click(inputLocator + "3]");
+ waitForAjaxCompletion();
+ Assert.assertTrue(selenium.getText(outputText).endsWith("5:ajaxSubmit"));
+ selenium.click(inputLocator + "4]");
+ waitForAjaxCompletion();
+ Assert.assertTrue(selenium.getText(outputText).endsWith("5:ajaxSingleSubmit"));
+ selenium.type(inputLocator + "1]", "abc5");
+ selenium.click(inputLocator + "3]");
+ waitForAjaxCompletion();
+ chekStructure();
+ selenium.type(inputLocator + "1]", "fail");
+ selenium.type(liLocator + "3]/input[1]", "fail");
+ selenium.click(inputLocator + "3]");
+ waitForAjaxCompletion();
+ chekStructure(3, 6);
+ }
- private final static String TABLE_ID = "repeat_table";
+ /**
+ * rows defined by ajaxKeys attribute are re-rendered correctly
+ */
+ @Test
+ public void testAjaxKeys(Template template) {
+ init(template);
+ chekStructure();
+ clickAjaxCommandAndWait(reRender);
+ chekStructure(3, 7);
+ }
- private final static String REPEAT_ID = "repeat";
-
- private final static String INPUT_ID = "input";
-
- private final static String OUTPUT_ID = "output";
-
- private final static String APPLY_BUTTON_ID = "apply";
-
- private final static String RENDERED_CHECKBOX_ID = "rendered_checkbox";
-
- private final static String LINK_ID = "link";
-
- private final static String BUTTON_ID = "button";
-
- private final static String RERENDERED_COUNTER_ID =
"rerendered_change_counter";
-
- private final static String NOT_RERENDERED_COUNTER_ID =
"not_rerendered_change_counter";
-
- private final static String INPUT_VALUE_OUTPUT_ID = "input_output";
-
- @Override
- public String getTestUrl() {
- return "pages/ajaxRepeat/ajaxRepeatTest.xhtml";
- }
-
- @Test
- public void testRerenderingRowsWithAjaxKeysAttribute(Template template) throws
Exception {
- renderPage(template);
- writeStatus("Testing rerendering rows with ajaxKeys attribute...");
- // link action update counter fields in all rows but add only current
- // row number to ajaxKeys so only current row counter should be
- // rerendered
- clickReset();
- AssertRendered(TABLE_ID);
- assertRowsCount(DEFAULT_ROWS_COUNT, TABLE_ID);
-
- for (int i = 0; i < DEFAULT_ROWS_COUNT; i++) {
- for (int j = 0; j < DEFAULT_ROWS_COUNT; j++) {
- if (j < i) {
- assertOutputText(j, RERENDERED_COUNTER_ID, new Integer(j + 1).toString());
- } else {
- assertOutputText(j, RERENDERED_COUNTER_ID, "0");
+ /**
+ * components using rowKeyVar and stateVar variables are correctly output to the
client
+ */
+ @Test
+ public void testVars(Template template) {
+ init(template);
+ Assert.assertEquals(selenium.getText(repeat + ":c_2:first"), "0",
"Attribute 'stateVar' works wrong.");
+ Assert.assertEquals(selenium.getText(repeat + ":c_2:rows"), "0",
"Attribute 'stateVar' works wrong.");
+ int rows = selenium.getXpathCount("id('"+ repeat +
"')/div").intValue();
+ for (int i = 0; i < rows; i++) {
+ Assert.assertEquals(selenium.getText(repeat + ":c_" + i +
":rowKeyVar"), Integer.toString(i));
}
- assertOutputText(j, NOT_RERENDERED_COUNTER_ID, "0");
- }
-
- writeStatus("click link " + i);
- String linkId = getFullTableElementId(i, LINK_ID);
- clickAjaxCommandAndWait(linkId);
-
- for (int j = 0; j < DEFAULT_ROWS_COUNT; j++) {
- if (j < i) {
- assertOutputText(j, RERENDERED_COUNTER_ID, new Integer(j + 1).toString());
- } else if (j == i) {
- assertOutputText(j, RERENDERED_COUNTER_ID, new Integer(j + 1).toString());
- } else {
- assertOutputText(j, RERENDERED_COUNTER_ID, "0");
- }
- assertOutputText(i, NOT_RERENDERED_COUNTER_ID, "0");
- }
}
- }
- @Test
- public void testRerenderingRowsWithoutAjaxKeysAttribute(Template template) throws
Exception {
- renderPage(template);
- writeStatus("Testing rerendering rows without ajaxKeys attribute...");
- // button action update counter fields in all rows and sets ajaxKeys to
- // null so counters in all rows should be rerendered.
- clickReset();
- AssertRendered(TABLE_ID);
- assertRowsCount(DEFAULT_ROWS_COUNT, TABLE_ID);
-
- for (int i = 0; i < DEFAULT_ROWS_COUNT; i++) {
- for (int j = 0; j < DEFAULT_ROWS_COUNT; j++) {
- assertOutputText(j, RERENDERED_COUNTER_ID, new Integer(i).toString());
- assertOutputText(j, NOT_RERENDERED_COUNTER_ID, "0");
- }
-
- writeStatus("click button " + i);
- String buttonId = getFullTableElementId(i, BUTTON_ID);
- clickAjaxCommandAndWait(buttonId);
-
- for (int j = 0; j < DEFAULT_ROWS_COUNT; j++) {
- assertOutputText(j, RERENDERED_COUNTER_ID, new Integer(i + 1).toString());
- assertOutputText(j, NOT_RERENDERED_COUNTER_ID, "0");
- }
+ /**
+ * componentState attribute stores nested components state across requests
+ */
+ @Test
+ public void testComponentState(Template template) {
+ init(template);
+ Assert.assertEquals(selenium.getText(repeat + ":c_2:firstState"),
"0", "Attribute 'componentState' works wrong.");
+ Assert.assertEquals(selenium.getText(repeat + ":c_2:rowsState"),
"0", "Attribute 'componentState' works wrong.");
}
- }
- @Test
- public void testRerenderingRowsWithValidation(Template template) throws Exception {
- renderPage(template);
- writeStatus("Testing rerendering rows with validation...");
- clickReset();
- AssertRendered(TABLE_ID);
- assertRowsCount(DEFAULT_ROWS_COUNT, TABLE_ID);
-
- for (Integer i = 0; i < DEFAULT_ROWS_COUNT; i++) {
- assertInputValue(i, INPUT_ID, i.toString());
- assertOutputText(i, INPUT_VALUE_OUTPUT_ID, i.toString());
- assertOutputText(i, RERENDERED_COUNTER_ID, "0");
-
- writeStatus("set not valid value for row " + i + " input...");
- type(getFullTableElementId(i, INPUT_ID), "200");
- writeStatus("click link " + i);
- String linkId = getFullTableElementId(i, LINK_ID);
- clickAjaxCommandAndWait(linkId);
-
- assertInputValue(i, INPUT_ID, "200");
- assertOutputText(i, INPUT_VALUE_OUTPUT_ID, i.toString());
- assertOutputText(i, RERENDERED_COUNTER_ID, "0");
-
- writeStatus("set valid value for row " + i + " input...");
- type(getFullTableElementId(i, INPUT_ID), "99");
- writeStatus("click link " + i);
- clickAjaxCommandAndWait(linkId);
-
- assertInputValue(i, INPUT_ID, "99");
- assertOutputText(i, INPUT_VALUE_OUTPUT_ID, "99");
- assertOutputText(i, RERENDERED_COUNTER_ID, new Integer(i + 1).toString());
+ /**
+ * rowKeyConverter outputs correct client ids
+ */
+ @Test
+ public void testRowKeyConverter(Template template) {
+ init(template);
+ Assert.assertTrue(selenium.getAttribute("xpath=id('"+ panelGroup +
"')/div[3]/input[1]@id").indexOf(":c_2") != -1, "Attribute
'rowKeyConverter' works wrong.");
}
- }
- @Test
- public void testRenderedAttribute(Template template) throws Exception {
- renderPage(template);
- writeStatus("Testing rendered attribute...");
- clickReset();
- AssertRendered(TABLE_ID);
- // repeat has rendered=true and should present on page as table with
- // default 5 rows
- assertRowsCount(5, TABLE_ID);
-
- writeStatus("change rendered attribute to false");
- // change rendered attribute to false
- String checkboxId = getParentId() + FORM1_ID + RENDERED_CHECKBOX_ID;
- selenium.click(checkboxId);
- writeStatus("Click apply");
- clickApply();
-
- // repeat now has rendered=false and should not present on page as table
- // with default 0 rows
- assertRowsCount(0, TABLE_ID);
- }
-
- @Test
- public void testFirstAttribute(Template template) throws Exception {
- renderPage(template);
- writeStatus("Testing first attribute...");
- clickReset();
- AssertRendered(TABLE_ID);
- // repeat has rendered=true and should present on page as table with
- // default 5 rows
- assertRowsCount(5, TABLE_ID);
- // first row default value 0
- String fullInput0Id = getParentId() + FORM2_ID + REPEAT_ID + ":" + 0 +
":" + OUTPUT_ID;
- String fullInput1Id = getParentId() + FORM2_ID + REPEAT_ID + ":" + 1 +
":" + OUTPUT_ID;
- AssertPresent(fullInput0Id);
- AssertPresent(fullInput1Id);
- writeStatus("change first attribute to 1");
-
- String inputId = getParentId() + FORM1_ID + FIRST_INPUT_ID;
- type(inputId, "1");
- writeStatus("Click apply");
- clickApply();
-
- // now first row value should be 1
- AssertNotPresent(fullInput0Id);
- AssertPresent(fullInput1Id);
- }
-
- @Test
- public void testRowsAttribute(Template template) throws Exception {
- renderPage(template);
- writeStatus("Testing rows attribute...");
- clickReset();
- AssertRendered(TABLE_ID);
- // repeat has rows=0 and table with all default 5 rows should present on
- // page
- assertRowsCount(5, TABLE_ID);
-
- writeStatus("change rendered attribute to false");
- // change rows attribute to 3
- String inputId = getParentId() + FORM1_ID + ROWS_INPUT_ID;
- type(inputId, "3");
- writeStatus("Click apply");
- clickApply();
-
- // repeat now has rendered=false and should not present on page as table
- // with default 0 rows
- assertRowsCount(3, TABLE_ID);
- // last row should have value 2
- assertInputValue(2, INPUT_ID, "2");
- }
-
- @Test
- public void testRowKeyVarAttribute(Template template) throws Exception {
- renderPage(template);
- writeStatus("Testing rowKeyVar attribute...");
- clickReset();
- AssertRendered(TABLE_ID);
- String inputId = getParentId() + FORM1_ID + ROWS_COUNT_INPUT_ID;
- Integer rowsCount = new Integer(getValueById(inputId));
- // repeat has rows=0 and table with all default 5 rows should present on
- // page
- assertRowsCount(rowsCount, TABLE_ID);
-
- // output text has value="#{rowKey % 2 == 0 ? '0' : '1'}"
- for (int i = 0; i < rowsCount; i++) {
- assertOutputText(i, OUTPUT_ID, i % 2 == 0 ? "0" : "1");
+ private void chekStructure(int ... notEqualRows) {
+ String dataTableRowLocator = "id('"+ dataTable + "')/div";
+ int count = selenium.getXpathCount(dataTableRowLocator).intValue();
+ for (int i = 1; i <= count; i++) {
+ if (Arrays.binarySearch(notEqualRows, i) < 0) {
+ Assert.assertEquals(selenium.getValue("xpath=id('"+ repeat +
"')/div[" + i + "]/input"),
+ selenium.getText("xpath=" + dataTableRowLocator + "[" + i +
"]"));
+ } else {
+ Assert.assertFalse(selenium.
+ getValue("xpath=id('"+ repeat + "')/div[" + i +
"]/input").
+ equals(selenium.getText("xpath=" + dataTableRowLocator + "[" +
i + "]")));
+ }
+ }
}
- }
- private void clickReset() {
- String buttonId = getParentId() + FORM2_ID + RESET_BUTTON_ID;
- writeStatus("Click reset button");
- clickCommandAndWait(buttonId);
+ @Override
+ public String getTestUrl() {
+ return "pages/ajaxRepeat/ajaxRepeatTest.xhtml";
}
-
- private void clickApply() {
- String buttonId = getParentId() + FORM1_ID + APPLY_BUTTON_ID;
- clickCommandAndWait(buttonId);
- }
-
- private void assertInputValue(int row, String inputId, String value) {
- String fullInputId = getFullTableElementId(row, inputId);
- String elementValue = getValueById(fullInputId);
- Assert.assertEquals(elementValue, value);
- }
-
- private void assertOutputText(int row, String outputId, String value) {
- String fullInputId = getFullTableElementId(row, outputId);
- String elementValue = getTextById(fullInputId);
- Assert.assertEquals(elementValue, value);
- }
-
- private String getFullTableElementId(int row, String elementId) {
- return getParentId() + FORM2_ID + REPEAT_ID + ":" + row + ":" +
elementId;
- }
-
}