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