+ \r
+ /**\r
+ * @param scriptName \r
+ * @return tree root\r
+ * @throws Exception\r
+ */\r
+ public static CompositeNode loadConfigByGroovy(String scriptName) throws Exception {\r
+ InputStream configStream = NodeHelper.class.getResourceAsStream(scriptName);\r
+ Binding binding = new Binding();\r
+ GroovyShell gShell = new GroovyShell(binding);\r
+ LOG.debug("groovy: starting script parse.. " + scriptName);\r
+ Script configScript = gShell.parse(new InputStreamReader(configStream));\r
+ LOG.debug("groovy: starting script.. " + scriptName);\r
+ configScript.run();\r
+ LOG.debug("groovy: digging result");\r
+ Object xmlGen = binding.getVariable("xmlGen");\r
+ LOG.debug("xmlGen = " + xmlGen);\r
+ Method getter = xmlGen.getClass().getDeclaredMethod("getBuilder", new Class[0]);\r
+ MyNodeBuilder builder = (MyNodeBuilder) getter.invoke(xmlGen, new Object[0]);\r
+ \r
+ return builder.getRootNode();\r
+ }\r
+ \r
+ /**\r
+ * @param pattern , e.g.: <pre>"//{0}:network/{1}:xx[text() = 'sss']"</pre>\r
+ * @param nsArg , e.g.: <pre>{"uri:ns1", "uri:ns2"}</pre>\r
+ * @return pattern with namespaces: <pre>//uri:ns1:network/uri:ns2:xx[text() = ''sss'']"</pre>\r
+ */\r
+ public static String AddNamespaceToPattern(String pattern, Object... nsArg) {\r
+ Object[] ns = nsArg;\r
+ String patternNs = pattern.replaceAll("'", "''");\r
+ if (ns == null) {\r
+ ns = new Object[]{""};\r
+ } else {\r
+ // add ':' into pattern after placeholders\r
+ patternNs = patternNs.replaceAll("(\\{[0-9]+\\})", "$1:");\r
+ }\r
+ \r
+ return MessageFormat.format(patternNs, ns);\r
+ }\r
+\r
+ /**\r
+ * @param tree\r
+ * @param xmlFile \r
+ * @param clazz \r
+ * @throws Exception\r
+ * @throws SAXException\r
+ * @throws IOException\r
+ */\r
+ public static void compareXmlTree(Document tree, String xmlFile, Class<?> clazz) throws Exception,\r
+ SAXException, IOException {\r
+ ByteArrayOutputStream actualRaw = new ByteArrayOutputStream();\r
+ dumpDoc(tree, new PrintStream(actualRaw));\r
+ Reader actualReader = new InputStreamReader(new ByteArrayInputStream(actualRaw.toByteArray()));\r
+ \r
+ Reader expectedReader = new InputStreamReader(clazz.getResourceAsStream(xmlFile));\r
+ Diff myDiff = new Diff(expectedReader, actualReader);\r
+ myDiff.overrideDifferenceListener(new IgnoreWhiteCharsDiffListener());\r
+ \r
+ boolean similar = myDiff.similar();\r
+ if (! similar) {\r
+ System.out.println(new String(actualRaw.toByteArray()));\r
+ }\r
+ Assert.assertEquals(myDiff.toString(), true, similar);\r
+ }\r