| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 129 (129) |
Complexity: 28 |
Complexity Density: 0.31 |
|
| 14 |
|
public class LicenseHeaderTest { |
| 15 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 16 |
0
|
public static void main(String[] args) {... |
| 17 |
0
|
new LicenseHeaderTest().exec(args); |
| 18 |
|
} |
| 19 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.18 |
|
| 20 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 43 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 53 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 61 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 74 |
0
|
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 |
|
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 94 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 107 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 117 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 126 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 140 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 149 |
0
|
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 |
|
} |