View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.test;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.Marshaller;
29  import javax.xml.bind.Unmarshaller;
30  
31  /**
32   * A class with some assertion utilities for JAXB-related operations. 
33   * 
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public final class JAXBAssert {
37  	
38  	private JAXBAssert() {}
39  	
40  	/**
41  	 * Performs the following steps and assertions:
42  	 * 
43  	 * <ol>
44  	 *   <li>Creates a new JAXBContext with the given list of classesToBeBound.</li>
45  	 *   <li>Marshals the provided objectToMarshall to XML.</li>
46  	 *   <li>Unmarshals the marshaled XML to recreate the original object.<li>
47  	 *   <li>Asserts that the newly unmarhsaled object and the original provided object are equal by invoking {@link Object#equals(Object)}.</li>
48  	 *   <li>Unmarshals the given expectedXml to an object and asserts that it is equal with the original unmarshaled object by invoking {@link Object#equals(Object)}.</li>
49  	 * </ol>  
50  	 *  
51  	 * @param objectToMarshal the object to marshal to XML using JAXB
52  	 * @param expectedXml an XML string that will be unmarshaled to an Object and compared with objectToMarshal using {@link Object#equals(Object)}
53  	 * @param classesToBeBound - list of java classes to be recognized by the created JAXBContext
54  	 * 
55  	 */
56  	public static void assertEqualXmlMarshalUnmarshal(Object objectToMarshal, String expectedXml, Class<?> ... classesToBeBound) {
57  		String marshaledXml = null;
58  		try {
59  		  JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);
60  		  Marshaller marshaller = jaxbContext.createMarshaller();
61  		  
62  		  StringWriter stringWriter = new StringWriter();
63  		  
64  		  marshaller.marshal(objectToMarshal, stringWriter);
65  
66  		  marshaledXml = stringWriter.toString();
67  		  
68  		  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
69  		  
70  		  Object actual = unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
71  		  assertEquals("Unmarshalled object should be equal to original objectToMarshall.", objectToMarshal, actual);
72  		  
73  		  Object expected = unmarshaller.unmarshal(new StringReader(expectedXml.trim()));
74  		  assertEquals("Unmarshalled objects should be equal.", expected, actual);
75  		} catch (Throwable e) {
76  			System.err.println("Outputting marshaled XML from failed assertion:\n" + marshaledXml);
77  			if (e instanceof RuntimeException) {
78  				throw (RuntimeException)e;
79  			} else if (e instanceof Error) {
80  				throw (Error)e;
81  			}
82  			throw new RuntimeException("Failed to marshall/unmarshall with JAXB.  See the nested exception for details.", e);
83  		}
84  	}
85  	
86  	public static void assertEqualXmlMarshalUnmarshalWithResource(Object objectToMarshal, InputStream expectedXml, Class<?> ... classesToBeBound) throws IOException {
87  		BufferedReader reader = new BufferedReader(new InputStreamReader(expectedXml));
88  		StringWriter writer = new StringWriter();
89  		int data = -1;
90  		while ((data = reader.read()) != -1) {
91  			writer.write(data);
92  		}
93  		assertEqualXmlMarshalUnmarshal(objectToMarshal, writer.toString(), classesToBeBound);
94  	}
95  	
96  
97  }