alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package sample.tools;

/**
 * A custom Ant task that finds compile logs containing compile
 * errors.  The compile logs with errors are sent as email attachments using
 * information in monitor.properties.
 */

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.sun.java_cup.internal.terminal;

import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class TestResultCheck extends Task {

	//directory containing of build source, parent of features and plugins
	private String dir = "";
	private String output = "testresultlist.txt";

	//keep track of testresults logs
	private Hashtable testResultReports;

	public TestResultCheck() {
		testResultReports = new Hashtable();
	}

	public void execute() throws BuildException {
		findLogs(dir);
		printTestResultLog();
	}

	// test
	public static void main(String[] args) {
		TestResultCheck checker = new TestResultCheck();
		checker.dir="D:\\workspace\\jdt-builder-sample\\build\\I.TestBuild\\testresults";
		checker.output="d:\\workspace\\jdt-builder-sample\\build\\I.TestBuild\\log.txt";
		checker.execute();
	}

	private void findLogs(String file) {
		File aFile = new File(file);

		// basis case
		if (aFile.isFile()) {
			if(aFile.getAbsolutePath().endsWith(".xml")){
				parse(aFile);
			}
		} else {
			//recurse into directories looking for and reading compile logs
			File files[] = aFile.listFiles();

			for (int i = 0; i < files.length; i++) {
				findLogs(files[i].getAbsolutePath());
			}
		}
	}

	private void parse(File file) {
		Document aDocument = null;
		BufferedReader reader = null;

		try {
			reader = new BufferedReader(new FileReader(file));

			InputSource inputSource = new InputSource(reader);
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = null;

			try {
				builder = factory.newDocumentBuilder();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}

			try {
				aDocument = builder.parse(inputSource);
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			// Get summary of problems
			NodeList nodeList = aDocument.getElementsByTagName("testsuite");

			File htmlReport = new File(file.getParentFile(), file.getName()
					.replaceAll(".xml", ".html"));

			if (nodeList == null)
				testResultReports.put(htmlReport, "fail");

			for (int i = 0; i < nodeList.getLength(); i++) {
				Node problemSummaryNode = nodeList.item(i);
				NamedNodeMap aNamedNodeMap = problemSummaryNode.getAttributes();
				Node errorNode = aNamedNodeMap.getNamedItem("errors");
				Node failureNode = aNamedNodeMap.getNamedItem("failures");
				if ((errorNode != null && !errorNode.getNodeValue().equals("0"))
						|| (failureNode != null && !failureNode.getNodeValue()
								.equals("0"))) {
					testResultReports.put(htmlReport, "fail");
				
				return;
				}
			}
			testResultReports.put(htmlReport, "ok");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (reader != null)
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	private void printTestResultLog() {
		// print log with names of test plug-ins and test result report
		Enumeration enumeration = testResultReports.keys();

		if (testResultReports.size() > 0) {
			PrintWriter out = null;
			try {
				out = new PrintWriter(new FileWriter(new File(output)));
				out.println("<h3>JUnit Test Results");
				while (enumeration.hasMoreElements()) {
					Object testResultReport = enumeration.nextElement();
					String image=testResultReports.get(testResultReport).equals("fail")?"FAIL.gif":"OK.gif";
					File testResultFile = new File(testResultReport.toString());
					out.println("<a href=\"@testresultdir@/html/"+testResultFile.getName()+"\">"+testResultFile.getName().replaceAll(".html","")+""+"
"); } } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) out.close(); } } } /** * Gets the directory containing JUnit test reports * @return Returns a String */ public String getDir() { return dir; } /** * Sets the directory containing JUnit test reports. * @param dir The JUnit test directory to set */ public void setDir(String dir) { this.dir = dir; } public String getOutput() { return output; } public void setOutput(String output) { this.output = output; } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.