001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.core.test;
017
018 import org.apache.commons.lang.StringUtils;
019
020 import static org.junit.Assert.assertEquals;
021
022 import java.io.BufferedReader;
023 import java.io.IOException;
024 import java.io.InputStream;
025 import java.io.InputStreamReader;
026 import java.io.StringReader;
027 import java.io.StringWriter;
028
029 import javax.xml.bind.JAXBContext;
030 import javax.xml.bind.Marshaller;
031 import javax.xml.bind.Unmarshaller;
032
033 /**
034 * A class with some assertion utilities for JAXB-related operations.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038 public final class JAXBAssert {
039
040 private JAXBAssert() {}
041
042 /**
043 * Performs the following steps and assertions:
044 *
045 * <ol>
046 * <li>Creates a new JAXBContext with the given list of classesToBeBound.</li>
047 * <li>Marshals the provided objectToMarshall to XML.</li>
048 * <li>Unmarshals the marshaled XML to recreate the original object.<li>
049 * <li>Asserts that the newly unmarhsaled object and the original provided object are equal by invoking {@link Object#equals(Object)}.</li>
050 * <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>
051 * </ol>
052 *
053 * @param objectToMarshal the object to marshal to XML using JAXB
054 * @param expectedXml an XML string that will be unmarshaled to an Object and compared with objectToMarshal using {@link Object#equals(Object)}
055 * @param classesToBeBound - list of java classes to be recognized by the created JAXBContext
056 *
057 */
058 public static void assertEqualXmlMarshalUnmarshal(Object objectToMarshal, String expectedXml, Class<?> ... classesToBeBound) {
059 String marshaledXml = null;
060 try {
061 JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);
062 Marshaller marshaller = jaxbContext.createMarshaller();
063
064 StringWriter stringWriter = new StringWriter();
065
066 marshaller.marshal(objectToMarshal, stringWriter);
067
068 marshaledXml = stringWriter.toString();
069
070 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
071
072 Object actual = unmarshaller.unmarshal(new StringReader(stringWriter.toString()));
073 assertEquals("Unmarshalled object should be equal to original objectToMarshall.", objectToMarshal, actual);
074
075 if (StringUtils.isBlank(expectedXml)) {
076 throw new IllegalArgumentException("Expected XML must be specified.");
077 }
078
079 Object expected = unmarshaller.unmarshal(new StringReader(expectedXml.trim()));
080 assertEquals("Unmarshalled objects should be equal.", expected, actual);
081 } catch (Throwable e) {
082 System.err.println("Outputting marshaled XML from failed assertion:\n" + marshaledXml);
083 if (e instanceof RuntimeException) {
084 throw (RuntimeException)e;
085 } else if (e instanceof Error) {
086 throw (Error)e;
087 }
088 throw new RuntimeException("Failed to marshall/unmarshall with JAXB. See the nested exception for details.", e);
089 }
090 }
091
092 public static void assertEqualXmlMarshalUnmarshalWithResource(Object objectToMarshal, InputStream expectedXml, Class<?> ... classesToBeBound) throws IOException {
093 BufferedReader reader = new BufferedReader(new InputStreamReader(expectedXml));
094 StringWriter writer = new StringWriter();
095 int data = -1;
096 while ((data = reader.read()) != -1) {
097 writer.write(data);
098 }
099 assertEqualXmlMarshalUnmarshal(objectToMarshal, writer.toString(), classesToBeBound);
100 }
101
102
103 }