import static org.junit.Assert.*;
import org.junit.After;
import java.lang.management.*;
import java.io.*;
import java.net.*;
import javax.xml.xpath.*;
import org.xml.sax.InputSource;
import org.w3c.dom.NodeList;

public class SearchInHeap {
  @After public void searchInHeap() throws IOException, InterruptedException, XPathExpressionException {
    String pid = ManagementFactory.getRuntimeMXBean().getName().replaceAll("@.*", "");

    String tmp = System.getProperty("temporaryDir");
    String hprof = tmp + "/" + pid + ".hprof";
    String jhatPort = Integer.toString(7000 + Integer.parseInt(System.getProperty("org.gradle.test.worker")));

    Runtime.getRuntime().exec(new String[] {"jmap", String.format("-dump:format=b,file=%s", hprof), pid}).waitFor();
    String jhat = "jhat -port " + jhatPort + " " + hprof;
    Process p=Runtime.getRuntime().exec(jhat);

    BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line=reader.readLine();
    while(!line.matches("Server is ready."))
    {
      line=reader.readLine();
    }
    Thread.sleep(1000);

    URL oqlUrl = new URL("http://localhost:" + jhatPort + "/oql/?query=" + URLEncoder.encode("select a.toString() from [C a where /really_unique_string/(a.toString())", "UTF-8"));
    URLConnection oql = oqlUrl.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(oql.getInputStream()));
    String inputLine;
    String xml = "";

    while ((inputLine = in.readLine()) != null)
      xml += inputLine;
    in.close();

    xml = xml.replaceAll("(\\w+)=([^'\"][^ ]+?)([^'\"])", "$1='$2'$3");
    xml = xml.replaceAll("\000", "");
    String xpath = "//table[@border=1]//td";
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(xpath, new InputSource(new StringReader(xml)), XPathConstants.NODESET);
    assertEquals(0, nodeList.getLength());

    Runtime.getRuntime().exec(new String[] {"pkill", "-f", jhat}).waitFor();
    new File(hprof).delete();
  }
}

@@ -427,17 +427,16 @@
 %22, %22%22);%0A
-%0A
     Stri
@@ -634,25 +634,146 @@
 worker%22)));%0A
-%0A
+    String jhat = %22jhat -port %22 + jhatPort + %22 %22 + hprof;%0A    try %7B%0A      System.out.println(%22Dumping heap with jmap%22);%0A  
     Runtime.
@@ -881,66 +881,54 @@
 ();%0A
+%0A  
     S
-tring jhat = %22jhat -port %22 + jhatPort + %22 %22 + hprof;%0A
+ystem.out.println(%22Starting jhat%22);%0A  
     
@@ -971,24 +971,26 @@
 jhat);%0A%0A    
+ 
+ 
 BufferedRead
@@ -1062,24 +1062,26 @@
 am()));%0A    
+  
 String line=
@@ -1095,24 +1095,26 @@
 readLine();%0A
+  
     while(!l
@@ -1150,18 +1150,22 @@
 %22))%0A    
+ 
+ 
 %7B%0A
+  
       li
@@ -1194,35 +1194,132 @@
     
+  
 %7D%0A    
-Thread.sleep(1000);%0A%0A
+  System.out.println(%22jhat ready!%22);%0A      Thread.sleep(1000);%0A%0A      System.out.println(%22Running OQL query...%22);%0A  
     
@@ -1498,16 +1498,18 @@
 ));%0A    
+  
 URLConne
@@ -1541,24 +1541,26 @@
 nnection();%0A
+  
     Buffered
@@ -1636,24 +1636,26 @@
 am()));%0A    
+  
 String input
@@ -1660,24 +1660,26 @@
 utLine;%0A    
+ 
+ 
 String xml =
@@ -1684,16 +1684,18 @@
  = %22%22;%0A%0A
+  
     whil
@@ -1736,24 +1736,26 @@
 null)%0A      
+  
 xml += input
@@ -1756,24 +1756,26 @@
  inputLine;%0A
+  
     in.close
@@ -1775,24 +1775,93 @@
 n.close();%0A%0A
+      // What I don't do just not to add an external dependency...%0A  
     xml = xm
@@ -1920,24 +1920,26 @@
 2'$3%22);%0A    
+  
 xml = xml.re
@@ -1960,24 +1960,26 @@
 %22, %22%22);%0A    
+ 
+ 
 String xpath
@@ -2007,16 +2007,18 @@
 %5D//td%22;%0A
+  
     XPat
@@ -2062,24 +2062,26 @@
 newXPath();%0A
+  
     NodeList
@@ -2194,16 +2194,18 @@
 T);%0A    
+  
 assertEq
@@ -2209,16 +2209,33 @@
 tEquals(
+%22Secret found!%22, 
 0, nodeL
@@ -2252,17 +2252,38 @@
 gth());%0A
-%0A
+    %7D%0A    finally %7B%0A  
     Runt
@@ -2355,16 +2355,18 @@
 ();%0A    
+  
 new File
@@ -2383,14 +2383,20 @@
 lete();%0A
+    %7D%0A
   %7D%0A%7D%0A

Use try so we can clean up in finally