1 16 package org.apache.ws.jaxme.junit; 17 18 import java.io.StringReader ; 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 ; 36 37 38 41 public class NestedGroupTest extends BaseTestCase { 42 44 public NestedGroupTest(String pName) { 45 super(pName); 46 } 47 48 private String getPersons() throws Exception { 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 62 public void testNestedGroup() throws Exception { 63 } 65 66 private String getMailTemplate1(boolean pMixed) { 67 final String 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 getMailTemplate2(boolean pMixed) { 86 final String 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 getMailTemplate3(boolean pMixed) { 106 final String 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 129 public void testMailTemplate() throws Exception { 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 149 public void testMailTemplateError() throws Exception { 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 (new StringReader (getMailTemplate3(false)))); 156 } catch (Throwable t) { 157 } 158 assertTrue(h.ok); 159 } 160 161 164 public void testMailTemplateMixed() throws Exception { 165 unmarshalMarshalUnmarshal(MailTemplateMixed.class, getMailTemplate1(true), false); 166 unmarshalMarshalUnmarshal(MailTemplateMixed.class, getMailTemplate2(true), false); 167 } 168 169 171 public void testMailTemplateMixedError() throws Exception { 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 (new StringReader (getMailTemplate3(true)))); 178 } catch (Throwable t) { 179 } 180 assertTrue(h.ok); 181 } 182 183 185 public void testInheritance() throws Exception { 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 |