001: package test.converter;
002:
003: import com.thoughtworks.qdox.JavaDocBuilder;
004: import com.thoughtworks.qdox.model.JavaClass;
005: import com.thoughtworks.qdox.model.JavaMethod;
006: import com.thoughtworks.qdox.model.Type;
007:
008: import java.io.BufferedReader;
009: import java.io.File;
010: import java.io.FileNotFoundException;
011: import java.io.FileReader;
012: import java.io.IOException;
013: import java.util.ArrayList;
014: import java.util.Arrays;
015: import java.util.List;
016: import java.util.regex.Pattern;
017:
018: import org.testng.AnnotationConverter;
019: import org.testng.Assert;
020: import org.testng.annotations.Test;
021:
022: public class AnnotationConverterTest {
023:
024: /**
025: *
026: * @param fileName The filename to parse
027: * @param regexp The regular expression
028: * @param resultLines An out parameter that will contain all the lines
029: * that matched the regexp
030: * @return A List<Integer> containing the lines of all the matches
031: *
032: * Note that the size() of the returned valuewill always be equal to
033: * result.size() at the end of this function.
034: */
035: static List<Integer> grep(File fileName, String regexp,
036: List<String> resultLines) {
037: List<Integer> resultLineNumbers = new ArrayList<Integer>();
038: BufferedReader fr = null;
039: try {
040: fr = new BufferedReader(new FileReader(fileName));
041: String line = fr.readLine();
042: int currentLine = 0;
043: Pattern p = Pattern.compile(".*" + regexp + ".*");
044:
045: while (null != line) {
046: // ppp("COMPARING " + p + " TO @@@" + line + "@@@");
047: if (p.matcher(line).matches()) {
048: resultLines.add(line);
049: resultLineNumbers.add(currentLine);
050: }
051:
052: line = fr.readLine();
053: currentLine++;
054: }
055: } catch (FileNotFoundException e) {
056: e.printStackTrace();
057: } catch (IOException e) {
058: e.printStackTrace();
059: } finally {
060: if (null != fr) {
061: try {
062: fr.close();
063: } catch (IOException ex) {
064: ex.printStackTrace();
065: }
066: }
067: }
068:
069: return resultLineNumbers;
070:
071: }
072:
073: public static void ppp(String s) {
074: System.out.println("[AnnotationConverterTest] " + s);
075: }
076:
077: /**
078: * @param fileName
079: * @param tag
080: * @param expected A list of line numbers where the tag is expected
081: * to be present
082: */
083: private void checkOutput(String packageName, String fileName,
084: String tag, List expected) {
085: String outputDir = System.getProperty("java.io.tmpdir");
086: String packageDir = packageName
087: .replace('.', File.separatorChar);
088: List<String> resultLines = new ArrayList<String>();
089: File file = new File(outputDir, packageDir + File.separatorChar
090: + fileName);
091: List<Integer> actualLineNumbers = grep(file, tag, resultLines);
092: Assert.assertEquals(actualLineNumbers, expected, file
093: + "\n tag:" + tag);
094:
095: }
096:
097: /**
098: * @param sourceDir
099: * @param annotationType
100: * @param outputDir
101: */
102: private void convertSource(File sourceDir, String annotationType) {
103: String outputDir = System.getProperty("java.io.tmpdir");
104: List<String> args = new ArrayList<String>();
105: args.add("-quiet");
106: args.add(annotationType);
107: args.add("-srcdir");
108: args.add(sourceDir.getAbsolutePath());
109: args.add("-d");
110: args.add(outputDir);
111: String[] argv = args.toArray(new String[args.size()]);
112: AnnotationConverter.main(argv);
113: }
114:
115: private void runAnnotationTest(File sourcePath, String pkg,
116: String fileName, List<?>[] expected) {
117: convertSource(sourcePath, "-annotation");
118: checkOutput(pkg, fileName, "@Test", expected[0]);
119: checkOutput(pkg, fileName, "@BeforeSuite", expected[1]);
120: checkOutput(pkg, fileName, "@AfterMethod", expected[2]);
121: checkOutput(pkg, fileName, "@ExpectedExceptions", expected[3]);
122: checkOutput(pkg, fileName, "@DataProvider", expected[4]);
123: checkOutput(pkg, fileName, "@Factory", expected[5]);
124: checkOutput(pkg, fileName, "@Parameters", expected[6]);
125: }
126:
127: @Test(parameters={"source-directory"})
128: public void testAnnotations(String dir) {
129: runAnnotationTest(new File(dir), "test.sample",
130: "ConverterSample3.java", new List[] {
131: Arrays.asList(13, 27, 38, 46, 71, 95, 104),
132: Arrays.asList(19), Arrays.asList(54),
133: Arrays.asList(37, 70), Arrays.asList(79),
134: Arrays.asList(87), Arrays.asList(69) });
135: }
136:
137: @Test(parameters={"source-directory"})
138: public void testAnnotationsNoPackage(String dir) {
139: runAnnotationTest(new File(dir, "../../.."), "",
140: "ConverterSample4.java", new List[] {
141: Arrays.asList(28, 39, 47, 71),
142: Arrays.asList(20), Arrays.asList(55),
143: Arrays.asList(38, 70), Arrays.asList(79),
144: Arrays.asList(87), Arrays.asList() });
145: }
146:
147: @Test(parameters={"source-directory"})
148: public void testQdoxExtractsLineNumbers(String dir) {
149: JavaDocBuilder builder = new JavaDocBuilder();
150: try {
151: builder.addSource(new File(dir, "ConverterSample3.java"));
152: } catch (Exception e) {
153: Assert.fail("Could not read specified file");
154: }
155: JavaClass fooClass = builder
156: .getClassByName("test.sample.ConverterSample3");
157: Assert
158: .assertEquals(fooClass.getLineNumber(), 13,
159: "An older (or regressed) version of qdox.jar will report 0 as the line number");
160: JavaMethod getIMethod = fooClass.getMethodBySignature(
161: "plainTest", Type.EMPTY_ARRAY);
162: Assert
163: .assertEquals(getIMethod.getLineNumber(), 25,
164: "An older (or regressed) version of qdox.jar will report 0 as the line number");
165: }
166:
167: }
|