Permalink
Browse files

Beutify XML output from JUnit.

  • Loading branch information...
1 parent ae52da5 commit a8f611ca4d6605fe412e60dcc3a6265841d2411c @serras serras committed Jul 24, 2011
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
+ <classpathentry exported="true" kind="lib" path="lib/jdom.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
@@ -23,3 +23,5 @@ Bundle-Localization: plugin
Export-Package: net.sf.eclipsefp.haskell.debug.core.internal,
net.sf.eclipsefp.haskell.debug.core.internal.debug,
net.sf.eclipsefp.haskell.debug.core.internal.launch;x-friends:="net.sf.eclipsefp.haskell.haddock,net.sf.eclipsefp.haskell.ui,net.sf.eclipsefp.haskell.debug.ui"
+Bundle-ClassPath: .,
+ lib/jdom.jar
@@ -3,4 +3,5 @@ output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.properties,\
- plugin.xml
+ plugin.xml,\
+ lib/jdom.jar
Binary file not shown.
@@ -0,0 +1,110 @@
+package net.sf.eclipsefp.haskell.debug.core.internal.launch;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+import org.jdom.DefaultJDOMFactory;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.jdom.output.XMLOutputter;
+
+/**
+ * This class includes a method to create, from a JUnit-like test output
+ * generated by test-framework, a new one where the "classname" attributes have
+ * been replaces by nested "testsuite", so they are better shown in the JUnit
+ * view.
+ *
+ * @author Alejandro Serrano
+ *
+ */
+public class TestSuiteBeautifier {
+
+ static final String TESTRUN = "testrun"; //$NON-NLS-1$
+ static final String TESTSUITE = "testsuite"; //$NON-NLS-1$
+ static final String TESTCASE = "testcase"; //$NON-NLS-1$
+ static final String CLASSNAME = "classname"; //$NON-NLS-1$
+ static final String NAME = "name"; //$NON-NLS-1$
+ static final String GROUP_SEPARATOR = "\\."; //$NON-NLS-1$
+
+ public static void beuatify( final File file ) throws IOException, JDOMException {
+ try {
+ FileInputStream contents = new FileInputStream( file );
+ String fileContents = new Scanner( contents ).useDelimiter( "\\Z" ).next(); //$NON-NLS-1$
+ contents.close();
+
+ file.delete();
+ FileOutputStream newContents = new FileOutputStream( file );
+ byte[] bytes = beautify( fileContents ).getBytes();
+ newContents.write( bytes );
+ } catch (Exception e) {
+ throw new JDOMException( );
+ }
+ }
+
+ public static String beautify( final String xml ) throws JDOMException, IOException {
+
+ SAXBuilder parser = new SAXBuilder();
+ StringReader reader = new StringReader( xml );
+ Document doc = parser.build( reader );
+ reader.close();
+
+ Element root = doc.getRootElement(); // testrun
+ Element testsuite = root; // .getChild( TESTSUITE ); // testsuite
+ // Inside the testsuite, we have a bunch of testcase elements
+ ArrayList<Element> testcases = new ArrayList<Element>();
+ for (Element testcase : (List<Element>)testsuite.getChildren( TESTCASE )) {
+ testcases.add(testcase);
+ }
+ for( Element testcase: testcases ) {
+ // Get classname
+ String classname = testcase.getAttributeValue( CLASSNAME, "" ); //$NON-NLS-1$
+ // Add to the nested testsuite
+ if( classname.length() > 0 ) {
+ // Remove from its position
+ testsuite.removeContent( testcase );
+ String[] innerTestSuites = classname.split( GROUP_SEPARATOR );
+ Element newNestedTestsuite = getInnerElement( testsuite,
+ innerTestSuites );
+ newNestedTestsuite.addContent( testcase );
+ }
+ }
+
+ return new XMLOutputter().outputString( doc );
+ }
+
+ public static Element getInnerElement( final Element e, final String[] inside ) {
+ if( inside.length == 0 ) {
+ return e;
+ }
+
+ String toFind = inside[ 0 ];
+ Element found = null;
+ for( Element tsuite: ( List<Element> )e.getChildren( TESTSUITE ) ) {
+ String name = tsuite.getAttributeValue( NAME, "" );
+ if( name.equals( toFind ) ) {
+ found = tsuite;
+ break;
+ }
+ }
+
+ if( found == null ) {
+ found = new DefaultJDOMFactory().element( TESTSUITE );
+ found.setAttribute( NAME, toFind );
+ e.addContent( found );
+ }
+
+ String[] newInside = new String[ inside.length - 1 ];
+ for( int i = 1; i < inside.length; i++ ) {
+ newInside[ i - 1 ] = inside[ i ];
+ }
+
+ return getInnerElement( found, newInside );
+ }
+}
@@ -14,14 +14,16 @@
import org.eclipse.ui.PlatformUI;
/**
- * launch delegate for Haskell executables executables
+ * launch delegate for test-framework test suites
*
- * @author JP Moresmau
+ * @author Alejandro Serrano
*
*/
public class TestSuiteHaskellLaunchDelegate extends
ExecutableOrTestSuiteHaskellLaunchDelegate {
+ static final String JUNIT_VIEW = "org.eclipse.jdt.junit.ResultView"; //$NON-NLS-1$
+
String filename = null;
private String getFilename() {
@@ -64,14 +66,21 @@ protected void preProcessDefinitionCreation(
protected void postProcessFinished() {
// Get file and parse output
final String fname = getFilename();
+ final File file = new File( fname );
+ try {
+ TestSuiteBeautifier.beuatify( file );
+ } catch (Exception e) {
+ // Do nothing
+ }
+
Display.getDefault().syncExec( new Runnable() {
public void run() {
try {
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
- page.showView( "org.eclipse.jdt.junit.ResultView" ); //$NON-NLS-1$
- JUnitCore.importTestRunSession( new File( fname ) );
+ page.showView( JUNIT_VIEW );
+ JUnitCore.importTestRunSession( file );
} catch( CoreException e ) {
// Do nothing
}

0 comments on commit a8f611c

Please sign in to comment.