001: package test.converter;
002:
003: import org.testng.Assert;
004: import org.testng.JUnitConverter;
005: import org.testng.annotations.Test;
006:
007: import java.io.BufferedReader;
008: import java.io.File;
009: import java.io.FileNotFoundException;
010: import java.io.FileReader;
011: import java.io.IOException;
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.List;
015: import java.util.regex.Pattern;
016:
017: public class JUnitConverterTest {
018:
019: /**
020: *
021: * @param fileName The filename to parse
022: * @param regexp The regular expression
023: * @param resultLines An out parameter that will contain all the lines
024: * that matched the regexp
025: * @return A List<Integer> containing the lines of all the matches
026: *
027: * Note that the size() of the returned valuewill always be equal to
028: * result.size() at the end of this function.
029: */
030: static public List grep(File fileName, String regexp,
031: List resultLines) {
032: List resultLineNumbers = new ArrayList();
033: try {
034: BufferedReader fr = new BufferedReader(new FileReader(
035: fileName));
036: String line = fr.readLine();
037: int currentLine = 0;
038: Pattern p = Pattern.compile(".*" + regexp + ".*");
039:
040: while (null != line) {
041: // ppp("COMPARING " + p + " TO @@@" + line + "@@@");
042: if (p.matcher(line).matches()) {
043: resultLines.add(line);
044: resultLineNumbers.add(currentLine);
045: }
046:
047: line = fr.readLine();
048: currentLine++;
049: }
050: } catch (FileNotFoundException e) {
051: e.printStackTrace();
052: } catch (IOException e) {
053: e.printStackTrace();
054: }
055:
056: return resultLineNumbers;
057:
058: }
059:
060: public static void ppp(String s) {
061: System.out.println("[JUnitConverterTest] " + s);
062: }
063:
064: /**
065: * @param fileName
066: * @param tag
067: * @param annotationType
068: * @param expected A list of line numbers where the tag is expected
069: * to be present
070: */
071: private void runTest(File sourceDir, String packageName,
072: String fileName, String tag, String annotationType,
073: List expected) {
074: String outputDir = System.getProperty("java.io.tmpdir");
075: String packageDir = packageName
076: .replace('.', File.separatorChar);
077: List args = new ArrayList();
078: args.add("-quiet");
079: args.add(annotationType);
080: args.add("-srcdir");
081: args.add(sourceDir.getAbsolutePath());
082: args.add("-d");
083: args.add(outputDir);
084: String[] argv = (String[]) args
085: .toArray(new String[args.size()]);
086: JUnitConverter.main(argv);
087:
088: List resultLines = new ArrayList();
089: File file = new File(outputDir, packageDir + File.separatorChar
090: + fileName);
091: List actualLineNumbers = grep(file, tag, resultLines);
092: Assert.assertEquals(actualLineNumbers, expected, file
093: + "\n tag:" + tag);
094:
095: }
096:
097: private void runJavaDocTest(File sourcePath, String pkg,
098: String fileName, List[] expected) {
099: runTest(sourcePath, pkg, fileName, "@testng.test", "-javadoc",
100: expected[0]);
101: runTest(sourcePath, pkg, fileName, "@testng.before-method",
102: "-javadoc", expected[1]);
103: runTest(sourcePath, pkg, fileName, "@testng.after-method",
104: "-javadoc", expected[2]);
105: }
106:
107: private void runAnnotationTest(File sourcePath, String pkg,
108: String fileName, List[] expected) {
109: runTest(sourcePath, pkg, fileName, "@Test", "-annotation",
110: expected[0]);
111: runTest(sourcePath, pkg, fileName, "@BeforeMethod",
112: "-annotation", expected[1]);
113: runTest(sourcePath, pkg, fileName, "@AfterMethod",
114: "-annotation", expected[2]);
115: }
116:
117: @Test(parameters={"source-directory"})
118: public void testAnnotations(String dir) {
119: runAnnotationTest(new File(dir), "test.converter",
120: "ConverterSample1.java", new List[] {
121: Arrays.asList(23, 30, 35), Arrays.asList(9),
122: Arrays.asList(18) });
123: }
124:
125: @Test(parameters={"source-directory"})
126: public void testAnnotationsNoPackage(String dir) {
127: runAnnotationTest(new File(dir, "../../.."), "",
128: "ConverterSample2.java", new List[] {
129: Arrays.asList(23, 30, 35), Arrays.asList(9),
130: Arrays.asList(18) });
131: }
132:
133: @Test(parameters={"source-directory"})
134: public void testJavaDoc(String dir) {
135: runJavaDocTest(new File(dir), "test.converter",
136: "ConverterSample1.java", new List[] {
137: Arrays.asList(25, 34, 41), Arrays.asList(7),
138: Arrays.asList(18) });
139: }
140:
141: @Test(parameters={"source-directory"})
142: public void testJavaDocNoPackage(String dir) {
143: runJavaDocTest(new File(dir, "../../.."), "",
144: "ConverterSample2.java", new List[] {
145: Arrays.asList(25, 34, 41), Arrays.asList(7),
146: Arrays.asList(18) });
147: }
148:
149: }
|