|
1 | 1 | import com.opencsv.CSVWriter; |
2 | | -import entity.FileEntity; |
| 2 | +import entity.ClassEntity; |
3 | 3 | import entity.MethodEntity; |
4 | 4 |
|
5 | 5 | import java.io.FileWriter; |
|
11 | 11 |
|
12 | 12 | public class ResultsWriter { |
13 | 13 |
|
14 | | - List<FileEntity> files; |
15 | | - String testFileCSV,testMethodCSV; |
| 14 | + private CSVWriter classCSVWriter, methodCSVWriter; |
16 | 15 |
|
17 | | - public ResultsWriter(List<FileEntity> files){ |
18 | | - this.files = files; |
| 16 | + public static ResultsWriter createResultsWriter() throws IOException { |
| 17 | + return new ResultsWriter(); |
19 | 18 | } |
20 | 19 |
|
21 | | - public void outputToCSV(boolean includeMethodDetails) throws IOException { |
22 | | - String time = String.valueOf(Calendar.getInstance().getTimeInMillis()); |
23 | | - |
24 | | - if(includeMethodDetails){ |
25 | | - testFileCSV = MessageFormat.format("{0}_{1}_{2}.{3}", "Output","TestFile",time, "csv"); |
26 | | - testMethodCSV = MessageFormat.format("{0}_{1}_{2}.{3}", "Output","TestMethod", time, "csv"); |
27 | | - outputTestFileResults(); |
28 | | - outputTestMethodResults(); |
29 | | - } |
30 | | - else |
31 | | - { |
32 | | - testFileCSV = MessageFormat.format("{0}_{1}_{2}.{3}", "Output","TestFile", time, "csv"); |
33 | | - outputTestFileResults(); |
34 | | - } |
| 20 | + private ResultsWriter() throws IOException { |
| 21 | + String time = String.valueOf(Calendar.getInstance().getTimeInMillis()); |
| 22 | + String classFileName = MessageFormat.format("{0}_{1}_{2}.{3}", "Output", "Class", time, "csv"); |
| 23 | + String methodFileName = MessageFormat.format("{0}_{1}_{2}.{3}", "Output", "Method", time, "csv"); |
| 24 | + methodCSVWriter = new CSVWriter(new FileWriter(methodFileName), ','); |
| 25 | + classCSVWriter = new CSVWriter(new FileWriter(classFileName), ','); |
| 26 | + |
| 27 | + createClassFile(); |
| 28 | + createMethodFile(); |
35 | 29 | } |
36 | 30 |
|
37 | | - private void outputTestMethodResults() throws IOException { |
38 | | - CSVWriter writer = new CSVWriter(new FileWriter(testMethodCSV.toString()), ','); |
| 31 | + private void createClassFile() throws IOException { |
39 | 32 | List<String[]> fileLines = new ArrayList<String[]>(); |
40 | | - String[] columnNames = {"App","Tag", "FilePath", "RelativeFilePath", "FileName", "MethodName","TotalLines","MethodHasAnnotation", "MethodHasTestInName", "FileHasTestInName"}; |
| 33 | + String[] columnNames = { |
| 34 | + "App", |
| 35 | + "Tag", |
| 36 | + "FilePath", |
| 37 | + "RelativeFilePath", |
| 38 | + "FileName", |
| 39 | + "ClassName", |
| 40 | + "TotalImports", |
| 41 | + "TotalMethods", |
| 42 | + "TotalMethodStatements", |
| 43 | + "TotalTestMethods", |
| 44 | + "AnnotationCount", |
| 45 | + "TestsWithoutAnnotationCount", |
| 46 | + "HasTestInFileName", |
| 47 | + "HasTestInClassName", |
| 48 | + "junitFrameworkTest", |
| 49 | + "junitFrameworkTestCase", |
| 50 | + "orgJunitTest", |
| 51 | + "androidTestAndroidTestCase", |
| 52 | + "androidTestInstrumentationTestCase", |
| 53 | + "orgJunitAssert", |
| 54 | + "androidTestActivityInstrumentationTestCase2" |
| 55 | + }; |
41 | 56 | fileLines.add(columnNames); |
42 | 57 |
|
43 | | - String[] dataLine; |
44 | | - for (FileEntity fileEntity: files) { |
45 | | - try { |
46 | | - for (MethodEntity methodEntity: fileEntity.getMethods()) { |
47 | | - dataLine = new String[11]; |
48 | | - dataLine[0] = fileEntity.getAppName(); |
49 | | - dataLine[1] = fileEntity.getTagName(); |
50 | | - dataLine[2] = fileEntity.getFilePath(); |
51 | | - dataLine[3] = fileEntity.getRelativeFilePath(); |
52 | | - dataLine[4] = fileEntity.getFileName(); |
53 | | - dataLine[5] = methodEntity.getMethodName(); |
54 | | - dataLine[6] = String.valueOf(methodEntity.getTotalLines()); |
55 | | - dataLine[7] = methodEntity.isHasAnnotation()?"True":"False"; |
56 | | - dataLine[8] = methodEntity.isHasTestInName()?"True":"False"; |
57 | | - dataLine[9] = fileEntity.isHasTestInFileName()?"True":"False"; |
58 | | - dataLine[10] = fileEntity.getHas_androidtestActivityInstrumentationTestCase2()?"True":"False"; |
59 | | - |
60 | | - fileLines.add(dataLine); |
61 | | - } |
62 | | - } catch (Exception e) { |
63 | | - Util.writeException(e,"File: "+ fileEntity.getFilePath()); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - writer.writeAll(fileLines,false); |
68 | | - |
69 | | - writer.close(); |
| 58 | + classCSVWriter.writeAll(fileLines, false); |
| 59 | + classCSVWriter.flush(); |
70 | 60 | } |
71 | 61 |
|
72 | | - private void outputTestFileResults() throws IOException { |
73 | | - CSVWriter writer = new CSVWriter(new FileWriter(testFileCSV.toString()), ','); |
| 62 | + private void createMethodFile() throws IOException { |
74 | 63 | List<String[]> fileLines = new ArrayList<String[]>(); |
75 | | - String[] columnNames = {"App","Tag","FilePath", "RelativeFilePath", "FileName", "TotalMethods","TotalTestMethods","AnnotationCount", "TestsWithoutAnnotationCount", "HasTestInFileName", |
76 | | - "junitFrameworkTest", "junitFrameworkTestCase", "orgJunitTest", "androidTestAndroidTestCase", "androidTestInstrumentationTestCase", "orgJunitAssert", "androidTestActivityInstrumentationTestCase2"}; |
| 64 | + String[] columnNames = { |
| 65 | + "App", |
| 66 | + "Tag", |
| 67 | + "FilePath", |
| 68 | + "RelativeFilePath", |
| 69 | + "FileName", |
| 70 | + "ClassName", |
| 71 | + "MethodName", |
| 72 | + "TotalStatements", |
| 73 | + "TotalParameters", |
| 74 | + "ReturnType", |
| 75 | + "AccessModifier", |
| 76 | + "MethodHasAnnotation", |
| 77 | + "MethodHasTestInName", |
| 78 | + "FileHasTestInName", |
| 79 | + "ClassHasTestInName" |
| 80 | + }; |
77 | 81 | fileLines.add(columnNames); |
78 | 82 |
|
79 | | - String[] dataLine; |
80 | | - for (FileEntity fileEntity: files) { |
81 | | - try { |
82 | | - dataLine = new String[17]; |
83 | | - dataLine[0] = fileEntity.getAppName(); |
84 | | - dataLine[1] = fileEntity.getTagName(); |
85 | | - dataLine[2] = fileEntity.getFilePath(); |
86 | | - dataLine[3] = fileEntity.getRelativeFilePath(); |
87 | | - dataLine[4] = fileEntity.getFileName(); |
88 | | - dataLine[5] = String.valueOf(fileEntity.getMethods().size()); |
89 | | - dataLine[6] = String.valueOf(fileEntity.getTotalTestMethods()); |
90 | | - dataLine[7] = String.valueOf(fileEntity.getTestAnnotationCount()); |
91 | | - dataLine[8] = String.valueOf(fileEntity.getTestMethodWithoutAnnotationCount()); |
92 | | - dataLine[9] = fileEntity.isHasTestInFileName()?"True":"False"; |
93 | | - dataLine[10] = fileEntity.getHas_junitframeworkTest()?"True":"False"; |
94 | | - dataLine[11] = fileEntity.getHas_junitframeworkTestCase()?"True":"False"; |
95 | | - dataLine[12] = fileEntity.getHas_orgjunitTest()?"True":"False"; |
96 | | - dataLine[13] = fileEntity.getHas_androidtestAndroidTestCase()?"True":"False"; |
97 | | - dataLine[14] = fileEntity.getHas_androidtestInstrumentationTestCase()?"True":"False"; |
98 | | - dataLine[15] = fileEntity.getHas_orgjunitAssert()?"True":"False"; |
99 | | - dataLine[16] = fileEntity.getHas_androidtestActivityInstrumentationTestCase2()?"True":"False"; |
100 | | - |
101 | | - fileLines.add(dataLine); |
102 | | - } catch (Exception e) { |
103 | | - Util.writeException(e,"File: "+ fileEntity.getFilePath()); |
104 | | - } |
105 | | - } |
| 83 | + methodCSVWriter.writeAll(fileLines, false); |
| 84 | + methodCSVWriter.flush(); |
| 85 | + } |
106 | 86 |
|
107 | | - writer.writeAll(fileLines,false); |
| 87 | + public void outputToCSV(ClassEntity classEntity) throws IOException { |
| 88 | + outputClassDetails(classEntity); |
| 89 | + outputMethodDetails(classEntity); |
| 90 | + } |
108 | 91 |
|
109 | | - writer.close(); |
| 92 | + public void closeOutputFiles() throws IOException { |
| 93 | + classCSVWriter.close(); |
| 94 | + methodCSVWriter.close(); |
110 | 95 | } |
111 | 96 |
|
| 97 | + private void outputMethodDetails(ClassEntity classEntity) throws IOException { |
| 98 | + List<String[]> fileLines = new ArrayList<String[]>(); |
| 99 | + String[] dataLine; |
| 100 | + for (MethodEntity methodEntity : classEntity.getMethods()) { |
| 101 | + |
| 102 | + dataLine = new String[15]; |
| 103 | + dataLine[0] = classEntity.getAppName(); |
| 104 | + dataLine[1] = classEntity.getTagName(); |
| 105 | + dataLine[2] = classEntity.getFilePath(); |
| 106 | + dataLine[3] = classEntity.getRelativeFilePath(); |
| 107 | + dataLine[4] = classEntity.getFileName(); |
| 108 | + dataLine[5] = classEntity.getClassName(); |
| 109 | + dataLine[6] = methodEntity.getMethodName(); |
| 110 | + dataLine[7] = String.valueOf(methodEntity.getTotalStatements()); |
| 111 | + dataLine[8] = String.valueOf(methodEntity.getParameterCount()); |
| 112 | + dataLine[9] = methodEntity.getReturnType(); |
| 113 | + dataLine[10] = methodEntity.getAccessModifier(); |
| 114 | + dataLine[11] = String.valueOf(methodEntity.isHasAnnotation()); |
| 115 | + dataLine[12] = String.valueOf(methodEntity.isHasTestInName()); |
| 116 | + dataLine[13] = String.valueOf(classEntity.isHasTestInFileName()); |
| 117 | + dataLine[14] = String.valueOf(classEntity.isHasTestInClassName()); |
| 118 | + |
| 119 | + fileLines.add(dataLine); |
| 120 | + } |
| 121 | + methodCSVWriter.writeAll(fileLines, false); |
| 122 | + methodCSVWriter.flush(); |
| 123 | + } |
112 | 124 |
|
| 125 | + private void outputClassDetails(ClassEntity classEntity) throws IOException { |
| 126 | + List<String[]> fileLines = new ArrayList<String[]>(); |
| 127 | + String[] dataLine; |
| 128 | + |
| 129 | + dataLine = new String[20]; |
| 130 | + dataLine[0] = classEntity.getAppName(); |
| 131 | + dataLine[1] = classEntity.getTagName(); |
| 132 | + dataLine[2] = classEntity.getFilePath(); |
| 133 | + dataLine[3] = classEntity.getRelativeFilePath(); |
| 134 | + dataLine[4] = classEntity.getFileName(); |
| 135 | + dataLine[5] = classEntity.getClassName(); |
| 136 | + dataLine[6] = String.valueOf(classEntity.getTotalImports()); |
| 137 | + dataLine[7] = String.valueOf(classEntity.getTotalMethods()); |
| 138 | + dataLine[8] = String.valueOf(classEntity.getTotalMethodStatement()); |
| 139 | + dataLine[9] = String.valueOf(classEntity.getTestAnnotationCount()); |
| 140 | + dataLine[10] = String.valueOf(classEntity.getTestMethodWithoutAnnotationCount()); |
| 141 | + dataLine[11] = String.valueOf(classEntity.isHasTestInFileName()); |
| 142 | + dataLine[12] = String.valueOf(classEntity.isHasTestInClassName()); |
| 143 | + dataLine[13] = String.valueOf(classEntity.getHas_junitframeworkTest()); |
| 144 | + dataLine[14] = String.valueOf(classEntity.getHas_androidtestAndroidTestCase()); |
| 145 | + dataLine[15] = String.valueOf(classEntity.getHas_orgjunitAssert()); |
| 146 | + dataLine[16] = String.valueOf(classEntity.getHas_junitframeworkTestCase()); |
| 147 | + dataLine[17] = String.valueOf(classEntity.getHas_orgjunitTest()); |
| 148 | + dataLine[18] = String.valueOf(classEntity.getHas_androidtestInstrumentationTestCase()); |
| 149 | + dataLine[19] = String.valueOf(classEntity.getHas_androidtestActivityInstrumentationTestCase2()); |
| 150 | + |
| 151 | + fileLines.add(dataLine); |
| 152 | + |
| 153 | + classCSVWriter.writeAll(fileLines, false); |
| 154 | + classCSVWriter.flush(); |
| 155 | + } |
113 | 156 | } |
0 commit comments