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.
convert additional properties to be a plugin
- Loading branch information
amckenzie
committed
Sep 12, 2017
1 parent
ce2498b
commit 591e70a
Showing
6 changed files
with
127 additions
and
108 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
52 changes: 0 additions & 52 deletions
52
...est/java/uk/gov/justice/generation/pojo/generators/AdditionalPropertiesGeneratorTest.java
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
...ation/pojo/generators/plugin/classmodifying/AddAdditionalPropertiesToClassPluginTest.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,102 @@ | ||
package uk.gov.justice.generation.pojo.generators.plugin.classmodifying; | ||
|
||
import static com.squareup.javapoet.TypeSpec.classBuilder; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import uk.gov.justice.generation.pojo.dom.ClassDefinition; | ||
|
||
import com.squareup.javapoet.TypeSpec; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class AddAdditionalPropertiesToClassPluginTest { | ||
|
||
@InjectMocks | ||
private AddAdditionalPropertiesToClassPlugin addAdditionalPropertiesToClassPlugin; | ||
|
||
@Test | ||
public void shouldGenerateTheAdditionalPropertiesFieldDeclarationIfAllowAdditionalPropertiesIsTrue() throws Exception { | ||
|
||
final String expectedField = "private final java.util.Map" + | ||
"<java.lang.String, java.lang.Object> additionalProperties " + | ||
"= new java.util.HashMap<>();\n"; | ||
|
||
final TypeSpec.Builder classBuilder = classBuilder("MyClass"); | ||
|
||
final ClassDefinition classDefinition = mock(ClassDefinition.class); | ||
final PluginContext pluginContext = mock(PluginContext.class); | ||
|
||
when(classDefinition.allowAdditionalProperties()).thenReturn(true); | ||
|
||
final TypeSpec.Builder modifiedBuilder = addAdditionalPropertiesToClassPlugin.generateWith( | ||
classBuilder, | ||
classDefinition, | ||
pluginContext); | ||
|
||
final TypeSpec typeSpec = modifiedBuilder.build(); | ||
|
||
assertThat(typeSpec.fieldSpecs.size(), is(1)); | ||
assertThat(typeSpec.fieldSpecs.get(0).toString(), is(expectedField)); | ||
} | ||
|
||
@Test | ||
public void shouldGenerateTheAdditionalPropertiesGetterAndSetterIfAllowAdditionalPropertiesIsTrue() throws Exception { | ||
|
||
final String expectedGetter = "@com.fasterxml.jackson.annotation.JsonAnyGetter\n" + | ||
"public java.util.Map<java.lang.String, java.lang.Object> getAdditionalProperties() {\n" + | ||
" return additionalProperties;\n" + | ||
"}\n"; | ||
|
||
final String expectedSetter = "@com.fasterxml.jackson.annotation.JsonAnySetter\n" + | ||
"public void setAdditionalProperty(" + | ||
"final java.lang.String name, " + | ||
"final java.lang.Object value) {\n" + | ||
" additionalProperties.put(name, value);\n" + | ||
"}\n"; | ||
|
||
final TypeSpec.Builder classBuilder = classBuilder("MyClass"); | ||
|
||
final ClassDefinition classDefinition = mock(ClassDefinition.class); | ||
final PluginContext pluginContext = mock(PluginContext.class); | ||
|
||
when(classDefinition.allowAdditionalProperties()).thenReturn(true); | ||
|
||
final TypeSpec.Builder modifiedBuilder = addAdditionalPropertiesToClassPlugin.generateWith( | ||
classBuilder, | ||
classDefinition, | ||
pluginContext); | ||
|
||
final TypeSpec typeSpec = modifiedBuilder.build(); | ||
|
||
assertThat(typeSpec.methodSpecs.size(), is(2)); | ||
assertThat(typeSpec.methodSpecs.get(0).toString(), is(expectedGetter)); | ||
assertThat(typeSpec.methodSpecs.get(1).toString(), is(expectedSetter)); | ||
} | ||
|
||
@Test | ||
public void shouldNotModifyTheClassSpecIfAdditionalPropertiesIsFalse() throws Exception { | ||
|
||
final TypeSpec.Builder classBuilder = classBuilder("MyClass"); | ||
|
||
final ClassDefinition classDefinition = mock(ClassDefinition.class); | ||
final PluginContext pluginContext = mock(PluginContext.class); | ||
|
||
when(classDefinition.allowAdditionalProperties()).thenReturn(false); | ||
|
||
final TypeSpec.Builder modifiedBuilder = addAdditionalPropertiesToClassPlugin.generateWith( | ||
classBuilder, | ||
classDefinition, | ||
pluginContext); | ||
|
||
final TypeSpec typeSpec = modifiedBuilder.build(); | ||
|
||
assertThat(typeSpec.fieldSpecs.size(), is(0)); | ||
assertThat(typeSpec.methodSpecs.size(), is(0)); | ||
} | ||
} |
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