[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/ustutt/ncunit/java/ncunit/output Comparison.java, NONE, 1.1 TokenList.java, NONE, 1.1 NCUnit.java, NONE, 1.1 TestCaseAnalyzer.java, NONE, 1.1 TestXMLParser.java, NONE, 1.1

Andreas Lachenmann lachenmann at users.sourceforge.net
Tue Feb 20 04:33:08 PST 2007


Update of /cvsroot/tinyos/tinyos-1.x/contrib/ustutt/ncunit/java/ncunit/output
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv4808/contrib/ustutt/ncunit/java/ncunit/output

Added Files:
	Comparison.java TokenList.java NCUnit.java 
	TestCaseAnalyzer.java TestXMLParser.java 
Log Message:
added files to repository

--- NEW FILE: Comparison.java ---
/**
 * Copyright (c) 2007, Institute of Parallel and Distributed Systems
 * (IPVS), Universität Stuttgart. 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 * 
 *  - Neither the names of the Institute of Parallel and Distributed
 *    Systems and Universität Stuttgart nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package ncunit.output;

import java.util.Iterator;

import net.tinyos.nesc.dump.xml.IntegerConstant;
import net.tinyos.nesc.dump.xml.Xfunction;
import net.tinyos.nesc.dump.xml.Xvariable;
import avrora.sim.Simulator;

public class Comparison {
	
	private Simulator simulator;
	
	public Comparison(Simulator simulator) {
		super();
		this.simulator = simulator;
	}
	
	public boolean equalsMem(int address1, int address2, int size) {
		for (int i=0; i<size; i++) {
			if (simulator.getInterpreter().getDataByte(address1 + i) != simulator.getInterpreter().getDataByte(address2 + i)) {
				return false;
			}
		}
		return true;
	}
	
	public boolean equalsParam(Xfunction function, Xvariable param, byte[] compValue) {
		int register = 24;
		Iterator iter = function.parameters.iterator();
		while (iter.hasNext() && (register >= 8)) {
			Xvariable currentParam = (Xvariable) iter.next();
			if (currentParam.equals(param)) {
				if (((IntegerConstant) currentParam.type.size).value > 2) {
					register -= ((IntegerConstant) currentParam.type.size).value - 2;
					if (register % 2 != 0) {
						register--;
					}
				}
				// TODO How to compare values pointed to by pointers? Additional operator?
				if (register >= 8) {
					for (int i=0; i<((IntegerConstant) currentParam.type.size).value; i++) {
						if (compValue[i] != simulator.getInterpreter().getRegisterByte(register + i)) {
							return false;
						}
					}
					return true;
				}
				register -= 2;
			}
			else {
				register -= ((IntegerConstant) currentParam.type.size).value;
				if (register % 2 != 0) {
					register--;
				}
			}
		}
		// TODO remaining parameters on stack
		// param not found or too many parameters
		return false;
	}

	public boolean notEqualsParam(Xfunction function, Xvariable param, byte[] compValue) {
		int register = 24;
		Iterator iter = function.parameters.iterator();
		while (iter.hasNext() && (register >= 8)) {
			Xvariable currentParam = (Xvariable) iter.next();
			if (currentParam.equals(param)) {
				if (((IntegerConstant) currentParam.type.size).value > 2) {
					register -= ((IntegerConstant) currentParam.type.size).value - 2;
					if (register % 2 != 0) {
						register--;
					}
				}
				// TODO How to compare values pointed to by pointers? Additional operator?
				if (register >= 8) {
					boolean foundDifference = false;
					for (int i=0; i<((IntegerConstant) currentParam.type.size).value; i++) {
						if (compValue[i] != simulator.getInterpreter().getRegisterByte(register + i)) {
							foundDifference = true;
						}
					}
					if (foundDifference) {
						return true;
					}
					else {
						return false;
					}
				}
				register -= 2;
			}
			else {
				register -= ((IntegerConstant) currentParam.type.size).value;
				if (register % 2 != 0) {
					register--;
				}
			}
		}
		// TODO remaining parameters on stack
		// param not found or too many parameters
		return false;
	}

}

--- NEW FILE: TokenList.java ---
/**
 * Copyright (c) 2007, Institute of Parallel and Distributed Systems
 * (IPVS), Universität Stuttgart. 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 * 
 *  - Neither the names of the Institute of Parallel and Distributed
 *    Systems and Universität Stuttgart nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package ncunit.output;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;

import ncunit.parser.Token;

/**
 * 
 * 
 * @author lachenas
 */
public class TokenList {

	private Token head;

	private Token tail;

	public TokenList(Token head, Token tail) {
		this.head = head;
		this.tail = tail;
	}

	public void print(PrintWriter pw) {
		for (Token p = head; true; p = p.next) {
			printSpecialTokens(pw, p.specialToken);
			pw.print(p.image);
			if (p == tail) {
				break;
			}
		}
		pw.close();
	}

	private void printSpecialTokens(PrintWriter pw, Token st) {
		if (st != null) {
			printSpecialTokens(pw, st.specialToken);
			pw.print(st.image);
		}
	}

	public void printWithSpecials(PrintWriter pw) {
		for (Token p = head; p != tail; p = p.next) {
			printSpecialTokens(pw, p.specialToken);
			pw.print(p.image);
		}
	}
	
	public InputStream getOutputAsInputStream() throws IOException {
		PipedOutputStream outputStream = new PipedOutputStream();
		final PrintWriter outputWriter = new PrintWriter(outputStream);
		PipedInputStream result =  new PipedInputStream(outputStream);
		new Thread() {
			public void run() {
				print(outputWriter);
				outputWriter.close();
			}
		}.start();
		return result;
	}

}

--- NEW FILE: NCUnit.java ---
/**
 * Copyright (c) 2007, Institute of Parallel and Distributed Systems
 * (IPVS), Universität Stuttgart. 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 * 
 *  - Neither the names of the Institute of Parallel and Distributed
 *    Systems and Universität Stuttgart nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package ncunit.output;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ncunit.parser.NCUnitParser;
import ncunit.parser.ParseException;

public class NCUnit {
	
	private HashMap<String, List<String>> testFunctions = null;
	private HashMap<String, Set<String>> assertUses = null;
	private String currentFileName;
	private int testCaseCounter = 1;
	private HashSet<String> uninlineFunctions = new HashSet<String>();

	public NCUnit() {
		super();
	}
	
	public String generateTestCalls() {
		String result = "";
		result += "#include <avr/pgmspace.h>\n";
		result += "static const prog_uchar _testCaseNumber = 0xff;\n\n";
		result += "command void TestStarter.startTest() {\n";
		result += "call Leds.init();\n";
		result += "  switch (PRG_RDB(&_testCaseNumber)) {\n";
		List<String> functionList = testFunctions.get(currentFileName);
		for (String functionName : functionList) {
			result += "  case "+testCaseCounter+":\n";
			result += "call Leds.greenToggle();\n";
			result += "  signal TestStarter.testCaseStart(\"Test case \\\""+functionName+"\\\": \");\n";
			result += "  "+functionName+"();\n";
			result += "  signal TestStarter.testCaseEnd();\n";
			result += "  break;\n";
			testCaseCounter++;
		}
		result += "  default:";
		result += "call Leds.yellowToggle();\n";
		result += "  }\n";
		result += "}";
		return result;
	}

	public void process(String xmlFile, String outputDir) {
		try {
			TestXMLParser xmlParser = new TestXMLParser(new FileInputStream(xmlFile));
			testFunctions = xmlParser.searchTestAttribute();
			assertUses = xmlParser.searchAssertUses();
			
		    for (String fileName : testFunctions.keySet()) {
		    	currentFileName = fileName;
		    	parseFile(fileName, outputDir, assertUses.get(fileName));
		    }
		    for (String fileName : assertUses.keySet()) {
		    	if (!testFunctions.keySet().contains(fileName)) {
			    	currentFileName = fileName;
			    	parseFile(fileName, outputDir, assertUses.get(fileName));
		    	}
		    }
		    
		    writeTestStarterC(outputDir);
		    writeStartScripts(outputDir);
		    writeUninlineFunctions(outputDir);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (ParseException e) {
			e.printStackTrace();
			System.exit(-1);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

	private void parseFile(String fileName, String outputDir, Set<String> assertInterfaces) throws ParseException, IOException{
		System.out.println("Parsing file "+fileName);
		NCUnitParser ncParser;
		File file = new File(fileName);
		ncParser = new NCUnitParser(this, new FileInputStream(file), assertInterfaces);
		ncParser.nesCFile().print(new PrintWriter(new FileOutputStream(outputDir+"/"+file.getName())));
		uninlineFunctions.addAll(ncParser.getUninlineFunctions());
	}
	
	private void writeTestStarterC(String outputDir) {
		try {
			PrintWriter writer = new PrintWriter(new FileOutputStream(outputDir+"/TestStarterC.nc"));
			writer.println("configuration TestStarterC {");
			writer.println("  provides interface TestStarter;");
			writer.println("}");
			writer.println("implementation {");
			for (String fileName : testFunctions.keySet()) {
				String componentName = getComponentName(fileName);
				writer.println("  components "+componentName+";");
				writer.println("  TestStarter = "+componentName+";");
			}
			writer.println("}");
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private void writeUninlineFunctions(String outputDir) {
		if (uninlineFunctions.isEmpty()) {
			new File(outputDir+"/uninline.txt").delete();
		}
		else {
			try {
				PrintWriter writer = new PrintWriter(outputDir+"/uninline.txt");
				writer.print("-funinline=");
				boolean isFirst = true;
				for (String function : uninlineFunctions) {
					if (!isFirst) {
						writer.print(",");
					}
					else {
						isFirst = false;
					}
					writer.print(function.replaceAll("\\.", "\\\\\\$"));
				}
				writer.println();
				writer.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
	}
	
	private String getComponentName(String fileName) {
		String componentName = fileName.substring(0, fileName.lastIndexOf(".nc"));
		if (componentName.indexOf("/") >= 0) {
			componentName = componentName.substring(componentName.lastIndexOf("/")+1);
		}
		if (componentName.indexOf("\\") >= 0) {
			componentName = componentName.substring(componentName.lastIndexOf("\\")+1);
		}
		return componentName;
	}
	
	private void writeStartScripts(String outputDir) {
		int counter = 1;
		for (String moduleName : testFunctions.keySet()) {
			String componentName = getComponentName(moduleName);
			for (int i=0; i<testFunctions.get(moduleName).size(); i++) {
				try {
					PrintWriter writer = new PrintWriter(outputDir+"/testcase"+counter);
					writer.println("#!/bin/bash");
					writer.println("echo \"nCUnit: "+componentName+"."+testFunctions.get(moduleName).get(i)+"\"");
					writer.println("cd "+new File(outputDir).getAbsolutePath().replace('\\', '/'));
					writer.println("if [[ $OS = \"Windows_NT\" ]]");
					writer.println("then");
					writer.println("  NCUNIT_CP=\"`cygpath --windows ${NCUNIT_ROOT}/bin`;`cygpath --windows ${NESC_CP}`;`cygpath --windows ${AVRORA_ROOT}/bin`;${CLASSPATH}\"");
					writer.println("else");
					writer.println("  NCUNIT_CP=\"${NCUNIT_ROOT}/bin:${NESC_CP}:${AVRORA_ROOT}/bin:${CLASSPATH}\"");
					writer.println("fi");
					writer.println("java -cp \"$NCUNIT_CP\" avrora.Main -colors=false -banner=false -platform=mica2 -seconds=100 -monitors=ncunit.avrora.NCUnitMonitor,ncunit.avrora.DebugOutputMonitor -test-component="+componentName+" -test-case="+counter+" -symbol-table=../mica2test/main.st -xml-file=output.xml ../mica2test/main.od");
					writer.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
				counter++;
			}
			
		}
		try {
			PrintWriter writer = new PrintWriter(outputDir+"/start_tests");
			writer.println("#!/bin/bash");
			for (int i=1; i<counter; i++) {
				writer.println("bash "+new File(outputDir).getAbsolutePath().replace('\\', '/')+"/testcase"+i+" > testoutput"+i+".out");
			}
			// call result analyzer
			writer.println("if [[ $OS = \"Windows_NT\" ]]");
			writer.println("then");
			writer.println("  NCUNIT_CP=\"`cygpath --windows ${NCUNIT_ROOT}/bin`\"");
			writer.println("else");
			writer.println("  NCUNIT_CP=\"${NCUNIT_ROOT}/bin\"");
			writer.println("fi");
			writer.print("java -cp \"$NCUNIT_CP\" ncunit.output.TestCaseAnalyzer");
			for (int i=1; i<counter; i++) {
				writer.print(" testoutput"+i+".out");
			}
			writer.println();
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println("Parameters: xmlFile outputDir");
			System.exit(-1);
		}
		new NCUnit().process(args[0], args[1]);
	}

}

--- NEW FILE: TestCaseAnalyzer.java ---
/**
 * Copyright (c) 2007, Institute of Parallel and Distributed Systems
 * (IPVS), Universität Stuttgart. 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 * 
 *  - Neither the names of the Institute of Parallel and Distributed
 *    Systems and Universität Stuttgart nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package ncunit.output;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TestCaseAnalyzer {
	
	public void analyze(String[] fileNames) {
		int successCounter = 0;
		int failCounter = 0;
		for (String file : fileNames) {
			if (isSuccessful(file)) {
				successCounter++;
			}
			else {
				failCounter++;
			}
		}
		System.out.println("===============================================");
		System.out.println("Test cases: "+(successCounter+failCounter));
		System.out.println("Successful: "+successCounter);
		System.out.println("Failed:     "+failCounter);
	}
	
	private boolean isSuccessful(String fileName) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(fileName));
			String currentLine = reader.readLine();
			if ((currentLine != null) && (currentLine.indexOf("nCUnit:") >= 0)) {
				// first line has to start with name of function
				String testFunction = currentLine.substring(currentLine.indexOf(':')+1).trim();
				currentLine = reader.readLine();
				while (currentLine != null) {
					if (currentLine.indexOf("nCUnit: Completed test case successfully") >= 0) {
						reader.close();
						System.out.println(fileName+": Successfully executed "+testFunction);
						return true;
					}
					else if (currentLine.indexOf("nCUnit: Assertion failed during test case") >= 0) {
						reader.close();
						System.out.println(fileName+": Assertion failed in "+testFunction);
						return false;
					}
					currentLine = reader.readLine();
				}
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// unreachable if correct file processed
		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length == 0) {
			System.err.println("Parameters: name of test case output files");
			System.exit(1);
		}
		new TestCaseAnalyzer().analyze(args);
	}

}

--- NEW FILE: TestXMLParser.java ---
/**
 * Copyright (c) 2007, Institute of Parallel and Distributed Systems
 * (IPVS), Universität Stuttgart. 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 * 
 *  - Neither the names of the Institute of Parallel and Distributed
 *    Systems and Universität Stuttgart nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package ncunit.output;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import net.tinyos.nesc.dump.NDReader;
import net.tinyos.nesc.dump.xml.Xfunction;
import net.tinyos.nesc.dump.xml.Xinterface;
import net.tinyos.nesc.dump.xml.Xnesc;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class TestXMLParser {

	public TestXMLParser(InputStream stream) {
		super();
		try {
			new NDReader().parse(new InputSource(stream));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}
	
	public HashMap<String, List<String>> searchTestAttribute() {
		HashMap<String, List<String>> testFunctions = new HashMap<String, List<String>>(); 
		ListIterator functionListIter = Xnesc.functionList.listIterator();
		while (functionListIter.hasNext()) {
			Xfunction currentFunction = (Xfunction) functionListIter.next();
			if (currentFunction.attributeLookup("test") != null) {
				//System.out.println(currentFunction.location.filename);
				List<String> functions = testFunctions.get(currentFunction.location.filename);
				if (functions == null) {
					functions = new LinkedList<String>();
					testFunctions.put(currentFunction.location.filename, functions);
				}
				functions.add(currentFunction.name);
			}
		}
		return testFunctions;
	}
	
	public HashMap<String, Set<String>> searchAssertUses() {
		HashMap<String, Set<String>> assertUses = new HashMap<String, Set<String>>(); 
		ListIterator<Xinterface> interfaceIter = (ListIterator<Xinterface>) Xnesc.interfaceList.listIterator();
		while (interfaceIter.hasNext()) {
			Xinterface currentInterface = interfaceIter.next();
			if ("Assert".equals(currentInterface.instance.parent.qname) && !currentInterface.provided) {
				Set<String> interfaces = assertUses.get(currentInterface.location.filename);
				if (interfaces == null) {
					interfaces = new HashSet<String>();
					assertUses.put(currentInterface.location.filename, interfaces);
				}
				interfaces.add(currentInterface.name);
			}
		}
		return assertUses;
	}
	
}



More information about the Tinyos-contrib-commits mailing list