01: package de.java2html.javasource.test;
02:
03: import java.io.IOException;
04: import java.io.StringReader;
05: import java.util.HashMap;
06: import java.util.Map;
07:
08: import de.java2html.javasource.JavaSource;
09: import de.java2html.javasource.JavaSourceParser;
10: import de.java2html.javasource.JavaSourceType;
11:
12: import junit.framework.TestCase;
13:
14: /**
15: * @author Markus Gebhard
16: */
17: public abstract class JavaSourceParserTestCase extends TestCase {
18:
19: private static Map map;
20:
21: /* Defines a simple language for encoding JavaSourcType constants
22: * to one character each.
23: * Space character == ignore */
24: static {
25: map = new HashMap();
26: //TODO Mar 12, 2004 (Markus Gebhard) Generics: map.put("G"), JavaSourceType.GENRICS);
27: map.put("A", JavaSourceType.ANNOTATION);
28: map.put("_", JavaSourceType.BACKGROUND);
29: map.put(":", JavaSourceType.LINE_NUMBERS); //irrelevant for parsing
30: map.put("#", JavaSourceType.COMMENT_BLOCK);
31: map.put("/", JavaSourceType.COMMENT_LINE);
32: map.put("K", JavaSourceType.KEYWORD);
33: map.put("S", JavaSourceType.STRING);
34: map.put("'", JavaSourceType.CHAR_CONSTANT);
35: map.put("1", JavaSourceType.NUM_CONSTANT);
36: map.put("{", JavaSourceType.PARENTHESIS);
37: map.put("T", JavaSourceType.CODE_TYPE);
38: map.put("C", JavaSourceType.CODE);
39: map.put("@", JavaSourceType.JAVADOC_KEYWORD);
40: map.put("<", JavaSourceType.JAVADOC_HTML_TAG);
41: map.put("L", JavaSourceType.JAVADOC_LINKS);
42: map.put("*", JavaSourceType.JAVADOC);
43: map.put("-", JavaSourceType.UNDEFINED); //irrelevant for parsing
44: }
45:
46: protected static JavaSource doParse(String text) throws IOException {
47: StringReader stringReader = new StringReader(text);
48: JavaSourceParser parser = new JavaSourceParser();
49: return parser.parse(stringReader);
50: }
51:
52: protected void assertParsedTypesEquals(String sourceCode,
53: String typeCode) throws IOException {
54: assertNotNull("SourceCode null - testcase broken", sourceCode);
55: assertNotNull("TypeCode null - testcase broken", typeCode);
56: JavaSource source = doParse(sourceCode);
57:
58: assertTrue(
59: "Error in TestCase: more types specified than resulted",
60: source.getClassification().length >= typeCode.length());
61:
62: for (int i = 0; i < typeCode.length(); ++i) {
63: if (typeCode.charAt(i) == ' ') {
64: continue;
65: }
66: JavaSourceType expectedType = getSourceTypeForTypeCode(typeCode
67: .charAt(i));
68: if (expectedType == null) {
69: throw new IllegalArgumentException(
70: "Unknown type mapping charcter for testing: '"
71: + typeCode.charAt(i) + "'");
72: }
73: assertEquals("At character " + i + " ("
74: + sourceCode.substring(i) + "):", expectedType,
75: source.getClassification()[i]);
76: }
77: }
78:
79: private JavaSourceType getSourceTypeForTypeCode(char character) {
80: return (JavaSourceType) map.get(String.valueOf(character));
81: }
82: }
|