Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Merge branch 'SMA-3'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed May 7, 2013
2 parents 8b600db + dfe3c29 commit f9e3ca6
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final void run(String[] args) {
CommandLine commandLine = null;
try {
commandLine = new PosixParser().parse(OPTIONS, args);
String outputType = commandLine.getOptionValue(OptionsFactory.OPTION_KEY_OUTPUT_TYPE);
String[] outputTypes = commandLine.getOptionValues(OptionsFactory.OPTION_KEY_OUTPUT_TYPE);
String outputPath = commandLine.getOptionValue(OptionsFactory.OPTION_KEY_OUTPUT_PATH);
String[] excludes = commandLine.getOptionValues(OptionsFactory.OPTION_KEY_EXCLUDE);

Expand All @@ -59,10 +59,11 @@ final void run(String[] args) {
try {
ConfigurableApplicationContext applicationContext = getApplicationContext();
applicationContext.addBeanFactoryPostProcessor(new ConfigurationRegisteringBeanFactoryPostProcessor(new Configuration(inputPath,
outputPath, outputType, excludes)));
outputPath, outputTypes, excludes)));
applicationContext.refresh();
applicationContext.getBean(MigrationAnalysisExecutor.class).execute();
} catch (RuntimeException re) {
this.logger.debug("Analysis failed", re);
this.logger.error("A failure occurred. Please see earlier output for details.");
exit(-1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,25 @@ public void execute() {
private void analyzeArchive(File archive, File inputFile) {
FileSystem fileSystem = createFileSystem(archive);
AnalysisResult analysis = this.analysisEngine.analyze(fileSystem, this.configuration.getExcludes(), archive.getName());
RenderEngine renderEngine = getRenderEngine();
renderEngine.render(analysis, getOutputPath(inputFile, archive));

for (String outputType : this.configuration.getOutputTypes()) {
RenderEngine renderEngine = getRenderEngine(outputType);
renderEngine.render(analysis, getOutputPath(inputFile, archive, outputType));
}
fileSystem.cleanup();
}

private RenderEngine getRenderEngine() {
private RenderEngine getRenderEngine(String outputType) {
for (RenderEngine renderEngine : this.renderEngines) {
if (renderEngine.canRender(this.configuration.getOutputType())) {
if (renderEngine.canRender(outputType)) {
return renderEngine;
}
}

throw new IllegalArgumentException(String.format("No rendering engine for report type '%s' is available", this.configuration.getOutputType()));
throw new IllegalArgumentException(String.format("No rendering engine for report type '%s' is available", outputType));
}

private String getOutputPath(File inputFile, File archive) {
private String getOutputPath(File inputFile, File archive, String outputType) {
File root;

if (inputFile.equals(archive)) {
Expand All @@ -100,7 +103,7 @@ private String getOutputPath(File inputFile, File archive) {
}

String directoryName = archive.getName() + ".migration-analysis";
return new File(new File(root, directoryName), this.configuration.getOutputType()).getAbsolutePath();
return new File(new File(root, directoryName), outputType).getAbsolutePath();
}

private FileSystem createFileSystem(File archive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ final class Configuration {

private static final String DEFAULT_OUTPUT_PATH = ".";

private static final String DEFAULT_OUTPUT_TYPE = "html";
private static final String[] DEFAULT_OUTPUT_TYPES = new String[] { "html" };

private final String inputPath;

private final String outputPath;

private final String outputType;
private final String[] outputTypes;

private final String[] excludes;

Configuration(String inputPath, String outputPath, String outputType, String[] excludes) {
Configuration(String inputPath, String outputPath, String[] outputTypes, String[] excludes) {
this.inputPath = inputPath;
this.outputPath = outputPath == null ? DEFAULT_OUTPUT_PATH : outputPath;
this.outputType = outputType == null ? DEFAULT_OUTPUT_TYPE : outputType;
this.outputTypes = outputTypes == null ? DEFAULT_OUTPUT_TYPES : outputTypes;
this.excludes = excludes == null ? DEFAULT_EXCLUDES : excludes;
}

Expand All @@ -56,10 +56,10 @@ public String getOutputPath() {
}

/**
* @return The type of report to produce
* @return The types of report to produce
*/
public String getOutputType() {
return this.outputType;
public String[] getOutputTypes() {
return this.outputTypes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Options create() {
Options options = new Options();

options.addOption(OptionBuilder //
.withDescription("The type of the output to be created. Defaults to html.") //
.withDescription("The type of the output to be created. Defaults to html. Can be specified multiple times.") //
.hasArg() //
.withArgName("outputType") //
.create(OPTION_KEY_OUTPUT_TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public final class CommandLineMigrationAnalysisExecutorTests {

private static final String OUTPUT_PATH = "output";

private static final String REPORT_TYPE = "html";
private static final String REPORT_TYPE_HTML = "html";

private static final String REPORT_TYPE_XML = "xml";

private final AnalysisEngine analysisEngine = mock(AnalysisEngine.class);

Expand All @@ -62,14 +64,15 @@ public void execute() throws IOException {

File archive = new File(INPUT_PATH_ARCHIVE_JAR);

configureBehaviour(archive, archive);
configureBehaviour(archive, REPORT_TYPE_HTML, archive);

CommandLineMigrationAnalysisExecutor executor = new CommandLineMigrationAnalysisExecutor(this.analysisEngine, this.renderEngines,
this.fileSystemFactory, this.archiveDiscoverer, new Configuration(INPUT_PATH_ARCHIVE_JAR, OUTPUT_PATH, REPORT_TYPE, new String[0]));
this.fileSystemFactory, this.archiveDiscoverer, new Configuration(INPUT_PATH_ARCHIVE_JAR, OUTPUT_PATH, new String[] { REPORT_TYPE_HTML },
new String[0]));

executor.execute();

verifyBehaviour(outputLocation, archive, archive);
verifyBehaviour(outputLocation, archive, REPORT_TYPE_HTML, archive);
}

@Test
Expand All @@ -80,29 +83,55 @@ public void executeWithMultipleArchives() throws IOException {
File archive1 = new File(inputLocation, "alpha.ear");
File archive2 = new File(new File(inputLocation, "bravo"), "charlie.war");

configureBehaviour(inputLocation, archive1, archive2);
configureBehaviour(inputLocation, REPORT_TYPE_HTML, archive1, archive2);

CommandLineMigrationAnalysisExecutor executor = new CommandLineMigrationAnalysisExecutor(this.analysisEngine, this.renderEngines,
this.fileSystemFactory, this.archiveDiscoverer, new Configuration(inputLocation.getPath(), OUTPUT_PATH, REPORT_TYPE, new String[0]));
this.fileSystemFactory, this.archiveDiscoverer, new Configuration(inputLocation.getPath(), OUTPUT_PATH,
new String[] { REPORT_TYPE_HTML }, new String[0]));

executor.execute();

verifyBehaviour(outputLocation, inputLocation, archive1, archive2);
verifyBehaviour(outputLocation, inputLocation, REPORT_TYPE_HTML, archive1, archive2);
}

@Test
public void executeWithMultipleOutputTypes() throws IOException {
File outputLocation = new File(OUTPUT_PATH);
File inputLocation = new File("src/test/resources/archives");

File archive = new File(inputLocation, "alpha.ear");

configureBehaviour(inputLocation, new String[] { REPORT_TYPE_HTML, REPORT_TYPE_XML }, archive);

CommandLineMigrationAnalysisExecutor executor = new CommandLineMigrationAnalysisExecutor(this.analysisEngine, this.renderEngines,
this.fileSystemFactory, this.archiveDiscoverer, new Configuration(inputLocation.getPath(), OUTPUT_PATH, new String[] { REPORT_TYPE_HTML,
REPORT_TYPE_XML }, new String[0]));

executor.execute();

verifyBehaviour(outputLocation, inputLocation, new String[] { REPORT_TYPE_HTML, REPORT_TYPE_XML }, archive);
}

@Test
public void executeWithNonExistentInputPath() {
CommandLineMigrationAnalysisExecutor executor = new CommandLineMigrationAnalysisExecutor(this.analysisEngine, this.renderEngines,
this.fileSystemFactory, this.archiveDiscoverer, new Configuration("does/not/exist", REPORT_TYPE, OUTPUT_PATH, new String[0]));
this.fileSystemFactory, this.archiveDiscoverer, new Configuration("does/not/exist", OUTPUT_PATH, new String[] { REPORT_TYPE_HTML },
new String[0]));
executor.execute();

verifyNoMoreInteractions(this.analysisEngine, this.fileSystemFactory, this.archiveDiscoverer);
}

private void configureBehaviour(File inputLocation, File... archives) throws IOException {
private void configureBehaviour(File inputLocation, String outputType, File... archives) throws IOException {
this.configureBehaviour(inputLocation, new String[] { outputType }, archives);
}

private void configureBehaviour(File inputLocation, String[] outputTypes, File... archives) throws IOException {

when(this.archiveDiscoverer.discover(inputLocation)).thenReturn(Arrays.asList(archives));
when(this.renderEngine.canRender("html")).thenReturn(true);
for (String outputType : outputTypes) {
when(this.renderEngine.canRender(outputType)).thenReturn(true);
}

for (File archive : archives) {
when(this.fileSystemFactory.createFileSystem(archive)).thenReturn(this.fileSystem);
Expand All @@ -111,22 +140,27 @@ private void configureBehaviour(File inputLocation, File... archives) throws IOE

}

private void verifyBehaviour(File outputLocation, File inputLocation, File... archives) {
private void verifyBehaviour(File outputLocation, File inputLocation, String outputType, File... archives) {
this.verifyBehaviour(outputLocation, inputLocation, new String[] { outputType }, archives);
}

private void verifyBehaviour(File outputLocation, File inputLocation, String[] outputTypes, File... archives) {
for (File archive : archives) {
verify(this.analysisEngine).analyze(this.fileSystem, new String[0], archive.getName());
verify(this.renderEngine).render(this.analysisResult, getOutputPath(inputLocation, outputLocation, archive));
for (String outputType : outputTypes) {
verify(this.renderEngine).render(this.analysisResult, getOutputPath(inputLocation, outputLocation, archive, outputType));
}
}
}

private String getOutputPath(File inputLocation, File outputLocation, File archive) {
private String getOutputPath(File inputLocation, File outputLocation, File archive, String outputType) {
String outputPath;
if (inputLocation.equals(archive)) {
outputPath = new File(new File(outputLocation, archive.getName() + ".migration-analysis"), "html").getAbsolutePath();
outputPath = new File(new File(outputLocation, archive.getName() + ".migration-analysis"), outputType).getAbsolutePath();

} else {
outputPath = new File(new File(new File(outputLocation, archive.getParentFile().getPath().substring(inputLocation.getPath().length())),
archive.getName() + ".migration-analysis"), "html").getAbsolutePath();
archive.getName() + ".migration-analysis"), outputType).getAbsolutePath();
}
return outputPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@ public final class ConfigurationTests {

@Test
public void fullySpecifiedConfiguration() {
assertConfiguration(new Configuration("input/path", "output/path", "output-type", new String[] { "excluded", "items" }), "input/path",
"output/path", "output-type", new String[] { "excluded", "items" });
assertConfiguration(new Configuration("input/path", "output/path", new String[] { "output-type" }, new String[] { "excluded", "items" }),
"input/path", "output/path", new String[] { "output-type" }, new String[] { "excluded", "items" });
}

@Test
public void outputPathDefaultsToCurrentWorkingDirectory() {
assertConfiguration(new Configuration("input/path", null, "output-type", new String[] { "excluded", "items" }), "input/path", ".",
"output-type", new String[] { "excluded", "items" });
assertConfiguration(new Configuration("input/path", null, new String[] { "output-type" }, new String[] { "excluded", "items" }),
"input/path", ".", new String[] { "output-type" }, new String[] { "excluded", "items" });
}

@Test
public void outputTypeDefaultsToHtml() {
assertConfiguration(new Configuration("input/path", "output/path", null, new String[] { "excluded", "items" }), "input/path", "output/path",
"html", new String[] { "excluded", "items" });
new String[] { "html" }, new String[] { "excluded", "items" });
}

@Test
public void excludesDefaultsToAnEmptyArray() {
assertConfiguration(new Configuration("input/path", "output/path", "output-type", null), "input/path", "output/path", "output-type",
new String[0]);
assertConfiguration(new Configuration("input/path", "output/path", new String[] { "output-type" }, null), "input/path", "output/path",
new String[] { "output-type" }, new String[0]);
}

private void assertConfiguration(Configuration configuration, String expectedInputPath, String expectedOutputPath, String expectedOutputType,
private void assertConfiguration(Configuration configuration, String expectedInputPath, String expectedOutputPath, String[] expectedOutputTypes,
String[] expectedExcludes) {
assertEquals(expectedInputPath, configuration.getInputPath());
assertEquals(expectedOutputPath, configuration.getOutputPath());
assertEquals(expectedOutputType, configuration.getOutputType());
assertArrayEquals(expectedOutputTypes, configuration.getOutputTypes());
assertArrayEquals(expectedExcludes, configuration.getExcludes());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.migrationanalyzer.render;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -39,7 +40,7 @@ public final class ModelAndView {
* @param viewName The view name
*/
public ModelAndView(Map<String, Object> model, String viewName) {
this.model = model;
this.model = new HashMap<String, Object>(model);
this.viewName = viewName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public void renderViewWithModel(String viewName, Map<String, Object> model, Writ
if (view != null) {
this.logger.debug("Rendering view {} from view name {}", view, viewName);
view.render(model, writer);
} else {
this.logger.warn("View {} not found", viewName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package org.springframework.migrationanalyzer.render;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -28,10 +30,20 @@ public class ModelAndViewTests {
@Test
public void test() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("a", "alpha");
model.put("b", "alpha");

String viewName = "viewName";

ModelAndView modelAndView = new ModelAndView(model, viewName);
assertSame(model, modelAndView.getModel());
assertEquals(model, modelAndView.getModel());
assertSame(viewName, modelAndView.getViewName());
}

@Test
public void modelCanBeUpdated() {
ModelAndView modelAndView = new ModelAndView(Collections.<String, Object> emptyMap(), "view-name");
modelAndView.getModel().put("a", "alpha");
assertEquals("alpha", modelAndView.getModel().get("a"));
}
}
Loading

0 comments on commit f9e3ca6

Please sign in to comment.