/* * Copyright (c) 2013 S.C. Axemblr Software Solutions S.R.L * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.axemblr.provisionr.core; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Before; public abstract class BaseJaxbTest { protected JAXBContext jaxb; static { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreAttributeOrder(true); } @Before public void setUp() throws JAXBException { jaxb = JAXBContext.newInstance(getContextClasses()); } public abstract Class[] getContextClasses(); /** * Marshal an object as XML using a standard JAXB Context */ protected String asXml(Object obj) throws Exception { ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); PrintStream out = new PrintStream(outBytes); Marshaller marshaller = jaxb.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(obj, out); return outBytes.toString(); } }