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 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from CJSCommonPlatform/create-source-writer
Add a non duplicating source writer
- Loading branch information
Showing
32 changed files
with
916 additions
and
89 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
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
94 changes: 94 additions & 0 deletions
94
...on-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/SourceWriterIT.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,94 @@ | ||
package uk.gov.justice.generation.pojo.integration.test; | ||
|
||
import static com.jayway.jsonassert.JsonAssert.with; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import uk.gov.justice.generation.bootstrap.GenerationContextFactory; | ||
import uk.gov.justice.generation.pojo.core.GenerationContext; | ||
import uk.gov.justice.generation.pojo.dom.ClassDefinition; | ||
import uk.gov.justice.generation.pojo.dom.ClassName; | ||
import uk.gov.justice.generation.pojo.dom.FieldDefinition; | ||
import uk.gov.justice.generation.pojo.generators.ClassGeneratable; | ||
import uk.gov.justice.generation.pojo.generators.JavaGeneratorFactory; | ||
import uk.gov.justice.generation.pojo.integration.utils.ClassCompiler; | ||
import uk.gov.justice.generation.pojo.write.SourceWriter; | ||
import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Constructor; | ||
import java.math.BigDecimal; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
|
||
public class SourceWriterIT { | ||
|
||
private final SourceWriter sourceWriter = new SourceWriter(); | ||
private final ClassCompiler classCompiler = new ClassCompiler(); | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper(); | ||
|
||
@Test | ||
public void shouldWriteASingleSourceFile() throws Exception { | ||
|
||
final File rootOutputDirectory = new File("target/sources-generated-from-tests"); | ||
rootOutputDirectory.delete(); | ||
final String packageName = "org.bloggs.fred"; | ||
|
||
final ClassDefinition addressDefinition = addressDefinition(packageName); | ||
|
||
|
||
final File sourceOutputDirectory = new File("./target/test-generation"); | ||
final File classesOutputDirectory = new File("./target/test-classes"); | ||
|
||
final GenerationContext generationContext = new GenerationContextFactory().create(sourceOutputDirectory); | ||
|
||
sourceOutputDirectory.delete(); | ||
|
||
final ClassGeneratable addressGenerator = new JavaGeneratorFactory().createClassGeneratorFor(addressDefinition); | ||
sourceWriter.write(addressGenerator, generationContext); | ||
final Class<?> addressClass = classCompiler.compile(addressGenerator, sourceOutputDirectory, classesOutputDirectory); | ||
|
||
assertThat(addressClass.getName(), is(addressDefinition.getClassName().getFullyQualifiedName())); | ||
|
||
final String firstLine = "firstLine"; | ||
final String postCode = "postCode"; | ||
|
||
final Constructor<?> addressConstructor = addressClass.getConstructor(String.class, String.class); | ||
final Object address = addressConstructor.newInstance(firstLine, postCode); | ||
|
||
|
||
final String addressJson = objectMapper.writeValueAsString(address); | ||
|
||
with(addressJson) | ||
.assertThat("$.firstLine", is(firstLine)) | ||
.assertThat("$.postCode", is(postCode)) | ||
; | ||
} | ||
|
||
|
||
private ClassDefinition addressDefinition(final String packageName) { | ||
final ClassDefinition addressDefinition = new ClassDefinition("address", new ClassName(packageName, "Address")); | ||
addressDefinition.addFieldDefinition(new FieldDefinition("firstLine", new ClassName(String.class))); | ||
addressDefinition.addFieldDefinition(new FieldDefinition("postCode", new ClassName(String.class))); | ||
|
||
return addressDefinition; | ||
} | ||
|
||
private ClassDefinition employeeDefinition(final String packageName, final ClassDefinition addressDefinition) { | ||
final ClassDefinition employeeDefinition = new ClassDefinition("employee", new ClassName(packageName, "Employee")); | ||
employeeDefinition.addFieldDefinition(new FieldDefinition("firstName", new ClassName(String.class))); | ||
employeeDefinition.addFieldDefinition(new FieldDefinition("lastName", new ClassName(String.class))); | ||
employeeDefinition.addFieldDefinition(new FieldDefinition("poundsPerHour", new ClassName(BigDecimal.class))); | ||
employeeDefinition.addFieldDefinition(new FieldDefinition("startDate", new ClassName(ZonedDateTime.class))); | ||
employeeDefinition.addFieldDefinition(new FieldDefinition("favouriteColours", new ClassName(List.class), new ClassName(String.class))); | ||
employeeDefinition.addFieldDefinition(addressDefinition); | ||
|
||
return employeeDefinition; | ||
} | ||
|
||
|
||
} |
6 changes: 3 additions & 3 deletions
6
...on-test/src/test/java/uk/gov/justice/generation/pojo/integration/utils/ClassCompiler.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
28 changes: 28 additions & 0 deletions
28
...ator-core/src/main/java/uk/gov/justice/generation/bootstrap/GenerationContextFactory.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,28 @@ | ||
package uk.gov.justice.generation.bootstrap; | ||
|
||
import static java.lang.String.format; | ||
|
||
import uk.gov.justice.generation.pojo.core.GenerationContext; | ||
import uk.gov.justice.generation.pojo.write.SourceCodeWriteException; | ||
|
||
import java.io.File; | ||
|
||
public class GenerationContextFactory { | ||
|
||
public GenerationContext create(final File sourceRootDirectory) { | ||
|
||
if (! sourceRootDirectory.exists()) { | ||
throw new SourceCodeWriteException(format("Source code root directory '%s' does not exist", sourceRootDirectory.getAbsolutePath())); | ||
} | ||
|
||
if (! sourceRootDirectory.isDirectory()) { | ||
throw new SourceCodeWriteException(format("Source code root directory '%s' is not a directory", sourceRootDirectory.getAbsolutePath())); | ||
} | ||
|
||
if (! sourceRootDirectory.canWrite()) { | ||
throw new SourceCodeWriteException(format("Source code root directory '%s' is not writable", sourceRootDirectory.getAbsolutePath())); | ||
} | ||
|
||
return new GenerationContext(sourceRootDirectory); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/core/GenerationContext.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,23 @@ | ||
package uk.gov.justice.generation.pojo.core; | ||
|
||
import java.io.File; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class GenerationContext { | ||
|
||
private final File sourceRootDirectory; | ||
|
||
public GenerationContext(final File sourceRootDirectory) { | ||
this.sourceRootDirectory = sourceRootDirectory; | ||
} | ||
|
||
public File getSourceRootDirectory() { | ||
return sourceRootDirectory; | ||
} | ||
|
||
public Logger getLoggerFor(final Class<?> clazz) { | ||
return LoggerFactory.getLogger(clazz); | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/dom/ClassDefinition.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
2 changes: 0 additions & 2 deletions
2
pojo-generator-core/src/main/java/uk/gov/justice/generation/pojo/dom/FieldDefinition.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
12 changes: 12 additions & 0 deletions
12
...erator-core/src/main/java/uk/gov/justice/generation/pojo/generators/ClassGeneratable.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,12 @@ | ||
package uk.gov.justice.generation.pojo.generators; | ||
|
||
import uk.gov.justice.generation.pojo.dom.ClassName; | ||
|
||
import com.squareup.javapoet.TypeSpec; | ||
|
||
public interface ClassGeneratable { | ||
|
||
TypeSpec generate(); | ||
|
||
ClassName getClassName(); | ||
} |
Oops, something went wrong.