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.net.URL;
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.List;
044:
045: import org.apache.commons.lang.builder.EqualsBuilder;
046:
047: import com.gargoylesoftware.htmlunit.MockWebConnection;
048: import com.gargoylesoftware.htmlunit.WebClient;
049: import com.gargoylesoftware.htmlunit.WebTestCase;
050:
051: /**
052: * Test class for {@link HTMLParserListener}.<br/>
053: * We probably don't need to check the details of the messages generated by the
054: * parser but just that we catch and "transmit" them.
055: *
056: * @version $Revision: 2132 $
057: * @author Marc Guillemot
058: */
059: public class HTMLParserListenerTest extends WebTestCase {
060: static class MessageInfo {
061: private boolean error_; // versus warning
062: private String message_;
063: private URL url_;
064: private int line_;
065: private int column_;
066:
067: /**
068: * Utility class to hold data.
069: * @param error The error
070: * @param message The message
071: * @param url The url
072: * @param line The line number
073: * @param column The column number
074: * @param key Ignored value
075: */
076: MessageInfo(final boolean error, final String message,
077: final URL url, final int line, final int column,
078: final String key) {
079: error_ = error;
080: message_ = message;
081: url_ = url;
082: line_ = line;
083: column_ = column;
084: // ignore key
085: }
086:
087: /** @see Object#toString() */
088: public String toString() {
089: return message_ + " (" + url_ + " " + line_ + ":" + column_
090: + ")";
091: }
092:
093: /**
094: * Compares according to error, message, url and line.
095: * @see Object#equals(Object)
096: */
097: public boolean equals(final Object obj) {
098: if (!(obj instanceof MessageInfo)) {
099: return false;
100: }
101: final MessageInfo other = (MessageInfo) obj;
102: final EqualsBuilder builder = new EqualsBuilder();
103: builder.append(error_, other.error_);
104: builder.append(message_, other.message_);
105: builder.append(url_.toExternalForm(), other.url_
106: .toExternalForm());
107: builder.append(line_, other.line_);
108: return builder.isEquals();
109: }
110:
111: /**
112: * Stub to fix Eclipse warning
113: * @see Object#hashCode()
114: */
115: public int hashCode() {
116: return super .hashCode();
117: }
118: }
119:
120: /**
121: * Create an instance
122: * @param name The name of the test
123: */
124: public HTMLParserListenerTest(final String name) {
125: super (name);
126: }
127:
128: /**
129: * @exception Exception If the test fails
130: */
131: public void testSimple() throws Exception {
132: final String htmlContent = "<html>\n"
133: + "<head>\n<title>foo\n</head>\n"
134: + "<body>\nfoo\n</body>\n</html>";
135:
136: final WebClient webClient = new WebClient();
137: assertNull(webClient.getHTMLParserListener());
138: WebClient.setIgnoreOutsideContent(true);
139:
140: final List messages = new ArrayList();
141: final HTMLParserListener collecter = new HTMLParserListener() {
142: public void error(final String message, final URL url,
143: final int line, final int column, final String key) {
144: messages.add(new MessageInfo(true, message, url, line,
145: column, key));
146: }
147:
148: public void warning(final String message, final URL url,
149: final int line, final int column, final String key) {
150: messages.add(new MessageInfo(false, message, url, line,
151: column, key));
152: }
153: };
154: webClient.setHTMLParserListener(collecter);
155:
156: final MockWebConnection webConnection = new MockWebConnection(
157: webClient);
158: webConnection.setDefaultResponse(htmlContent);
159: webClient.setWebConnection(webConnection);
160:
161: final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST);
162: assertEquals("foo", page.getTitleText());
163:
164: // ignore column and key
165: final MessageInfo expectedError = new MessageInfo(
166: false,
167: "End element <head> automatically closes element <title>.",
168: URL_FIRST, 4, -1, null);
169: assertEquals(Collections.singletonList(expectedError), messages);
170: }
171: }
|