【问题标题】:Why does writing to the stream in this test work in IntelliJ's "Run", but not when I use Maven "test"?为什么在此测试中写入流在 IntelliJ 的“运行”中有效,但在我使用 Maven“测试”时无效?
【发布时间】:2015-04-09 12:53:49
【问题描述】:

在我的应用程序中,当我运行“mvn clean test”时,OutputStream 没有向文件写入任何内容,但是当我通过右键单击运行 testWritingToFile() 在 IntelliJ 中运行它时,它会出现问题工作正常并输出到文件。

我有以下测试:

@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();

}

这是我从测试中调用的类 (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
    }
}

这是它扩展的抽象类(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;
    }
}

如果这很重要,我正在使用 jrockit jdk 1.6.0_29。 IntelliJ 和 Maven 都设置为运行该 JDK。

我必须将OutputStream 直接传递给每个方法吗?为什么writeXmlToStream() 方法不起作用?

编辑:也只是为了验证流在两种情况下都有效,我将测试更改为这个,当我从 maven 运行时,Test1 和 Test2 之间没有内容,但是当我使用 IntelliJ 运行时,xml 出现在两者之间字符串:

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();
}

maven 的输出是“Test1Test2”,IntelliJ “Run”的输出是“Test1<test>This is a test.</test>Test2

【问题讨论】:

  • 定义“不起作用”。
  • 澄清了我的问题。当我说不起作用时,我的意思是xml文件是空的。
  • 在 maven compile 和 maven surefire 步骤中是否记录了任何错误或警告?
  • 您确定 maven 不只是将文件写入文件系统上您不期望的位置吗?
  • 你的类有一个严重的设计错误:如果你在调用 getXmlStreamWriter() 之后设置输出流,编写器仍然使用旧的输出流。如果您在 createXMLStreamWriter() 中以某种方式犯了同样的错误,它可以解释差异。

标签: java outputstream fileoutputstream java-6 xmlstreamwriter


【解决方案1】:

解决方案是我没有设置编码类型。或者他们以某种方式有所不同。当我将编码设置为 UTF-8 时,文件被写入就好了:

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

我发现这一点是因为当我在 IntelliJ 中设置 maven 目标时,开关 -Dfile.encoding=UTF-8 将附加到 mvn 命令的末尾,然后测试将正确构建 XML 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    相关资源
    最近更新 更多