KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > junit > NestedGroupTest


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.ws.jaxme.junit;
17
18 import java.io.StringReader JavaDoc;
19
20 import javax.xml.bind.JAXBContext;
21 import javax.xml.bind.Unmarshaller;
22 import javax.xml.bind.ValidationEvent;
23 import javax.xml.bind.ValidationEventHandler;
24
25 import org.apache.ws.jaxme.ValidationEvents;
26 import org.apache.ws.jaxme.impl.ValidationEventImpl;
27 import org.apache.ws.jaxme.test.misc.group.impl.PersonsImpl;
28 import org.apache.ws.jaxme.test.misc.types.FsDirectory;
29 import org.apache.ws.jaxme.test.misc.types.FsDirectoryType;
30 import org.apache.ws.jaxme.test.misc.types.FsFile;
31 import org.apache.ws.jaxme.test.misc.types.FsNode;
32 import org.apache.ws.jaxme.test.nestedgroups.MailTemplate;
33 import org.apache.ws.jaxme.test.nestedgroups.MailTemplateMixed;
34 import org.apache.ws.jaxme.test.nestedgroups.impl.MailTemplateImpl;
35 import org.xml.sax.InputSource JavaDoc;
36
37
38 /**
39  * @author <a HREF="mailto:iasandcb@tmax.co.kr">Ias</a>
40  */

41 public class NestedGroupTest extends BaseTestCase {
42     /** Creates a new instance with the given name.
43      */

44     public NestedGroupTest(String JavaDoc pName) {
45         super(pName);
46     }
47
48     private String JavaDoc getPersons() throws Exception JavaDoc {
49         return
50         "<Persons xmlns=\"" + new PersonsImpl().getQName().getNamespaceURI() + "\">\n"
51         + " <Person Alias=\"Ias\">\n"
52         + " <FirstName>Changshin</FirstName>\n"
53         + " <LastName>Lee</LastName>\n"
54         + " <Age>30</Age>\n"
55         + " </Person>\n"
56         + "</Persons>";
57     }
58     
59     /** Tests unmarshalling and marshalling of the
60      * document returned by {@link #getPersons()}.
61      */

62     public void testNestedGroup() throws Exception JavaDoc {
63         //unmarshalMarshalUnmarshal(Persons.class, getPersons());
64
}
65
66     private String JavaDoc getMailTemplate1(boolean pMixed) {
67         final String JavaDoc a, b, c, d, name;
68         if (pMixed) {
69             name = "Mixed";
70             a = "abc";
71             b = "bcd";
72             c = "cde";
73             d = "def";
74         } else {
75             name = a = b = c = d = "";
76         }
77         return
78         "<ng:MailTemplate" + name + " name=\"foo\" language=\"bar\""
79         + " xmlns:ng=\"" + new MailTemplateImpl().getQName().getNamespaceURI() + "\">\n"
80         + " " + a + "<ng:subject>A test subject</ng:subject>" + b + "\n"
81         + " " + c + "<ng:body>A test body</ng:body>" + d + "\n"
82         + "</ng:MailTemplate" + name + ">";
83     }
84
85     private String JavaDoc getMailTemplate2(boolean pMixed) {
86         final String JavaDoc a, b, c, d, name;
87         if (pMixed) {
88             name = "Mixed";
89             a = "abc";
90             b = "bcd";
91             c = "cde";
92             d = "def";
93         } else {
94             name = a = b = c = d = "";
95         }
96         return
97         "<ng:MailTemplate" + name + " name=\"foo\" language=\"bar\""
98         + " xmlns:ng=\"" + new MailTemplateImpl().getQName().getNamespaceURI() + "\">\n"
99         + " " + a + "<ng:subject>A test subject</ng:subject>" + d + "\n"
100         + " " + b + "<ng:prepend>A prefix</ng:prepend>\n"
101         + " " + c + "<ng:append>A suffix</ng:append>\n"
102         + "</ng:MailTemplate" + name + ">";
103     }
104
105     private String JavaDoc getMailTemplate3(boolean pMixed) {
106         final String JavaDoc a, b, c, d, name;
107         if (pMixed) {
108             name = "Mixed";
109             a = "abc";
110             b = "bcd";
111             c = "cde";
112             d = "def";
113         } else {
114             name = a = b = c = d = "";
115         }
116         return
117         "<ng:MailTemplate" + name + " name=\"foo\" language=\"bar\""
118         + " xmlns:ng=\"" + new MailTemplateImpl().getQName().getNamespaceURI() + "\">\n"
119         + " " + a + "<ng:subject>A test subject</ng:subject>" + d + "\n"
120         + " " + b + "<ng:prepend>A prefix</ng:prepend>\n"
121         + " " + c + "<ng:append>A suffix</ng:append>\n"
122         + " <ng:body>A test body</ng:body>\n"
123         + "</ng:MailTemplate" + name + ">";
124     }
125
126     /** Tests unmarshalling and marshalling of an instance of
127      * {@link MailTemplate}.
128      */

129     public void testMailTemplate() throws Exception JavaDoc {
130         unmarshalMarshalUnmarshal(MailTemplate.class, getMailTemplate1(false));
131         unmarshalMarshalUnmarshal(MailTemplate.class, getMailTemplate2(false));
132     }
133
134     private class MyEventHandler implements ValidationEventHandler {
135         private boolean ok;
136         public boolean handleEvent(ValidationEvent pEvent) {
137             if (pEvent instanceof ValidationEventImpl) {
138                 ValidationEventImpl ev = (ValidationEventImpl) pEvent;
139                 if (ValidationEvents.EVENT_CHOICE_GROUP_REUSE.equals(ev.getErrorCode())) {
140                     ok = true;
141                 }
142             }
143             return false;
144         }
145     }
146
147     /** Tests proper handling of the choice group.
148      */

149     public void testMailTemplateError() throws Exception JavaDoc {
150         JAXBContext context = JAXBContext.newInstance(getPackageName(MailTemplate.class));
151         Unmarshaller unmarshaller = context.createUnmarshaller();
152         MyEventHandler h = new MyEventHandler();
153         unmarshaller.setEventHandler(h);
154         try {
155             unmarshaller.unmarshal(new InputSource JavaDoc(new StringReader JavaDoc(getMailTemplate3(false))));
156         } catch (Throwable JavaDoc t) {
157         }
158         assertTrue(h.ok);
159     }
160
161     /** Tests unmarshalling and marshalling of an instance of
162      * {@link MailTemplateMixed}.
163      */

164     public void testMailTemplateMixed() throws Exception JavaDoc {
165         unmarshalMarshalUnmarshal(MailTemplateMixed.class, getMailTemplate1(true), false);
166         unmarshalMarshalUnmarshal(MailTemplateMixed.class, getMailTemplate2(true), false);
167     }
168
169     /** Tests proper handling of the choice group.
170      */

171     public void testMailTemplateMixedError() throws Exception JavaDoc {
172         JAXBContext context = JAXBContext.newInstance(getPackageName(MailTemplateMixed.class));
173         Unmarshaller unmarshaller = context.createUnmarshaller();
174         MyEventHandler h = new MyEventHandler();
175         unmarshaller.setEventHandler(h);
176         try {
177             unmarshaller.unmarshal(new InputSource JavaDoc(new StringReader JavaDoc(getMailTemplate3(true))));
178         } catch (Throwable JavaDoc t) {
179         }
180         assertTrue(h.ok);
181     }
182
183     /** Tests proper inheritance of xs:extension.
184      */

185     public void testInheritance() throws Exception JavaDoc {
186         assertTrue(FsNode.class.isAssignableFrom(FsFile.class));
187         assertTrue(FsNode.class.isAssignableFrom(FsDirectory.class));
188         assertTrue(FsNode.class.isAssignableFrom(FsDirectoryType.class));
189     }
190 }
191
Popular Tags