Menu

#787 WebResponseData.getBody() can return null

2.4
closed
None
5
2012-10-21
2009-03-03
No

jboss_jsfunit calls WebResponseImpl.getRsponseBody(). getResponseBody() calls responseData_.getBody() which apparently does return 'null' sometimes. This crashes the test in a thread with a 'null pintyer exception'.

getBody() should return a byte[] and possibly that should never be null. I changed that function to:

...
public byte[] getBody() {
if(body_ == null) body_ = String.valueOf("").getBytes();
return body_;
}
...

This fix makes sure that no null is returned even though the webresponse was null.

I checked the svn version and the classes are exactly the same.

I also added a catch in ThreadManager.java but that might be a bit of overkill:

...
final Thread newThread = new Thread(job, "HtmlUnit Managed Thread #" + threadId + " for WebWindow '" + ww.getName() + "': " + label) {
@Override
public void run() {
try {
super.run();
} catch(Exception e) {
String threadDescription = "HtmlUnit Managed Thread #" + threadId + " for WebWindow '" + ww.getName() + "': " + label;
LOG.error("startThread ("+threadDescription+"): "+e,e);
} finally {
threadMap_.remove(new Integer(threadId));
}
}
};
...

MAG,
Milo van der Zee

Discussion

  • Ahmed Ashour

    Ahmed Ashour - 2009-03-03

    Hi Milo,

    Why did you mark as 'fixed'?

    Can you provide a test case that throws NPE?

     
  • Milo van der Zee

    Hello Ahmed,

    I marked it as fixed because I don'ty have any problem with the provided patch. It might not be an elegant fix but it does the job.

    I don't have a testcase but I can provide the stacktrace in an error situation:

    2009-03-03 12:35:18,341 () INFO com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController - Re-synchronized call to http://localhost:8080/VetManager/pages/calendar/calendarTab.jsf
    2009-03-03 12:35:19,750 (Tests,127.0.0.1,3C3EAC) WARN com.vetmanager.base.LifeCycleListener - 'APPLY_REQUEST_VALUES(2)' for '/pages/calendar/calendarTab.xhtml' took more then 1000ms! (1397ms)
    Exception in thread "HtmlUnit Managed Thread #3 for WebWindow : XMLHttpRequest.send" java.lang.NullPointerException
    at java.lang.String.<init>(String.java:594)
    at org.jboss.jsfunit.framework.FaceletsErrorPageException.isFaceletsErrorPage(FaceletsErrorPageException.java:55)
    at org.jboss.jsfunit.framework.FaceletsErrorPageDetector.afterRequest(FaceletsErrorPageDetector.java:39)
    at org.jboss.jsfunit.framework.JSFUnitWebConnection.notifyListenersAfter(JSFUnitWebConnection.java:103)
    at org.jboss.jsfunit.framework.JSFUnitWebConnection.getResponse(JSFUnitWebConnection.java:84)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1487)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1445)
    at com.gargoylesoftware.htmlunit.javascript.host.XMLHttpRequest.doSend(XMLHttpRequest.java:399)
    at com.gargoylesoftware.htmlunit.javascript.host.XMLHttpRequest.access$000(XMLHttpRequest.java:54)
    at com.gargoylesoftware.htmlunit.javascript.host.XMLHttpRequest$1.run(XMLHttpRequest.java:361)
    at org.mozilla.javascript.Context.call(Context.java:515)
    at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:507)
    at com.gargoylesoftware.htmlunit.javascript.host.XMLHttpRequest$2.run(XMLHttpRequest.java:367)
    at java.lang.Thread.run(Thread.java:619)
    at com.gargoylesoftware.htmlunit.ThreadManager$1.run(ThreadManager.java:116)
    ======================

    MAG,
    Milo van der Zee

     
  • Ahmed Ashour

    Ahmed Ashour - 2009-03-03

    Hi Milo,

    The NPE happens from FaceletsErrorPageException.java:55, so the code should be changed there, no in HtmlUnit.

    Please re-open the bug if you encounter an exception triggered by HtmlUnit 'internal' usage of WebResponseImpl.getRsponseBody().

     
  • Milo van der Zee

    Ok, my understandig was that returning null for a byte[] was not very nice. I now agree that it should not be a problem and I did submit a bug to the jboss jsfunit team (https://jira.jboss.org/jira/browse/JSFUNIT-199).
    Thanks and till next time.

    MAG,
    Milo

     

Log in to post a comment.