Source Code Cross Referenced for MappingTestCase.java in  » EJB-Server-JBoss-4.2.1 » testsuite » org » jboss » test » xml » 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 » EJB Server JBoss 4.2.1 » testsuite » org.jboss.test.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JBoss, Home of Professional Open Source.
003:         * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004:         * as indicated by the @author tags. See the copyright.txt file in the
005:         * distribution for a full listing of individual contributors.
006:         *
007:         * This is free software; you can redistribute it and/or modify it
008:         * under the terms of the GNU Lesser General Public License as
009:         * published by the Free Software Foundation; either version 2.1 of
010:         * the License, or (at your option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this software; if not, write to the Free
019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021:         */
022:        package org.jboss.test.xml;
023:
024:        import org.jboss.logging.Logger;
025:        import org.jboss.xb.binding.DtdMarshaller;
026:        import org.jboss.xb.binding.MappingObjectModelFactory;
027:        import org.jboss.xb.binding.MappingObjectModelProvider;
028:        import org.jboss.xb.binding.Marshaller;
029:        import org.jboss.xb.binding.SimpleTypeBindings;
030:        import org.jboss.xb.binding.Unmarshaller;
031:        import org.jboss.xb.binding.UnmarshallerFactory;
032:        import org.jboss.xb.binding.sunday.MarshallerImpl;
033:        import org.jboss.test.xml.person.Address;
034:        import org.jboss.test.xml.person.Person;
035:        import org.jboss.test.xml.choice.Root;
036:        import org.jboss.test.xml.choice.Choice2;
037:        import org.jboss.test.xml.choice.Choice1;
038:        import org.jboss.test.xml.immutable.Child1;
039:        import org.jboss.test.xml.immutable.Child2;
040:        import org.jboss.test.xml.immutable.Child3;
041:        import org.jboss.test.xml.immutable.Parent;
042:        import org.jboss.test.xml.immutable.ImmutableChoice;
043:        import org.xml.sax.SAXException;
044:
045:        import junit.framework.TestCase;
046:
047:        import java.io.InputStream;
048:        import java.io.StringWriter;
049:        import java.io.Reader;
050:        import java.io.InputStreamReader;
051:        import java.io.StringReader;
052:        import java.io.FileReader;
053:        import java.io.IOException;
054:        import java.util.ArrayList;
055:        import java.util.Arrays;
056:        import java.util.List;
057:        import java.net.URL;
058:
059:        /**
060:         * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
061:         * @version <tt>$Revision: 57211 $</tt>
062:         */
063:        public class MappingTestCase extends TestCase {
064:            private static final Logger log = Logger
065:                    .getLogger(MappingTestCase.class);
066:
067:            public MappingTestCase(String name) {
068:                super (name);
069:            }
070:
071:            public void testUnmarshalling() throws Exception {
072:                Reader xmlReader = new FileReader("resources/xml/person.xml");
073:
074:                Person person = unmarshalPerson(xmlReader);
075:                xmlReader.close();
076:
077:                assertNotNull("Person is null", person);
078:                assertEquals(person.getFirstName(), "Vasiliy");
079:                assertEquals(person.getLastName(), "Poupkin");
080:                assertEquals(person.getDateOfBirth(),
081:                        SimpleTypeBindings.JAVA_UTIL_DATE
082:                                .unmarshal("1980-01-01"));
083:
084:                assertEquals(person.getPhones(), Arrays.asList(new Object[] {
085:                        "01", "02" }));
086:
087:                ArrayList list = new ArrayList();
088:                Address addr1 = new Address();
089:                addr1.setStreet("prosp. Rad. Ukr. 11A, 70");
090:                list.add(addr1);
091:                addr1 = new Address();
092:                addr1.setStreet("Sky 7");
093:                list.add(addr1);
094:                assertEquals(person.getAddresses(), list);
095:            }
096:
097:            public void testMarshalling() throws Exception {
098:                log.debug("<test-mapping-marshalling>");
099:
100:                final Person person = Person.newInstance();
101:                StringWriter xmlOutput = new StringWriter();
102:
103:                InputStream is = getResource("xml/person.dtd");
104:                Reader dtdReader = new InputStreamReader(is);
105:
106:                // create an instance of DTD marshaller
107:                Marshaller marshaller = new DtdMarshaller();
108:
109:                // map publicId to systemId as it should appear in the resulting XML file
110:                marshaller.mapPublicIdToSystemId("-//DTD Person//EN",
111:                        "resources/xml/person.dtd");
112:
113:                // create an instance of ObjectModelProvider with the book instance to be marshalled
114:                MappingObjectModelProvider provider = new MappingObjectModelProvider();
115:                provider.mapFieldToElement(Person.class, "dateOfBirth", "",
116:                        "date-of-birth", SimpleTypeBindings.JAVA_UTIL_DATE);
117:
118:                // marshal the book
119:                marshaller.marshal(dtdReader, provider, person, xmlOutput);
120:
121:                // close DTD reader
122:                dtdReader.close();
123:
124:                final String xml = xmlOutput.getBuffer().toString();
125:                log.debug("marshalled: " + xml);
126:
127:                // check unmarshalled person
128:                Person unmarshalled = unmarshalPerson(new StringReader(xml));
129:                assertEquals(person, unmarshalled);
130:
131:                log.debug("</test-mapping-marshalling>");
132:            }
133:
134:            public void testChoice() throws Exception {
135:                log.debug("testChoice> started");
136:                long startTime = System.currentTimeMillis();
137:
138:                String xsdUrl = getXsd("xml/choice.xsd");
139:                Root root = newChoiceRoot();
140:                String xml = marshalChoiceRoot(xsdUrl, root);
141:
142:                StringReader reader = new StringReader(xml);
143:                Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
144:                        .newUnmarshaller();
145:                Root unmarshalled = (Root) unmarshaller.unmarshal(reader,
146:                        new MappingObjectModelFactory(), null);
147:                log.info("unmarhalled:\n" + unmarshalled);
148:
149:                assertEquals(root, unmarshalled);
150:
151:                log.debug("testChoice> done in "
152:                        + (System.currentTimeMillis() - startTime));
153:            }
154:
155:            public void testImmutable() throws Exception {
156:                log.debug("testImmutable> started");
157:                long startTime = System.currentTimeMillis();
158:
159:                String xsd = getXsd("xml/immutable.xsd");
160:
161:                Parent parent = newImmutableParent();
162:                String xml = marshalImmutableParent(xsd, parent);
163:
164:                StringReader reader = new StringReader(xml);
165:                Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
166:                        .newUnmarshaller();
167:                Parent unmarshalled = (Parent) unmarshaller.unmarshal(reader,
168:                        new MappingObjectModelFactory(), null);
169:                log.info("unmarhalled:\n" + unmarshalled);
170:
171:                assertEquals(parent, unmarshalled);
172:
173:                log.debug("testImmutable> done in "
174:                        + (System.currentTimeMillis() - startTime));
175:            }
176:
177:            // Private
178:
179:            private String marshalImmutableParent(String xsd, Parent parent)
180:                    throws IOException, SAXException {
181:                StringWriter writer = new StringWriter();
182:                MarshallerImpl marshaller = new MarshallerImpl();
183:                marshaller.declareNamespace("imm",
184:                        "http://www.jboss.org/test/xml/immutable/");
185:                marshaller.marshal(xsd, new MappingObjectModelProvider(),
186:                        parent, writer);
187:
188:                log.info("parent:\n" + parent);
189:                log.info("marshalled root:\n" + writer.getBuffer());
190:                return writer.getBuffer().toString();
191:            }
192:
193:            private Parent newImmutableParent() {
194:                Child1 child1 = new Child1("child1");
195:                List child2 = Arrays.asList(new Object[] {
196:                        new Child2("child2_1"), new Child2("child2_2") });
197:                List others = Arrays.asList(new Object[] {
198:                        new Child3("child3_1"), new Child3("child3_2"),
199:                        new Child3("child3_3") });
200:                List choice = Arrays.asList(new Object[] {
201:                        new ImmutableChoice("choice1"),
202:                        new ImmutableChoice(new Child1("child1")) });
203:                Parent parent = new Parent(child1, child2, others, choice);
204:                return parent;
205:            }
206:
207:            private static String getXsd(String path) {
208:                URL xsdUrl = Thread.currentThread().getContextClassLoader()
209:                        .getResource(path);
210:                if (xsdUrl == null) {
211:                    throw new IllegalStateException("XSD not found: " + path);
212:                }
213:                return xsdUrl.getFile();
214:            }
215:
216:            private static String marshalChoiceRoot(String xsdUrl, Root root)
217:                    throws IOException, SAXException {
218:                StringWriter writer = new StringWriter();
219:                MarshallerImpl marshaller = new MarshallerImpl();
220:                marshaller.declareNamespace("chs",
221:                        "http://www.jboss.org/test/xml/choice/");
222:                //marshaller.setProperty(Marshaller.PROP_OUTPUT_INDENTATION, "false");
223:                marshaller.marshal(xsdUrl, new MappingObjectModelProvider(),
224:                        root, writer);
225:
226:                log.info("marshalled root:\n" + writer.getBuffer());
227:                return writer.getBuffer().toString();
228:            }
229:
230:            private static Root newChoiceRoot() {
231:                String a = "a";
232:                String b = "b";
233:                String c = "c";
234:
235:                List choice1 = Arrays.asList(new Choice1[] {
236:                        new Choice1(a, null), new Choice1(null, b) });
237:                List choice2 = Arrays.asList(new Choice2[] {
238:                        new Choice2(a, b, null), new Choice2(a, null, c) });
239:
240:                Root root = new Root();
241:                root.setChoice1(choice1);
242:                root.setChoice2(choice2);
243:                return root;
244:            }
245:
246:            private Person unmarshalPerson(Reader xmlReader) throws Exception {
247:                MappingObjectModelFactory factory = new MappingObjectModelFactory();
248:                factory.mapElementToClass("person", Person.class);
249:                factory.mapElementToField("date-of-birth", Person.class,
250:                        "dateOfBirth", SimpleTypeBindings.JAVA_UTIL_DATE);
251:                factory.mapElementToClass("phones", ArrayList.class);
252:                factory.mapElementToClass("addresses", ArrayList.class);
253:                factory.mapElementToClass("address", Address.class);
254:
255:                Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
256:                        .newUnmarshaller();
257:
258:                return (Person) unmarshaller
259:                        .unmarshal(xmlReader, factory, null);
260:            }
261:
262:            private static InputStream getResource(String name) {
263:                InputStream is = Thread.currentThread().getContextClassLoader()
264:                        .getResourceAsStream(name);
265:                if (is == null) {
266:                    throw new IllegalStateException("Resource not found: "
267:                            + name);
268:                }
269:                return is;
270:            }
271:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.