This project has retired. For details please refer to its Attic page.
GetPublisherDetailTest 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.apache.juddi.api_v3;
16  
17  import java.io.StringReader;
18  import java.io.StringWriter;
19  import java.text.SimpleDateFormat;
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.List;
23  import javax.xml.bind.JAXB;
24  
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.bind.JAXBElement;
27  import javax.xml.bind.JAXBException;
28  import javax.xml.bind.Marshaller;
29  import javax.xml.bind.Unmarshaller;
30  import javax.xml.namespace.QName;
31  import javax.xml.transform.stream.StreamSource;
32  
33  import static junit.framework.Assert.fail;
34  import static junit.framework.Assert.assertEquals;
35  import static junit.framework.Assert.assertTrue;
36  import org.junit.Ignore;
37  
38  import org.junit.Test;
39  import org.uddi.api_v3.AuthToken;
40  import org.uddi.api_v3.Contact;
41  import org.uddi.api_v3.ObjectFactory;
42  import org.uddi.api_v3.PersonName;
43  import org.uddi.repl_v3.CommunicationGraph;
44  import org.uddi.repl_v3.Operator;
45  import org.uddi.repl_v3.OperatorStatusType;
46  import org.uddi.repl_v3.ReplicationConfiguration;
47  
48  /**
49   * Testing marshalling functionality, making sure UTF-8 is handled correctly.
50   * 
51   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
52   */
53  public class GetPublisherDetailTest {
54  
55  	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"
56                                                         +"    <ns3:authInfo>AuthInfo String</ns3:authInfo>\n"
57                                                         +"</fragment>";
58  	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"
59          +"    <ns3:authInfo>AuthInfo String</ns3:authInfo>\n"
60          +"</fragment>";
61  	private final static String UTF8_WORD = "メインページ";
62  
63  	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"
64          +"    <ns3:authInfo>" + UTF8_WORD + "</ns3:authInfo>\n"
65          +"</fragment>";
66  	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"
67          +"    <ns3:authInfo>" + UTF8_WORD + "</ns3:authInfo>\n"
68          +"</fragment>";
69  	/**
70  	 * Testing going from object to XML using JAXB using a XML Fragment.
71  	 */
72  	@Test 
73  	public void marshall()
74  	{
75  		try {
76  			JAXBContext jaxbContext=JAXBContext.newInstance("org.apache.juddi.api_v3");
77  			Marshaller marshaller = jaxbContext.createMarshaller();
78  			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
79  			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
80  			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
81  			ObjectFactory factory = new ObjectFactory();
82  			GetPublisherDetail getPublisherDetail = new GetPublisherDetail();
83  			getPublisherDetail.authInfo = "some token";
84  			
85  			StringWriter writer = new StringWriter();
86  			JAXBElement<GetPublisherDetail> element = new JAXBElement<GetPublisherDetail>(new QName("","fragment"),GetPublisherDetail.class,getPublisherDetail);
87  			marshaller.marshal(element,writer);
88  			String actualXml=writer.toString();
89  			System.out.println(actualXml);
90  			
91  			
92  		} catch (JAXBException jaxbe) {
93  			jaxbe.printStackTrace();
94  			
95  			fail("No exception should be thrown");
96  		}
97  	}
98          
99          @Test
100         public void marshallReplicationMessage() throws Exception{
101                 ReplicationConfiguration r = new ReplicationConfiguration();
102                 r.setCommunicationGraph(new CommunicationGraph());
103                         Operator op = new Operator();
104                         op.setOperatorNodeID("a node");
105                         op.setSoapReplicationURL("http://localhost/services/replication");
106                         
107                         op.getContact().add(new Contact());
108                         op.getContact().get(0).getPersonName().add(new PersonName("Unknown", null));
109                         op.setOperatorStatus(OperatorStatusType.NORMAL);
110                         
111                         r.getOperator().add(op);
112                         r.getCommunicationGraph().getNode().add("a node");
113                         r.getCommunicationGraph().getControlledMessage().add("*");
114                         r.setSerialNumber(0);
115                         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddkkmmZ");
116                         r.setTimeOfConfigurationUpdate(sdf.format(new Date()));
117                         r.setRegistryContact(new org.uddi.repl_v3.ReplicationConfiguration.RegistryContact());
118                         r.getRegistryContact().setContact(new Contact());
119                         r.getRegistryContact().getContact().getPersonName().add(new PersonName("Unknown", null));
120                                 JAXB.marshal(r, System.out);
121         }
122          @Test
123          @Ignore
124         public void testWithCXFMarhsaller() throws Exception{
125                 org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(null, this, null, this);
126         }
127        
128 	
129 }