1   package org.apache.ws.scout.registry;
2   
3   import static junit.framework.Assert.fail;
4   import static org.junit.Assert.assertEquals;
5   
6   import java.io.StringReader;
7   import java.util.List;
8   
9   import javax.xml.bind.JAXBContext;
10  import javax.xml.bind.JAXBElement;
11  import javax.xml.bind.JAXBException;
12  import javax.xml.bind.Unmarshaller;
13  import javax.xml.parsers.DocumentBuilder;
14  import javax.xml.parsers.DocumentBuilderFactory;
15  import javax.xml.transform.stream.StreamSource;
16  
17  import org.apache.ws.scout.model.uddi.v2.AssertionStatusReport;
18  import org.junit.Test;
19  import org.w3c.dom.Document;
20  import org.xml.sax.InputSource;
21  
22  public class JAXBTest {
23  
24  	private static String PUBLISHER_ASSERTION_RESPONSE="<assertionStatusReport generic=\"2.0\" operator=\"jUDDI.org\" xmlns=\"urn:uddi-org:api_v2\"><assertionStatusItem completionStatus=\"status:fromKey_incomplete\"><fromKey>5173FA70-81E6-11DE-B7B9-A9A7A2431DC4</fromKey><toKey>517AD840-81E6-11DE-B7B9-C5758FAC28A0</toKey><keyedReference keyName=\"Concept\" keyValue=\"Implements\" tModelKey=\"UUID:DB77450D-9FA8-45D4-A7BC-04411D14E384\"/><keysOwned><fromKey>5173FA70-81E6-11DE-B7B9-A9A7A2431DC4</fromKey></keysOwned></assertionStatusItem></assertionStatusReport>";
25      private static String FROM_KEY="5173FA70-81E6-11DE-B7B9-A9A7A2431DC4";
26  	/**
27  	 * Test handling of utf8 characters
28  	 */
29  	@Test
30  	public void unmarshallUTF8()
31  	{
32  		try {
33  			JAXBContext jaxbContext=JAXBContext.newInstance("org.apache.ws.scout.model.uddi.v2");
34  			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
35  			StringReader reader = new StringReader(PUBLISHER_ASSERTION_RESPONSE);
36  			JAXBElement<AssertionStatusReport> utf8Element = unMarshaller.unmarshal(new StreamSource(reader),AssertionStatusReport.class);
37  			List<org.apache.ws.scout.model.uddi.v2.AssertionStatusItem> items =  utf8Element.getValue().getAssertionStatusItem();
38  			System.out.println(items);
39  			assertEquals(FROM_KEY,items.get(0).getFromKey());
40  		} catch (JAXBException jaxbe) {
41  			fail("No exception should be thrown");
42  		}
43  	}
44  	
45  	@Test
46  	public void unmarshallElement()
47  	{
48  		try {
49  			JAXBContext jaxbContext=JAXBContext.newInstance("org.apache.ws.scout.model.uddi.v2");
50  			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
51  			
52  			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
53  			factory.setNamespaceAware(true);
54  		    DocumentBuilder db = factory.newDocumentBuilder();
55  		    InputSource is = new InputSource();
56  		    is.setCharacterStream(new StringReader(PUBLISHER_ASSERTION_RESPONSE));
57  		    Document document = db.parse(is);
58  		    
59  			JAXBElement<AssertionStatusReport> utf8Element = unMarshaller.unmarshal(document,AssertionStatusReport.class);
60  			List<org.apache.ws.scout.model.uddi.v2.AssertionStatusItem> items =  utf8Element.getValue().getAssertionStatusItem();
61  			System.out.println(items);
62  			assertEquals(FROM_KEY,items.get(0).getFromKey());
63  		} catch (Exception jaxbe) {
64  			fail("No exception should be thrown");
65  		}
66  	}
67  	
68  	
69  	
70  
71  
72  }