Author: lfryc(a)redhat.com
Date: 2010-09-22 03:41:46 -0400 (Wed, 22 Sep 2010)
New Revision: 19306
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/RepeatAttributes.java
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/SimpleModel.java
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/TestSimple.java
Modified:
modules/tests/metamer/trunk/application/src/main/webapp/components/a4jRepeat/simple.xhtml
Log:
initial tests for a4j:repeat - simple sample (RFPL-819)
Modified:
modules/tests/metamer/trunk/application/src/main/webapp/components/a4jRepeat/simple.xhtml
===================================================================
---
modules/tests/metamer/trunk/application/src/main/webapp/components/a4jRepeat/simple.xhtml 2010-09-22
06:45:55 UTC (rev 19305)
+++
modules/tests/metamer/trunk/application/src/main/webapp/components/a4jRepeat/simple.xhtml 2010-09-22
07:41:46 UTC (rev 19306)
@@ -59,20 +59,23 @@
rows="#{a4jRepeatBean.attributes['rows'].value}"
value="#{a4jRepeatBean.dataList}"
var="item">
- <li><h:inputText value="#{item.text}" />
+ <li>
+ <h:inputText value="#{item.text}" />
<h:commandLink value="Link">
<f:ajax render="@form" execute="@form"
/>
<f:setPropertyActionListener
target="#{a4jRepeatBean.selectedDataItem}" value="#{item}" />
</h:commandLink>
- <h:outputText id="statusBegin"
value="begin=#{status.begin}, "/>
- <h:outputText id="statusEnd"
value="end=#{status.end}, "/>
- <h:outputText id="statusIndex"
value="index=#{status.index}, "/>
- <h:outputText id="statusCount"
value="count=#{status.count}, "/>
- <h:outputText id="statusFirst"
value="first=#{status.first}, "/>
- <h:outputText id="statusLast"
value="last=#{status.last}, "/>
- <h:outputText id="statusEven"
value="even=#{status.even}, "/>
- <h:outputText id="statusRowCount"
value="rowCount=#{status.rowCount}"/>
+ <a4j:outputPanel styleClass="statuses">
+ <h:outputText id="statusBegin"
value="begin=#{status.begin}, "/>
+ <h:outputText id="statusEnd"
value="end=#{status.end}, "/>
+ <h:outputText id="statusIndex"
value="index=#{status.index}, "/>
+ <h:outputText id="statusCount"
value="count=#{status.count}, "/>
+ <h:outputText id="statusFirst"
value="first=#{status.first}, "/>
+ <h:outputText id="statusLast"
value="last=#{status.last}, "/>
+ <h:outputText id="statusEven"
value="even=#{status.even}, "/>
+ <h:outputText id="statusRowCount"
value="rowCount=#{status.rowCount}"/>
+ </a4j:outputPanel>
</li>
</a4j:repeat>
</ul>
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/RepeatAttributes.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/RepeatAttributes.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/RepeatAttributes.java 2010-09-22
07:41:46 UTC (rev 19306)
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer.ftest.a4jRepeat;
+
+import org.jboss.test.selenium.locator.JQueryLocator;
+import org.jboss.test.selenium.locator.ExtendedLocator;
+import org.richfaces.tests.metamer.ftest.AbstractComponentAttributes;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class RepeatAttributes extends AbstractComponentAttributes {
+
+ public RepeatAttributes() {
+ }
+
+ public RepeatAttributes(ExtendedLocator<JQueryLocator> root) {
+ super(root);
+ }
+
+ public void setFirst(int first) {
+ setProperty("first", first);
+ }
+
+ public void setRendered(boolean rendered) {
+ setProperty("rendered", rendered);
+ }
+
+ public void setRows(int rows) {
+ setProperty("rows", rows);
+ }
+}
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/SimpleModel.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/SimpleModel.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/SimpleModel.java 2010-09-22
07:41:46 UTC (rev 19306)
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer.ftest.a4jRepeat;
+
+import static org.jboss.test.selenium.locator.LocatorFactory.jq;
+import static org.jboss.test.selenium.utils.text.SimplifiedFormat.format;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.jboss.test.selenium.framework.AjaxSelenium;
+import org.jboss.test.selenium.framework.AjaxSeleniumProxy;
+import org.jboss.test.selenium.locator.JQueryLocator;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class SimpleModel {
+
+ private AjaxSelenium selenium = AjaxSeleniumProxy.getInstance();
+
+ JQueryLocator row = jq("#list li");
+ JQueryLocator statuses = jq("span.statuses");
+
+ Map<Integer, String> texts = new HashMap<Integer, String>();
+
+ public boolean isRendered() {
+ return selenium.isElementPresent(row);
+ }
+
+ public int getTotalRowCount() {
+ return selenium.getCount(row);
+ }
+
+ public int getBegin(int position) {
+ return Integer.valueOf(getValue("begin", position));
+ }
+
+ public int getEnd(int position) {
+ return Integer.valueOf(getValue("end", position));
+ }
+
+ public int getIndex(int position) {
+ return Integer.valueOf(getValue("index", position));
+ }
+
+ public int getCount(int position) {
+ return Integer.valueOf(getValue("count", position));
+ }
+
+ public boolean isFirst(int position) {
+ return Boolean.valueOf(getValue("first", position));
+ }
+
+ public boolean isLast(int position) {
+ return Boolean.valueOf(getValue("last", position));
+ }
+
+ public boolean isEven(int position) {
+ return Boolean.valueOf(getValue("even", position));
+ }
+
+ public int getRowCount(int position) {
+ return Integer.valueOf(getValue("rowCount", position));
+ }
+
+ private String getValue(String name, int position) {
+ String obtained;
+ if (texts.containsKey(position)) {
+ obtained = texts.get(position);
+ } else {
+ JQueryLocator locator = getRowOnPosition(position).getDescendant(statuses);
+ obtained = selenium.getText(locator);
+ texts.put(position, obtained);
+ }
+ return parseValue(name, obtained);
+ }
+
+ private JQueryLocator getRowOnPosition(int position) {
+ return row.getNthOccurence(position);
+ }
+
+ private String parseValue(String name, String text) {
+ Pattern pattern = Pattern.compile(format("(?:^|.*
){0}=([^,]+)(?:,.*|$)", name));
+ Matcher matcher = pattern.matcher(text);
+ if (!matcher.find()) {
+ throw new IllegalArgumentException(format("the text '{0}' cannot
be parsed", text));
+ }
+ return matcher.group(1);
+ }
+}
\ No newline at end of file
Added:
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/TestSimple.java
===================================================================
---
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/TestSimple.java
(rev 0)
+++
modules/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/a4jRepeat/TestSimple.java 2010-09-22
07:41:46 UTC (rev 19306)
@@ -0,0 +1,144 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+package org.richfaces.tests.metamer.ftest.a4jRepeat;
+
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
+import static org.testng.Assert.assertEquals;
+
+import java.net.URL;
+
+import org.richfaces.tests.metamer.ftest.AbstractMetamerTest;
+import org.richfaces.tests.metamer.ftest.annotations.Inject;
+import org.richfaces.tests.metamer.ftest.annotations.Use;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class TestSimple extends AbstractMetamerTest {
+
+ protected static final int ELEMENTS_TOTAL = 20;
+
+ SimpleModel model;
+ RepeatAttributes attributes = new RepeatAttributes();
+
+ @Inject
+ @Use(empty = false)
+ Integer first;
+
+ @Inject
+ @Use(empty = false)
+ Integer rows;
+
+ int expectedFirst;
+ int expectedRows;
+
+ @Override
+ public URL getTestUrl() {
+ return buildUrl(contextPath,
"faces/components/a4jRepeat/simple.xhtml");
+ }
+
+ @BeforeMethod
+ public void prepareAttributes() {
+ model = new SimpleModel();
+
+ if (first != null) {
+ attributes.setFirst(first);
+ }
+ if (rows != null) {
+ attributes.setRows(rows);
+ }
+ }
+
+ @Test
+ public void testRenderedAttribute() {
+ attributes.setRendered(false);
+ assertEquals(model.isRendered(), false);
+ }
+
+ @Test
+ @Use(field = "first", ints = { -2, -1, 0, 1, ELEMENTS_TOTAL / 2,
ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL,
+ ELEMENTS_TOTAL + 1 })
+ public void testFirstAttribute() {
+ verifyRepeat();
+ }
+
+ @Test
+ @Use(field = "rows", ints = { -2, -1, 0, 1, ELEMENTS_TOTAL / 2,
ELEMENTS_TOTAL - 1, ELEMENTS_TOTAL,
+ ELEMENTS_TOTAL + 1 })
+ public void testRowsAttribute() {
+ verifyRepeat();
+ }
+
+ private void verifyRepeat() {
+ countExpectedValues();
+ verifyCounts();
+ verifyRows();
+ }
+
+ private void verifyCounts() {
+ assertEquals(model.getTotalRowCount(), expectedRows);
+ if (expectedRows > 0) {
+ assertEquals(model.getIndex(1), expectedFirst);
+ }
+ }
+
+ private void verifyRows() {
+ int rowCount = model.getTotalRowCount();
+ for (int position = 1; position <= rowCount; position++) {
+ assertEquals(model.getBegin(position), expectedFirst, "begin");
+ assertEquals(model.getEnd(position), expectedFirst + expectedRows - 1,
"end");
+ assertEquals(model.getIndex(position), expectedFirst + position - 1,
"index");
+ assertEquals(model.getCount(position), position, "count");
+ assertEquals(model.isFirst(position), position == 1, "first");
+ assertEquals(model.isLast(position), position == rowCount,
"last");
+ assertEquals(model.isEven(position), (position % 2) == 0, "even");
+ // TODO fails because of rowCount on page doesn't eqaul to rowCount, but
ELEMENTS_TOTAL
+ // assertEquals(model.getRowCount(position), rowCount,
"rowCount");
+ }
+ }
+
+ private void countExpectedValues() {
+ if (first == null || first < 0) {
+ expectedFirst = 0;
+ } else {
+ expectedFirst = first;
+ }
+
+ if (rows == null || rows < 1 || rows > ELEMENTS_TOTAL) {
+ expectedRows = ELEMENTS_TOTAL;
+ } else {
+ expectedRows = rows;
+ }
+
+ expectedFirst = minMax(0, expectedFirst, ELEMENTS_TOTAL);
+ expectedRows = min(expectedRows, ELEMENTS_TOTAL - expectedFirst);
+ }
+
+ private int minMax(int min, int value, int max) {
+ return max(0, min(max, value));
+ }
+}
\ No newline at end of file