Clover Coverage Report - Maven Impex Plugin 1.0.24-SNAPSHOT
Coverage timestamp: Thu Oct 27 2011 00:36:24 EDT
../../../../img/srcFileCovDistChart0.png 10% of files have more coverage
71   215   24   3.55
6   148   0.34   20
20     1.2  
1    
 
  IdentifyInvalidDataFiles       Line # 32 71 0% 24 97 0% 0.0
 
No Tests
 
1    package org.apache.torque.mojo;
2   
3    import java.io.File;
4    import java.io.IOException;
5    import java.io.OutputStream;
6    import java.util.List;
7    import java.util.Properties;
8    import java.util.Set;
9    import java.util.TreeSet;
10   
11    import org.apache.commons.io.FileUtils;
12    import org.apache.commons.io.IOUtils;
13    import org.apache.maven.plugin.MojoExecutionException;
14    import org.apache.maven.plugin.MojoFailureException;
15    import org.apache.tools.ant.DirectoryScanner;
16    import org.apache.torque.engine.database.model.Database;
17    import org.apache.torque.engine.database.model.Table;
18    import org.kuali.core.db.torque.SetUtils;
19    import org.kuali.core.db.torque.Utils;
20   
21    /**
22    * This mojo identifies data files that are present on the file system but are not present in schema.xml. This can
23    * happen if a table is removed from the schema.
24    *
25    * It sets a project property called "impex.data.invalid". This property is a comma delimited list of filenames that
26    * have no match in the db schema.
27    *
28    * If it finds any invalid files it will also set the project property "impex.found.invalid=true"
29    *
30    * @goal id-invalid-data-files
31    */
 
32    public class IdentifyInvalidDataFiles extends BaseMojo {
33    private static final String FS = System.getProperty("file.separator");
34   
35    /**
36    * @parameter expression="${extension}" default-value=".xml"
37    * @required
38    */
39    private String extension;
40   
41    /**
42    * @parameter expression="${dataDir}" default-value="${project.basedir}/src/main/impex"
43    * @required
44    */
45    private File dataDir;
46   
47    /**
48    * @parameter expression="${dataDirIncludes}" default-value="*.xml"
49    */
50    private String dataDirIncludes;
51   
52    /**
53    * @parameter expression="${dataDirExcludes}" default-value="schema.xml"
54    */
55    private String dataDirExcludes;
56   
57    /**
58    * @parameter expression="${schemaXMLFile}" default-value="src/main/impex/schema.xml"
59    */
60    private String schemaXMLFile;
61   
62    /**
63    * @parameter expression="${targetDatabase}" default-value="oracle"
64    */
65    private String targetDatabase;
66   
67    /**
68    * Any invalid files are listed in this file. One file per line
69    *
70    * @parameter expression="${impex.markedForRemoval}" default-value="${project.build.directory}/impex/invalid.txt"
71    */
72    private File markedForRemoval;
73   
 
74  0 toggle @Override
75    protected void executeMojo() throws MojoExecutionException, MojoFailureException {
76  0 Utils utils = new Utils();
77  0 try {
78  0 getLog().info("Examining " + dataDir.getAbsolutePath());
79  0 Database db = utils.getDatabase(schemaXMLFile, targetDatabase);
80  0 DirectoryScanner ds = getDirectoryScanner();
81  0 Set<File> existing = getExistingFiles(ds);
82  0 Set<File> allowed = getDatabaseFiles(db);
83  0 Set<File> invalid = SetUtils.difference(existing, allowed);
84  0 getLog().info(existing.size() + " data files currently exist");
85  0 getLog().info(invalid.size() + " of those are invalid");
86  0 StringBuilder sb = new StringBuilder();
87  0 int count = 0;
88  0 StringBuilder invalidFiles = new StringBuilder();
89  0 for (File file : invalid) {
90  0 if (count != 0) {
91  0 sb.append(",");
92    }
93  0 sb.append("**/src/main/impex/" + file.getName());
94  0 invalidFiles.append(file.getCanonicalPath() + "\n");
95  0 getLog().info("Marked for removal: " + file.getName());
96  0 count++;
97    }
98  0 Properties properties = getProject().getProperties();
99  0 properties.setProperty("impex.data.invalid", sb.toString());
100  0 if (count > 0) {
101  0 properties.setProperty("impex.found.invalid", Boolean.TRUE.toString());
102  0 createFile(markedForRemoval, invalidFiles.toString());
103    }
104    } catch (Exception e) {
105  0 throw new MojoExecutionException("Error executing mojo", e);
106    }
107    }
108   
 
109  0 toggle protected void createFile(File file, String contents) throws IOException {
110  0 OutputStream out = null;
111  0 try {
112  0 out = FileUtils.openOutputStream(file);
113  0 IOUtils.write(contents, out);
114    } finally {
115  0 IOUtils.closeQuietly(out);
116    }
117    }
118   
 
119  0 toggle protected Set<File> getDatabaseFiles(Database db) {
120  0 List<?> tables = db.getTables();
121  0 Set<File> files = new TreeSet<File>();
122  0 for (Object object : tables) {
123  0 Table table = (Table) object;
124  0 String tableName = table.getName();
125  0 String filename = dataDir.getAbsolutePath() + FS + tableName + extension;
126  0 File file = new File(filename);
127  0 files.add(file);
128    }
129  0 return files;
130    }
131   
 
132  0 toggle protected Set<File> getExistingFiles(DirectoryScanner ds) {
133  0 ds.scan();
134  0 String[] relativeFilenames = ds.getIncludedFiles();
135  0 Set<File> files = new TreeSet<File>();
136  0 for (int i = 0; i < relativeFilenames.length; i++) {
137  0 String filename = ds.getBasedir().getAbsolutePath() + FS + relativeFilenames[i];
138  0 File file = new File(filename);
139  0 files.add(file);
140    }
141  0 return files;
142    }
143   
 
144  0 toggle protected DirectoryScanner getDirectoryScanner() {
145  0 DirectoryScanner ds = new DirectoryScanner();
146  0 ds.setBasedir(dataDir);
147  0 ds.setIncludes(new String[] { dataDirIncludes });
148  0 ds.setExcludes(new String[] { dataDirExcludes });
149  0 return ds;
150    }
151   
 
152  0 toggle protected File getFile(Table table) {
153  0 String tableName = table.getName();
154  0 String filename = dataDir.getAbsolutePath() + FS + tableName + extension;
155  0 File file = new File(filename);
156  0 return file;
157    }
158   
 
159  0 toggle public File getDataDir() {
160  0 return dataDir;
161    }
162   
 
163  0 toggle public void setDataDir(File dataDir) {
164  0 this.dataDir = dataDir;
165    }
166   
 
167  0 toggle public String getDataDirIncludes() {
168  0 return dataDirIncludes;
169    }
170   
 
171  0 toggle public void setDataDirIncludes(String dataDirIncludes) {
172  0 this.dataDirIncludes = dataDirIncludes;
173    }
174   
 
175  0 toggle public String getDataDirExcludes() {
176  0 return dataDirExcludes;
177    }
178   
 
179  0 toggle public void setDataDirExcludes(String dataDirExcludes) {
180  0 this.dataDirExcludes = dataDirExcludes;
181    }
182   
 
183  0 toggle public String getSchemaXMLFile() {
184  0 return schemaXMLFile;
185    }
186   
 
187  0 toggle public void setSchemaXMLFile(String schemaXMLFile) {
188  0 this.schemaXMLFile = schemaXMLFile;
189    }
190   
 
191  0 toggle public String getExtension() {
192  0 return extension;
193    }
194   
 
195  0 toggle public void setExtension(String extension) {
196  0 this.extension = extension;
197    }
198   
 
199  0 toggle public String getTargetDatabase() {
200  0 return targetDatabase;
201    }
202   
 
203  0 toggle public void setTargetDatabase(String targetDatabase) {
204  0 this.targetDatabase = targetDatabase;
205    }
206   
 
207  0 toggle public File getMarkedForRemoval() {
208  0 return markedForRemoval;
209    }
210   
 
211  0 toggle public void setMarkedForRemoval(File markedForRemoval) {
212  0 this.markedForRemoval = markedForRemoval;
213    }
214   
215    }