Author: lfryc(a)redhat.com
Date: 2010-07-23 10:14:10 -0400 (Fri, 23 Jul 2010)
New Revision: 18213
Added:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Inject.java
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Named.java
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Use.java
Modified:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java
Log:
implemented parametrization of test properties using @Factory, @Use and @Inject
annotations
Added:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Inject.java
===================================================================
---
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Inject.java
(rev 0)
+++
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Inject.java 2010-07-23
14:14:10 UTC (rev 18213)
@@ -0,0 +1,11 @@
+package org.richfaces.tests.metamer.ftest.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import static java.lang.annotation.ElementType.*;
+
+@Target({ TYPE, FIELD, METHOD, PARAMETER })
+(a)Retention(RetentionPolicy.RUNTIME)
+public @interface Inject {
+}
Added:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Named.java
===================================================================
---
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Named.java
(rev 0)
+++
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Named.java 2010-07-23
14:14:10 UTC (rev 18213)
@@ -0,0 +1,12 @@
+package org.richfaces.tests.metamer.ftest.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import static java.lang.annotation.ElementType.*;
+
+@Target({ PARAMETER })
+(a)Retention(RetentionPolicy.RUNTIME)
+public @interface Named {
+ String value();
+}
Added:
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Use.java
===================================================================
---
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Use.java
(rev 0)
+++
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/annotations/Use.java 2010-07-23
14:14:10 UTC (rev 18213)
@@ -0,0 +1,16 @@
+package org.richfaces.tests.metamer.ftest.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import static java.lang.annotation.ElementType.*;
+
+@Target({ TYPE, FIELD, METHOD, PARAMETER })
+(a)Retention(RetentionPolicy.RUNTIME)
+public @interface Use {
+ String field() default "";
+
+ String[] value() default {};
+
+ int[] ints() default {};
+}
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
13:06:59 UTC (rev 18212)
+++
root/tests/metamer/trunk/ftest-source/src/main/java/org/richfaces/tests/metamer/ftest/richDataScroller/TestPagination.java 2010-07-23
14:14:10 UTC (rev 18213)
@@ -24,15 +24,29 @@
import static org.jboss.test.selenium.locator.LocatorFactory.id;
import static org.jboss.test.selenium.utils.URLUtils.buildUrl;
+import java.lang.annotation.Annotation;
+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.jboss.test.selenium.locator.JQueryLocator;
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.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.*;
@@ -46,35 +60,257 @@
*/
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 };
IdLocator attributeFastStep = id("form:attributes:fastStepInput");
IdLocator attributeMaxPages = id("form:attributes:maxPagesInput");
- AssertingDataScroller dataScroller = new AssertingDataScroller(null);
+ @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]"));
- JQueryLocator scroller1 = pjq("span.rf-ds[id$=scroller1]");
- JQueryLocator scroller2 = pjq("span.rf-ds[id$=scroller2]");
+ @Inject
+ @Use(ints = { 2 })
+ int fastStep;
+ @Inject
+ @Use(ints = { 3 })
+ int maxPages;
+
+ public TestPagination() {
+ }
+
+ public TestPagination(AssertingDataScroller dataScroller,
@Named("fastStep") int fastStep,
+ @Named("maxPages") int maxPages) {
+ this.dataScroller = dataScroller;
+ this.fastStep = fastStep;
+ this.maxPages = maxPages;
+ }
+
@Override
public URL getTestUrl() {
return buildUrl(contextPath,
"faces/components/richDataScroller/simple.xhtml");
}
- @Test(dataProvider = "templates")
- public void testNumberedPagesScrollerOutsideTable(String template) {
- testNumberedPages(scroller1);
+ @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;
}
- @Test(dataProvider = "templates")
- public void testNumberedPagesFooterScroller(String template) {
- testNumberedPages(scroller2);
+ 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;
}
- public void testNumberedPages(JQueryLocator scrollerLocator) {
- dataScroller.setRoot(scrollerLocator);
- dataScroller.setFastStep(2);
+ private class NamedType {
+ private Class<?> parameterType;
+ private String name;
+
+ public NamedType(Class<?> parameterType, String name) {
+ this.parameterType = parameterType;
+ this.name = name;
+ }
+
+ public NamedType(Class<?> parameterType, Annotation[] annotations) {
+ this.parameterType = parameterType;
+ for (Annotation annotation : annotations) {
+ if (annotation.annotationType() == Named.class) {
+ this.name = ((Named) annotation).value();
+ }
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + getOuterType().hashCode();
+ result = prime * result + ((name == null) ? 0 : name.hashCode());
+ result = prime * result + ((parameterType == null) ? 0 :
parameterType.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ NamedType other = (NamedType) obj;
+ if (!getOuterType().equals(other.getOuterType())) {
+ return false;
+ }
+ if (name == null) {
+ if (other.name != null) {
+ return false;
+ }
+ } else if (!name.equals(other.name)) {
+ return false;
+ }
+ if (parameterType == null) {
+ if (other.parameterType != null) {
+ return false;
+ }
+ } else if (!parameterType.equals(other.parameterType)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "NamedType(" + parameterType.getSimpleName() + ",
" + name + ")";
+ }
+
+ private TestPagination getOuterType() {
+ return TestPagination.this;
+ }
+
+ }
+
+ 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);
dataScroller.setLastPage(dataScroller.getLastPage());
for (int pageNumber : PAGES) {