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