This discussion is archived
5 Replies Latest reply: Jun 24, 2011 4:43 AM by gimbal2 RSS

Unable to test creation of XML document

Mohan Newbie
Currently Being Moderated
I tried the JUnit forum first and they suggested a XML forum

I suspect that this could be a simple mistake in my code or a race condition. Please see the comments in the code.

My test is this. I create a org.w3c.dom.Document using my class EncryptedData

@Test
public void testDocument() throws TransformerException {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
documentBuilder = dbf.newDocumentBuilder();
Document document = documentBuilder.newDocument();
EncryptedData encryptedData = new EncryptedData( document );
assertNotNull( encryptedData.getEncryptedData() );
printXML( encryptedData.getEncryptedData() );
} catch (ParserConfigurationException e) {
}
}


This is EncryptedData.java.


private Element encryptedData;

private Document document;

public EncryptedData( Document document ) {
this.document = document;
encryptedData =
(Element)
document.createElement("EncryptedData");

+//When I append the child in the constructor there is no exception+
document.appendChild( encryptedData );


}

public Document getEncryptedData(){
+//When I append the child in this method there is an exception+
encryptedData =
(Element)
document.createElement("EncryptedData");
return document;
}


org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a
node where it is not permitted.
at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
at
com.fss.maps.security.xml.EncryptedData.getEncryptedData(EncryptedData.java:56)
at
com.fss.maps.security.test.XMLEncryptionTestCase.testDocument(XMLEncryptionTestC\
ase.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav\
a:25)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java\
:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:\
15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:4\
1)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20\
)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76\
)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50\
)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunne\
r.java:97)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter\
.java:192)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points