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.Collections;
042: import java.util.List;
043:
044: import com.gargoylesoftware.htmlunit.WebTestCase;
045:
046: /**
047: * Tests for {@link HtmlAttr}.
048: *
049: * @version $Revision: 2132 $
050: * @author Denis N. Antonioli
051: * @author Ahmed Ashour
052: * @author David K. Taylor
053: */
054: public class HtmlAttrTest extends WebTestCase {
055: /**
056: * Test object.
057: */
058: private HtmlAttr htmlAttr_;
059: /**
060: * Single test key value.
061: */
062: private static final String ENTRY_KEY = "key";
063: /**
064: * Single test attribute value.
065: */
066: private static final String ENTRY_VALUE = "value";
067:
068: /**
069: * A single dummy HtmlElement. Necessary, because HtmlAttr's constructor calls the method getPage().
070: */
071: static final HtmlElement HTML_ELEMENT;
072:
073: static {
074: HTML_ELEMENT = new HtmlElement(null, "dummy", null,
075: Collections.EMPTY_MAP) {
076: private static final long serialVersionUID = -3099722791571459332L;
077:
078: public HtmlPage getPage() {
079: return null;
080: }
081: };
082: }
083:
084: /**
085: * Create an instance
086: *
087: * @param name The name of the test
088: */
089: public HtmlAttrTest(final String name) {
090: super (name);
091: }
092:
093: /**
094: * {@inheritDoc}
095: */
096: protected void setUp() throws Exception {
097: super .setUp();
098: htmlAttr_ = new HtmlAttr(null, null, ENTRY_KEY, ENTRY_VALUE);
099: htmlAttr_.setParentNode(HTML_ELEMENT);
100: }
101:
102: /**
103: */
104: public void testGetName() {
105: assertEquals(ENTRY_KEY, htmlAttr_.getName());
106: }
107:
108: /**
109: */
110: public void testGetNodeName() {
111: assertEquals(ENTRY_KEY, htmlAttr_.getNodeName());
112: }
113:
114: /**
115: */
116: public void testGetNodeType() {
117: assertEquals(org.w3c.dom.Node.ATTRIBUTE_NODE, htmlAttr_
118: .getNodeType());
119: }
120:
121: /**
122: */
123: public void testGetNodeValue() {
124: assertEquals(ENTRY_VALUE, htmlAttr_.getNodeValue());
125: }
126:
127: /**
128: */
129: public void testGetKey() {
130: assertEquals(ENTRY_KEY, htmlAttr_.getName());
131: }
132:
133: /**
134: */
135: public void testGetValue() {
136: assertEquals(ENTRY_VALUE, htmlAttr_.getHtmlValue());
137: }
138:
139: /**
140: */
141: public void testSetValue() {
142: htmlAttr_.setHtmlValue("foo");
143: assertEquals("foo", htmlAttr_.getHtmlValue());
144: }
145:
146: /**
147: */
148: public void testGetParent() {
149: assertSame(HTML_ELEMENT, htmlAttr_.getParentDomNode());
150: }
151:
152: /**
153: * Test nodeType of {@link Attribute}.
154: *
155: * @throws Exception if the test fails
156: */
157: public void testNodeType() throws Exception {
158: final String content = "<html><head><title>foo</title><script>\n"
159: + " function test() {\n"
160: + " var attr = document.createAttribute('myAttrib');\n"
161: + " alert(attr.nodeType);\n"
162: + " }\n"
163: + "</script></head><body onload='test()'>\n"
164: + "</body></html>";
165: final String[] expectedAlerts = { "2" };
166: final List collectedAlerts = new ArrayList();
167: loadPage(content, collectedAlerts);
168: assertEquals(expectedAlerts, collectedAlerts);
169: }
170: }
|