This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...a/uk/gov/justice/maven/generator/io/files/parser/generator/FileParserInstanceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator; | ||
|
||
import static java.lang.Class.forName; | ||
import static java.lang.String.format; | ||
import static java.util.Arrays.asList; | ||
|
||
import uk.gov.justice.maven.generator.io.files.parser.FileParser; | ||
import uk.gov.justice.maven.generator.io.files.parser.FileParserFactory; | ||
|
||
public class FileParserInstanceFactory { | ||
|
||
public FileParser newInstanceOf(final String parserName) { | ||
|
||
try { | ||
final Object instance = forName(parserName).newInstance(); | ||
|
||
if (isFileParserFactory(instance)) { | ||
final FileParserFactory fileParserFactory = (FileParserFactory) instance; | ||
return fileParserFactory.create(); | ||
} | ||
|
||
return (FileParser) instance; | ||
|
||
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) { | ||
throw new FileParserInstantiationException(format("Failed to instantiate file parser: %s", parserName), ex); | ||
} | ||
} | ||
|
||
private boolean isFileParserFactory(final Object instance) { | ||
return asList(instance.getClass().getInterfaces()).contains(FileParserFactory.class); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...v/justice/maven/generator/io/files/parser/generator/FileParserInstantiationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator; | ||
|
||
public class FileParserInstantiationException extends RuntimeException { | ||
|
||
public FileParserInstantiationException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../gov/justice/maven/generator/io/files/parser/generator/FileParserInstanceFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator; | ||
|
||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import uk.gov.justice.maven.generator.io.files.parser.FileParser; | ||
import uk.gov.justice.maven.generator.io.files.parser.generator.parser.TestFileParser; | ||
import uk.gov.justice.maven.generator.io.files.parser.generator.parser.TestFileParserFactory; | ||
|
||
import org.junit.Test; | ||
|
||
public class FileParserInstanceFactoryTest { | ||
|
||
@Test | ||
public void shouldCreateInstanceOfFileParserFromGivenFileParserName() throws Exception { | ||
|
||
final String parserName = TestFileParser.class.getName(); | ||
|
||
final FileParser fileParser = new FileParserInstanceFactory().newInstanceOf(parserName); | ||
|
||
assertThat(fileParser, is(instanceOf(TestFileParser.class))); | ||
} | ||
|
||
@Test | ||
public void shouldCreateInstanceOfFileParserFromGivenParserFactoryName() throws Exception { | ||
|
||
final String parserFactoryName = TestFileParserFactory.class.getName(); | ||
|
||
final FileParser fileParser = new FileParserInstanceFactory().newInstanceOf(parserFactoryName); | ||
|
||
assertThat(fileParser, is(instanceOf(TestFileParser.class))); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionIfUnableToInstantiateFileParser() throws Exception { | ||
|
||
final String parserName = "unknown"; | ||
|
||
try { | ||
new FileParserInstanceFactory().newInstanceOf(parserName); | ||
fail(); | ||
} catch (final FileParserInstantiationException ex) { | ||
assertThat(ex.getMessage(), is("Failed to instantiate file parser: unknown")); | ||
assertThat(ex.getCause(), is(instanceOf(ClassNotFoundException.class))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
.../java/uk/gov/justice/maven/generator/io/files/parser/generator/parser/TestFileParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator.parser; | ||
|
||
import uk.gov.justice.maven.generator.io.files.parser.FileParser; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
|
||
public class TestFileParser implements FileParser<String> { | ||
|
||
@Override | ||
public Collection<String> parse(final Path baseDir, final Collection<Path> paths) { | ||
return null; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...k/gov/justice/maven/generator/io/files/parser/generator/parser/TestFileParserFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser.generator.parser; | ||
|
||
import uk.gov.justice.maven.generator.io.files.parser.FileParser; | ||
import uk.gov.justice.maven.generator.io.files.parser.FileParserFactory; | ||
|
||
public class TestFileParserFactory implements FileParserFactory<String> { | ||
|
||
public FileParser<String> create() { | ||
return new TestFileParser(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ommon/src/main/java/uk/gov/justice/maven/generator/io/files/parser/FileParserFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser; | ||
|
||
public interface FileParserFactory<T> { | ||
|
||
FileParser<T> create(); | ||
} |
11 changes: 11 additions & 0 deletions
11
...main/java/uk/gov/justice/maven/generator/io/files/parser/JsonSchemaFileParserFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package uk.gov.justice.maven.generator.io.files.parser; | ||
|
||
import javax.json.JsonObject; | ||
|
||
public class JsonSchemaFileParserFactory implements FileParserFactory<JsonObject> { | ||
|
||
@Override | ||
public FileParser<JsonObject> create() { | ||
return new JsonSchemaFileParser(); | ||
} | ||
} |