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.lang.reflect.Constructor;
041: import java.lang.reflect.InvocationTargetException;
042: import java.lang.reflect.Method;
043: import java.util.ArrayList;
044: import java.util.List;
045: import java.util.Map;
046:
047: import junit.framework.Test;
048: import junit.framework.TestSuite;
049:
050: import com.gargoylesoftware.htmlunit.WebTestCase;
051:
052: /**
053: * <p>Tests for all the generated attribute accessors. This test case will
054: * dynamically generate tests for all the various attributes. The code
055: * is fairly complicated but doing it this way is much easier than writing
056: * individual tests for all the attributes.
057: * </p>
058: * With the new custom DOM, this test has somewhat lost its significance.
059: * We simply set and get the attributes and compare the results.
060: *
061: * @version $Revision: 2132 $
062: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
063: * @author Christian Sell
064: * @author Marc Guillemot
065: * @author Ahmed Ashour
066: */
067: public class AttributesTest extends WebTestCase {
068:
069: private final Class classUnderTest_;
070: private final Method method_;
071: private final HtmlPage page_;
072: private final String attributeName_;
073:
074: private static final List EXCLUDED_METHODS = new ArrayList();
075: static {
076: EXCLUDED_METHODS.add("getHtmlElementsByAttribute");
077: EXCLUDED_METHODS.add("getOneHtmlElementByAttribute");
078: EXCLUDED_METHODS.add("getAttribute");
079: }
080:
081: /**
082: * Return a test suite containing a separate test for each attribute
083: * on each element.
084: *
085: * @return The test suite
086: * @throws Exception If the tests cannot be created.
087: */
088: public static Test suite() throws Exception {
089: final HtmlPage page = loadPage("<html><head><title>foo</title></head><body></body></html>");
090:
091: final TestSuite suite = new TestSuite();
092: final String[] classesToTest = { "HtmlAddress", "HtmlAnchor",
093: "HtmlApplet", "HtmlArea", "HtmlBase", "HtmlBaseFont",
094: "HtmlBidirectionalOverride", "HtmlBlockQuote",
095: "HtmlBody", "HtmlBreak", "HtmlButton",
096: "HtmlButtonInput", "HtmlCaption", "HtmlCenter",
097: "HtmlCheckBoxInput", "HtmlDefinitionDescription",
098: "HtmlDefinitionList", "HtmlDefinitionTerm",
099: "HtmlDeletedText", "HtmlDivision", "HtmlElement",
100: "HtmlFieldSet", "HtmlFileInput", "HtmlFont",
101: "HtmlForm", "HtmlFrame", "HtmlFrameSet", "HtmlHead",
102: "HtmlHeader1", "HtmlHeader2", "HtmlHeader3",
103: "HtmlHeader4", "HtmlHeader5", "HtmlHeader6",
104: "HtmlHiddenInput", "HtmlHorizontalRule", "HtmlImage",
105: "HtmlImageInput", "HtmlInlineFrame",
106: "HtmlInlineQuotation", "HtmlInsertedText",
107: "HtmlIsIndex", "HtmlLabel", "HtmlLegend", "HtmlLink",
108: "HtmlListItem", "HtmlMap", "HtmlMenu", "HtmlMeta",
109: "HtmlNoFrames", "HtmlNoScript", "HtmlObject",
110: "HtmlOption", "HtmlOptionGroup", "HtmlOrderedList",
111: /*"HtmlPage",*/"HtmlParagraph", "HtmlParameter",
112: "HtmlPasswordInput", "HtmlPreformattedText",
113: "HtmlRadioButtonInput", "HtmlResetInput", "HtmlScript",
114: "HtmlSelect", "HtmlSpan", "HtmlStyle",
115: "HtmlSubmitInput", "HtmlTable", "HtmlTableBody", /*"HtmlTableCell",*/
116: "HtmlTableColumn", "HtmlTableColumnGroup",
117: "HtmlTableDataCell", "HtmlTableFooter",
118: "HtmlTableHeader", "HtmlTableHeaderCell",
119: "HtmlTableRow", "HtmlTextArea", "HtmlTextDirection",
120: "HtmlTextInput", "HtmlTitle", "HtmlUnorderedList" };
121: for (int i = 0; i < classesToTest.length; i++) {
122: final Class clazz = Class
123: .forName("com.gargoylesoftware.htmlunit.html."
124: + classesToTest[i]);
125: addTestsForClass(clazz, page, suite);
126: }
127: return suite;
128: }
129:
130: /**
131: * Add all the tests for a given class.
132: *
133: * @param clazz The class to create tests for.
134: * @param page The HtmlPage that will be passed into the constructor of the
135: * objects to be tested.
136: * @param suite The suite that all the tests will be placed inside.
137: * @throws Exception If the tests cannot be created.
138: */
139: private static void addTestsForClass(final Class clazz,
140: final HtmlPage page, final TestSuite suite)
141: throws Exception {
142:
143: final Method[] methods = clazz.getMethods();
144: for (int i = 0; i < methods.length; i++) {
145: final String methodName = methods[i].getName();
146: if (methodName.startsWith("get")
147: && methodName.endsWith("Attribute")
148: && !EXCLUDED_METHODS.contains(methodName)) {
149:
150: String attributeName = methodName.substring(3,
151: methodName.length() - 9).toLowerCase();
152: if (attributeName.equals("xmllang")) {
153: attributeName = "xml:lang";
154: } else if (attributeName.equals("columns")) {
155: attributeName = "cols";
156: } else if (attributeName.equals("columnspan")) {
157: attributeName = "colspan";
158: } else if (attributeName.equals("textdirection")) {
159: attributeName = "dir";
160: } else if (attributeName.equals("httpequiv")) {
161: attributeName = "http-equiv";
162: } else if (attributeName.equals("acceptcharset")) {
163: attributeName = "accept-charset";
164: } else if (attributeName.equals("htmlfor")) {
165: attributeName = "for";
166: }
167: suite.addTest(new AttributesTest(attributeName, clazz,
168: methods[i], page));
169: }
170: }
171: }
172:
173: /**
174: * Create an instance of the test. This will test one specific attribute
175: * on one specific class.
176: * @param attributeName The name of the attribute to test.
177: * @param classUnderTest The class containing the attribute.
178: * @param method The "getter" method for the specified attribute.
179: * @param page The page that will be passed into the constructor of the object
180: * to be tested.
181: */
182: public AttributesTest(final String attributeName,
183: final Class classUnderTest, final Method method,
184: final HtmlPage page) {
185:
186: super (createTestName(classUnderTest, method));
187: classUnderTest_ = classUnderTest;
188: method_ = method;
189: page_ = page;
190: attributeName_ = attributeName;
191: }
192:
193: /**
194: * Create a name for this particular test that reflect the attribute being tested.
195: * @param clazz The class containing the attribute.
196: * @param method The getter method for the attribute.
197: * @return The new name.
198: */
199: private static String createTestName(final Class clazz,
200: final Method method) {
201: String className = clazz.getName();
202: final int index = className.lastIndexOf('.');
203: className = className.substring(index + 1);
204:
205: return "testAttributes_" + className + '_' + method.getName();
206: }
207:
208: /**
209: * Run the actual test.
210: * @throws Exception If the test fails.
211: */
212: protected void runTest() throws Exception {
213: final String value = new String("value");
214:
215: final HtmlElement objectToTest = (HtmlElement) getNewInstanceForClassUnderTest();
216: objectToTest.setAttributeValue(attributeName_, value);
217:
218: final Object noObjects[] = new Object[0];
219: final Object result = method_.invoke(objectToTest, noObjects);
220: assertSame(value, result);
221: }
222:
223: /**
224: * Create a new instance of the class being tested.
225: * @return The new instance.
226: * @throws Exception If the new object cannot be created.
227: */
228: private Object getNewInstanceForClassUnderTest() throws Exception {
229: final Object newInstance;
230: if (classUnderTest_ == HtmlTableRow.class) {
231: newInstance = HTMLParser.getFactory(HtmlTableRow.TAG_NAME)
232: .createElement(page_, HtmlTableRow.TAG_NAME, null);
233: } else if (classUnderTest_ == HtmlTableHeaderCell.class) {
234: newInstance = HTMLParser.getFactory(
235: HtmlTableHeaderCell.TAG_NAME).createElement(page_,
236: HtmlTableHeaderCell.TAG_NAME, null);
237: } else if (classUnderTest_ == HtmlTableDataCell.class) {
238: newInstance = HTMLParser.getFactory(
239: HtmlTableDataCell.TAG_NAME).createElement(page_,
240: HtmlTableDataCell.TAG_NAME, null);
241: } else {
242: final Constructor constructor = classUnderTest_
243: .getDeclaredConstructor(new Class[] {
244: HtmlPage.class, Map.class });
245: try {
246: newInstance = constructor.newInstance(new Object[] {
247: page_, null });
248: } catch (final InvocationTargetException e) {
249: final Throwable targetException = e
250: .getTargetException();
251: if (targetException instanceof Exception) {
252: throw (Exception) targetException;
253: } else if (targetException instanceof Error) {
254: throw (Error) targetException;
255: } else {
256: throw e;
257: }
258: }
259: }
260:
261: return newInstance;
262: }
263: }
|