Update of /cvsroot/xdoclet/generama/src/main/java/org/generama/astunit
In directory sc8-pr-cvs1:/tmp/cvs-serv28151/src/main/java/org/generama/astunit
Added Files:
ASTTestCase.java
Log Message:
Applied GRA-1 - Lauren't port of ASTUnit
--- NEW FILE: ASTTestCase.java ---
package org.generama.astunit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import junit.framework.TestCase;
import antlr.collections.AST;
import antlr.TokenStreamException;
import antlr.RecognitionException;
import org.generama.astunit.parser.JavaLexer;
import org.generama.astunit.parser.JavaRecognizer;
/**
* <p>
* ASTTestCase is a JUnit extension that compares two Java sources
* (typically one kept as a static expected result and a generated one) on the AST
level.
* </p>
* <p>
* Also see QDox' <a
href="http://qdox.codehaus.org/apidocs/com/thoughtworks/qdox/junit/APITestCase.html">APITestCase</a>
for comparing two sources on the API level.
* </p>
*
* @author Aslak Hellesøy (Original JavaCC version)
* @author Laurent Etiemble (Port to ANTLR)
*/
public abstract class ASTTestCase extends TestCase {
/**
* Compares two source files.
*
* @param expected the expected source
* @param actual the actual source
*/
public static void assertEquals(File expected, File actual)
throws FileNotFoundException, RecognitionException, TokenStreamException {
assertAstEquals(expected, actual);
}
/**
* Compares two source readers.
*
* @param expected the expected source
* @param actual the actual source
*/
public static void assertEquals(Reader expected, Reader actual) throws
RecognitionException, TokenStreamException {
assertAstEquals(expected, actual);
}
/**
* Compares AST of both source files.
*
* <p><b>Note:</b> This method is for backward naming compatiblity
* with xjavadoc.codeunit.CodeTestCase.</p>
*
* @param expected the expected source
* @param actual the actual source
*/
public static void assertAstEquals(File expected, File actual)
throws FileNotFoundException, RecognitionException, TokenStreamException {
assertNotDir(expected, actual);
FileReader expectedReader = new FileReader(expected);
FileReader actualReader = new FileReader(actual);
assertAstEquals(expectedReader, actualReader);
}
/**
* Compares AST of both source readers.
*
* <p><b>Note:</b> This method is for backward naming compatiblity
* with xjavadoc.codeunit.CodeTestCase.</p>
*
* @param expected the expected source
* @param actual the actual source
*/
public static void assertAstEquals(Reader expected, Reader actual) throws
RecognitionException, TokenStreamException {
// Create a lexer that reads from the reader
JavaLexer expectedLexer = new JavaLexer(expected);
// Create a parser that reads from the scanner
JavaRecognizer expectedParser = new JavaRecognizer(expectedLexer);
// Create a lexer that reads from the reader
JavaLexer actualLexer = new JavaLexer(actual);
// Create a parser that reads from the scanner
JavaRecognizer actualParser = new JavaRecognizer(actualLexer);
// Parses the streams
expectedParser.compilationUnit();
actualParser.compilationUnit();
// Gets the root ASTs
AST expectedRoot = expectedParser.getAST();
AST actualRoot = actualParser.getAST();
// Test for equality
assertAstEquals(expectedRoot, actualRoot);
}
/**
* Generic method to compare recursively two AST nodes.
* <p>
* There are three steps for the comparison :
* <ul>
* <li>- nodes are compared to each others</li>
* <li>- first child nodes are compared to each others</li>
* <li>- next sibling nodes are compared to each others</li>
* </ul>
* If all these comparison succeed, nodes are considered to be equals
* </p>
*
* @param expected node
* @param actual node
*/
private static void assertAstEquals(AST expected, AST actual) {
// Little hack to only build the detail buffer if
// there is a problem
if (!expected.equals(actual)) {
StringBuffer buffer = new StringBuffer("AST nodes must be equal");
buffer.append("\n");
buffer.append("Expected : Type=");
buffer.append(expected.getType());
buffer.append(" text='");
buffer.append(expected.getText());
buffer.append("'\n");
buffer.append("Actual : Type=");
buffer.append(actual.getType());
buffer.append(" text='");
buffer.append(actual.getText());
buffer.append("'");
assertTrue(buffer.toString(), expected.equals(actual));
}
// Test the child side
AST expectedChild = expected.getFirstChild();
AST actualChild = actual.getFirstChild();
if (expectedChild == null) {
assertNull("Null first child was expected", actualChild);
} else {
assertNotNull("Not null first child was expected", actualChild);
assertAstEquals(expectedChild, actualChild);
}
// Test the right side
AST expectedSibling = expected.getNextSibling();
AST actualSibling = actual.getNextSibling();
if (expectedSibling == null) {
assertNull("Null next sibling was expected", actualSibling);
} else {
assertNotNull("Not null next sibling was expected", actualSibling);
assertAstEquals(expectedSibling, actualSibling);
}
}
private static void assertNotDir(File expected, File actual) {
if (expected.isDirectory())
fail(
expected.getAbsolutePath() + " - should not have been a
directory");
if (actual.isDirectory())
fail(actual.getAbsolutePath() + " - should not have been a directory");
}
}
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel