001 package org.springframework.beans;
002
003 import java.io.File;
004 import java.io.FileInputStream;
005 import java.io.IOException;
006 import java.io.InputStream;
007 import java.io.OutputStream;
008 import java.util.ArrayList;
009 import java.util.HashSet;
010 import java.util.Iterator;
011 import java.util.List;
012 import java.util.Properties;
013 import java.util.Set;
014
015 import org.apache.commons.io.FileUtils;
016 import org.apache.commons.io.IOUtils;
017 import org.junit.Test;
018 import org.springframework.core.io.DefaultResourceLoader;
019 import org.springframework.core.io.Resource;
020 import org.springframework.core.io.ResourceLoader;
021
022 public class CopyrightHandlerTest {
023 String tmpdir = System.getProperty("java.io.tmpdir");
024
025 public static final String ECL_CR = "${ecl.cr}";
026 public static final String ECL_LF = "${ecl.lf}";
027 public static final String CR = "\r";
028 public static final String LF = "\n";
029
030 public static void main(String[] args) {
031 new CopyrightHandlerTest().exec(args);
032 }
033
034 protected String getBaseDir() {
035 String basedir = System.getProperty("ecl.basedir");
036 if (basedir == null) {
037 throw new RuntimeException("Set the system property 'ecl.basedir'");
038 } else {
039 return basedir;
040 }
041
042 }
043
044 @Test
045 public void testDuplicateCopyright() {
046 try {
047 String basedir = getBaseDir();
048 System.out.println("Examining " + basedir);
049 System.out.println("Tmp Dir: " + tmpdir);
050 ProblemFileContext context = new MultipleCopyrightContext(basedir);
051 ProblemFileDetector detector = new ProblemFileDetector();
052 List<File> files = detector.getProblemFiles(context);
053 System.out.println("Located " + files.size() + " unknown files with multiple 'Copyright' lines");
054 Properties invalidEcl = getProperties("invalid-ecl.properties");
055 Set<String> contentsToRemove = getValues(invalidEcl);
056 ContentRemover remover = new ContentRemover();
057 Iterator<File> itr = files.iterator();
058 List<File> updatedFiles = new ArrayList<File>();
059 List<File> nonUpdatedFiles = new ArrayList<File>();
060 while (itr.hasNext()) {
061 File file = itr.next();
062 boolean updated = remover.removeContent(file, contentsToRemove);
063 if (!updated) {
064 copy(file);
065 nonUpdatedFiles.add(file);
066 } else {
067 updatedFiles.add(file);
068 }
069 }
070 System.out.println("Updated files");
071 System.out.println("---------------------");
072 for (File file : updatedFiles) {
073 System.out.println(file.getAbsolutePath());
074 }
075 System.out.println();
076 System.out.println("Multi-copyright files");
077 System.out.println("---------------------");
078 for (File file : nonUpdatedFiles) {
079 System.out.println(file.getAbsolutePath());
080 }
081 } catch (Throwable t) {
082 t.printStackTrace();
083 }
084 }
085
086 protected void exec(String[] args) {
087 testDuplicateCopyright();
088 }
089
090 protected Set<String> getValues(Properties properties) {
091 Set<String> values = new HashSet<String>();
092 Set<String> keys = properties.stringPropertyNames();
093 for (String key : keys) {
094 String value = properties.getProperty(key);
095 if (values.contains(value)) {
096 throw new RuntimeException("key " + key + " is a duplicate");
097 }
098 values.add(value);
099 }
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 }