Author: lfryc(a)redhat.com
Date: 2010-07-23 10:15:35 -0400 (Fri, 23 Jul 2010)
New Revision: 18215
Modified:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/AbstractMetamerTest.java
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java
Log:
all @Factory from TestPagination prototype was moved to AbstractMetamerTest
Modified:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/AbstractMetamerTest.java
===================================================================
---
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/AbstractMetamerTest.java 2010-07-23
14:14:55 UTC (rev 18214)
+++
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/AbstractMetamerTest.java 2010-07-23
14:15:35 UTC (rev 18215)
@@ -25,17 +25,31 @@
import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
import static org.testng.Assert.assertEquals;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.net.URL;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
import org.jboss.test.selenium.AbstractTestCase;
import org.jboss.test.selenium.dom.Event;
import org.jboss.test.selenium.encapsulated.JavaScript;
import org.jboss.test.selenium.locator.ElementLocator;
import org.jboss.test.selenium.locator.JQueryLocator;
+import org.richfaces.tests.metamer.ftest.annotations.Inject;
+import org.richfaces.tests.metamer.ftest.annotations.Use;
+import org.richfaces.tests.metamer.ftest.internal.NamedType;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
+import org.testng.annotations.Factory;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
@@ -53,6 +67,9 @@
public static final long TIMEOUT = 5000;
private String[][] templates;
+ public AbstractMetamerTest() {
+ }
+
/**
* Returns the url to test page to be opened by Selenium
*
@@ -72,7 +89,7 @@
return templates;
}
- return new Object[][]{{"plain"}};
+ return new Object[][] { { "plain" } };
}
/**
@@ -81,7 +98,7 @@
* @param urlParams
*/
@BeforeClass(alwaysRun = true)
- @Parameters({"templates"})
+ @Parameters({ "templates" })
public void initTemplates(@Optional String urlParams) {
if (urlParams == null) {
return;
@@ -172,14 +189,14 @@
assertEquals(selenium.getAlert(), event.getEventName(), event.getEventName()
+ " attribute did not change correctly");
}
-
+
/**
* Hides header, footer and inputs for attributes.
*/
protected void hideControls() {
selenium.getEval(new JavaScript("window.hideControls()"));
}
-
+
/**
* Shows header, footer and inputs for attributes.
*/
@@ -187,4 +204,144 @@
selenium.getEval(new JavaScript("window.showControls()"));
}
+ @Factory
+ public Object[] createTests() {
+ // obtain parametrized constructor
+ Constructor<?> constructor = null;
+ for (Constructor<?> c : this.getClass().getConstructors()) {
+ if (c.getParameterTypes().length > 0) {
+ if (constructor == null) {
+ constructor = c;
+ } else {
+ throw new IllegalStateException("there cannot be more than one
parametrized constructor");
+ }
+ }
+ }
+
+ Map<NamedType, List<? extends Object>> parameters = new
LinkedHashMap<NamedType, List<? extends Object>>();
+
+ // fill all parameters with null values to sorted map
+ for (int i = 0; i < constructor.getParameterTypes().length; i++) {
+ NamedType namedType = new NamedType(constructor.getParameterTypes()[i],
+ constructor.getParameterAnnotations()[i]);
+
+ if (parameters.containsKey(namedType)) {
+ throw new IllegalStateException("there are ambiguous
parameters");
+ } else {
+ parameters.put(namedType, null);
+ }
+ }
+
+ List<Field> injections = new LinkedList<Field>();
+
+ for (Field field : this.getClass().getDeclaredFields()) {
+ if (field.getAnnotation(Inject.class) != null) {
+ if (field.getAnnotation(Use.class) != null) {
+ NamedType namedType = new NamedType(field.getType(), (String) null);
+ if (!parameters.containsKey(namedType)) {
+ namedType = new NamedType(field.getType(), field.getName());
+ }
+ if (!parameters.containsKey(namedType)) {
+ throw new IllegalStateException("cannot find equivalent
constructor parameter");
+ }
+
+ parameters.put(namedType, getUseParameter(field.getType(),
field.getAnnotation(Use.class)));
+ } else {
+ injections.add(field);
+ }
+ }
+ }
+
+ if (!injections.isEmpty()) {
+ throw new IllegalStateException("all injections weren't
satisfied");
+ }
+
+ Object[][] parameterArray = new Object[parameters.size()][];
+ int parameterPosition = 0;
+ for (List<? extends Object> parameter : parameters.values()) {
+ parameterArray[parameterPosition++] = parameter.toArray();
+ }
+
+ Object[][] combinations = generateCombinations(parameterArray);
+
+ Object[] result = new Object[combinations.length];
+ for (int i = 0; i < combinations.length; i++) {
+ try {
+ result[i] = constructor.newInstance(combinations[i]);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return result;
+ }
+
+ private Object[][] generateCombinations(Object[][] toCombine) {
+ int count = 1;
+ for (int outter = 0; outter < toCombine.length; outter++) {
+ count *= toCombine[outter].length;
+ }
+
+ Object[][] result = new Object[count][toCombine.length];
+
+ int[] position = new int[toCombine.length];
+ Arrays.fill(position, 0);
+ // int iterate = 0;
+
+ for (int i = 0; i < count; i++) {
+ result[i] = new Object[toCombine.length];
+
+ for (int j = 0; j < toCombine.length; j++) {
+ result[i][j] = toCombine[j][position[j]];
+ }
+
+ // iterate
+ if (i + 1 < count) {
+ for (int j = 0; j < toCombine.length; j++) {
+ position[j] += 1;
+ if (position[j] < toCombine[j].length) {
+ break;
+ } else {
+ position[j] = 0;
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ private List<? extends Object> getUseParameter(Class<?> parameterType,
Use use) {
+ if (parameterType == int.class || parameterType == Integer.class) {
+ return Arrays.asList(ArrayUtils.toObject(use.ints()));
+ }
+
+ // tries satisfy parameter from fields
+ List<Object> result = new LinkedList<Object>();
+ for (int i = 0; i < use.value().length; i++) {
+ boolean satisfied = false;
+ for (Field field : this.getClass().getDeclaredFields()) {
+ String name = use.value()[i];
+ name = StringUtils.replace(name, "*", ".+");
+ name = StringUtils.replace(name, "?", ".");
+ Pattern pattern = Pattern.compile(name);
+ if (pattern.matcher(field.getName()).matches()) {
+ if (parameterType.isAssignableFrom(field.getType())) {
+ try {
+ result.add(field.get(this));
+ satisfied = true;
+ } catch (Exception e) {
+ throw new IllegalStateException("the field cannot be
obtained");
+ }
+ }
+ }
+ }
+ if (satisfied) {
+ continue;
+ }
+ throw new IllegalStateException("cannot find the field satysfying
injection point");
+ }
+ return result;
+ }
+
}
Modified:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java
===================================================================
---
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java 2010-07-23
14:14:55 UTC (rev 18214)
+++
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java 2010-07-23
14:15:35 UTC (rev 18215)
@@ -21,37 +21,24 @@
*******************************************************************************/
package org.richfaces.tests.metamer.ftest.richDataScroller;
+import static org.jboss.test.selenium.guard.request.RequestTypeGuardFactory.guardHttp;
import static org.jboss.test.selenium.locator.LocatorFactory.id;
import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
+import static org.testng.Assert.assertFalse;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
import java.net.URL;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Pattern;
-import org.apache.commons.lang.ArrayUtils;
-import org.apache.commons.lang.StringUtils;
import org.jboss.test.selenium.locator.IdLocator;
import org.richfaces.tests.metamer.ftest.AbstractMetamerTest;
import org.richfaces.tests.metamer.ftest.annotations.Inject;
import org.richfaces.tests.metamer.ftest.annotations.Named;
import org.richfaces.tests.metamer.ftest.annotations.Use;
-import org.richfaces.tests.metamer.ftest.internal.NamedType;
import org.richfaces.tests.metamer.ftest.richExtendedDataTable.AssertingDataScroller;
import org.richfaces.tests.metamer.ftest.richExtendedDataTable.DataTable;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Factory;
import org.testng.annotations.Test;
-import static org.testng.Assert.*;
-import static org.jboss.test.selenium.guard.request.RequestTypeGuardFactory.*;
-
/**
* Test the functionality of switching pages using DataScroller bound to DataTable.
*
@@ -60,18 +47,20 @@
*/
public class TestPagination extends AbstractMetamerTest {
+
+
private static final int FAST_STEP = 2;
private static final int[] PAGES = new int[] { 3, 6, 1, 4, 6, 2, 4, 5 };
+
+ @Inject
+ @Use(value = { "dataScroller*" })
+ public AssertingDataScroller dataScroller;
+ public AssertingDataScroller dataScroller1 = new
AssertingDataScroller(pjq("span.rf-ds[id$=scroller1]"));
+ public AssertingDataScroller dataScroller2 = new
AssertingDataScroller(pjq("span.rf-ds[id$=scroller2]"));
IdLocator attributeFastStep = id("form:attributes:fastStepInput");
IdLocator attributeMaxPages = id("form:attributes:maxPagesInput");
- @Inject
- @Use(value = { "dataScroller*" })
- AssertingDataScroller dataScroller;
- AssertingDataScroller dataScroller1 = new
AssertingDataScroller(pjq("span.rf-ds[id$=scroller1]"));
- AssertingDataScroller dataScroller2 = new
AssertingDataScroller(pjq("span.rf-ds[id$=scroller2]"));
-
DataTable dataTable = new
DataTable(pjq("table.rf-dt[id$=richDataTable]"));
@Inject
@@ -97,146 +86,8 @@
return buildUrl(contextPath,
"faces/components/richDataScroller/simple.xhtml");
}
- @Factory
- public Object[] createTests() {
- // obtain parametrized constructor
- Constructor<?> constructor = null;
- for (Constructor<?> c : this.getClass().getConstructors()) {
- if (c.getParameterTypes().length > 0) {
- if (constructor == null) {
- constructor = c;
- } else {
- throw new IllegalStateException("there cannot be more than one
parametrized constructor");
- }
- }
- }
+
- Map<NamedType, List<? extends Object>> parameters = new
LinkedHashMap<NamedType, List<? extends Object>>();
-
- // fill all parameters with null values to sorted map
- for (int i = 0; i < constructor.getParameterTypes().length; i++) {
- NamedType namedType = new NamedType(constructor.getParameterTypes()[i],
- constructor.getParameterAnnotations()[i]);
-
- if (parameters.containsKey(namedType)) {
- throw new IllegalStateException("there are ambiguous
parameters");
- } else {
- parameters.put(namedType, null);
- }
- }
-
- List<Field> injections = new LinkedList<Field>();
-
- for (Field field : this.getClass().getDeclaredFields()) {
- if (field.getAnnotation(Inject.class) != null) {
- if (field.getAnnotation(Use.class) != null) {
- NamedType namedType = new NamedType(field.getType(), (String) null);
- if (!parameters.containsKey(namedType)) {
- namedType = new NamedType(field.getType(), field.getName());
- }
- if (!parameters.containsKey(namedType)) {
- throw new IllegalStateException("cannot find equivalent
constructor parameter");
- }
-
- parameters.put(namedType, getUseParameter(field.getType(),
field.getAnnotation(Use.class)));
- } else {
- injections.add(field);
- }
- }
- }
-
- if (!injections.isEmpty()) {
- throw new IllegalStateException("all injections weren't
satisfied");
- }
-
- Object[][] parameterArray = new Object[parameters.size()][];
- int parameterPosition = 0;
- for (List<? extends Object> parameter : parameters.values()) {
- parameterArray[parameterPosition++] = parameter.toArray();
- }
-
- Object[][] combinations = generateCombinations(parameterArray);
-
- Object[] result = new Object[combinations.length];
- for (int i = 0; i < combinations.length; i++) {
- try {
- result[i] = constructor.newInstance(combinations[i]);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- return result;
- }
-
- private Object[][] generateCombinations(Object[][] toCombine) {
- int count = 1;
- for (int outter = 0; outter < toCombine.length; outter++) {
- count *= toCombine[outter].length;
- }
-
- Object[][] result = new Object[count][toCombine.length];
-
- int[] position = new int[toCombine.length];
- Arrays.fill(position, 0);
- // int iterate = 0;
-
- for (int i = 0; i < count; i++) {
- result[i] = new Object[toCombine.length];
-
- for (int j = 0; j < toCombine.length; j++) {
- result[i][j] = toCombine[j][position[j]];
- }
-
- // iterate
- if (i + 1 < count) {
- for (int j = 0; j < toCombine.length; j++) {
- position[j] += 1;
- if (position[j] < toCombine[j].length) {
- break;
- } else {
- position[j] = 0;
- }
- }
- }
- }
-
- return result;
- }
-
- private List<? extends Object> getUseParameter(Class<?> parameterType,
Use use) {
- if (parameterType == int.class || parameterType == Integer.class) {
- return Arrays.asList(ArrayUtils.toObject(use.ints()));
- }
-
- // tries satisfy parameter from fields
- List<Object> result = new LinkedList<Object>();
- for (int i = 0; i < use.value().length; i++) {
- boolean satisfied = false;
- for (Field field : this.getClass().getDeclaredFields()) {
- String name = use.value()[i];
- name = StringUtils.replace(name, "*", ".+");
- name = StringUtils.replace(name, "?", ".");
- Pattern pattern = Pattern.compile(name);
- if (pattern.matcher(field.getName()).matches()) {
- if (parameterType.isAssignableFrom(field.getType())) {
- try {
- result.add(field.get(this));
- satisfied = true;
- } catch (Exception e) {
- throw new IllegalStateException("the field cannot be
obtained");
- }
- }
- }
- }
- if (satisfied) {
- continue;
- }
- throw new IllegalStateException("cannot find the field satysfying
injection point");
- }
- return result;
- }
-
@Test
public void testNumberedPages() {
dataScroller.setFastStep(FAST_STEP);