Why does writing to the stream in this test work in IntelliJ's "Run", but not when I use Maven "test"?

280 Views Asked by At

In my application I have a problem where the the OutputStream does not write anything to the file when I run "mvn clean test", but when I run it in IntelliJ by right-clicking on run testWritingToFile(), it works fine and outputs to the file.

I have the following test:

@Test
public void testWritingToFile() throws Exception {
    File fileXml = new File("xmltest.xml");
    OutputStream outputStream = new FileOutputStream(fileXml);
    XmlStreamWriting xmlStreamWriting = new XmlStreamWriting(outputStream);

    xmlStreamWriting.writeXmlToStream();

    outputStream.close();

}

Here is the class I'm calling from the test (XmlStreamWriting.java):

public class XmlStreamWriting extends XmlStreamWritingBase {
    public XmlStreamWriting(OutputStream outputStream) {
        this.setOutputStream(outputStream);
    }

    public void writeXmlToStream() throws XMLStreamException {
        XMLStreamWriter xmlStreamWriter = this.getXmlStreamWriter();
        xmlStreamWriter.writeStartElement("test");
        xmlStreamWriter.writeCharacters("This is a test.");
        xmlStreamWriter.writeEndElement(); //test
    }
}

And here is the abstract class it extends (XmlStreamWritingBase.java):

public abstract class XmlStreamWritingBase {
    private XMLStreamWriter xmlStreamWriterPrivate;
    private OutputStream outputStream;

    public XMLStreamWriter getXmlStreamWriter() throws XMLStreamException {
        if(xmlStreamWriterPrivate == null) {
            XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
            xmlStreamWriterPrivate = xmlOutputFactory.createXMLStreamWriter(this.getOutputStream());
        }
        return this.xmlStreamWriterPrivate;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }
}

I'm using the jrockit jdk 1.6.0_29 if that matters. Both IntelliJ and Maven are setup to run that JDK.

Do I have to pass the OutputStream to every method directly? Why doesn't the writeXmlToStream() method work?

EDIT: Also just to verify the stream works in both cases, I changed the test to this and when I run from maven there is no content between Test1 and Test2, but when I run with IntelliJ, the xml appears between the two strings:

public void testWritingToFile() throws Exception {
    File fileXml = new File("xmltest.xml");
    OutputStream outputStream = new FileOutputStream(fileXml);

    outputStream.write("Test1".getBytes());

    XmlStreamWriting xmlStreamWriting = new XmlStreamWriting(outputStream);

    xmlStreamWriting.writeXmlToStream();

    outputStream.write("Test2".getBytes());

    outputStream.close();
}

The output for maven is "Test1Test2", the output for IntelliJ "Run" is "Test1<test>This is a test.</test>Test2"

1

There are 1 best solutions below

0
On BEST ANSWER

The solution was that I was not setting an encoding type. Or they differed somehow. When I set the encoding to UTF-8 the file is written to just fine:

xmlOutputFactory.createXMLStreamWriter(this.getOutputStream(), "UTF-8");

I discovered this because when I set a maven goal inside IntelliJ, the switch -Dfile.encoding=UTF-8 would be appended to the end of the mvn command and then the test would correctly build the XML file.