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.
- Loading branch information
amckenzie
committed
Aug 21, 2017
1 parent
12d681e
commit 6bf8323
Showing
9 changed files
with
118 additions
and
125 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
...n-test/src/test/java/uk/gov/justice/generation/pojo/integration/test/EnumGeneratorIT.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,109 @@ | ||
package uk.gov.justice.generation.pojo.integration.test; | ||
|
||
import static com.jayway.jsonassert.JsonAssert.with; | ||
import static java.util.Arrays.asList; | ||
import static java.util.stream.Collectors.reducing; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.apache.commons.io.FileUtils.cleanDirectory; | ||
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.EnumDefinition; | ||
import uk.gov.justice.generation.pojo.dom.FieldDefinition; | ||
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.util.List; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class EnumGeneratorIT { | ||
|
||
private final SourceWriter sourceWriter = new SourceWriter(); | ||
private final ClassCompiler classCompiler = new ClassCompiler(); | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper(); | ||
private final JavaGeneratorFactory javaGeneratorFactory = new JavaGeneratorFactory(); | ||
|
||
private File sourceOutputDirectory; | ||
private File classesOutputDirectory; | ||
|
||
@Before | ||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
public void setup() throws Exception { | ||
sourceOutputDirectory = new File("./target/test-generation"); | ||
classesOutputDirectory = new File("./target/test-classes"); | ||
|
||
sourceOutputDirectory.mkdirs(); | ||
classesOutputDirectory.mkdirs(); | ||
|
||
if (sourceOutputDirectory.exists()) { | ||
cleanDirectory(sourceOutputDirectory); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
public void shouldGenerateJavaClassSourceCode() throws Exception { | ||
|
||
final String packageName = "uk.gov.justice.pojo"; | ||
|
||
final GenerationContext generationContext = new GenerationContextFactory().create(sourceOutputDirectory); | ||
final ClassDefinition studentDefinition = studentDefinition(packageName); | ||
final EnumDefinition colourDefinition = colourDefinition(packageName); | ||
|
||
final List<? extends Class<?>> classes = javaGeneratorFactory | ||
.createClassGeneratorsFor(asList(colourDefinition, studentDefinition)) | ||
.stream() | ||
.map(classGenerator -> { | ||
sourceWriter.write(classGenerator, generationContext); | ||
return classCompiler.compile(classGenerator, sourceOutputDirectory, classesOutputDirectory); | ||
}).collect(toList()); | ||
|
||
assertThat(classes.get(0).getName(), is(colourDefinition.getClassName().getFullyQualifiedName())); | ||
assertThat(classes.get(1).getName(), is(studentDefinition.getClassName().getFullyQualifiedName())); | ||
|
||
final String enumName = colourDefinition.getEnumValues().get(0); | ||
|
||
|
||
final Class<? extends Enum> enumClass = (Class<? extends Enum>) classes.get(0); | ||
final Enum red = Enum.valueOf(enumClass, enumName.toUpperCase()); | ||
|
||
final Constructor<?> studentConstructor = classes.get(1).getConstructor(String.class, Integer.class, classes.get(0)); | ||
final String name = "Fred"; | ||
final Integer age = 21; | ||
final Object student = studentConstructor.newInstance(name, age, red); | ||
|
||
final String studentJson = objectMapper.writeValueAsString(student); | ||
|
||
with(studentJson) | ||
.assertThat("$.name", is(name)) | ||
.assertThat("$.age", is(age)) | ||
.assertThat("$.favouriteColour", is("Red")) | ||
; | ||
} | ||
|
||
private ClassDefinition studentDefinition(final String packageName) { | ||
|
||
return new ClassDefinition("student", new ClassName(packageName, "Student")) | ||
.addFieldDefinition(new FieldDefinition("name", new ClassName(String.class))) | ||
.addFieldDefinition(new FieldDefinition("age", new ClassName(Integer.class))) | ||
.addFieldDefinition(new FieldDefinition("favouriteColour", new ClassName(packageName, "Colour"))) | ||
; | ||
} | ||
|
||
private EnumDefinition colourDefinition(final String packageName) { | ||
|
||
return new EnumDefinition("favouriteColour", new ClassName(packageName, "Colour"), asList("Red", "Green", "Blue")); | ||
} | ||
} |
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
43 changes: 0 additions & 43 deletions
43
...-core/src/main/java/uk/gov/justice/generation/pojo/generators/StringElementGenerator.java
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
...rc/test/java/uk/gov/justice/generation/pojo/generators/ElementGeneratableFactoryTest.java
This file was deleted.
Oops, something went wrong.
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
31 changes: 0 additions & 31 deletions
31
...e/src/test/java/uk/gov/justice/generation/pojo/generators/StringElementGeneratorTest.java
This file was deleted.
Oops, something went wrong.
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