Revision: 672
http://svn.sourceforge.net/jwebunit/?rev=672&view=rev
Author: henryju
Date: 2007-03-03 10:53:28 -0800 (Sat, 03 Mar 2007)
Log Message:
-----------
Apply a long waiting patch from Chris Eldredge concerning ResourcesBundle
formatting. [993058]
Modified Paths:
--------------
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
branches/1.x/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
branches/1.x/src/changes/changes.xml
Added Paths:
-----------
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResourceBundleAssertionsTest.java
branches/1.x/jwebunit-commons-tests/src/main/resources/MessageBundle.properties
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/ResourceBundleAssertionsTest/
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/ResourceBundleAssertionsTest/testPage.html
Added:
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResourceBundleAssertionsTest.java
===================================================================
---
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResourceBundleAssertionsTest.java
(rev 0)
+++
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ResourceBundleAssertionsTest.java
2007-03-03 18:53:28 UTC (rev 672)
@@ -0,0 +1,141 @@
+/******************************************************************************
+ * jWebUnit project (http://jwebunit.sourceforge.net) *
+ * Distributed open-source, see full license under LICENCE.txt *
+
******************************************************************************/
+package net.sourceforge.jwebunit.tests;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+import net.sourceforge.jwebunit.tests.util.JettySetup;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Tests for MessageBundle related assertions
+ * @author Chris Eldredge
+ */
+public class ResourceBundleAssertionsTest extends JWebUnitAPITestCase {
+
+ static final String resourceBundleName = "MessageBundle";
+
+ static final String pageTitle = "Keyed Message Title - Main Page";
+
+ static final String text = "This is a test page.";
+
+ public static Test suite() {
+ Test suite = new TestSuite(ResourceBundleAssertionsTest.class);
+ return new JettySetup(suite);
+ }
+
+ public void setUp() throws Exception {
+ super.setUp();
+ getTestContext().setBaseUrl(HOST_PATH +
"/ResourceBundleAssertionsTest");
+ getTestContext().setResourceBundleName(resourceBundleName);
+ beginAt("/testPage.html");
+ }
+
+ public void testGetMessage() {
+ assertEquals(pageTitle, getMessage("title.fixed"));
+ }
+
+ public void testGetMessageWithArg() {
+ assertEquals(pageTitle, getMessage("title.with.args", new String[] {
"Main Page" }));
+ }
+
+ /**
+ * Verify number formatting works in Default Locale
+ */
+ public void testGetFormattedMessage() {
+ final Double amount = new Double(1234.56);
+ NumberFormat numberFormat =
NumberFormat.getCurrencyInstance(Locale.getDefault());
+ final String expectedValue = numberFormat.format(amount);
+
+ assertEquals(expectedValue, tester
+ .getMessage("currency.with.args", new Object[] { amount }));
+ }
+
+ /**
+ * Verify Test uses the Locale specified by framework
+ */
+ public void testGetLocalizedFormattedMessage() {
+ final Double amount = new Double(1234.56);
+ NumberFormat numberFormat =
NumberFormat.getCurrencyInstance(Locale.FRANCE);
+ final String expectedValue = numberFormat.format(amount);
+
+ getTestContext().setLocale(Locale.FRANCE);
+
+ assertEquals(expectedValue, tester
+ .getMessage("currency.with.args", new Object[] { amount }));
+ }
+
+ public void testAssertTitleEqualsKey() throws Throwable {
+ assertPassFail("assertTitleEqualsKey", "title.fixed",
"title.not.used");
+ }
+
+ public void testAssertTitleEqualsKeyWithArg() throws Throwable {
+ //assertTitleEqualsKey("title.with.args", new String[] {"Main Page"});
+ assertPassFail("assertTitleEqualsKey", new Object[] {
"title.with.args",
+ new Object[] { "Main Page" } }, new Object[] {
"title.with.args",
+ new Object[] { "Wrong" } });
+ }
+
+ public void testAssertKeyPresent() throws Throwable {
+ assertPassFail("assertKeyPresent", "text.fixed", "title.not.used");
+ }
+
+ public void testAssertKeyPresentWithArg() throws Throwable {
+ assertPassFail("assertKeyPresent",
+ new Object[] { "text.with.args", new Object[] { "test" } },
new Object[] {
+ "text.with.args", new Object[] { "wrong" } });
+ }
+
+ public void testAssertTextNotPresent() throws Throwable {
+ assertPassFail("assertTextNotPresent", "no such text", text);
+ }
+
+ public void testAssertNotKeyPresent() throws Throwable {
+ assertPassFail("assertKeyNotPresent", "title.not.used", "text.fixed");
+ }
+
+ public void testAssertKeyNotPresentWithArg() throws Throwable {
+ assertPassFail("assertKeyNotPresent", new Object[] { "text.with.args",
+ new Object[] { "wrong" } }, new Object[] { "text.with.args",
+ new Object[] { "test" } });
+ }
+
+ public void testAssertKeyInTable() throws Throwable {
+ assertPassFail("assertKeyInTable", new Object[] { "testTable",
"table.fixed" },
+ new Object[] { "testTable", "title.not.used" });
+ }
+
+ public void testAssertKeyInTableWithArgs() throws Throwable {
+ assertPassFail("assertKeyInTable", new Object[] { "testTable",
"table.with.args",
+ new Object[] { "Data", "Table" } }, new Object[] {
"testTable", "table.with.args",
+ new Object[] { "wrong" } });
+ }
+
+ public void testAssertKeyNotInTable() throws Throwable {
+ assertPassFail("assertKeyNotInTable", new Object[] { "testTable",
"title.not.used" },
+ new Object[] { "testTable", "table.fixed" });
+ }
+
+ public void testAssertKeysInTable() throws Throwable {
+ assertPassFail("assertKeysInTable", new Object[] { "testTable",
+ new String[] { "table.fixed", "table2.fixed" } }, new Object[]
{ "testTable",
+ new String[] { "table.fixed", "title.not.used" } });
+ }
+
+ public void testAssertKeysInTableWithArgs() throws Throwable {
+ // okay, it's not pretty but somebody *might* want to use it...
+ assertPassFail("assertKeysInTable",
+ new Object[] {
+ "testTable",
+ new String[] { "table.with.args", "table2.with.args" },
+ new Object[][] { new Object[] { "Data", "Table" },
+ new Object[] { "Data", "In" } } }, new
Object[] { "testTable",
+ new String[] { "table.with.args", "table2.with.args" },
+ new Object[][] { new Object[] { "Wrong" }, new
Object[] { "Wrong" } } });
+ }
+
+}
\ No newline at end of file
Added:
branches/1.x/jwebunit-commons-tests/src/main/resources/MessageBundle.properties
===================================================================
---
branches/1.x/jwebunit-commons-tests/src/main/resources/MessageBundle.properties
(rev 0)
+++
branches/1.x/jwebunit-commons-tests/src/main/resources/MessageBundle.properties
2007-03-03 18:53:28 UTC (rev 672)
@@ -0,0 +1,16 @@
+# properties for ResourceBundleAssertionsTest
+title.not.used=This title is only used to verify assertion failure
+title.fixed=Keyed Message Title - Main Page
+title.with.args=Keyed Message Title - {0}
+
+currency.fixed=$1,234.56
+currency.with.args={0, number, currency}
+
+text.fixed=This is a test page.
+text.with.args=This is a {0} page.
+
+table.fixed=Cell Data In Table
+table.with.args=Cell {0} In {1}
+
+table2.fixed=Cell2 Data In Table
+table2.with.args=Cell2 {0} {1} Table
\ No newline at end of file
Added:
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/ResourceBundleAssertionsTest/testPage.html
===================================================================
---
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/ResourceBundleAssertionsTest/testPage.html
(rev 0)
+++
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/ResourceBundleAssertionsTest/testPage.html
2007-03-03 18:53:28 UTC (rev 672)
@@ -0,0 +1,19 @@
+<html>
+ <head>
+ <title>Keyed Message Title - Main Page</title>
+ </head>
+ <body>
+ This is a test page.
+ <table summary="testTable">
+ <tr id="row1">
+ <td>table text</td>
+ </tr>
+ <tr>
+ <td>Cell Data In Table</td>
+ </tr>
+ <tr>
+ <td>Cell2 Data In Table</td>
+ </tr>
+ </table>
+ </body>
+</html>
\ No newline at end of file
Modified:
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
===================================================================
---
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2007-03-03 17:49:31 UTC (rev 671)
+++
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java
2007-03-03 18:53:28 UTC (rev 672)
@@ -13,6 +13,7 @@
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
+import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
@@ -226,13 +227,36 @@
}
return message;
}
+
+ /**
+ * Return the value of a web resource based on its key, using MessageFormat
+ * to perform parametric substitution with formatting.
+ *
+ * @see MessageFormat
+ * @param key
+ * name of the web resource.
+ * @param args
+ * array of arguments to be formatted into message
+ * @return value of the web resource after formatting
+ */
+ public String getMessage(String key, Object[] args) {
+ String message = getMessage(key);
+ // TODO: Struts escapes single quotes... maybe this should too
+
+ MessageFormat format = new MessageFormat(message,
getTestContext().getLocale());
+
+ return format.format(args);
+ }
+
// Assertions
/**
- * Assert title of current html page in conversation matches an expected
value.
+ * Assert title of current html page in conversation matches an expected
+ * value.
*
- * @param title expected title value
+ * @param title
+ * expected title value
*/
public void assertTitleEquals(String title) {
Assert.assertEquals(title, getTestingEngine().getPageTitle());
@@ -255,23 +279,45 @@
}
/**
- * Assert title of current html page matches the value of a specified web
resource.
+ * Assert title of current html page matches the value of a specified web
+ * resource.
*
- * @param titleKey web resource key for title
+ * @param titleKey
+ * web resource key for title
*/
public void assertTitleEqualsKey(String titleKey) {
- Assert.assertEquals(getMessage(titleKey), getTestingEngine()
- .getPageTitle());
+ Assert.assertEquals(getMessage(titleKey),
getTestingEngine().getPageTitle());
}
+
+ /**
+ * Assert title of current page matches formatted message resource
+ *
+ * @param titleKey
+ * @param args
+ */
+ public void assertTitleEqualsKey(String titleKey, Object[] args) {
+ Assert.assertEquals(getMessage(titleKey, args),
getTestingEngine().getPageTitle());
+ }
/**
* Assert that a web resource's value is present.
*
- * @param key web resource name
+ * @param key
+ * web resource name
*/
public void assertKeyPresent(String key) {
assertTextPresent(getMessage(key));
}
+
+ /**
+ * Assert that a web resource's value (with formatting) is present
+ *
+ * @param key
+ * @param args
+ */
+ public void assertKeyPresent(String key, Object[] args) {
+ assertTextPresent(getMessage(key, args));
+ }
/**
* Assert that supplied text is present.
@@ -305,6 +351,16 @@
public void assertKeyNotPresent(String key) {
assertTextNotPresent(getMessage(key));
}
+
+ /**
+ * Assert that a web resource's formatted value is not present.
+ *
+ * @param key
+ * web resource name
+ */
+ public void assertKeyNotPresent(String key, Object[] args) {
+ assertTextNotPresent(getMessage(key, args));
+ }
/**
* Assert that supplied text is not present.
@@ -368,11 +424,25 @@
public void assertKeyInTable(String tableSummaryOrId, String key) {
assertTextInTable(tableSummaryOrId, getMessage(key));
}
+
+ /**
+ * Assert that the value of a given web resource is present in a specific
+ * table.
+ *
+ * @param tableSummaryOrId
+ * summary or id attribute value of table
+ * @param key
+ * web resource name
+ */
+ public void assertKeyInTable(String tableSummaryOrId, String key, Object[]
args) {
+ assertTextInTable(tableSummaryOrId, getMessage(key, args));
+ }
/**
* Assert that supplied text is present in a specific table.
*
- * @param tableSummaryNameOrId summary, name or id attribute value of table
+ * @param tableSummaryNameOrId
+ * summary, name or id attribute value of table
* @param text
*/
public void assertTextInTable(String tableSummaryNameOrId, String text) {
@@ -406,12 +476,29 @@
assertKeyInTable(tableSummaryOrId, keys[i]);
}
}
+
+ /**
+ * Assert that the values of a set of web resources are all present in a
+ * specific table.
+ *
+ * @param tableSummaryOrId
+ * summary or id attribute value of table
+ * @param keys
+ * Array of web resource names.
+ */
+ public void assertKeysInTable(String tableSummaryOrId, String[] keys,
Object[][] args) {
+ for (int i = 0; i < keys.length; i++) {
+ assertKeyInTable(tableSummaryOrId, keys[i], args[i]);
+ }
+ }
/**
* Assert that a set of text values are all present in a specific table.
*
- * @param tableSummaryOrId summary, name or id attribute value of table
- * @param text Array of expected text values.
+ * @param tableSummaryOrId
+ * summary, name or id attribute value of table
+ * @param text
+ * Array of expected text values.
*/
public void assertTextInTable(String tableSummaryOrId, String[] text) {
for (int i = 0; i < text.length; i++) {
Modified:
branches/1.x/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
===================================================================
---
branches/1.x/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
2007-03-03 17:49:31 UTC (rev 671)
+++
branches/1.x/jwebunit-htmlunit-plugin/src/test/java/net/sourceforge/jwebunit/htmlunit/JWebUnitTest.java
2007-03-03 18:53:28 UTC (rev 672)
@@ -50,6 +50,7 @@
suite.addTestSuite(NonHtmlContentTest.class);
suite.addTestSuite(RedirectionTest.class);
suite.addTestSuite(ImageTest.class);
+ suite.addTestSuite(ResourceBundleAssertionsTest.class);
// $JUnit-END$
return new JettySetup(suite);
}
Modified: branches/1.x/src/changes/changes.xml
===================================================================
--- branches/1.x/src/changes/changes.xml 2007-03-03 17:49:31 UTC (rev
671)
+++ branches/1.x/src/changes/changes.xml 2007-03-03 18:53:28 UTC (rev
672)
@@ -8,6 +8,9 @@
</properties>
<body>
<release version="1.4-RC4" date="UNKNOW">
+ <action type="add" dev="Julien Henry" issue="993058" due-to="Chris
Eldredge">
+ Add new ResourceBundle related methods allowing formatting of
resources.
+ </action>
<action type="fix" dev="Julien Henry" issue="1633967" due-to="Dirk
Jerusalem">
Table.getRows is now public.
</action>
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
JWebUnit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development