Java tutorial
/* ReviewResults.java: Stores results of a Git-VCR run Copyright 2015 Bob W. Hogg. All Rights Reserved. This file is part of Git-VCR. Git-VCR is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Git-VCR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Git-VCR. If not, see <http://www.gnu.org/licenses/>. */ package com.github.rwhogg.git_vcr.review; import com.github.rwhogg.git_vcr.*; import java.io.*; import java.nio.file.*; import java.util.*; import org.apache.commons.configuration.*; import org.apache.commons.io.*; import org.apache.commons.lang3.tuple.*; import org.eclipse.jgit.patch.*; /** * ReviewResults allow you to do a code review * FIXME: this class has a bad name, since it does reviewing itself! */ public class ReviewResults { @SuppressWarnings("rawtypes") private Map<String, Map<Class, List<String>>> results; private HierarchicalINIConfiguration config; private Patch patch; private String patchName; private boolean isNew; /** * Constructor * @param patch the patch itself * @param configuration the configuration * @param isNew whether to review the old or new files */ public ReviewResults(String patchName, Patch patch, HierarchicalINIConfiguration configuration, boolean isNew) { this.patchName = patchName; this.patch = patch; this.results = new HashMap<>(); this.config = configuration; this.isNew = isNew; } /** * getChangesFor returns the result of reviewing a given file * @param fileName the name of the file to get changes for * @return a mapping of review tools to the changes they found */ @SuppressWarnings("rawtypes") public Map<Class, List<String>> getResultsFor(String fileName) { return results.get(fileName); } /** * Retrieve the tests that will be run according to the configuration * @return a Map of MIMEtypes to a list of review tools * @throws ParseException if there is an invalid MIMEtype * @throws ClassNotFoundException if a review tool class is not found * @throws IllegalAccessException if we could not access the review tool class * @throws InstantiationException if we could not instantiate a review tool class */ private Map<String, List<ReviewTool>> getTests() throws InstantiationException, IllegalAccessException, ClassNotFoundException { Map<String, List<ReviewTool>> tests = new HashMap<>(); // get the types of files this configuration can handle Configuration typesSection = config.getSection("MIMEtypes"); Iterator<String> typesHandled = typesSection.getKeys(); while (typesHandled.hasNext()) { String mimeType = typesHandled.next(); String[] toolNames = typesSection.getStringArray(mimeType); List<ReviewTool> tools = new LinkedList<>(); for (String tool : toolNames) { tool = tool.trim(); // construct the class ReviewTool reviewTool = (ReviewTool) Class.forName(tool).newInstance(); reviewTool.setConfiguration(config.getSection(tool)); tools.add(reviewTool); } tests.put(mimeType, tools); } return tests; } /** * review runs the code review on each file * @throws ClassNotFoundException If we cannot find a review tool class * @throws ReviewFailedException If code review failed for some reason * @throws IllegalAccessException If we cannot access a review tool class * @throws InstantiationException if we cannot instantiate a review tool class */ @SuppressWarnings("rawtypes") public void review() throws InstantiationException, IllegalAccessException, ClassNotFoundException, ReviewFailedException { Map<String, List<ReviewTool>> tests = getTests(); results = new HashMap<>(); for (ImmutablePair<String, String> changePair : Util.getFilesChanged(patch)) { String oldFileName = changePair.left; String newFileName = changePair.right; // FIXME: what to do if one or more is null? // FIXME: not sure what to do about this case. For now, just assert. assert FilenameUtils.getExtension(oldFileName).equals(FilenameUtils.getExtension(newFileName)); File file = new File(isNew ? newFileName : oldFileName); String mimeType = null; try { mimeType = Files.probeContentType(file.toPath()); } catch (IOException e) { // FIXME Auto-generated catch block e.printStackTrace(); } // run the tests List<ReviewTool> tools = tests.get(mimeType); Map<Class, List<String>> resultsForThisFile = new HashMap<>(); if (tools != null) { for (ReviewTool tool : tools) { List<String> resultsFromTool; resultsFromTool = tool.getResults(file); resultsForThisFile.put(tool.getClass(), resultsFromTool); } } results.put(oldFileName, resultsForThisFile); } } /** * getPatch returns the name of the patch file used. * @return the patch file used to generate this review */ public Patch getPatch() { return patch; } /** * getPatchName returns the name of the patch file used. * @return the name of the patch file used to generate this review */ public String getPatchName() { return patchName; } }