Source Code Cross Referenced for WidgetTestHelper.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » cocoon » forms » formmodel » 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 » Content Management System » apache lenya 2.0 » org.apache.cocoon.forms.formmodel 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.cocoon.forms.formmodel;
019:
020:        import java.util.Locale;
021:
022:        import javax.xml.parsers.DocumentBuilder;
023:        import javax.xml.parsers.DocumentBuilderFactory;
024:
025:        import junit.framework.Assert;
026:
027:        import org.apache.avalon.framework.service.ServiceManager;
028:        import org.apache.cocoon.forms.FormsConstants;
029:        import org.apache.cocoon.forms.FormManager;
030:        import org.apache.cocoon.xml.AttributesImpl;
031:        import org.apache.cocoon.xml.dom.DOMBuilder;
032:        import org.apache.commons.jxpath.JXPathContext;
033:        import org.apache.commons.jxpath.Pointer;
034:        import org.w3c.dom.Document;
035:        import org.xml.sax.SAXException;
036:        import org.xml.sax.helpers.NamespaceSupport;
037:
038:        /**
039:         * Helper class to build Widget test cases.
040:         * 
041:         * @version $Id: WidgetTestHelper.java 449149 2006-09-23 03:58:05Z crossley $
042:         */
043:        public class WidgetTestHelper {
044:
045:            // Private constructor as we only have static methods
046:            private WidgetTestHelper() {
047:            }
048:
049:            /**
050:             * Get the result of a widget's generateSaxFragment() method as a Document.
051:             * <p>
052:             * The widget's fragment is encapsulated in a root &lt;fi:fragment&gt; element,
053:             * since there's no guarantee that a widget outputs a single top-level element
054:             * (there can be several elements, or even none if the widget is invisible)
055:             * 
056:             * @param widget the widget of which we want the fragment
057:             * @param locale the locale to be used to generate the fragment
058:             * @return the document containing the fragment
059:             */
060:            public static Document getWidgetFragment(Widget widget,
061:                    Locale locale) throws SAXException {
062:
063:                DOMBuilder domBuilder = new DOMBuilder();
064:                // Start document and "fi:fragment" root element
065:                domBuilder.startDocument();
066:                domBuilder.startPrefixMapping(FormsConstants.INSTANCE_PREFIX,
067:                        FormsConstants.INSTANCE_NS);
068:                // FIXME: why simply declaring the prefix isn't enough?
069:                AttributesImpl attr = new AttributesImpl();
070:                attr.addCDATAAttribute(NamespaceSupport.XMLNS, "fi:",
071:                        "xmlns:fi", FormsConstants.INSTANCE_NS);
072:                domBuilder
073:                        .startElement(FormsConstants.INSTANCE_NS, "fragment",
074:                                FormsConstants.INSTANCE_PREFIX_COLON
075:                                        + "fragment", attr);
076:
077:                widget.generateSaxFragment(domBuilder, locale);
078:
079:                // End "fi:fragment" element and document
080:                domBuilder.endElement(FormsConstants.INSTANCE_NS, "fragment",
081:                        FormsConstants.INSTANCE_PREFIX_COLON + "fragment");
082:                domBuilder.endPrefixMapping(FormsConstants.INSTANCE_PREFIX);
083:                domBuilder.endDocument();
084:
085:                // Return the document
086:                return domBuilder.getDocument();
087:            }
088:
089:            public static void assertXPathEquals(String expected, String xpath,
090:                    Document doc) {
091:                // use xpath as the message
092:                assertXPathEquals(xpath, expected, xpath, doc);
093:            }
094:
095:            public static void assertXPathEquals(String message,
096:                    String expected, String xpath, Document doc) {
097:                JXPathContext ctx = JXPathContext.newContext(doc);
098:                ctx.setLenient(true);
099:                Assert.assertEquals(message, expected, ctx.getValue(xpath));
100:            }
101:
102:            public static void assertXPathExists(String xpath, Document doc) {
103:                // use xpath as message
104:                assertXPathExists(xpath, xpath, doc);
105:            }
106:
107:            public static void assertXPathExists(String message, String xpath,
108:                    Document doc) {
109:                JXPathContext ctx = JXPathContext.newContext(doc);
110:                ctx.setLenient(true);
111:                Pointer pointer = ctx.getPointer(xpath);
112:                Assert.assertNotNull(message, pointer.getNode());
113:            }
114:
115:            public static void assertXPathNotExists(String xpath, Document doc) {
116:                // use xpath as message
117:                assertXPathNotExists(xpath, xpath, doc);
118:            }
119:
120:            public static void assertXPathNotExists(String message,
121:                    String xpath, Document doc) {
122:                JXPathContext ctx = JXPathContext.newContext(doc);
123:                ctx.setLenient(true);
124:                Pointer pointer = ctx.getPointer(xpath);
125:                Assert.assertNull(message, pointer.getNode());
126:            }
127:
128:            /**
129:             * Load a Form whose definition relative to a given object (typically, the TestCase class).
130:             * 
131:             * @param manager the ServiceManager that will be used to create the form
132:             * @param obj the object relative to which the resource will be read
133:             * @param resource the relative resource name for the form definition
134:             * @return the Form
135:             * @throws Exception
136:             */
137:            public static Form loadForm(ServiceManager manager, Object obj,
138:                    String resource) throws Exception {
139:                // Load the document
140:                DocumentBuilderFactory factory = DocumentBuilderFactory
141:                        .newInstance();
142:                // Grmbl... why isn't this true by default?
143:                factory.setNamespaceAware(true);
144:                DocumentBuilder parser = factory.newDocumentBuilder();
145:                Document doc = parser.parse(obj.getClass()
146:                        .getResource(resource).toExternalForm());
147:
148:                // Create the form
149:                FormManager formManager = (FormManager) manager
150:                        .lookup(FormManager.ROLE);
151:                try {
152:                    return formManager.createForm(doc.getDocumentElement());
153:                } finally {
154:                    manager.release(formManager);
155:                }
156:            }
157:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.