This project has retired. For details please refer to its Attic page.
InitialContextInfoTest 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;
16  
17  import static junit.framework.Assert.assertEquals;
18  import static junit.framework.Assert.fail;
19  
20  import java.io.StringReader;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.naming.Context;
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.bind.JAXBException;
27  import javax.xml.bind.Marshaller;
28  import javax.xml.bind.Unmarshaller;
29  import javax.xml.transform.stream.StreamSource;
30  
31  import org.apache.juddi.InitialContextInfo;
32  import org.apache.juddi.Property;
33  import org.apache.juddi.jaxb.JAXBMarshaller;
34  import org.junit.Test;
35  import org.uddi.api_v3.InstanceDetails;
36  
37  
38  /**
39   * Testing marshalling functionality, making sure UTF-8 is handled correctly.
40   * 
41   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
42   */
43  public class InitialContextInfoTest {
44  	
45  	@Test
46  	public void initialContextInfo_Java2XML()
47  	{
48  		try {
49  			List<Property> properties= new ArrayList<Property>();
50  			Property property1 = new Property(Context.INITIAL_CONTEXT_FACTORY,"value1");
51  			properties.add(property1);
52  			Property property2 = new Property(Context.PROVIDER_URL,"value2");
53  			properties.add(property2);
54  			InitialContextInfo contextInfo = new InitialContextInfo();
55  			contextInfo.setProperty(properties);
56  			JAXBContext jc = JAXBContext.newInstance(InitialContextInfo.class);
57  			Marshaller m = jc.createMarshaller();
58  			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
59  	        m.marshal(contextInfo, System.out);
60  		} catch (JAXBException e) {
61  			e.printStackTrace();
62  			fail("No exception should be thrown");
63  		}
64  	}
65  	
66  	@Test
67  	public void initialContextInfo_XML2JAVA()
68  	{
69  		try {
70  			String INITIAL_CONTEXT_XML = "<initialContextInfo>" +
71  								"<contextProperty value=\"value1\" name=\"name1\"/>" +
72  								"<contextProperty value=\"value2\" name=\"name2\"/>" +
73  							 "</initialContextInfo>";
74  			
75  			StringReader reader = new StringReader(INITIAL_CONTEXT_XML);
76  			JAXBContext jc = JAXBContext.newInstance(InitialContextInfo.class);
77  			Unmarshaller um = jc.createUnmarshaller();
78  			InitialContextInfo icInfo = (InitialContextInfo)  um.unmarshal(new StreamSource(reader), InitialContextInfo.class).getValue();
79  			assertEquals("name1", icInfo.getContextProperty().get(0).getName());
80  			assertEquals("name2", icInfo.getContextProperty().get(1).getName());
81  			assertEquals("value2", icInfo.getContextProperty().get(1).getValue());
82  		} catch (JAXBException jaxbe) {
83  			jaxbe.printStackTrace();
84  			fail("No exception should be thrown");
85  		}
86  	}
87  	
88  	@Test
89  	public void bindingTemplate_XML2JAVA()
90  	{
91  		try {
92  			String instanceDetailsStr = 
93  				    
94  		            "<instanceDetails xmlns=\"urn:uddi-org:api_v3\">" +
95  				      "<instanceParms><![CDATA[ " +
96                          "<initialContextInfo>" +
97                            "<contextProperty value=\"value1\" name=\"name1\"/>" +
98                            "<contextProperty value=\"value2\" name=\"name2\"/>" +
99                          "</initialContextInfo> ]]>" +
100                       "</instanceParms>" +
101                     "</instanceDetails>";
102 	
103 			InstanceDetails details = (InstanceDetails) JAXBMarshaller.unmarshallFromString(instanceDetailsStr,JAXBMarshaller.PACKAGE_UDDIAPI);
104 			String instanceParmsStr = details.getInstanceParms();
105 			if (instanceParmsStr!=null) {
106 				InitialContextInfo icInfo = (InitialContextInfo) JAXBMarshaller.unmarshallFromString(instanceParmsStr, JAXBMarshaller.PACKAGE_JUDDI);
107 				assertEquals("name1" , icInfo.getContextProperty().get(0).getName());
108 				assertEquals("name2" , icInfo.getContextProperty().get(1).getName());
109 				assertEquals("value2", icInfo.getContextProperty().get(1).getValue());
110 			} else {
111 				fail("We should have data in instanceParmsStr.");
112 			}
113 		} catch (JAXBException jaxbe) {
114 			jaxbe.printStackTrace();
115 			fail("No exception should be thrown");
116 		}
117 	}
118 	
119 }