Search in sources :

Example 36 with MarshallingContext

use of com.thoughtworks.xstream.converters.MarshallingContext in project ddf by codice.

the class CswTransformProviderTest method testMarshalNoTransformers.

@Test(expected = ConversionException.class)
public void testMarshalNoTransformers() throws Exception {
    when(mockMetacardManager.getTransformerBySchema(anyString())).thenReturn(null);
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    provider.marshal(getMetacard(), writer, context);
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Test(org.junit.Test)

Example 37 with MarshallingContext

use of com.thoughtworks.xstream.converters.MarshallingContext in project ddf by codice.

the class CswTransformProviderTest method testMarshalOtherSchema.

@Test
public void testMarshalOtherSchema() throws Exception {
    when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord(), StandardCharsets.UTF_8), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 38 with MarshallingContext

use of com.thoughtworks.xstream.converters.MarshallingContext in project workflow-cps-plugin by jenkinsci.

the class CpsThreadGroup method asXml.

@CpsVmThreadOnly
String asXml() {
    XStream xs = new XStream();
    // (and anyway these Describable objects would be serialized fine by XStream, just not JBoss Marshalling).
    for (SingleTypedPickleFactory<?> stpf : ExtensionList.lookup(SingleTypedPickleFactory.class)) {
        Class<?> factoryType = Functions.getTypeParameter(stpf.getClass(), SingleTypedPickleFactory.class, 0);
        xs.registerConverter(new Converter() {

            @Override
            public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
                Pickle p = stpf.writeReplace(source);
                assert p != null : "failed to pickle " + source + " using " + stpf;
                context.convertAnother(p);
            }

            @Override
            public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
                // unused
                throw new UnsupportedOperationException();
            }

            @SuppressWarnings("rawtypes")
            @Override
            public boolean canConvert(Class type) {
                return factoryType.isAssignableFrom(type);
            }
        });
    }
    // Could also register a convertor for FlowExecutionOwner, though it seems harmless.
    return xs.toXML(this);
}
Also used : Pickle(org.jenkinsci.plugins.workflow.pickles.Pickle) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) Converter(com.thoughtworks.xstream.converters.Converter) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext)

Aggregations

MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)38 Test (org.junit.Test)23 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)22 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)18 StringWriter (java.io.StringWriter)18 XStream (com.thoughtworks.xstream.XStream)12 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)12 Metacard (ddf.catalog.data.Metacard)12 ByteArrayInputStream (java.io.ByteArrayInputStream)12 JAXBElement (javax.xml.bind.JAXBElement)10 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)10 GetRecordsResponseType (net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType)8 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)8 ObjectFactory (net.opengis.cat.csw.v_2_0_2.ObjectFactory)8 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)8 SearchResultsType (net.opengis.cat.csw.v_2_0_2.SearchResultsType)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Matchers.anyString (org.mockito.Matchers.anyString)7 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)6