Revision: 868 http://jwebunit.svn.sourceforge.net/jwebunit/?rev=868&view=rev Author: henryju Date: 2010-10-20 09:17:31 +0000 (Wed, 20 Oct 2010)
Log Message: ----------- [2970512] Fixed issue with absolute image path. (merge) Modified Paths: -------------- trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ImageTest.java trunk/jwebunit-commons-tests/src/main/resources/testcases/ImageTest/PageWithImages.html trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java trunk/src/changes/changes.xml Modified: trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ImageTest.java =================================================================== --- trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ImageTest.java 2010-10-20 09:16:23 UTC (rev 867) +++ trunk/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/ImageTest.java 2010-10-20 09:17:31 UTC (rev 868) @@ -19,12 +19,7 @@ package net.sourceforge.jwebunit.tests; -import static net.sourceforge.jwebunit.junit.JWebUnit.assertImagePresent; -import static net.sourceforge.jwebunit.junit.JWebUnit.assertImageValid; -import static net.sourceforge.jwebunit.junit.JWebUnit.assertImageValidAndStore; -import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt; -import static net.sourceforge.jwebunit.junit.JWebUnit.getImage; -import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl; +import static net.sourceforge.jwebunit.junit.JWebUnit.*; import static org.junit.Assert.assertNotNull; import java.awt.Image; @@ -47,7 +42,8 @@ beginAt("/PageWithImages.html"); } - @Test public void testSimpleImagePresenceAssertion() throws Throwable { + @Test + public void testSimpleImagePresenceAssertion() throws Throwable { assertImagePresent("images/Image1.gif", "image 1"); assertImagePresent("images/Image2.png", "image 2"); assertImagePresent("images/photos/Image3.jpg", "image 3"); @@ -59,23 +55,28 @@ assertFail("assertImagePresent", new Object[]{"images/Image2.png", "wrong alt"}); } - @Test public void testGifCanBeLoaded() throws Throwable { + @Test + public void testGifCanBeLoaded() throws Throwable { assertPass("assertImageValid", new Object[]{"images/Image1.gif", "image 1"}); } - @Test public void testPngCanBeLoaded() throws Throwable { + @Test + public void testPngCanBeLoaded() throws Throwable { assertPass("assertImageValid", new Object[]{"images/Image2.png", "image 2"}); } - @Test public void testJpgCanBeLoaded() throws Throwable { + @Test + public void testJpgCanBeLoaded() throws Throwable { assertPass("assertImageValid", new Object[]{"images/photos/Image3.jpg", "image 3"}); } - @Test public void testFailsOnInvalidImages() throws Throwable { + @Test + public void testFailsOnInvalidImages() throws Throwable { assertFail("assertImageValid", new Object[]{"images/InvalidImage.gif", "invalid image"}); } - @Test public void testSavesImage() throws Throwable { + @Test + public void testSavesImage() throws Throwable { File testOut = File.createTempFile("jwebunit-test-", ".png"); testOut.deleteOnExit(); assertImageValidAndStore("images/Image2.png", "image 2", testOut); @@ -84,16 +85,25 @@ assertNotNull(testImg); } - @Test public void testImagesAreExposed() throws Throwable { + @Test + public void testImagesAreExposed() throws Throwable { Image image = getImage("images/Image1.gif", "image 1"); // let's just assume it's ok if the image is there assertNotNull(image); } - @Test public void testRelativePathsAreCorrectlyResolved() { + @Test + public void testRelativePathsAreCorrectlyResolved() { beginAt("/somedir/AnotherPageWithImages.html"); assertImageValid("Image4.gif", "image 4 - same dir"); assertImageValid("images/Image5.png", "image 5 - subdir"); assertImageValid("../images/photos/Image3.jpg", "image 3 again - topdir"); } + + @Test + public void testAbsolutePath() { + assertImagePresent("/jwebunit/ImageTest/images/Image1.gif", "absolute image 1"); + assertImageValid("/jwebunit/ImageTest/images/Image1.gif", "absolute image 1"); + } + } Modified: trunk/jwebunit-commons-tests/src/main/resources/testcases/ImageTest/PageWithImages.html =================================================================== --- trunk/jwebunit-commons-tests/src/main/resources/testcases/ImageTest/PageWithImages.html 2010-10-20 09:16:23 UTC (rev 867) +++ trunk/jwebunit-commons-tests/src/main/resources/testcases/ImageTest/PageWithImages.html 2010-10-20 09:17:31 UTC (rev 868) @@ -28,6 +28,7 @@ <body> <div id="test"> <img src="images/Image1.gif" alt="image 1" /> + <img src="/jwebunit/ImageTest/images/Image1.gif" alt="absolute image 1" /> <img src="images/Image2.png" alt="image 2" /> <img src="images/photos/Image3.jpg" alt="image 3" /> <img src="somedir/Image4.gif" /> Modified: trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java =================================================================== --- trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java 2010-10-20 09:16:23 UTC (rev 867) +++ trunk/jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java 2010-10-20 09:17:31 UTC (rev 868) @@ -232,6 +232,15 @@ } + /** + * This way of creating URL is not standard as absolute path are not correctly handled. We have to keep this + * non standard method for {...@link #beginAt(String)} that advertise a bad usage for a long time. + * @param url Absolute or relative URL. If start with '/', then it is incorrectly appended to baseURL. + * @param baseURL Base URL of the page + * @return Final absolute URL. + * @throws MalformedURLException + */ + @Deprecated private URL createUrl(String url, URL baseURL) throws MalformedURLException { if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) { @@ -245,6 +254,24 @@ } /** + * + * @param url Absolute or relative URL + * @param baseURL Base URL of the page + * @return Final absolute URL. + * @throws MalformedURLException + */ + private URL createUrlFixed(String url, URL baseURL) throws MalformedURLException { + if (url.startsWith("http://") || url.startsWith("https://") //Absolute URL + || url.startsWith("file://")) { + return new URL(url); + } else if (url.startsWith("www.")) { //Absolute URL with missing scheme (accepted by some browsers) + return new URL("http://" + url); + } else { //Relative path + return new URL(baseURL, url); + } + } + + /** * Return the value of a web resource based on its key. This translates to a property file lookup with the locale * based on the current TestContext. * @@ -3426,7 +3453,7 @@ assertImagePresent(imageSrc, imageAlt); URL imageUrl = null; try { - imageUrl = createUrl(imageSrc, getTestingEngine().getPageURL()); + imageUrl = createUrlFixed(imageSrc, getTestingEngine().getPageURL()); } catch (MalformedURLException e1) { fail(e1.getLocalizedMessage()); } Modified: trunk/src/changes/changes.xml =================================================================== --- trunk/src/changes/changes.xml 2010-10-20 09:16:23 UTC (rev 867) +++ trunk/src/changes/changes.xml 2010-10-20 09:17:31 UTC (rev 868) @@ -27,7 +27,7 @@ <properties> <title>JWebUnit changes</title> <author email="henryju at users.sourceforge.net"> - Julien Henry + Julien HENRY </author> </properties> <body> @@ -37,6 +37,9 @@ </action> </release> <release version="2.5" date="UNKNOW" description="Small fixes and dependency updates"> + <action type="fix" dev="henryju" issue="2970512" due-to="Todd Owen"> + Fixed handling of absolute image path (when src attribute start with a /). + </action> <action type="update" dev="henryju"> Updated to slf4j 1.6.1. </action> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Download new Adobe(R) Flash(R) Builder(TM) 4 The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly Flex(R) Builder(TM)) enable the development of rich applications that run across multiple browsers and platforms. Download your free trials today! http://p.sf.net/sfu/adobe-dev2dev _______________________________________________ JWebUnit-development mailing list JWebUnit-development@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jwebunit-development