Clover Coverage Report - Kuali Spring Utilities 1.1.2-SNAPSHOT
Coverage timestamp: Tue Nov 6 2012 17:56:04 EST
../../../img/srcFileCovDistChart0.png 52% of files have more coverage
89   166   28   7.42
28   147   0.31   12
12     2.33  
1    
 
  LicenseHeaderTest       Line # 14 89 0% 28 129 0% 0.0
 
No Tests
 
1    package org.springframework.beans;
2   
3    import java.io.File;
4    import java.io.FileInputStream;
5    import java.io.FileOutputStream;
6    import java.io.IOException;
7    import java.io.InputStream;
8    import java.io.OutputStream;
9    import java.util.ArrayList;
10    import java.util.List;
11   
12    import org.apache.commons.io.IOUtils;
13   
 
14    public class LicenseHeaderTest {
15   
 
16  0 toggle public static void main(String[] args) {
17  0 new LicenseHeaderTest().exec(args);
18    }
19   
 
20  0 toggle protected void exec(String[] args) {
21  0 try {
22  0 long beg = System.currentTimeMillis();
23  0 String dir = "c:/eclipse/sts/2.6.1/ide/ws/rice-trunk";
24  0 List<File> xmlFiles = getFiles(new File(dir));
25  0 System.out.println(xmlFiles.size());
26  0 List<File> problemFiles = getProblemFiles(xmlFiles);
27  0 String[] invalidHeaders = getInvalidHeaders();
28  0 System.out.println(problemFiles.size());
29  0 for (int i = 0; i < problemFiles.size(); i++) {
30  0 File problemFile = problemFiles.get(i);
31  0 System.out.println(problemFile.getAbsolutePath());
32  0 reWrite(problemFile, invalidHeaders);
33    }
34  0 long end = System.currentTimeMillis();
35  0 long elapsed = end - beg;
36  0 double seconds = elapsed / 1000D;
37  0 System.out.println(seconds);
38    } catch (Throwable t) {
39  0 t.printStackTrace();
40    }
41    }
42   
 
43  0 toggle protected void reWrite(File file, String[] invalidHeaders) throws IOException {
44  0 InputStream in = new FileInputStream(file);
45  0 String oldContent = IOUtils.toString(in);
46  0 in.close();
47  0 String newContent = getGoodContent(oldContent, invalidHeaders);
48  0 OutputStream out = new FileOutputStream(file);
49  0 IOUtils.write(newContent.getBytes(), out);
50  0 out.close();
51    }
52   
 
53  0 toggle protected String getGoodContent(String content, String[] invalidHeaders) {
54  0 for (String invalidHeader : invalidHeaders) {
55  0 content = content.replace(invalidHeader, "");
56    }
57  0 return content;
58   
59    }
60   
 
61  0 toggle protected String[] getInvalidHeaders() throws IOException {
62  0 String baseDir = "C:/eclipse/sts/2.6.1/ide/ws/kuali-spring-util/src/test/resources";
63  0 String[] filenames = { "invalid1.txt", "invalid2.txt", "invalid3.txt", "invalid4.txt", "invalid5.txt" };
64  0 String[] invalidHeaders = new String[filenames.length];
65  0 int count = 0;
66  0 for (String filename : filenames) {
67  0 String fullFileName = baseDir + "/" + filename;
68  0 String content = IOUtils.toString(new FileInputStream(fullFileName));
69  0 invalidHeaders[count++] = content;
70    }
71  0 return invalidHeaders;
72    }
73   
 
74  0 toggle protected List<File> getProblemFiles(List<File> files) throws IOException {
75  0 List<File> problemFiles = new ArrayList<File>();
76  0 for (File file : files) {
77  0 List<String> lines = IOUtils.readLines(new FileInputStream(file));
78   
79  0 int prologIndex = getPrologIndex(lines);
80  0 if (prologIndex != -1 && prologIndex != 0) {
81    // problemFiles.add(file);
82  0 continue;
83    }
84   
85  0 boolean copyRightIssue = isMultipleCopyRights(lines);
86  0 if (copyRightIssue) {
87  0 problemFiles.add(file);
88  0 continue;
89    }
90    }
91  0 return problemFiles;
92    }
93   
 
94  0 toggle protected boolean isMultipleCopyRights(List<String> strings) {
95  0 int count = 0;
96  0 for (int i = 0; i < strings.size(); i++) {
97  0 String s = strings.get(i);
98  0 String lowerCase = s.toLowerCase();
99  0 int pos = lowerCase.indexOf("copyright");
100  0 if (pos != -1) {
101  0 count++;
102    }
103    }
104  0 return count > 1;
105    }
106   
 
107  0 toggle protected int getPrologIndex(List<String> strings) {
108  0 for (int i = 0; i < strings.size(); i++) {
109  0 String s = strings.get(i);
110  0 if (isProlog(s)) {
111  0 return i;
112    }
113    }
114  0 return -1;
115    }
116   
 
117  0 toggle protected boolean isProlog(String s) {
118  0 String lowerCase = s.toLowerCase();
119  0 int pos1 = lowerCase.indexOf("<?xml");
120  0 int pos2 = lowerCase.indexOf("?>");
121  0 boolean flag1 = pos1 != -1 && pos2 != -1;
122  0 boolean flag2 = pos2 > pos1;
123  0 return flag1 && flag2;
124    }
125   
 
126  0 toggle protected boolean isSkip(File file) {
127  0 String name = file.getAbsolutePath();
128  0 if (name.contains(".svn")) {
129  0 return true;
130    }
131  0 if (name.contains("config\\ide")) {
132  0 return true;
133    }
134  0 if (name.contains("db\\impex")) {
135  0 return true;
136    }
137  0 return false;
138    }
139   
 
140  0 toggle protected boolean isAdd(File file) {
141  0 String name = file.getAbsolutePath();
142  0 if (name.contains(".x")) {
143  0 return true;
144    } else {
145  0 return false;
146    }
147    }
148   
 
149  0 toggle protected List<File> getFiles(File dir) {
150  0 File[] contents = dir.listFiles();
151  0 List<File> files = new ArrayList<File>();
152  0 for (File file : contents) {
153  0 if (isSkip(file)) {
154  0 continue;
155    }
156  0 if (file.isDirectory()) {
157  0 files.addAll(getFiles(file));
158    } else {
159  0 if (isAdd(file)) {
160  0 files.add(file);
161    }
162    }
163    }
164  0 return files;
165    }
166    }