Clover Coverage Report - Kuali Spring Utilities 1.1.2-SNAPSHOT
Coverage timestamp: Tue Nov 6 2012 17:56:04 EST
78   163   19   7.09
8   146   0.24   11
11     1.73  
1    
 
  CopyrightHandlerTest       Line # 22 78 0% 19 88 9.3% 0.0927835
 
  (1)
 
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  0 toggle public static void main(String[] args) {
31  0 new CopyrightHandlerTest().exec(args);
32    }
33   
 
34  1 toggle 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   
 
44  1 toggle @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   
 
86  0 toggle protected void exec(String[] args) {
87  0 testDuplicateCopyright();
88    }
89   
 
90  0 toggle 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   
 
103  0 toggle 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   
 
119  0 toggle 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   
 
131  0 toggle 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   
 
143  0 toggle 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   
 
149  0 toggle 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   
 
155  0 toggle 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    }