Author: nbelaevski
Date: 2010-10-20 13:54:06 -0400 (Wed, 20 Oct 2010)
New Revision: 19618
Added:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTestRule.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestData.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestDataHolder.java
Modified:
branches/RF-8742/bom/pom.xml
branches/RF-8742/ui/validator/ui/pom.xml
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTest.java
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/DateTimeConverterTest.java
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/test.xhtml
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/testConverter.js
Log:
https://jira.jboss.org/browse/RF-9511
Modified: branches/RF-8742/bom/pom.xml
===================================================================
--- branches/RF-8742/bom/pom.xml 2010-10-20 15:39:47 UTC (rev 19617)
+++ branches/RF-8742/bom/pom.xml 2010-10-20 17:54:06 UTC (rev 19618)
@@ -194,11 +194,6 @@
<artifactId>cssparser</artifactId>
<version>0.9.5</version>
</dependency>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>5.14</version>
- </dependency>
<!-- Optional cache dependencies -->
<dependency>
Modified: branches/RF-8742/ui/validator/ui/pom.xml
===================================================================
--- branches/RF-8742/ui/validator/ui/pom.xml 2010-10-20 15:39:47 UTC (rev 19617)
+++ branches/RF-8742/ui/validator/ui/pom.xml 2010-10-20 17:54:06 UTC (rev 19618)
@@ -89,11 +89,6 @@
<version>1.0.5-SNAPSHOT</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <scope>test</scope>
- </dependency>
</dependencies>
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTest.java 2010-10-20
15:39:47 UTC (rev 19617)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTest.java 2010-10-20
17:54:06 UTC (rev 19618)
@@ -40,17 +40,20 @@
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
+import junit.framework.AssertionFailedError;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.ScriptUtils;
import org.jboss.test.faces.htmlunit.HtmlUnitEnvironment;
-import org.testng.Assert;
-import org.testng.annotations.AfterTest;
-import org.testng.annotations.BeforeTest;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.google.common.base.Strings;
/**
* @author Nick Belaevski
@@ -68,8 +71,6 @@
private String clientConverterClassName;
- private String submittedValue;
-
private String convertedValueAsScript;
private String conversionErrorMessage;
@@ -112,10 +113,6 @@
attributes.put(name, value);
}
- public void setSubmittedValue(String submittedValue) {
- this.submittedValue = submittedValue;
- }
-
public String getConverterParametersString() {
return converterParametersString;
}
@@ -178,17 +175,24 @@
}
}
+ @Rule
+ public BaseConverterTestRule rule = new BaseConverterTestRule();
+
private TestBean testBean;
private HtmlUnitEnvironment environment;
private String converterScriptName;
+ private String submittedValue;
+
+ private String failureMessage;
+
public BaseConverterTest(String converterScriptName) {
this.converterScriptName = converterScriptName;
}
- @BeforeTest
+ @Before
public void setUp() throws Exception {
testBean = new TestBean();
@@ -208,14 +212,53 @@
environment.start();
}
- @AfterTest
+ @After
public void tearDown() throws Exception {
+ submittedValue = null;
+ failureMessage = null;
+
testBean = null;
environment.release();
environment = null;
}
+ private String formattedParametersData() {
+ StringBuilder sb = new StringBuilder();
+
+ sb.append("TestData {");
+
+ sb.append("submittedValue=\"");
+ sb.append(submittedValue);
+ sb.append("\"");
+
+ if (!Strings.isNullOrEmpty(failureMessage)) {
+ sb.append(", failureMessage=\"");
+ sb.append(failureMessage);
+ sb.append("\"");
+ }
+
+ sb.append("}");
+
+ return sb.toString();
+ }
+
+ private void fail(String message) {
+ Assert.fail(MessageFormat.format("{0}: {1}", formattedParametersData(),
message));
+ }
+
+ private void assertEquals(Object expected, Object actual) {
+ if (expected == null && actual == null) {
+ return;
+ }
+ if (expected != null && expected.equals(actual)) {
+ return;
+ }
+
+ throw new AssertionFailedError(MessageFormat.format("{0}: expected {1} but
was {2}",
+ formattedParametersData(), expected, actual));
+ }
+
protected String serializeObjectToScript(Object object) {
if (object instanceof Date) {
Calendar calendar = Calendar.getInstance();
@@ -246,21 +289,25 @@
testBean.setAttribute(name, value);
}
- public void assertConversionOk(String submittedValue) throws Exception {
- testBean.setSubmittedValue(submittedValue);
-
+ public void setSubmittedValue(String submittedValue) {
+ this.submittedValue = submittedValue;
+ }
+
+ public void setFailureMessage(String failureMessage) {
+ this.failureMessage = failureMessage;
+ }
+
+ public void assertConversionOk() throws Exception {
HtmlPage page = environment.getPage("/test.jsf");
Assert.assertTrue(page.getWebClient().isJavaScriptEnabled());
ScriptResult conversionMessageResult =
page.executeJavaScript("verifyConversion()");
if (!ScriptResult.isUndefined(conversionMessageResult)) {
- Assert.fail(conversionMessageResult.getJavaScriptResult().toString());
+ fail(conversionMessageResult.getJavaScriptResult().toString());
}
}
- public void assertConversionFailure(String submittedValue, String failureMessage)
throws Exception {
- testBean.setSubmittedValue(submittedValue);
-
+ public void assertConversionFailure() throws Exception {
HtmlPage page = environment.getPage("/test.jsf");
Assert.assertNotNull(testBean.getConverterException());
@@ -270,12 +317,12 @@
ScriptResult conversionErrorResult =
page.executeJavaScript("window.conversionError");
if (ScriptResult.isUndefined(conversionErrorResult)) {
- Assert.fail("converter has not thrown an error");
+ fail("converter has not thrown an error");
return;
}
Scriptable conversionError = (Scriptable)
conversionErrorResult.getJavaScriptResult();
- Assert.assertEquals(conversionError.get("message", conversionError),
failureMessage);
+ assertEquals(conversionError.get("message", conversionError),
failureMessage);
}
Added:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTestRule.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTestRule.java
(rev 0)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/BaseConverterTestRule.java 2010-10-20
17:54:06 UTC (rev 19618)
@@ -0,0 +1,72 @@
+/*
+ * 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.convert;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.rules.MethodRule;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.Lists;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class BaseConverterTestRule implements MethodRule {
+
+ public Statement apply(final Statement base, FrameworkMethod method, final Object
target) {
+ final List<TestData> testDataList = Lists.newArrayList();
+
+ TestDataHolder testDataHolder = method.getAnnotation(TestDataHolder.class);
+ if (testDataHolder != null) {
+ testDataList.addAll(Arrays.asList(testDataHolder.value()));
+ }
+
+ TestData testData = method.getAnnotation(TestData.class);
+ if (testData != null) {
+ testDataList.add(testData);
+ }
+
+ return new Statement() {
+
+ @Override
+ public void evaluate() throws Throwable {
+ BaseConverterTest baseConverterTest = (BaseConverterTest) target;
+
+ for (TestData testData : testDataList) {
+ baseConverterTest.setSubmittedValue(testData.submittedValue());
+
+ if (!Strings.isNullOrEmpty(testData.failureMessage())) {
+ baseConverterTest.setFailureMessage(testData.failureMessage());
+ }
+
+ base.evaluate();
+ }
+ }
+ };
+ }
+
+}
Modified:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/DateTimeConverterTest.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/DateTimeConverterTest.java 2010-10-20
15:39:47 UTC (rev 19617)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/DateTimeConverterTest.java 2010-10-20
17:54:06 UTC (rev 19618)
@@ -23,10 +23,8 @@
import javax.faces.convert.DateTimeConverter;
-import org.testng.annotations.DataProvider;
-import org.testng.annotations.Test;
+import org.junit.Test;
-
/**
* @author Nick Belaevski
*
@@ -37,35 +35,28 @@
super("org/richfaces/convert/testConverter.js");
}
- @DataProvider(name = "successTest")
- public Object[][] getSuccessTestData() {
- return new Object[][]{
- {"17-10-2010"}
- };
- }
-
- @DataProvider(name = "failuresTest")
- public Object[][] getFailuresTestData() {
- return new Object[][]{
- {"10/17/2010", "Date should be in dd-MM-yyyy format"}
- };
- }
-
- @Test(dataProvider = "successTest")
- public void testSuccess(String submittedValue) throws Exception {
+ @Test
+ @TestDataHolder({
+ @TestData(submittedValue = "18-10-2010"),
+ @TestData(submittedValue = "17-10-2010"),
+ })
+ public void testSuccess() throws Exception {
setClientConverterClassName("org.rf.DateTimeConverter");
setConverterId(DateTimeConverter.CONVERTER_ID);
setAttribute("pattern", "dd-MM-yyyy");
- assertConversionOk(submittedValue);
+ assertConversionOk();
}
- @Test(dataProvider = "failuresTest")
- public void testFailures(String submittedValue, String conversionErrorMessage) throws
Exception {
+ @Test
+ @TestDataHolder({
+ @TestData(submittedValue = "10/17/2010", failureMessage =
TEST_COMPONENT_LABEL + ": '10/17/2010' could not be understood as a
date.")
+ })
+ public void testFailures() throws Exception {
setClientConverterClassName("org.rf.DateTimeConverter");
setConverterId(DateTimeConverter.CONVERTER_ID);
setAttribute("pattern", "dd-MM-yyyy");
- assertConversionFailure(submittedValue, conversionErrorMessage);
+ assertConversionFailure();
}
}
Added: branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestData.java
===================================================================
--- branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestData.java
(rev 0)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestData.java 2010-10-20
17:54:06 UTC (rev 19618)
@@ -0,0 +1,42 @@
+/*
+ * 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.convert;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Test data annotation for {@link BaseConverterTest}
+ * @author Nick Belaevski
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+(a)Target(ElementType.METHOD)
+public @interface TestData {
+
+ public String submittedValue();
+
+ public String failureMessage() default "";
+
+}
Added:
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestDataHolder.java
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestDataHolder.java
(rev 0)
+++
branches/RF-8742/ui/validator/ui/src/test/java/org/richfaces/convert/TestDataHolder.java 2010-10-20
17:54:06 UTC (rev 19618)
@@ -0,0 +1,40 @@
+/*
+ * 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.convert;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Holder element for multiple {@link TestData} elements
+ * @author Nick Belaevski
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+(a)Target(ElementType.METHOD)
+public @interface TestDataHolder {
+
+ public TestData[] value();
+
+}
Modified:
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/test.xhtml
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/test.xhtml 2010-10-20
15:39:47 UTC (rev 19617)
+++
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/test.xhtml 2010-10-20
17:54:06 UTC (rev 19618)
@@ -12,7 +12,7 @@
<h:outputScript name="converter.js" />
</h:head>
<h:body>
- <script type="text/javascript">
+ <script type="text/javascript">/* <![CDATA[ */
window.expected = #{testBean.convertedValueAsScript};
try {
@@ -22,17 +22,29 @@
window.conversionError = e;
}
+ function equals(a, b) {
+ if (a === b) {
+ return true;
+ }
+
+ if (a instanceof Date && b instanceof Date) {
+ return a.getTime() === b.getTime();
+ }
+
+ //TODO - implement equality checking for all supported objects in a proper way
+ return false;
+ }
+
function verifyConversion() {
if (window.conversionError) {
return "conversion error caught: " + window.conversionError.message;
}
- //TODO - implement equality checking for all supported objects in a proper way
- if (window.actual != window.expected) {
+ if (!equals(window.actual, window.expected)) {
return "expected [" + window.expected + "] but was [" +
window.actual + "]";
}
}
- </script>
+ /* ]]> */</script>
</h:body>
</f:view>
</html>
\ No newline at end of file
Modified:
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/testConverter.js
===================================================================
---
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/testConverter.js 2010-10-20
15:39:47 UTC (rev 19617)
+++
branches/RF-8742/ui/validator/ui/src/test/resources/org/richfaces/convert/testConverter.js 2010-10-20
17:54:06 UTC (rev 19618)
@@ -1,6 +1,7 @@
window.org = {
rf: {
DateTimeConverter: function() {
+ return new Date(2010, 9, 18);
}
}
}
\ No newline at end of file