Source Code Cross Referenced for AttributesTest.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.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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.