This project has retired. For details please refer to its Attic page.
AuthInfoTest xref
View Javadoc
1   /*
2    * Copyright 2001-2009 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    *      http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.uddi.api_v3;
16  
17  import java.io.StringReader;
18  import java.io.StringWriter;
19  
20  import javax.xml.bind.JAXBContext;
21  import javax.xml.bind.JAXBElement;
22  import javax.xml.bind.JAXBException;
23  import javax.xml.bind.Marshaller;
24  import javax.xml.bind.Unmarshaller;
25  import javax.xml.namespace.QName;
26  import javax.xml.transform.stream.StreamSource;
27  
28  import static junit.framework.Assert.fail;
29  import static junit.framework.Assert.assertEquals;
30  import static junit.framework.Assert.assertTrue;
31  
32  import org.junit.Test;
33  import org.uddi.api_v3.AuthToken;
34  import org.uddi.api_v3.ObjectFactory;
35  
36  /**
37   * Testing marshalling functionality, making sure UTF-8 is handled correctly.
38   * 
39   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
40   */
41  public class AuthInfoTest {
42  
43  	private final static String EXPECTED_XML_FRAGMENT1 = "<fragment xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\" xmlns:ns3=\"urn:uddi-org:api_v3\">\n"
44                                                         +"    <ns3:authInfo>AuthInfo String</ns3:authInfo>\n"
45                                                         +"</fragment>";
46  	private final static String EXPECTED_XML_FRAGMENT2 = "<fragment xmlns:ns3=\"urn:uddi-org:api_v3\" xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\">\n"
47          +"    <ns3:authInfo>AuthInfo String</ns3:authInfo>\n"
48          +"</fragment>";
49  	private final static String UTF8_WORD = "メインページ";
50  
51  	private final static String EXPECTED_UTF8_XML_FRAGMENT1 = "<fragment xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\" xmlns:ns3=\"urn:uddi-org:api_v3\">\n"
52          +"    <ns3:authInfo>" + UTF8_WORD + "</ns3:authInfo>\n"
53          +"</fragment>";
54  	private final static String EXPECTED_UTF8_XML_FRAGMENT2 = "<fragment xmlns:ns3=\"urn:uddi-org:api_v3\" xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\">\n"
55          +"    <ns3:authInfo>" + UTF8_WORD + "</ns3:authInfo>\n"
56          +"</fragment>";
57  	/**
58  	 * Testing going from object to XML using JAXB using a XML Fragment.
59  	 */
60  	@Test 
61  	public void marshall()
62  	{
63  		try {
64  			JAXBContext jaxbContext=JAXBContext.newInstance("org.uddi.api_v3");
65  			Marshaller marshaller = jaxbContext.createMarshaller();
66  			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
67  			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
68  			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
69  			ObjectFactory factory = new ObjectFactory();
70  			AuthToken authToken = factory.createAuthToken();
71  			authToken.setAuthInfo("AuthInfo String");
72  			StringWriter writer = new StringWriter();
73  			JAXBElement<AuthToken> element = new JAXBElement<AuthToken>(new QName("","fragment"),AuthToken.class,authToken);
74  			marshaller.marshal(element,writer);
75  			String actualXml=writer.toString();
76  			
77  			boolean assertion = EXPECTED_XML_FRAGMENT1.equals(actualXml) || EXPECTED_XML_FRAGMENT2.equals(actualXml);
78  			assertTrue(assertion);
79  		} catch (JAXBException jaxbe) {
80  			jaxbe.printStackTrace();
81  			
82  			fail("No exception should be thrown");
83  		}
84  	}
85  	/**
86  	 * Unmarshall an xml fragment.
87  	 */
88  	@Test 
89  	public void unmarshall()
90  	{
91  		try {
92  			JAXBContext jaxbContext=JAXBContext.newInstance("org.uddi.api_v3");
93  			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
94  			StringReader reader = new StringReader(EXPECTED_XML_FRAGMENT1);
95  			JAXBElement<AuthToken> element = unMarshaller.unmarshal(new StreamSource(reader),AuthToken.class);
96  			String infoString = element.getValue().getAuthInfo();
97  			assertEquals("AuthInfo String", infoString);
98  		} catch (JAXBException jaxbe) {
99  			fail("No exception should be thrown");
100 		}
101 	}
102 	/**
103 	 * Test handling of utf8 characters
104 	 */
105 	@Test
106 	public void marshallUTF8()
107 	{
108 		try {
109 			JAXBContext jaxbContext=JAXBContext.newInstance("org.uddi.api_v3");
110 			Marshaller marshaller = jaxbContext.createMarshaller();
111 			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
112 			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
113 			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
114 			ObjectFactory factory = new ObjectFactory();
115 			AuthToken authToken = factory.createAuthToken();
116 			authToken.setAuthInfo(UTF8_WORD);
117 			StringWriter writer = new StringWriter();
118 			JAXBElement<AuthToken> element = new JAXBElement<AuthToken>(new QName("","fragment"),AuthToken.class,authToken);
119 			marshaller.marshal(element,writer);
120 			String actualXml=writer.toString();
121 
122 			boolean assertion = EXPECTED_UTF8_XML_FRAGMENT1.equals(actualXml) || EXPECTED_UTF8_XML_FRAGMENT2.equals(actualXml);
123 			assertTrue(assertion);
124 		} catch (JAXBException jaxbe) {
125 			fail("No exception should be thrown");
126 		}
127 	}
128 	
129 	/**
130 	 * Test handling of utf8 characters
131 	 */
132 	@Test
133 	public void unmarshallUTF8()
134 	{
135 		try {
136 			JAXBContext jaxbContext=JAXBContext.newInstance("org.uddi.api_v3");
137 			Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
138 			StringReader reader = new StringReader(EXPECTED_UTF8_XML_FRAGMENT1);
139 			JAXBElement<AuthToken> utf8Element = unMarshaller.unmarshal(new StreamSource(reader),AuthToken.class);
140 			String infoString = utf8Element.getValue().getAuthInfo();
141 			assertEquals(UTF8_WORD, infoString);
142 		} catch (JAXBException jaxbe) {
143 			fail("No exception should be thrown");
144 		}
145 	}
146 }