Source Code Cross Referenced for HtmlScriptTest.java in  » Testing » htmlunit » com » gargoylesoftware » htmlunit » html » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » htmlunit » com.gargoylesoftware.htmlunit.html 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         * 1. Redistributions of source code must retain the above copyright notice,
008:         *    this list of conditions and the following disclaimer.
009:         * 2. Redistributions in binary form must reproduce the above copyright notice,
010:         *    this list of conditions and the following disclaimer in the documentation
011:         *    and/or other materials provided with the distribution.
012:         * 3. The end-user documentation included with the redistribution, if any, must
013:         *    include the following acknowledgment:
014:         *
015:         *       "This product includes software developed by Gargoyle Software Inc.
016:         *        (http://www.GargoyleSoftware.com/)."
017:         *
018:         *    Alternately, this acknowledgment may appear in the software itself, if
019:         *    and wherever such third-party acknowledgments normally appear.
020:         * 4. The name "Gargoyle Software" must not be used to endorse or promote
021:         *    products derived from this software without prior written permission.
022:         *    For written permission, please contact info@GargoyleSoftware.com.
023:         * 5. Products derived from this software may not be called "HtmlUnit", nor may
024:         *    "HtmlUnit" appear in their name, without prior written permission of
025:         *    Gargoyle Software Inc.
026:         *
027:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028:         * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029:         * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030:         * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037:         */
038:        package com.gargoylesoftware.htmlunit.html;
039:
040:        import java.util.ArrayList;
041:        import java.util.List;
042:
043:        import com.gargoylesoftware.htmlunit.BrowserVersion;
044:        import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
045:        import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
046:        import com.gargoylesoftware.htmlunit.MockWebConnection;
047:        import com.gargoylesoftware.htmlunit.WebClient;
048:        import com.gargoylesoftware.htmlunit.WebTestCase;
049:
050:        /**
051:         * Tests for {@link HtmlScript}.
052:         *
053:         * @version $Revision: 2132 $
054:         * @author Marc Guillemot
055:         * @author Daniel Gredler
056:         */
057:        public class HtmlScriptTest extends WebTestCase {
058:
059:            /**
060:             * Creates an instance.
061:             * @param name The name of the test.
062:             */
063:            public HtmlScriptTest(final String name) {
064:                super (name);
065:            }
066:
067:            /**
068:             * Verifies that a failing HTTP status code for a JavaScript file request (like a 404 response)
069:             * results in a {@link FailingHttpStatusCodeException}, depending on how the client has been
070:             * configured.
071:             *
072:             * @see HtmlPage#loadJavaScriptFromUrl(java.net.URL, String)
073:             * @see WebClient#isThrowExceptionOnFailingStatusCode()
074:             * @throws Exception if an error occurs
075:             */
076:            public void testBadExternalScriptReference() throws Exception {
077:
078:                final String html = "<html><head><title>foo</title>\n"
079:                        + "<script src='inexistent.js'></script>\n"
080:                        + "</head><body></body></html>";
081:
082:                final WebClient client = new WebClient();
083:
084:                final MockWebConnection webConnection = new MockWebConnection(
085:                        client);
086:                webConnection.setDefaultResponse("inexistent", 404,
087:                        "Not Found", "text/html");
088:                webConnection.setResponse(URL_FIRST, html);
089:                client.setWebConnection(webConnection);
090:
091:                try {
092:                    client.getPage(URL_FIRST);
093:                    fail("Should throw.");
094:                } catch (final FailingHttpStatusCodeException e) {
095:                    final String url = URL_FIRST.toExternalForm();
096:                    assertTrue("exception contains url of failing script", e
097:                            .getMessage().indexOf(url) > -1);
098:                    assertEquals(404, e.getStatusCode());
099:                    assertEquals("Not Found", e.getStatusMessage());
100:                }
101:
102:                client.setThrowExceptionOnFailingStatusCode(false);
103:
104:                try {
105:                    client.getPage(URL_FIRST);
106:                } catch (final FailingHttpStatusCodeException e) {
107:                    fail("Should not throw.");
108:                }
109:            }
110:
111:            /**
112:             * @throws Exception If an error occurs.
113:             */
114:            public void testAsText() throws Exception {
115:                final String htmlContent = "<html><head><title>foo</title></head><body>\n"
116:                        + "<script id='script1'>\n"
117:                        + "    var foo = 132;\n"
118:                        + "</script></body></html>";
119:
120:                final HtmlPage page = loadPage(htmlContent);
121:
122:                final HtmlScript script = (HtmlScript) page
123:                        .getHtmlElementById("script1");
124:                assertEquals("", script.asText());
125:            }
126:
127:            /**
128:             * Verifies that the weird script src attribute used by the jQuery JavaScript library is
129:             * ignored silently (bug 1695279).
130:             * @throws Exception If the test fails.
131:             */
132:            public void testInvalidJQuerySrcAttribute() throws Exception {
133:                loadPage("<html><body><script src='//:'></script></body></html>");
134:            }
135:
136:            /**
137:             * Verifies that if a script element executes "window.location.href=someotherpage", then subsequent
138:             * script tags, and any onload handler for the original page do not run.
139:             * @throws Exception If the test fails
140:             */
141:            public void testChangingLocationSkipsFurtherScriptsOnPage()
142:                    throws Exception {
143:                final String firstPage = "<html><head></head>\n"
144:                        + "<body onload='alert(\"body onload executing but should be skipped\")'>\n"
145:                        + "<script>alert('First script executes')</script>\n"
146:                        + "<script>window.location.href='"
147:                        + URL_SECOND
148:                        + "'</script>\n"
149:                        + "<script>alert('Third script executing but should be skipped')</script>\n"
150:                        + "</body></html>";
151:
152:                final String secondPage = "<html><head></head><body>\n"
153:                        + "<script>alert('Second page loading')</script>\n"
154:                        + "</body></html>";
155:
156:                final WebClient client = new WebClient();
157:
158:                final MockWebConnection webConnection = new MockWebConnection(
159:                        client);
160:                webConnection.setResponse(URL_FIRST, firstPage);
161:                webConnection.setResponse(URL_SECOND, secondPage);
162:                client.setWebConnection(webConnection);
163:
164:                final List collectedAlerts = new ArrayList();
165:                client.setAlertHandler(new CollectingAlertHandler(
166:                        collectedAlerts));
167:
168:                client.getPage(URL_FIRST);
169:                final String[] expectedAlerts = { "First script executes",
170:                        "Second page loading" };
171:                assertEquals(expectedAlerts, collectedAlerts);
172:            }
173:
174:            /**
175:             * Verifies that a script element is not run when it is cloned.
176:             * See bug 1707788.
177:             * @throws Exception If an error occurs.
178:             */
179:            public void testScriptIsNotRunWhenCloned() throws Exception {
180:                final String html = "<html><body onload='document.body.cloneNode(true)'>\n"
181:                        + "<script>alert('a')</script></body></html>";
182:                final List collectedAlerts = new ArrayList();
183:                loadPage(html, collectedAlerts);
184:
185:                final String[] expectedAlerts = { "a" };
186:                assertEquals(expectedAlerts, collectedAlerts);
187:            }
188:
189:            /**
190:             * @throws Exception if an error occurs
191:             */
192:            public void testDefer() throws Exception {
193:                final String html = "<html><head>\n"
194:                        + "<script defer>alert('deferred')</script>\n"
195:                        + "<script>alert('normal')</script>\n" + "</head>\n"
196:                        + "<body onload='alert(\"onload\")'>test</body>\n"
197:                        + "</html>";
198:
199:                final List actualFF = new ArrayList();
200:                loadPage(BrowserVersion.FIREFOX_2, html, actualFF);
201:                final String[] expectedFF = new String[] { "deferred",
202:                        "normal", "onload" };
203:                assertEquals(expectedFF, actualFF);
204:
205:                final List actualIE = new ArrayList();
206:                loadPage(BrowserVersion.INTERNET_EXPLORER_7_0, html, actualIE);
207:                final String[] expectedIE = new String[] { "normal",
208:                        "deferred", "onload" };
209:                assertEquals(expectedIE, actualIE);
210:            }
211:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.