com.github.rwhogg.git_vcr.VelocityReport.java Source code

Java tutorial

Introduction

Here is the source code for com.github.rwhogg.git_vcr.VelocityReport.java

Source

/*
VelocityReport.java: A Velocity report of Git-VCR results
       
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;

import com.github.rwhogg.git_vcr.review.*;
import com.google.common.collect.*;
import java.io.*;
import java.util.*;
import org.apache.velocity.app.*;
import org.apache.velocity.runtime.*;
import org.apache.commons.lang3.tuple.*;
import org.apache.velocity.*;
import org.apache.velocity.runtime.resource.loader.*;
import org.eclipse.jgit.patch.*;

/**
 * VelocityReport describes Git-VCR results with an Apache Velocity-generated HTML page.
 */
public class VelocityReport {
    private Template template;
    private ReviewResults oldResults;
    private ReviewResults newResults;
    private Patch patch;

    /**
     * Constructor
     * @param patch the patch used to make this report
     * @param oldResults Results of the first review
     * @param newResults Results of the second review
     */
    public VelocityReport(Patch patch, ReviewResults oldResults, ReviewResults newResults) {
        this.patch = patch;
        this.oldResults = oldResults;
        this.newResults = newResults;

        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        Velocity.init();
        template = Velocity.getTemplate("templates/main.vm");
    }

    /**
     * makeVelocityContext generates a Velocity context from the given review results
     * @return the created context
     */
    private VelocityContext makeContext() {
        VelocityContext context = new VelocityContext();
        context.put("patch", oldResults.getPatchName());
        context.put("filesChanged", Util.getFilesChanged(patch));
        context.put("changeMap", extractChanges());
        return context;
    }

    /**
     * Override toString() to output the report itself
     * @return this report in rendered form
     */
    @Override
    public String toString() {
        StringWriter writer = new StringWriter();
        VelocityContext context = makeContext();
        template.merge(context, writer);
        return writer.toString();
    }

    /**
     * extract the changes between two reviews
     * @return A map of (old) files to changes in results
     */
    @SuppressWarnings("rawtypes")
    private Map<String, Changes> extractChanges() {
        Map<String, Changes> changeMap = new HashMap<>();

        List<ImmutablePair<String, String>> changedFiles = Util.getFilesChanged(patch);
        // FIXME: for now, we'll just assume no files were added or deleted
        for (ImmutablePair<String, String> filePair : changedFiles) {
            String oldFileName = filePair.getLeft();
            String newFileName = filePair.getRight();
            Changes changesForThisFile = new Changes();
            Map<Class, List<String>> resultsForOldFile = oldResults.getResultsFor(oldFileName);
            Map<Class, List<String>> resultsForNewFile = newResults.getResultsFor(newFileName);
            Set<Class> toolsUsed = resultsForOldFile.keySet();
            assert toolsUsed.equals(resultsForNewFile.keySet());

            // for each tool, go through and detect changes
            for (Class toolUsed : toolsUsed) {
                List<String> oldResultListFromThisTool = resultsForOldFile.get(toolUsed);
                List<String> newResultListFromThisTool = resultsForNewFile.get(toolUsed);
                Set<String> oldResultsFromThisTool = new HashSet<>(oldResultListFromThisTool);
                Set<String> newResultsFromThisTool = new HashSet<>(newResultListFromThisTool);
                Set<String> additions = Sets.difference(newResultsFromThisTool, oldResultsFromThisTool);
                Set<String> subtractions = Sets.difference(oldResultsFromThisTool, newResultsFromThisTool);

                // construct the change
                List<String> additionList = Arrays.asList(additions.toArray(new String[additions.size()]));
                List<String> subtractionList = Arrays.asList(subtractions.toArray(new String[subtractions.size()]));
                ImmutablePair<List<String>, List<String>> changeListPair = ImmutablePair.of(additionList,
                        subtractionList);
                changesForThisFile.put(toolUsed, changeListPair);
            }

            changeMap.put(oldFileName, changesForThisFile);
        }

        return changeMap;
    }
}