1 |
|
package org.springframework.beans; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
|
import java.io.FileInputStream; |
5 |
|
import java.io.IOException; |
6 |
|
import java.io.InputStream; |
7 |
|
import java.io.OutputStream; |
8 |
|
import java.util.ArrayList; |
9 |
|
import java.util.HashSet; |
10 |
|
import java.util.Iterator; |
11 |
|
import java.util.List; |
12 |
|
import java.util.Properties; |
13 |
|
import java.util.Set; |
14 |
|
|
15 |
|
import org.apache.commons.io.FileUtils; |
16 |
|
import org.apache.commons.io.IOUtils; |
17 |
|
import org.junit.Test; |
18 |
|
import org.springframework.core.io.DefaultResourceLoader; |
19 |
|
import org.springframework.core.io.Resource; |
20 |
|
import org.springframework.core.io.ResourceLoader; |
21 |
|
|
|
|
| 9.3% |
Uncovered Elements: 88 (97) |
Complexity: 19 |
Complexity Density: 0.24 |
|
22 |
|
public class CopyrightHandlerTest { |
23 |
|
String tmpdir = System.getProperty("java.io.tmpdir"); |
24 |
|
|
25 |
|
public static final String ECL_CR = "${ecl.cr}"; |
26 |
|
public static final String ECL_LF = "${ecl.lf}"; |
27 |
|
public static final String CR = "\r"; |
28 |
|
public static final String LF = "\n"; |
29 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
30 |
0
|
public static void main(String[] args) {... |
31 |
0
|
new CopyrightHandlerTest().exec(args); |
32 |
|
} |
33 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
34 |
1
|
protected String getBaseDir() {... |
35 |
1
|
String basedir = System.getProperty("ecl.basedir"); |
36 |
1
|
if (basedir == null) { |
37 |
1
|
throw new RuntimeException("Set the system property 'ecl.basedir'"); |
38 |
|
} else { |
39 |
0
|
return basedir; |
40 |
|
} |
41 |
|
|
42 |
|
} |
43 |
|
|
|
|
| 8.6% |
Uncovered Elements: 32 (35) |
Complexity: 4 |
Complexity Density: 0.13 |
1
PASS
|
|
44 |
1
|
@Test... |
45 |
|
public void testDuplicateCopyright() { |
46 |
1
|
try { |
47 |
1
|
String basedir = getBaseDir(); |
48 |
0
|
System.out.println("Examining " + basedir); |
49 |
0
|
System.out.println("Tmp Dir: " + tmpdir); |
50 |
0
|
ProblemFileContext context = new MultipleCopyrightContext(basedir); |
51 |
0
|
ProblemFileDetector detector = new ProblemFileDetector(); |
52 |
0
|
List<File> files = detector.getProblemFiles(context); |
53 |
0
|
System.out.println("Located " + files.size() + " unknown files with multiple 'Copyright' lines"); |
54 |
0
|
Properties invalidEcl = getProperties("invalid-ecl.properties"); |
55 |
0
|
Set<String> contentsToRemove = getValues(invalidEcl); |
56 |
0
|
ContentRemover remover = new ContentRemover(); |
57 |
0
|
Iterator<File> itr = files.iterator(); |
58 |
0
|
List<File> updatedFiles = new ArrayList<File>(); |
59 |
0
|
List<File> nonUpdatedFiles = new ArrayList<File>(); |
60 |
0
|
while (itr.hasNext()) { |
61 |
0
|
File file = itr.next(); |
62 |
0
|
boolean updated = remover.removeContent(file, contentsToRemove); |
63 |
0
|
if (!updated) { |
64 |
0
|
copy(file); |
65 |
0
|
nonUpdatedFiles.add(file); |
66 |
|
} else { |
67 |
0
|
updatedFiles.add(file); |
68 |
|
} |
69 |
|
} |
70 |
0
|
System.out.println("Updated files"); |
71 |
0
|
System.out.println("---------------------"); |
72 |
0
|
for (File file : updatedFiles) { |
73 |
0
|
System.out.println(file.getAbsolutePath()); |
74 |
|
} |
75 |
0
|
System.out.println(); |
76 |
0
|
System.out.println("Multi-copyright files"); |
77 |
0
|
System.out.println("---------------------"); |
78 |
0
|
for (File file : nonUpdatedFiles) { |
79 |
0
|
System.out.println(file.getAbsolutePath()); |
80 |
|
} |
81 |
|
} catch (Throwable t) { |
82 |
1
|
t.printStackTrace(); |
83 |
|
} |
84 |
|
} |
85 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
86 |
0
|
protected void exec(String[] args) {... |
87 |
0
|
testDuplicateCopyright(); |
88 |
|
} |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
90 |
0
|
protected Set<String> getValues(Properties properties) {... |
91 |
0
|
Set<String> values = new HashSet<String>(); |
92 |
0
|
Set<String> keys = properties.stringPropertyNames(); |
93 |
0
|
for (String key : keys) { |
94 |
0
|
String value = properties.getProperty(key); |
95 |
0
|
if (values.contains(value)) { |
96 |
0
|
throw new RuntimeException("key " + key + " is a duplicate"); |
97 |
|
} |
98 |
0
|
values.add(value); |
99 |
|
} |
100 |
0
|
return values; |
101 |
|
} |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
103 |
0
|
protected Properties getProperties(String location) {... |
104 |
0
|
ResourceLoader loader = new DefaultResourceLoader(); |
105 |
0
|
Resource resource = loader.getResource(location); |
106 |
0
|
Properties properties = new Properties(); |
107 |
0
|
InputStream in = null; |
108 |
0
|
try { |
109 |
0
|
in = resource.getInputStream(); |
110 |
0
|
properties.load(in); |
111 |
0
|
return properties; |
112 |
|
} catch (IOException e) { |
113 |
0
|
throw new RuntimeException(e); |
114 |
|
} finally { |
115 |
0
|
IOUtils.closeQuietly(in); |
116 |
|
} |
117 |
|
} |
118 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
119 |
0
|
public static final void write(File file, String content) {... |
120 |
0
|
OutputStream out = null; |
121 |
0
|
try { |
122 |
0
|
out = FileUtils.openOutputStream(file); |
123 |
0
|
IOUtils.write(content.getBytes(), out); |
124 |
|
} catch (IOException e) { |
125 |
0
|
throw new RuntimeException(e); |
126 |
|
} finally { |
127 |
0
|
IOUtils.closeQuietly(out); |
128 |
|
} |
129 |
|
} |
130 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
131 |
0
|
public static final String read(File file) {... |
132 |
0
|
InputStream in = null; |
133 |
0
|
try { |
134 |
0
|
in = new FileInputStream(file); |
135 |
0
|
return IOUtils.toString(in); |
136 |
|
} catch (IOException e) { |
137 |
0
|
throw new RuntimeException(e); |
138 |
|
} finally { |
139 |
0
|
IOUtils.closeQuietly(in); |
140 |
|
} |
141 |
|
} |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
143 |
0
|
public static final String unflatten(String s) {... |
144 |
0
|
s = s.replace(ECL_CR, CR); |
145 |
0
|
s = s.replace(ECL_LF, LF); |
146 |
0
|
return s; |
147 |
|
} |
148 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
149 |
0
|
public static final String flatten(String s) {... |
150 |
0
|
s = s.replace(CR, ECL_CR); |
151 |
0
|
s = s.replace(LF, ECL_LF); |
152 |
0
|
return s; |
153 |
|
} |
154 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
155 |
0
|
protected void copy(File file) {... |
156 |
0
|
String content = read(file); |
157 |
0
|
String flat = flatten(content); |
158 |
0
|
String filename = file.getName(); |
159 |
0
|
File flatFile = new File(tmpdir + "/" + filename); |
160 |
0
|
write(flatFile, flat); |
161 |
|
} |
162 |
|
|
163 |
|
} |