UtilBase { static String mangleIndexPage(String data, String mainJS, String mainClass) { Pattern loadClass = Pattern.compile("loadClass *\\( *[\"']" + mainClass); Matcher loadClassMatcher = loadClass.matcher(data); if (loadClassMatcher.find()) { return data; } int endOfBody = data.toLowerCase().lastIndexOf("</body>"); String newText = data.substring(0, endOfBody) + invokeSnippet(mainJS, mainClass) + data.substring(endOfBody); return newText; } private  UtilBase();   }
@Test public void testConvertDefaultPage() throws Exception { String data = "\n" + "<html>\n" + " <head>\n" + " </head>\n" + " <body>\n" + " <h1>H1</h1>\n" + "<!-- ${browser.bootstrap} -->\n" + " </body>\n" + "</html>"; String res = UtilBase.mangleIndexPage(data, "main.js", "org.apidesign.demo.Test"); assertNotEquals(res.indexOf("loadClass('org.apidesign.demo.Test'"), -1, "loadClass found"); assertNotEquals(res.indexOf("script src='bck2brwsr.js'>"), -1, "init vm found"); String res2 = UtilBase.mangleIndexPage(data, "main.js", "org.apidesign.demo.Test"); assertEquals(res2, res, "No change then"); }