KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ws.jaxme.junit;
2
3 import java.io.StringReader JavaDoc;
4
5 import javax.xml.bind.JAXBContext;
6 import javax.xml.bind.JAXBException;
7 import javax.xml.bind.ParseConversionEvent;
8 import javax.xml.bind.Unmarshaller;
9 import javax.xml.bind.ValidationEvent;
10 import javax.xml.bind.ValidationEventHandler;
11
12 import org.apache.ws.jaxme.test.misc.types.EAllSimpleTypeAttrs;
13 import org.xml.sax.InputSource JavaDoc;
14
15
16 /** Tests, whether various events are generated.
17  */

18 public class EventsTest extends BaseTestCase {
19     private class Handler implements ValidationEventHandler {
20         private ValidationEvent event;
21         public boolean handleEvent(ValidationEvent pEvent) {
22             event = pEvent;
23             return false;
24         }
25     };
26
27     private ValidationEvent unmarshal(String JavaDoc pXML) throws JAXBException {
28         Handler h = new Handler();
29         JAXBContext context = JAXBContext.newInstance(getPackageName(EAllSimpleTypeAttrs.class));
30         Unmarshaller unmarshaller = context.createUnmarshaller();
31         unmarshaller.setEventHandler(h);
32         try {
33             unmarshaller.unmarshal(new InputSource JavaDoc(new StringReader JavaDoc(pXML)));
34         } catch (JAXBException e) {
35             return h.event;
36         }
37         return null;
38     }
39
40     /** Tests, whether a ConversionEvent is generated.
41      */

42     public void testConversionEvents() throws JAXBException {
43         final String JavaDoc xml = "<ex:EAllSimpleTypeAttrs xmlns:ex='http://ws.apache.org/jaxme/test/misc/types' IntAttr=''/>";
44         ValidationEvent e = unmarshal(xml);
45         assertNotNull(e);
46         assertTrue(e instanceof ParseConversionEvent);
47     }
48 }
49
Popular Tags