Skip to content
Permalink
Browse files

Add mutable model option to kotlin generators (#4115)

* Add mutable model option to kotlin generators

fix #3803

* doc

* Change template name to modelMutable

* Samples
  • Loading branch information
Zomzog authored and wing328 committed Nov 27, 2019
1 parent 96c1bda commit 2dc022087462ec8b61ffc79cd18ddd5351f5107a
Showing with 1,743 additions and 49 deletions.
  1. +2 −1 bin/kotlin-springboot-petstore-all.sh
  2. +35 −0 bin/kotlin-springboot-petstore-server-model-mutable.sh
  3. +1 −1 bin/kotlin-vertx-server-petstore.sh
  4. +1 −0 docs/generators/kotlin-server.md
  5. +1 −0 docs/generators/kotlin-spring.md
  6. +1 −0 docs/generators/kotlin-vertx.md
  7. +1 −0 docs/generators/kotlin.md
  8. +5 −0 ...les/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
  9. +1 −1 modules/openapi-generator/src/main/resources/kotlin-client/data_class_opt_var.mustache
  10. +1 −1 modules/openapi-generator/src/main/resources/kotlin-client/data_class_req_var.mustache
  11. +1 −0 modules/openapi-generator/src/main/resources/kotlin-client/modelMutable.mustache
  12. +1 −1 modules/openapi-generator/src/main/resources/kotlin-server/data_class_opt_var.mustache
  13. +1 −1 modules/openapi-generator/src/main/resources/kotlin-server/data_class_req_var.mustache
  14. +1 −0 modules/openapi-generator/src/main/resources/kotlin-server/modelMutable.mustache
  15. +1 −1 modules/openapi-generator/src/main/resources/kotlin-spring/dataClassOptVar.mustache
  16. +1 −1 modules/openapi-generator/src/main/resources/kotlin-spring/dataClassReqVar.mustache
  17. +1 −1 modules/openapi-generator/src/main/resources/kotlin-spring/interfaceOptVar.mustache
  18. +1 −1 modules/openapi-generator/src/main/resources/kotlin-spring/interfaceReqVar.mustache
  19. +1 −0 modules/openapi-generator/src/main/resources/kotlin-spring/modelMutable.mustache
  20. +2 −2 modules/openapi-generator/src/main/resources/kotlin-vertx-server/data_class.mustache
  21. +1 −1 modules/openapi-generator/src/main/resources/kotlin-vertx-server/data_class_opt_var.mustache
  22. +1 −1 modules/openapi-generator/src/main/resources/kotlin-vertx-server/data_class_req_var.mustache
  23. +1 −0 modules/openapi-generator/src/main/resources/kotlin-vertx-server/modelMutable.mustache
  24. +24 −0 modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java
  25. +18 −35 modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJaxrsBaseTest.java
  26. +72 −0 modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinModelCodegenTest.java
  27. +23 −0 samples/server/petstore/kotlin-springboot-modelMutable/.openapi-generator-ignore
  28. +1 −0 samples/server/petstore/kotlin-springboot-modelMutable/.openapi-generator/VERSION
  29. +21 −0 samples/server/petstore/kotlin-springboot-modelMutable/README.md
  30. +54 −0 samples/server/petstore/kotlin-springboot-modelMutable/build.gradle.kts
  31. +153 −0 samples/server/petstore/kotlin-springboot-modelMutable/pom.xml
  32. +15 −0 samples/server/petstore/kotlin-springboot-modelMutable/settings.gradle
  33. +14 −0 ...es/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/Application.kt
  34. +29 −0 ...server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/Exceptions.kt
  35. +184 −0 ...les/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/PetApi.kt
  36. +22 −0 ...ver/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/PetApiService.kt
  37. +40 −0 ...petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/PetApiServiceImpl.kt
  38. +107 −0 ...s/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/StoreApi.kt
  39. +13 −0 ...r/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/StoreApiService.kt
  40. +23 −0 ...tstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/StoreApiServiceImpl.kt
  41. +161 −0 ...es/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApi.kt
  42. +21 −0 ...er/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApiService.kt
  43. +39 −0 ...etstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/api/UserApiServiceImpl.kt
  44. +29 −0 ...server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/model/Category.kt
  45. +33 −0 ...etstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/model/ModelApiResponse.kt
  46. +60 −0 ...es/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/model/Order.kt
  47. +64 −0 samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/model/Pet.kt
  48. +29 −0 samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/model/Tag.kt
  49. +53 −0 ...les/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/model/User.kt
  50. +10 −0 samples/server/petstore/kotlin-springboot-modelMutable/src/main/resources/application.yaml
  51. +148 −0 ...server/petstore/kotlin-springboot-modelMutable/src/test/kotlin/org/openapitools/api/PetApiTest.kt
  52. +77 −0 ...rver/petstore/kotlin-springboot-modelMutable/src/test/kotlin/org/openapitools/api/StoreApiTest.kt
  53. +143 −0 ...erver/petstore/kotlin-springboot-modelMutable/src/test/kotlin/org/openapitools/api/UserApiTest.kt
@@ -1,4 +1,5 @@
#!/bin/sh

./bin/kotlin-springboot-petstore-server.sh
./bin/kotlin-springboot-petstore-server-reactive.sh
./bin/kotlin-springboot-petstore-server-reactive.sh
./bin/kotlin-springboot-petstore-server-model-mutable.sh
@@ -0,0 +1,35 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=$(ls -ld "$SCRIPT")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=$(dirname "$SCRIPT")/..
APP_DIR=$(cd "${APP_DIR}"; pwd)
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -t modules/openapi-generator/src/main/resources/kotlin-spring -g kotlin-spring -o samples/server/petstore/kotlin-springboot-modelMutable --additional-properties=library=spring-boot,beanValidations=true,swaggerAnnotations=true,serviceImplementation=true,serializableModel=true,modelMutable=true"

echo "Cleaning previously generated files if any from samples/server/petstore/kotlin-springboot"
rm -rf samples/server/petstore/kotlin-springboot-modelMutable

echo "Generating Kotling Spring Boot server..."
java $JAVA_OPTS -jar $executable $ags
@@ -26,6 +26,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin-vertx -o samples/server/petstore/kotlin/vertx"
ags="$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin-vertx -o samples/server/petstore/kotlin/vertx --additional-properties=modelMutable=false"

java ${JAVA_OPTS} -jar ${executable} ${ags}
@@ -15,6 +15,7 @@ sidebar_label: kotlin-server
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle "@Parcelize" for generated models| |null|
|serializableModel|boolean - toggle "implements Serializable" for generated models| |null|
|modelMutable|Create mutable models| |false|
|library|library template (sub-template)|<dl><dt>**ktor**</dt><dd>ktor framework</dd><dl>|ktor|
|featureAutoHead|Automatically provide responses to HEAD requests for existing routes that have the GET verb defined.| |true|
|featureConditionalHeaders|Avoid sending content if client already has same content, by checking ETag or LastModified properties.| |false|
@@ -15,6 +15,7 @@ sidebar_label: kotlin-spring
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
|serializableModel|boolean - toggle &quot;implements Serializable&quot; for generated models| |null|
|modelMutable|Create mutable models| |false|
|title|server title name or client service name| |OpenAPI Kotlin Spring|
|basePackage|base package (invokerPackage) for generated code| |org.openapitools|
|serverPort|configuration the port in which the sever is to run on| |8080|
@@ -15,3 +15,4 @@ sidebar_label: kotlin-vertx
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
|serializableModel|boolean - toggle &quot;implements Serializable&quot; for generated models| |null|
|modelMutable|Create mutable models| |false|
@@ -15,6 +15,7 @@ sidebar_label: kotlin
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson'| |moshi|
|parcelizeModels|toggle &quot;@Parcelize&quot; for generated models| |null|
|serializableModel|boolean - toggle &quot;implements Serializable&quot; for generated models| |null|
|modelMutable|Create mutable models| |false|
|dateLibrary|Option. Date library to use|<dl><dt>**string**</dt><dd>String</dd><dt>**java8**</dt><dd>Java 8 native JSR310 (jvm only)</dd><dt>**threetenbp**</dt><dd>Threetenbp (jvm only)</dd><dl>|java8|
|collectionType|Option. Collection type to use|<dl><dt>**array**</dt><dd>kotlin.Array</dd><dt>**list**</dt><dd>kotlin.collections.List</dd><dl>|array|
|library|Library template (sub-template) to use|<dl><dt>**jvm-okhttp4**</dt><dd>[DEFAULT] Platform: Java Virtual Machine. HTTP client: OkHttp 4.2.0 (Android 5.0+ and Java 8+). JSON processing: Moshi 1.8.0.</dd><dt>**jvm-okhttp3**</dt><dd>Platform: Java Virtual Machine. HTTP client: OkHttp 3.12.4 (Android 2.3+ and Java 7+). JSON processing: Moshi 1.8.0.</dd><dt>**multiplatform**</dt><dd>Platform: Kotlin multiplatform. HTTP client: Ktor 1.2.4. JSON processing: Kotlinx Serialization: 0.12.0.</dd><dl>|jvm-okhttp4|
@@ -43,6 +43,9 @@

public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson}

public static final String MODEL_MUTABLE = "modelMutable";
public static final String MODEL_MUTABLE_DESC = "Create mutable models";

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractKotlinCodegen.class);

protected String artifactId;
@@ -225,6 +228,8 @@ public AbstractKotlinCodegen() {

cliOptions.add(new CliOption(CodegenConstants.PARCELIZE_MODELS, CodegenConstants.PARCELIZE_MODELS_DESC));
cliOptions.add(new CliOption(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC));

cliOptions.add(CliOption.newBoolean(MODEL_MUTABLE, MODEL_MUTABLE_DESC, false));
}

@Override
@@ -9,4 +9,4 @@
@SerializedName("{{{vendorExtensions.x-base-name-literal}}}")
{{/gson}}
{{/jvm}}
{{#multiplatform}}@SerialName(value = "{{{vendorExtensions.x-base-name-literal}}}") {{/multiplatform}}val {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
{{#multiplatform}}@SerialName(value = "{{{vendorExtensions.x-base-name-literal}}}") {{/multiplatform}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
@@ -9,4 +9,4 @@
@SerializedName("{{{vendorExtensions.x-base-name-literal}}}")
{{/gson}}
{{/jvm}}
{{#multiplatform}}@SerialName(value = "{{{vendorExtensions.x-base-name-literal}}}") @Required {{/multiplatform}}val {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
{{#multiplatform}}@SerialName(value = "{{{vendorExtensions.x-base-name-literal}}}") @Required {{/multiplatform}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
@@ -0,0 +1 @@
{{#modelMutable}}var{{/modelMutable}}{{^modelMutable}}val{{/modelMutable}}
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{/description}}
val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{/description}}
val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
@@ -0,0 +1 @@
{{#modelMutable}}var{{/modelMutable}}{{^modelMutable}}val{{/modelMutable}}
@@ -1,4 +1,4 @@
{{#useBeanValidation}}{{#required}}
{{^isReadOnly}}@get:NotNull{{/isReadOnly}} {{/required}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
@JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
@JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
@@ -1,4 +1,4 @@
{{#useBeanValidation}}{{#required}}
{{^isReadOnly}}@get:NotNull{{/isReadOnly}} {{/required}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
@JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isReadOnly}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}{{/isReadOnly}}
@JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isReadOnly}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}{{/isReadOnly}}
@@ -1,3 +1,3 @@
{{#swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? {{^discriminator}}= {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}{{/discriminator}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? {{^discriminator}}= {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}{{/discriminator}}
@@ -1,3 +1,3 @@
{{#swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
val {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
@@ -0,0 +1 @@
{{#modelMutable}}var{{/modelMutable}}{{^modelMutable}}val{{/modelMutable}}
@@ -36,7 +36,7 @@ data class {{classname}} (
}
{{/isEnum}}{{/vars}}{{/hasEnums}}
{{#requiredVars}}
var {{{name}}} get() = _{{{name}}} ?: throw IllegalArgumentException("{{{name}}} is required")
set(value){ _{{{name}}} = value }
{{>modelMutable}} {{{name}}} get() = _{{{name}}} ?: throw IllegalArgumentException("{{{name}}} is required")
{{#modelMutable}}set(value){ _{{{name}}} = value }{{/modelMutable}}
{{/requiredVars}}
}
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{/description}}
var {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
@@ -1,4 +1,4 @@
{{#description}}
/* {{{description}}} */
{{/description}}
@SerializedName("{{{name}}}") private var _{{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}?
@SerializedName("{{{name}}}") private {{>modelMutable}} _{{{name}}}: {{#isEnum}}{{classname}}.{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}?
@@ -0,0 +1 @@
{{#modelMutable}}var{{/modelMutable}}{{^modelMutable}}val{{/modelMutable}}
@@ -1,5 +1,6 @@
package org.openapitools.codegen;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
@@ -104,4 +105,27 @@ public static void assertValidJavaSourceCode(String javaSourceCode, String filen
fail("Java parse problem: " + filename, ex);
}
}


public static void assertFileContains(MockDefaultGenerator generator, String path, String... lines) {
final String generatedFile = generator.getFiles().get(path);
if (null == generatedFile) {
fail("File " + path + " not found in " + generator.getFiles().keySet());
}
String file = linearize(generatedFile);
assertNotNull(file);
for (String line : lines)
assertTrue(file.contains(linearize(line)));
}

private static String linearize(String target) {
return target.replaceAll("\r?\n", "").replaceAll("\\s+", "\\s");
}

public static void assertFileNotContains(MockDefaultGenerator generator, String path, String... lines) {
String file = linearize(generator.getFiles().get(path));
assertNotNull(file);
for (String line : lines)
assertFalse(file.contains(linearize(line)));
}
}

0 comments on commit 2dc0220

Please sign in to comment.
You can’t perform that action at this time.