Skip to content

XStreamMarshaller should convert XStream StreamException to Spring exception in case of unmarshalling an empty stream [SPR-9536] #14170

@spring-projects-issues

Description

@spring-projects-issues

Neil Brown opened SPR-9536 and commented

The constructor for an XStream XppReader can throw an XStream StreamException, e.g. in the case where it is constructed from an empty stream. (This can occur if an attempt is made to unmarshall an empty HTTP request). For consistency, the Spring XStreamMarshaller should catch this occurrence of a StreamException, and convert (wrap and rethrow) it to a Spring UnmarshallingFailureException as it does in other cases.

TestCase follows -

import java.io.StringReader;

import javax.xml.transform.stream.StreamSource;

import org.junit.Before;
import org.junit.Test;
import org.springframework.oxm.UnmarshallingFailureException;
import org.springframework.oxm.xstream.XStreamMarshaller;

import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.xml.XppReader;

/**
 * Unit tests for {@link XStreamMarshaller}.
 */
public class XStreamMarshallerTest {
  
  private XStreamMarshaller uut;

  /**
   * Set-up test fixtures.
   */
  @Before
  public void setUp() {
    this.uut = new XStreamMarshaller();
  }

  /**
   * Tests {@link org.springframework.oxm.xstream.XStreamMarshaller#unmarshalReader(java.io.Reader)} in the case
   * where the input stream is empty, and the marshaller is not configured to use a supplied stream driver, and so 
   * creates a new instance of {@link XppReader}.
   * <p>
   * In this scenario {@link XppReader#XppReader(java.io.Reader, com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer)} 
   * throws a {@link StreamException} when {@link XppReader#moveDown()} is invoked on the empty stream. The 
   * {@link XStreamMarshaller} should convert the {@link StreamException} to a generic Spring 
   * {@link UnmarshallingFailureException}.
   * 
   * @throws Exception If an unexpected error occurs on execution of this test.
   */
  @Test(expected=UnmarshallingFailureException.class)
  public final void testUnmarshalReader() throws Exception {
    this.uut.unmarshal(new StreamSource(new StringReader("")));
  }
}

Affects: 3.1.1

Sub-tasks:

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions