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