Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - System.setOut does not save an exception message
    primarykey
    data
    text
    <p>It seems that <code>System.setOut()</code> does not work in this test case.</p> <p>Here are problem description.</p> <ol> <li><code>test0</code> executes <code>System.setOut(new PrintStream(byteBuffer))</code> so that it stores standard output.</li> <li><code>test0</code> invokes <code>AddChild1_wy_v1.main</code>.</li> <li>In the <code>AddChild1_wy_v1.main</code>, <code>xml.addChild(null)</code> generates an exception message.</li> <li>The exception message should be stored in <code>byteBuffer</code>, but it seems it wasn't.. JVM stops running the test case once the exception message pops up. And the remaining code after <code>AddChild1_wy_v1.main</code> are not executed.</li> </ol> <p>Is there a way for jvm to execute the remaining code in <code>test0</code>?</p> <p><code>NanoAddChild1_wy_v1Tests.java</code></p> <pre><code>package tests; import junit.framework.TestCase; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import net.n3.nanoxml.*; public class NanoAddChild1_wy_v1Tests extends TestCase { public void test0() throws Exception { //addchild1.out String result; ByteArrayOutputStream byteBuffer; byteBuffer = new ByteArrayOutputStream(); System.setOut(new PrintStream(byteBuffer)); AddChild1_wy_v1.main(new String[] {"/home/junghyun/Dev/nanoxml/inputs/simple.xml"}); result = new String(byteBuffer.toByteArray()); assertEquals(result, "Exception in thread \"main\" java.lang.IllegalArgumentException: child must not be null\n\tat net.n3.nanoxml.XMLElement.addChild(XMLElement.java:165)\n\tat AddChild1_wy_v1.main(AddChild1_wy_v1.java:47)\n"); } } </code></pre> <p><code>AddChild1_wy_v1.java</code></p> <pre><code>package tests; import net.n3.nanoxml.IXMLParser; import net.n3.nanoxml.IXMLReader; import net.n3.nanoxml.StdXMLReader; import net.n3.nanoxml.XMLElement; import net.n3.nanoxml.XMLParserFactory; import net.n3.nanoxml.XMLWriter; public class AddChild1_wy_v1 { public static void main(String args[]) throws Exception { if (args.length == 0) { System.err.println("Usage: java DumpXML file.xml"); Runtime.getRuntime().exit(1); } IXMLParser parser = XMLParserFactory.createDefaultXMLParser(); IXMLReader reader = StdXMLReader.fileReader(args[0]); parser.setReader(reader); XMLElement xml = (XMLElement) parser.parse(); xml.addChild (null); (new XMLWriter(System.out)).write(xml); } } </code></pre> <p><img src="https://i.stack.imgur.com/aejcj.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload