@Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Type returnType = method.getGenericReturnType(); assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">", marshaller.supports(returnType)); try { // make sure the marshalling does not result in errors Object returnValue = method.invoke(primitives); marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream())); } catch (InvocationTargetException e) { fail(e.getMessage()); } } }, new ReflectionUtils.MethodFilter() {
public boolean supports(Type type) { if (type instanceof Class) { return supportsInternal((Class) type, true); } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (JAXBElement.class.equals(parameterizedType.getRawType())) { Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1, "Invalid amount of parameterized types in JAXBElement"); Type typeArgument = parameterizedType.getActualTypeArguments()[0]; if (typeArgument instanceof Class) { Class clazz = (Class) typeArgument; if (!isPrimitiveType(clazz) && !isStandardType(clazz) && !supportsInternal(clazz, false)) { return false; } } else if (typeArgument instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) typeArgument; return genericArrayType.getGenericComponentType().equals(Byte.TYPE); } else if (!supports(typeArgument)) { return false; } return true; } } return false; }
@Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Type returnType = method.getGenericReturnType(); assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">", marshaller.supports(returnType)); try { // make sure the marshalling does not result in errors Object returnValue = method.invoke(standardClasses); marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream())); } catch (InvocationTargetException e) { fail(e.getMessage()); } } }, new ReflectionUtils.MethodFilter() {
private void testSupports() throws Exception { assertTrue("Jaxb2Marshaller does not support Flights class", marshaller.supports(Flights.class)); assertTrue("Jaxb2Marshaller does not support Flights generic type", marshaller.supports((Type)Flights.class)); assertFalse("Jaxb2Marshaller supports FlightType class", marshaller.supports(FlightType.class)); assertFalse("Jaxb2Marshaller supports FlightType type", marshaller.supports((Type)FlightType.class)); Method method = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class); assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>", marshaller.supports(method.getGenericReturnType())); marshaller.setSupportJaxbElementClass(true); JAXBElement<FlightType> flightTypeJAXBElement = new JAXBElement<>(new QName("http://springframework.org", "flight"), FlightType.class, new FlightType()); assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>", marshaller.supports(flightTypeJAXBElement.getClass())); assertFalse("Jaxb2Marshaller supports class not in context path", marshaller.supports(DummyRootElement.class)); assertFalse("Jaxb2Marshaller supports type not in context path", marshaller.supports((Type)DummyRootElement.class)); method = getClass().getDeclaredMethod("createDummyRootElement"); assertFalse("Jaxb2Marshaller supports JAXBElement not in context path", marshaller.supports(method.getGenericReturnType())); assertFalse("Jaxb2Marshaller supports class not in context path", marshaller.supports(DummyType.class)); assertFalse("Jaxb2Marshaller supports type not in context path", marshaller.supports((Type)DummyType.class)); method = getClass().getDeclaredMethod("createDummyType"); assertFalse("Jaxb2Marshaller supports JAXBElement not in context path", marshaller.supports(method.getGenericReturnType())); testSupportsPrimitives(); testSupportsStandardClasses(); }
@Test public void supportsXmlRootElement() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class); marshaller.afterPropertiesSet(); assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class)); assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class)); assertFalse("Jaxb2Marshaller supports DummyType class", marshaller.supports(DummyType.class)); assertFalse("Jaxb2Marshaller supports DummyType type", marshaller.supports((Type)DummyType.class)); }
@Override public boolean supports(final Class<?> clazz) { return jaxb2Marshaller.supports(clazz); }