Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Add GeneratorFactory interface to allow factory creation of Generators
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Sep 25, 2017
1 parent 4d033c2 commit b338855
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 80 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## Unreleased
### Added
- GeneratorFactory interface to allow factory creation of Generators

## [1.7.0] - 2017-08-17
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uk.gov.justice.maven.generator.io.files.parser.core;

public interface GeneratorFactory<T> {

Generator<T> create();
}
2 changes: 1 addition & 1 deletion generator-plugin-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
<id>internal-jsons</id>
<configuration>
<generatorName>
uk.gov.justice.maven.generator.io.files.parser.json.test.JsonTitleAppendingGenerator
uk.gov.justice.maven.generator.io.files.parser.json.test.JsonTitleAppendingGeneratorFactory
</generatorName>
<parserName>uk.gov.justice.maven.generator.io.files.parser.JsonSchemaFileParser</parserName>
<basePackageName>uk.gov.justice.api</basePackageName>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.gov.justice.maven.generator.io.files.parser.generator;

import uk.gov.justice.maven.generator.io.files.parser.FileParser;
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScannerFactory;
import uk.gov.justice.maven.generator.io.files.parser.FileParser;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -13,19 +13,20 @@
*/
public class GenerateGoalProcessor {

private final GeneratorFactory generatorFactory;
private final MojoGeneratorFactory mojoGeneratorFactory;
private final FileParser parser;

private final FileTreeScannerFactory scannerFactory;

public GenerateGoalProcessor(final GeneratorFactory generatorFactory,
public GenerateGoalProcessor(final MojoGeneratorFactory mojoGeneratorFactory,
final FileTreeScannerFactory scannerFactory,
final FileParser parser) {
this.generatorFactory = generatorFactory;
this.mojoGeneratorFactory = mojoGeneratorFactory;
this.scannerFactory = scannerFactory;
this.parser = parser;
}

@SuppressWarnings("unchecked")
public void generate(final GenerateGoalConfig config) throws IOException {

final String[] includes = config.getIncludes().toArray(new String[config.getIncludes().size()]);
Expand All @@ -37,6 +38,6 @@ public void generate(final GenerateGoalConfig config) throws IOException {
final Collection<Path> paths = scannerFactory.create().find(config.getSourceDirectory(), includes, excludes);
parser
.parse(config.getSourceDirectory(), paths)
.forEach(file -> generatorFactory.instanceOf(config.getGeneratorName()).run(file, generatorConfig));
.forEach(file -> mojoGeneratorFactory.instanceOf(config.getGeneratorName()).run(file, generatorConfig));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import static java.lang.Class.forName;
import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE_PLUS_RUNTIME;

import uk.gov.justice.maven.generator.io.files.parser.FileParser;
import uk.gov.justice.maven.generator.io.files.parser.common.BasicMojo;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScannerFactory;
import uk.gov.justice.maven.generator.io.files.parser.FileParser;

import java.io.File;
import java.nio.file.Path;
Expand Down Expand Up @@ -57,13 +57,18 @@ public class GenerateMojo extends BasicMojo {
public void execute() throws MojoExecutionException {
if (!skip) {
configureDefaultFileIncludes();

project.addCompileSourceRoot(outputDirectory.getPath());
project.addTestCompileSourceRoot(outputDirectory.getPath());

final List<Path> sourcePaths = new ArrayList<>();
project.getCompileSourceRoots().stream().forEach(root -> sourcePaths.add(Paths.get(root)));

project.getCompileSourceRoots().forEach(root -> sourcePaths.add(Paths.get(root)));

try {
FileUtils.forceMkdir(outputDirectory);
new GenerateGoalProcessor(new GeneratorFactory(),
new GenerateGoalProcessor(
new MojoGeneratorFactory(),
new FileTreeScannerFactory(),
(FileParser) forName(parserName).newInstance())
.generate(configuration(sourcePaths));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package uk.gov.justice.maven.generator.io.files.parser.generator;

import static java.util.Arrays.asList;

import uk.gov.justice.maven.generator.io.files.parser.core.Generator;
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorFactory;

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

/**
* Factory for generators
*/
public class MojoGeneratorFactory {

private final Map<String, Generator> generators = new HashMap<>();

/**
* Returns an instance of a generator.
*
* @param generatorName fully qualified classname that implements {@link Generator}
* @return the generator
*/
public Generator instanceOf(final String generatorName) {

if (!generators.containsKey(generatorName)) {
generators.put(generatorName, createGenerator(generatorName));
}

return generators.get(generatorName);
}

private Generator createGenerator(final String generatorName) {

try {
final Class<?> generatorClass = Class.forName(generatorName);

if (isGeneratorFactory(generatorClass)) {
final GeneratorFactory generatorFactory = (GeneratorFactory) generatorClass.newInstance();

return generatorFactory.create();
}

return (Generator) generatorClass.newInstance();
} catch (final ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new IllegalArgumentException(String.format("Could not instantiate generator %s", generatorName), e);
}
}

private boolean isGeneratorFactory(final Class<?> generatorClass) {
return asList(generatorClass.getInterfaces()).contains(GeneratorFactory.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import uk.gov.justice.maven.generator.io.files.parser.FileParser;
import uk.gov.justice.maven.generator.io.files.parser.RamlFileParser;
import uk.gov.justice.maven.generator.io.files.parser.core.Generator;
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScanner;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScannerFactory;
import uk.gov.justice.maven.generator.io.files.parser.FileParser;
import uk.gov.justice.maven.generator.io.files.parser.RamlFileParser;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -53,7 +53,7 @@ public class GenerateGoalProcessorTest {
@Mock
private Generator generator;
@Mock
private GeneratorFactory generatorFactory;
private MojoGeneratorFactory mojoGeneratorFactory;
@Mock
private FileTreeScannerFactory scannerFactory;
@Mock
Expand All @@ -70,7 +70,7 @@ public class GenerateGoalProcessorTest {
public void setup() {
String generatorName = "mock.generator";

when(generatorFactory.instanceOf(generatorName)).thenReturn(generator);
when(mojoGeneratorFactory.instanceOf(generatorName)).thenReturn(generator);
when(config.getGeneratorName()).thenReturn(generatorName);
when(config.getSourceDirectory()).thenReturn(sourceDirectory.getRoot().toPath());
when(config.getIncludes()).thenReturn(asList(includes));
Expand Down Expand Up @@ -111,6 +111,7 @@ public void shouldCallGeneratorWithRamlFilesAndConfig() throws Exception {
}

@Test
@SuppressWarnings("unchecked")
public void shouldCallGeneratorWithEmptyRamlForEmptyFile() throws Exception {
File ramlFile = sourceDirectory.newFile("file3.raml");

Expand All @@ -129,6 +130,7 @@ public void shouldCallGeneratorWithEmptyRamlForEmptyFile() throws Exception {
}

@Test
@SuppressWarnings("unchecked")
public void shouldPassPatternsToFileTreeScanner() throws Exception {

final String[] customIncludes = {"**/*.txt"};
Expand Down Expand Up @@ -161,7 +163,7 @@ public void shouldNotInstatiateGeneratorIfNoRamlFilesToProcess() throws IOExcept
when(scanner.find(any(Path.class), any(String[].class), any(String[].class))).thenReturn(emptyList());
generateGoalProcessor.generate(config);

verifyZeroInteractions(generatorFactory);
verifyZeroInteractions(mojoGeneratorFactory);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
import uk.gov.justice.maven.generator.io.files.parser.core.Generator;
import uk.gov.justice.maven.generator.io.files.parser.generator.generators.NonInstantiableTestGenerator;
import uk.gov.justice.maven.generator.io.files.parser.generator.generators.TestGenerator;
import uk.gov.justice.maven.generator.io.files.parser.generator.generators.TestGeneratorFactory;

import org.junit.Before;
import org.junit.Test;

/**
* Unit tests for the {@link GeneratorFactory} class.
* Unit tests for the {@link MojoGeneratorFactory} class.
*/
public class GeneratorFactoryTest {
public class MojoGeneratorFactoryTest {

private GeneratorFactory factory;
private MojoGeneratorFactory factory;

@Before
public void setup() {
factory = new GeneratorFactory();
factory = new MojoGeneratorFactory();
}

@Test
Expand All @@ -30,6 +31,12 @@ public void shouldCreateGenerator() {
assertThat(generator, is(instanceOf(TestGenerator.class)));
}

@Test
public void shouldCreateGeneratorFromFactory() {
final Generator generator = factory.instanceOf(TestGeneratorFactory.class.getName());
assertThat(generator, is(instanceOf(TestGenerator.class)));
}

@Test
public void shouldCreateOnlyOneInstanceOfGenerator() {
final Generator generator1 = factory.instanceOf(TestGenerator.class.getName());
Expand All @@ -38,19 +45,27 @@ public void shouldCreateOnlyOneInstanceOfGenerator() {
assertThat(generator1, sameInstance(generator2));
}

@Test
public void shouldCreateOnlyOneInstanceOfGeneratorFromFactory() {
final Generator generator1 = factory.instanceOf(TestGeneratorFactory.class.getName());
final Generator generator2 = factory.instanceOf(TestGeneratorFactory.class.getName());

assertThat(generator1, sameInstance(generator2));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfClassDoesNotExists() {
factory.instanceOf("nonexistent.GeneratorClass");
}

@Test
public void shouldThrowAnIllegalArgumentExceptionIfThegeneratorCannotBeInstantiated() throws Exception {
public void shouldThrowExceptionIfThegeneratorCannotBeInstantiated() throws Exception {

try {
factory.instanceOf(NonInstantiableTestGenerator.class.getName());
} catch (final IllegalArgumentException expected) {
assertThat(expected.getMessage(), is("Could not instantiate generator " + NonInstantiableTestGenerator.class.getName()));
assertThat(expected.getCause(), is(instanceOf(NoSuchMethodException.class)));
assertThat(expected.getCause(), is(instanceOf(IllegalAccessException.class)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.justice.maven.generator.io.files.parser.generator.generators;

import uk.gov.justice.maven.generator.io.files.parser.core.Generator;
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorFactory;

public class TestGeneratorFactory implements GeneratorFactory {

@Override
public Generator create() {
return new TestGenerator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.gov.justice.maven.generator.io.files.parser.json.test;

import static java.lang.String.format;

import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig;

import java.nio.file.Path;
import java.nio.file.Paths;

public class FilePathProvider {

public static final String FILE_NAME = "json-titles.txt";

public Path filePath(final GeneratorConfig generatorConfig) {
return Paths.get(format("%s/%s", generatorConfig.getOutputDirectory().toString(), FILE_NAME));
}
}
Loading

0 comments on commit b338855

Please sign in to comment.