1

I have a function that takes in an ODataObject which contains some XML from an event fired from a message bus. I'm writing test cases to check how my function handles XML coming from the message bus that consists of only this: <?xml version="1.0" encoding="UTF-8"?>.

This is how I'm creating the DataObject that gets passed in to my other test cases

final Document document = DOMUtil.parse(TestEventXML)  
final DataObject dataObject = new ODataObject(document.getDocumentElement())  
myListener.onMessage(dataObject)

However, passing in the above string as TestEventXML returns this error:

java.lang.NullPointerException: Cannot invoke method getDocumentElement() on null object

because calling DOMUtil.parse() on an incorrectly formatted XML string will return null.

So, I need some kind of work around that would allow me to create a org.w3c.dom.Node that I can pass into the constructor for ODataObject out of an XML string that I have created. Any ideas, suggestions, etc would be greatly appreciated.

EDIT - Summary of question

To clarify, I know that the String I'm trying to pass in is just the header of an XML file with no content. But sometimes that's what comes through the message bus, so I need to test that my function handles it correctly. My function does have code in place to handle malformed XML, so my question is not how to check if XML is malformed. The problem is that I need to create a DataObject to test that I am handling it correctly. But I cannot create a DataObject using malformed XML because parse returns null.

EDIT 2 - How I'm currently handling Malformed/Null XML

In my onMessage function, I call the toString method on the incoming DataObject, which returns a string of the XML in the DataObject. Then, to create a groovy.util.Node object, I try new XmlParser().parseText(xmlString) and catch all SAXParseExcetions, which result from the XML being malformed or null. The onMessage function then just returns, skipping all the parsing code in the rest of the function.

Addendum - Another question that might answer my original question
org.w3c.dom.Node is an interface, so I can't just do new 0DataObject(new Node()). Is there a way to create a Node and set the correct attribute to be my XML String?

Please comment with any questions you have.

2
  • 1
    This appears to be a test scenario... What happens when, in the actual program flow, your listener is triggered with an ODataObject that was formed from nearly-empty XML? Maybe you want to see how the ODataObject is constructed and focus on finding a way to construct the ODataObject manually, instead of the Node. (Maybe I led you down the wrong path in my answer by focusing on the Node-- sorry!) Commented Jul 25, 2011 at 19:11
  • I think, in the future, I might look into that. For now, I found a fix that works using Groovy's MockFor class. I make a mock of the ODataObject, pass in a properly formatted XML and then intercept the toString function to return the malformed XML. Not pretty, but it works for now. Commented Jul 26, 2011 at 19:59

2 Answers 2

2

Fix that worked: Used Groovy's MockFor class to create a mock of an ODataObject. When creating the DataObject, I pass properly formatted XML. However, when the ODataObject's toString function is called, I intercept it and return the malformed XML.

I will try to look into the Platinum Azure's suggestion to see how ODataObject is being constructed by the message bus to which I'm listening to determine why it's returning the <?xml [...]> string.

Sign up to request clarification or add additional context in comments.

2 Comments

I should have just suggested mocking in the first place since this was a test scenario. Durr.
Haha. Well, I appreciate your help in any case. :)
1

I could be wrong, but I feel like you're trying to solve the wrong problem. I guess I'm not sure why you can't just construct the node manually, or even why you need to construct the node at all when you know the XML is no good.

If you're waiting for more information out of the message bus in question, and if you have control over the schema for the incoming data, maybe you should send a content length first and then wait for that much data before trying to do the XML parsing work? Since XML is a context-free grammar that is heavily based on balanced nodes, it's really not a good idea to try to construct an XML document in memory without having every last byte, including the closing of the root tag.

Otherwise, if you just have to deal with the null, consider employing the null object pattern.

8 Comments

What do you mean by "construct the node manually"? I don't have any control over what the message bus does. Sometimes it creates a DataObject that contains XML that is just the header. I agree that it doesn't make sense, but I don't think that, in my current position, I am in a position to suggest they make changes to that framework. I'll edit the post to mention how I'm currently handling malformed/null XML.
I don't know very much about the library you're trying to use (or Java or Groovy for that matter), but I basically meant new-ing it up and setting the attributes the hard way. :-P For all I know, there might not be a way to do that, in which case you'll have to fall back to changing your incoming data stream (if you have control over that) or something else.
Yeah, I'm not sure which attribute I'd be setting on the Node, I guess that's kinda part of my question.
When you say "just the header"... could you clarify that please? Is that the <?xml [...], or the root node (with nothing in it), or what?
I mean that the XML is just this <?xml version="1.0" encoding="UTF-8"?>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.