From 28433ae2b4efac97c0cba3ae67ffca1f723f1354 Mon Sep 17 00:00:00 2001 From: Travis-CI Date: Tue, 14 Sep 2021 02:06:20 +0000 Subject: [PATCH 01/12] updating poms for 0.1.3 branch with snapshot versions --- pom.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 4db6de3..8964a01 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 org.swisspush.jsonschema2pojo From e5785868127f192f38039fbd62f2f75a3f8d5c63 Mon Sep 17 00:00:00 2001 From: Travis-CI Date: Tue, 14 Sep 2021 02:06:20 +0000 Subject: [PATCH 02/12] updating poms for branch'release-0.1.3' with non-snapshot versions --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 8964a01..1927900 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.swisspush.jsonschema2pojo jsonschema2pojo-openenum - 0.1.3-SNAPSHOT + 0.1.3 jsonschema2pojo-openenum Config Builders for Kafka. A fluent API for configuring kafka clients. https://github.com/swisspush/jsonschema2pojo-openenum From 82ac44da00ab801a52bfb75a54f9387fa88ab888 Mon Sep 17 00:00:00 2001 From: Travis-CI Date: Tue, 14 Sep 2021 02:06:20 +0000 Subject: [PATCH 03/12] updating poms for 0.1.4-SNAPSHOT development --- pom.xml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 4db6de3..a7fac70 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,10 @@ - + 4.0.0 org.swisspush.jsonschema2pojo jsonschema2pojo-openenum - 0.1.3-SNAPSHOT + 0.1.4-SNAPSHOT jsonschema2pojo-openenum Config Builders for Kafka. A fluent API for configuring kafka clients. https://github.com/swisspush/jsonschema2pojo-openenum From 033ac04449e00558f8e5cccf43138333f3d837d7 Mon Sep 17 00:00:00 2001 From: Travis-CI Date: Tue, 14 Sep 2021 02:06:49 +0000 Subject: [PATCH 04/12] updating develop poms to master versions to avoid merge conflicts --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a7fac70..1927900 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.swisspush.jsonschema2pojo jsonschema2pojo-openenum - 0.1.4-SNAPSHOT + 0.1.3 jsonschema2pojo-openenum Config Builders for Kafka. A fluent API for configuring kafka clients. https://github.com/swisspush/jsonschema2pojo-openenum From af0fe8d4f7c55ccf82f95a26c3df7d86141778a4 Mon Sep 17 00:00:00 2001 From: Travis-CI Date: Tue, 14 Sep 2021 02:06:49 +0000 Subject: [PATCH 05/12] Updating develop poms back to pre merge state --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1927900..a7fac70 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.swisspush.jsonschema2pojo jsonschema2pojo-openenum - 0.1.3 + 0.1.4-SNAPSHOT jsonschema2pojo-openenum Config Builders for Kafka. A fluent API for configuring kafka clients. https://github.com/swisspush/jsonschema2pojo-openenum From 472c4d8931590fe9d33e5950a46cc6f6ac7755d7 Mon Sep 17 00:00:00 2001 From: Francesco Rigotti Date: Mon, 8 Jan 2024 21:30:24 +0100 Subject: [PATCH 06/12] #feat declaredValues constant and utility methods isDeclaredValue and isNotDeclaredValue to detect values not known at compile time --- README.md | 26 ++++++++++ .../openenum/OpenEnumRule.java | 50 +++++++++++++++++-- .../openenum/OpenEnumRuleTest.java | 48 ++++++++++++++++++ .../jsonschema2pojo/openenum/Status.java | 24 ++++++++- .../jsonschema2pojo/openenum/StatusTest.java | 20 ++++++++ 5 files changed, 163 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6a704ec..1689fff 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,18 @@ Generate enums as relaxed type-safe enum classes supporting unknown values. This allows backward compatibility when JSON schemas get new enum values whereas existing code does not yet know them. +Methods are in place to detect values not yet known to an application. + The generated code looks like: ```java package org.swisspush.jsonschema2pojo.openenum; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; @@ -19,6 +24,11 @@ public class Status { private final static Map values = new HashMap(); public final static Status OPEN = Status.fromString("OPEN"); public final static Status CLOSED = Status.fromString("CLOSED"); + /** + * Set containing all enum values declared at compile time.use it in your application to iterate over declared values. + * + */ + public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED)); private String value; private Status(String value) { @@ -37,6 +47,22 @@ public class Status { return this.value; } + /** + * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * + */ + public static Boolean isDeclaredValue(Status val) { + return Status.declaredValues.contains(val); + } + + /** + * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * + */ + public static Boolean isNotDeclaredValue(Status val) { + return (!Status.isDeclaredValue(val)); + } + } ``` diff --git a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java index a77dc64..d531e31 100644 --- a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java +++ b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java @@ -54,10 +54,13 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai container.owner().ref(String.class); JMethod factoryMethod = addFactoryMethod(_enum, backingType); - addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType, factoryMethod); + List enumConstants = addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType, factoryMethod); + addEnumConstantsSet(_enum, enumConstants); JFieldVar valueField = addValueField(_enum, backingType); addToString(_enum, valueField); - + // utility methods to detect non-declared values + addStaticMethodIsDeclaredValue(_enum); + addStaticMethodIsNotDeclaredValue(_enum); return _enum; } @@ -149,8 +152,9 @@ private boolean isString(JType type){ return type.fullName().equals(String.class.getName()); } - private void addEnumConstants(JsonNode node, JDefinedClass _enum, JsonNode customNames, JType type, JMethod factoryMethod) { + private List addEnumConstants(JsonNode node, JDefinedClass _enum, JsonNode customNames, JType type, JMethod factoryMethod) { Collection existingConstantNames = new ArrayList<>(); + List enumConstants = new ArrayList<>(); for (int i = 0; i < node.size(); i++) { JsonNode value = node.path(i); @@ -162,8 +166,48 @@ private void addEnumConstants(JsonNode node, JDefinedClass _enum, JsonNode custo JFieldVar constant = _enum.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, _enum, constantName); constant.init(_enum.staticInvoke(factoryMethod).arg(JExpr.lit(value.asText()))); + enumConstants.add(constant); } } + return enumConstants; + } + + private void addEnumConstantsSet(JDefinedClass _enum, List enumConstants) { + JClass fieldType = _enum.owner().ref(Set.class).narrow(_enum); + JFieldVar field = _enum.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, fieldType, "declaredValues"); + JClass fieldConcreteType = _enum.owner().ref(HashSet.class).narrow(_enum); + // Initialize the HashSet using Arrays.asList() + JClass arrays = _enum.owner().ref(Arrays.class); + JInvocation asListInvocation = arrays.staticInvoke("asList"); + // Add enum constants to the asListInvocation + for (JFieldVar constant : enumConstants) { + asListInvocation.arg(_enum.staticRef(constant)); + } + // Initialize the field with a new HashSet created from the list + field.init(JExpr._new(fieldConcreteType).arg(asListInvocation)); + + field.javadoc().add("Set containing all enum values declared at compile time."); + field.javadoc().add("use it in your application to iterate over declared values."); + } + + private void addStaticMethodIsDeclaredValue(JDefinedClass _enum) { + JMethod method = _enum.method(JMod.PUBLIC | JMod.STATIC, Boolean.class, "isDeclaredValue"); + JVar valueParam = method.param(_enum, "val"); + JExpression toReturn = _enum.staticRef("declaredValues").invoke("contains").arg(valueParam); + method.body()._return(toReturn); + + method.javadoc().add("returns true if given enum is part of the declared values."); + method.javadoc().add("use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of."); + } + + private void addStaticMethodIsNotDeclaredValue(JDefinedClass _enum) { + JMethod method = _enum.method(JMod.PUBLIC | JMod.STATIC, Boolean.class, "isNotDeclaredValue"); + JVar valueParam = method.param(_enum, "val"); + JExpression toReturn = JOp.not(_enum.staticInvoke("isDeclaredValue").arg(valueParam)); + method.body()._return(toReturn); + + method.javadoc().add("returns true if given enum is NOT part of the declared values."); + method.javadoc().add("use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of."); } private String getEnumName(String nodeName, JsonNode node, JClassContainer container) { diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java index 5444146..adc1e60 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java @@ -42,8 +42,11 @@ public void testSimpleEnum() throws IOException { "\n" + "package org.swisspush.jsonschema2pojo.openenum;\n" + "\n" + + "import java.util.Arrays;\n" + "import java.util.HashMap;\n" + + "import java.util.HashSet;\n" + "import java.util.Map;\n" + + "import java.util.Set;\n" + "import com.fasterxml.jackson.annotation.JsonCreator;\n" + "import com.fasterxml.jackson.annotation.JsonValue;\n" + "\n" + @@ -52,6 +55,11 @@ public void testSimpleEnum() throws IOException { " private final static Map values = new HashMap();\n" + " public final static Status OPEN = Status.fromString(\"OPEN\");\n" + " public final static Status CLOSED = Status.fromString(\"CLOSED\");\n" + + " /**\n" + + " * Set containing all enum values declared at compile time.use it in your application to iterate over declared values.\n" + + " * \n" + + " */\n" + + " public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED));\n" + " private String value;\n" + "\n" + " private Status(String value) {\n" + @@ -70,6 +78,22 @@ public void testSimpleEnum() throws IOException { " return this.value;\n" + " }\n" + "\n" + + " /**\n" + + " * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * \n" + + " */\n" + + " public static Boolean isDeclaredValue(Status val) {\n" + + " return Status.declaredValues.contains(val);\n" + + " }\n" + + "\n" + + " /**\n" + + " * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * \n" + + " */\n" + + " public static Boolean isNotDeclaredValue(Status val) {\n" + + " return (!Status.isDeclaredValue(val));\n" + + " }\n" + + "\n" + "}" + "\n"; @@ -98,8 +122,11 @@ public void testLowercaseValues() throws IOException { "\n" + "package org.swisspush.jsonschema2pojo.openenum;\n" + "\n" + + "import java.util.Arrays;\n" + "import java.util.HashMap;\n" + + "import java.util.HashSet;\n" + "import java.util.Map;\n" + + "import java.util.Set;\n" + "import com.fasterxml.jackson.annotation.JsonCreator;\n" + "import com.fasterxml.jackson.annotation.JsonValue;\n" + "\n" + @@ -108,6 +135,11 @@ public void testLowercaseValues() throws IOException { " private final static Map values = new HashMap();\n" + " public final static Status OPEN = Status.fromString(\"open\");\n" + " public final static Status CLOSED = Status.fromString(\"closed\");\n" + + " /**\n" + + " * Set containing all enum values declared at compile time.use it in your application to iterate over declared values.\n" + + " * \n" + + " */\n" + + " public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED));\n" + " private String value;\n" + "\n" + " private Status(String value) {\n" + @@ -126,6 +158,22 @@ public void testLowercaseValues() throws IOException { " return this.value;\n" + " }\n" + "\n" + + " /**\n" + + " * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * \n" + + " */\n" + + " public static Boolean isDeclaredValue(Status val) {\n" + + " return Status.declaredValues.contains(val);\n" + + " }\n" + + "\n" + + " /**\n" + + " * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * \n" + + " */\n" + + " public static Boolean isNotDeclaredValue(Status val) {\n" + + " return (!Status.isDeclaredValue(val));\n" + + " }\n" + + "\n" + "}" + "\n"; diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java index fef8492..33a3876 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java @@ -1,7 +1,7 @@ package org.swisspush.jsonschema2pojo.openenum; -import java.util.HashMap; -import java.util.Map; +import java.util.*; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; @@ -10,6 +10,11 @@ public class Status { private final static Map values = new HashMap(); public final static Status OPEN = Status.fromString("OPEN"); public final static Status CLOSED = Status.fromString("CLOSED"); + /** + * Set containing all enum values declared at compile time.use it in your application to iterate over declared values. + * + */ + public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED)); private String value; private Status(String value) { @@ -28,4 +33,19 @@ public String toString() { return this.value; } + /** + * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * + */ + public static Boolean isDeclaredValue(Status val) { + return Status.declaredValues.contains(val); + } + + /** + * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * + */ + public static Boolean isNotDeclaredValue(Status val) { + return !isDeclaredValue(val); + } } \ No newline at end of file diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java index 64cfb00..d958066 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java @@ -12,4 +12,24 @@ public void testOpenEnumPattern() { assertNotSame(Status.CLOSED, Status.fromString("OTHER")); assertEquals(Status.OPEN.toString(), "OPEN"); } + + @Test + public void isDeclaredValue() { + assertTrue(Status.isDeclaredValue(Status.OPEN)); + assertTrue(Status.isDeclaredValue(Status.CLOSED)); + assertTrue(Status.isDeclaredValue(Status.fromString("OPEN"))); + assertTrue(Status.isDeclaredValue(Status.fromString("CLOSED"))); + assertFalse(Status.isDeclaredValue(Status.fromString("NOT_DECLARED_VALUE"))); + assertFalse(Status.isDeclaredValue(Status.fromString("OTHER"))); + } + + @Test + public void isNotDeclaredValue() { + assertFalse(Status.isNotDeclaredValue(Status.OPEN)); + assertFalse(Status.isNotDeclaredValue(Status.CLOSED)); + assertFalse(Status.isNotDeclaredValue(Status.fromString("OPEN"))); + assertFalse(Status.isNotDeclaredValue(Status.fromString("CLOSED"))); + assertTrue(Status.isNotDeclaredValue(Status.fromString("NOT_DECLARED_VALUE"))); + assertTrue(Status.isNotDeclaredValue(Status.fromString("OTHER"))); + } } From 021fb8451add3d99f27c5e0efe376e0e58ce0f7a Mon Sep 17 00:00:00 2001 From: Francesco Rigotti Date: Tue, 9 Jan 2024 09:07:19 +0100 Subject: [PATCH 07/12] minor polishing --- README.md | 2 +- .../openenum/OpenEnumRule.java | 21 ++++++++++++------- .../jsonschema2pojo/openenum/StatusTest.java | 12 +++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1689fff..0057f2b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Generate enums as relaxed type-safe enum classes supporting unknown values. This allows backward compatibility when JSON schemas get new enum values whereas existing code does not yet know them. -Methods are in place to detect values not yet known to an application. +Enum classes have methods to detect values not yet known to an application. The generated code looks like: diff --git a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java index d531e31..371a843 100644 --- a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java +++ b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java @@ -55,7 +55,7 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai JMethod factoryMethod = addFactoryMethod(_enum, backingType); List enumConstants = addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType, factoryMethod); - addEnumConstantsSet(_enum, enumConstants); + addConstantDeclaredValues(_enum, enumConstants); JFieldVar valueField = addValueField(_enum, backingType); addToString(_enum, valueField); // utility methods to detect non-declared values @@ -172,16 +172,23 @@ private List addEnumConstants(JsonNode node, JDefinedClass _enum, Jso return enumConstants; } - private void addEnumConstantsSet(JDefinedClass _enum, List enumConstants) { - JClass fieldType = _enum.owner().ref(Set.class).narrow(_enum); - JFieldVar field = _enum.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, fieldType, "declaredValues"); - JClass fieldConcreteType = _enum.owner().ref(HashSet.class).narrow(_enum); + /** + * Adds new constant declaredValues, which is a Set containing all enum value constants + * (i.e.: all enum values known at compile time). + * + * @param enumClass + * @param enumConstants + */ + private void addConstantDeclaredValues(JDefinedClass enumClass, List enumConstants) { + JClass fieldType = enumClass.owner().ref(Set.class).narrow(enumClass); + JFieldVar field = enumClass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, fieldType, "declaredValues"); + JClass fieldConcreteType = enumClass.owner().ref(HashSet.class).narrow(enumClass); // Initialize the HashSet using Arrays.asList() - JClass arrays = _enum.owner().ref(Arrays.class); + JClass arrays = enumClass.owner().ref(Arrays.class); JInvocation asListInvocation = arrays.staticInvoke("asList"); // Add enum constants to the asListInvocation for (JFieldVar constant : enumConstants) { - asListInvocation.arg(_enum.staticRef(constant)); + asListInvocation.arg(enumClass.staticRef(constant)); } // Initialize the field with a new HashSet created from the list field.init(JExpr._new(fieldConcreteType).arg(asListInvocation)); diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java index d958066..3fcae5e 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java @@ -23,6 +23,18 @@ public void isDeclaredValue() { assertFalse(Status.isDeclaredValue(Status.fromString("OTHER"))); } + @Test + public void declaredValuesIsNotAffectedByFromStringCalls() { + assertEquals(2, Status.declaredValues.size()); + Status.fromString("OTHER"); + assertEquals(2, Status.declaredValues.size()); + } + + @Test + public void iterateDeclaredValues() { + Status.declaredValues.forEach(System.out::println); + } + @Test public void isNotDeclaredValue() { assertFalse(Status.isNotDeclaredValue(Status.OPEN)); From 2726f483e46aca6ec4c1ebb529ed4b344bf51ee7 Mon Sep 17 00:00:00 2001 From: Francesco Rigotti Date: Tue, 9 Jan 2024 14:29:49 +0100 Subject: [PATCH 08/12] declaredValues wrapped inside Collections.unmodifiableSet . removed isDeclaredValue . isNotDeclaredValue is an instance method . further tests . improved README.md --- README.md | 20 +++------ .../openenum/OpenEnumRule.java | 43 ++++++++----------- .../openenum/OpenEnumRuleTest.java | 38 ++++++---------- .../jsonschema2pojo/openenum/Status.java | 18 +++----- .../jsonschema2pojo/openenum/StatusTest.java | 29 ++++++------- 5 files changed, 55 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 0057f2b..0d04e01 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ Generate enums as relaxed type-safe enum classes supporting unknown values. This allows backward compatibility when JSON schemas get new enum values whereas existing code does not yet know them. -Enum classes have methods to detect values not yet known to an application. +Public constant `declaredValues` allows to iterate on values known at compile time. +Instance method `isNotDeclaredValue` allows to detect values not yet known at compile time to an application. The generated code looks like: @@ -12,6 +13,7 @@ The generated code looks like: package org.swisspush.jsonschema2pojo.openenum; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -28,7 +30,7 @@ public class Status { * Set containing all enum values declared at compile time.use it in your application to iterate over declared values. * */ - public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED)); + public final static Set declaredValues = Collections.unmodifiableSet(new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED))); private String value; private Status(String value) { @@ -48,19 +50,11 @@ public class Status { } /** - * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. * */ - public static Boolean isDeclaredValue(Status val) { - return Status.declaredValues.contains(val); - } - - /** - * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. - * - */ - public static Boolean isNotDeclaredValue(Status val) { - return (!Status.isDeclaredValue(val)); + public Boolean isNotDeclaredValue() { + return (!Status.declaredValues.contains(this)); } } diff --git a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java index 371a843..f9b4265 100644 --- a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java +++ b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java @@ -58,9 +58,7 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai addConstantDeclaredValues(_enum, enumConstants); JFieldVar valueField = addValueField(_enum, backingType); addToString(_enum, valueField); - // utility methods to detect non-declared values - addStaticMethodIsDeclaredValue(_enum); - addStaticMethodIsNotDeclaredValue(_enum); + addMethodIsNotDeclaredValue(_enum); return _enum; } @@ -183,38 +181,33 @@ private void addConstantDeclaredValues(JDefinedClass enumClass, List JClass fieldType = enumClass.owner().ref(Set.class).narrow(enumClass); JFieldVar field = enumClass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, fieldType, "declaredValues"); JClass fieldConcreteType = enumClass.owner().ref(HashSet.class).narrow(enumClass); - // Initialize the HashSet using Arrays.asList() - JClass arrays = enumClass.owner().ref(Arrays.class); - JInvocation asListInvocation = arrays.staticInvoke("asList"); + + JInvocation arraysAsListInvocation = enumClass.owner().ref(Arrays.class).staticInvoke("asList"); // Add enum constants to the asListInvocation for (JFieldVar constant : enumConstants) { - asListInvocation.arg(enumClass.staticRef(constant)); + arraysAsListInvocation.arg(enumClass.staticRef(constant)); } - // Initialize the field with a new HashSet created from the list - field.init(JExpr._new(fieldConcreteType).arg(asListInvocation)); - field.javadoc().add("Set containing all enum values declared at compile time."); - field.javadoc().add("use it in your application to iterate over declared values."); - } + // construct HashSet using Arrays.asList() + JInvocation hashSetConstructorInvocation = JExpr._new(fieldConcreteType).arg(arraysAsListInvocation); - private void addStaticMethodIsDeclaredValue(JDefinedClass _enum) { - JMethod method = _enum.method(JMod.PUBLIC | JMod.STATIC, Boolean.class, "isDeclaredValue"); - JVar valueParam = method.param(_enum, "val"); - JExpression toReturn = _enum.staticRef("declaredValues").invoke("contains").arg(valueParam); - method.body()._return(toReturn); + // wrap HashSet into UnmodifiableSet + JInvocation unmodifiableSetInvocation = enumClass.owner().ref(Collections.class).staticInvoke("unmodifiableSet").arg(hashSetConstructorInvocation); + + // Initialize the field to unmodifiable set + field.init(unmodifiableSetInvocation); - method.javadoc().add("returns true if given enum is part of the declared values."); - method.javadoc().add("use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of."); + field.javadoc().add("Set containing all enum values declared at compile time."); + field.javadoc().add(" Use it in your application to iterate over declared values."); } - private void addStaticMethodIsNotDeclaredValue(JDefinedClass _enum) { - JMethod method = _enum.method(JMod.PUBLIC | JMod.STATIC, Boolean.class, "isNotDeclaredValue"); - JVar valueParam = method.param(_enum, "val"); - JExpression toReturn = JOp.not(_enum.staticInvoke("isDeclaredValue").arg(valueParam)); + private void addMethodIsNotDeclaredValue(JDefinedClass _enum) { + JMethod method = _enum.method(JMod.PUBLIC, Boolean.class, "isNotDeclaredValue"); + JExpression toReturn = JOp.not(_enum.staticRef("declaredValues").invoke("contains").arg(JExpr._this())); method.body()._return(toReturn); - method.javadoc().add("returns true if given enum is NOT part of the declared values."); - method.javadoc().add("use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of."); + method.javadoc().add("returns true if this enum is NOT part of the declared values."); + method.javadoc().add(" Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of."); } private String getEnumName(String nodeName, JsonNode node, JClassContainer container) { diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java index adc1e60..d53881d 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java @@ -43,6 +43,7 @@ public void testSimpleEnum() throws IOException { "package org.swisspush.jsonschema2pojo.openenum;\n" + "\n" + "import java.util.Arrays;\n" + + "import java.util.Collections;\n" + "import java.util.HashMap;\n" + "import java.util.HashSet;\n" + "import java.util.Map;\n" + @@ -56,10 +57,10 @@ public void testSimpleEnum() throws IOException { " public final static Status OPEN = Status.fromString(\"OPEN\");\n" + " public final static Status CLOSED = Status.fromString(\"CLOSED\");\n" + " /**\n" + - " * Set containing all enum values declared at compile time.use it in your application to iterate over declared values.\n" + + " * Set containing all enum values declared at compile time. Use it in your application to iterate over declared values.\n" + " * \n" + " */\n" + - " public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED));\n" + + " public final static Set declaredValues = Collections.unmodifiableSet(new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED)));\n" + " private String value;\n" + "\n" + " private Status(String value) {\n" + @@ -79,19 +80,11 @@ public void testSimpleEnum() throws IOException { " }\n" + "\n" + " /**\n" + - " * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + " * \n" + " */\n" + - " public static Boolean isDeclaredValue(Status val) {\n" + - " return Status.declaredValues.contains(val);\n" + - " }\n" + - "\n" + - " /**\n" + - " * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + - " * \n" + - " */\n" + - " public static Boolean isNotDeclaredValue(Status val) {\n" + - " return (!Status.isDeclaredValue(val));\n" + + " public Boolean isNotDeclaredValue() {\n" + + " return (!Status.declaredValues.contains(this));\n" + " }\n" + "\n" + "}" + @@ -123,6 +116,7 @@ public void testLowercaseValues() throws IOException { "package org.swisspush.jsonschema2pojo.openenum;\n" + "\n" + "import java.util.Arrays;\n" + + "import java.util.Collections;\n" + "import java.util.HashMap;\n" + "import java.util.HashSet;\n" + "import java.util.Map;\n" + @@ -136,10 +130,10 @@ public void testLowercaseValues() throws IOException { " public final static Status OPEN = Status.fromString(\"open\");\n" + " public final static Status CLOSED = Status.fromString(\"closed\");\n" + " /**\n" + - " * Set containing all enum values declared at compile time.use it in your application to iterate over declared values.\n" + + " * Set containing all enum values declared at compile time. Use it in your application to iterate over declared values.\n" + " * \n" + " */\n" + - " public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED));\n" + + " public final static Set declaredValues = Collections.unmodifiableSet(new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED)));\n" + " private String value;\n" + "\n" + " private Status(String value) {\n" + @@ -159,19 +153,11 @@ public void testLowercaseValues() throws IOException { " }\n" + "\n" + " /**\n" + - " * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + - " * \n" + - " */\n" + - " public static Boolean isDeclaredValue(Status val) {\n" + - " return Status.declaredValues.contains(val);\n" + - " }\n" + - "\n" + - " /**\n" + - " * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + " * \n" + " */\n" + - " public static Boolean isNotDeclaredValue(Status val) {\n" + - " return (!Status.isDeclaredValue(val));\n" + + " public Boolean isNotDeclaredValue() {\n" + + " return (!Status.declaredValues.contains(this));\n" + " }\n" + "\n" + "}" + diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java index 33a3876..9e4c055 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java @@ -11,10 +11,10 @@ public class Status { public final static Status OPEN = Status.fromString("OPEN"); public final static Status CLOSED = Status.fromString("CLOSED"); /** - * Set containing all enum values declared at compile time.use it in your application to iterate over declared values. + * Set containing all enum values declared at compile time. Use it in your application to iterate over declared values. * */ - public final static Set declaredValues = new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED)); + public final static Set declaredValues = Collections.unmodifiableSet(new HashSet(Arrays.asList(Status.OPEN, Status.CLOSED))); private String value; private Status(String value) { @@ -34,18 +34,10 @@ public String toString() { } /** - * returns true if given enum is part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. * */ - public static Boolean isDeclaredValue(Status val) { - return Status.declaredValues.contains(val); - } - - /** - * returns true if given enum is NOT part of the declared values.use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. - * - */ - public static Boolean isNotDeclaredValue(Status val) { - return !isDeclaredValue(val); + public Boolean isNotDeclaredValue() { + return (!Status.declaredValues.contains(this)); } } \ No newline at end of file diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java index 3fcae5e..7ac3288 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java @@ -11,16 +11,8 @@ public void testOpenEnumPattern() { assertNotSame(Status.CLOSED, Status.OPEN); assertNotSame(Status.CLOSED, Status.fromString("OTHER")); assertEquals(Status.OPEN.toString(), "OPEN"); - } - - @Test - public void isDeclaredValue() { - assertTrue(Status.isDeclaredValue(Status.OPEN)); - assertTrue(Status.isDeclaredValue(Status.CLOSED)); - assertTrue(Status.isDeclaredValue(Status.fromString("OPEN"))); - assertTrue(Status.isDeclaredValue(Status.fromString("CLOSED"))); - assertFalse(Status.isDeclaredValue(Status.fromString("NOT_DECLARED_VALUE"))); - assertFalse(Status.isDeclaredValue(Status.fromString("OTHER"))); + Status s = Status.fromString(null); + System.out.println(s); } @Test @@ -30,6 +22,11 @@ public void declaredValuesIsNotAffectedByFromStringCalls() { assertEquals(2, Status.declaredValues.size()); } + @Test(expected = UnsupportedOperationException.class) + public void declaredValuesIsNotModifiable() { + Status.declaredValues.add(Status.fromString("UNKNOWN")); + } + @Test public void iterateDeclaredValues() { Status.declaredValues.forEach(System.out::println); @@ -37,11 +34,11 @@ public void iterateDeclaredValues() { @Test public void isNotDeclaredValue() { - assertFalse(Status.isNotDeclaredValue(Status.OPEN)); - assertFalse(Status.isNotDeclaredValue(Status.CLOSED)); - assertFalse(Status.isNotDeclaredValue(Status.fromString("OPEN"))); - assertFalse(Status.isNotDeclaredValue(Status.fromString("CLOSED"))); - assertTrue(Status.isNotDeclaredValue(Status.fromString("NOT_DECLARED_VALUE"))); - assertTrue(Status.isNotDeclaredValue(Status.fromString("OTHER"))); + assertFalse(Status.OPEN.isNotDeclaredValue()); + assertFalse(Status.CLOSED.isNotDeclaredValue()); + assertFalse(Status.fromString("OPEN").isNotDeclaredValue()); + assertFalse(Status.fromString("CLOSED").isNotDeclaredValue()); + assertTrue(Status.fromString("NOT_DECLARED_VALUE").isNotDeclaredValue()); + assertTrue(Status.fromString("OTHER").isNotDeclaredValue()); } } From 442c9680292e80d94fd8bd1ee35bc8963f424ef0 Mon Sep 17 00:00:00 2001 From: Francesco Rigotti Date: Tue, 9 Jan 2024 14:43:50 +0100 Subject: [PATCH 09/12] replaced isNotDeclaredValue with isDeclaredValue --- README.md | 8 ++++---- .../jsonschema2pojo/openenum/OpenEnumRule.java | 10 +++++----- .../jsonschema2pojo/openenum/OpenEnumRuleTest.java | 12 ++++++------ .../swisspush/jsonschema2pojo/openenum/Status.java | 6 +++--- .../jsonschema2pojo/openenum/StatusTest.java | 14 +++++++------- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 0d04e01..decf6f7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This allows backward compatibility when JSON schemas get new enum values whereas existing code does not yet know them. Public constant `declaredValues` allows to iterate on values known at compile time. -Instance method `isNotDeclaredValue` allows to detect values not yet known at compile time to an application. +Instance method `isDeclaredValue` allows to detect values not yet known at compile time to an application. The generated code looks like: @@ -50,11 +50,11 @@ public class Status { } /** - * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * returns true if this enum is part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. * */ - public Boolean isNotDeclaredValue() { - return (!Status.declaredValues.contains(this)); + public Boolean isDeclaredValue() { + return Status.declaredValues.contains(this); } } diff --git a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java index f9b4265..4ed1196 100644 --- a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java +++ b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java @@ -58,7 +58,7 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai addConstantDeclaredValues(_enum, enumConstants); JFieldVar valueField = addValueField(_enum, backingType); addToString(_enum, valueField); - addMethodIsNotDeclaredValue(_enum); + addMethodIsDeclaredValue(_enum); return _enum; } @@ -201,12 +201,12 @@ private void addConstantDeclaredValues(JDefinedClass enumClass, List field.javadoc().add(" Use it in your application to iterate over declared values."); } - private void addMethodIsNotDeclaredValue(JDefinedClass _enum) { - JMethod method = _enum.method(JMod.PUBLIC, Boolean.class, "isNotDeclaredValue"); - JExpression toReturn = JOp.not(_enum.staticRef("declaredValues").invoke("contains").arg(JExpr._this())); + private void addMethodIsDeclaredValue(JDefinedClass _enum) { + JMethod method = _enum.method(JMod.PUBLIC, Boolean.class, "isDeclaredValue"); + JExpression toReturn = _enum.staticRef("declaredValues").invoke("contains").arg(JExpr._this()); method.body()._return(toReturn); - method.javadoc().add("returns true if this enum is NOT part of the declared values."); + method.javadoc().add("returns true if this enum is part of the declared values."); method.javadoc().add(" Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of."); } diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java index d53881d..c695944 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java @@ -80,11 +80,11 @@ public void testSimpleEnum() throws IOException { " }\n" + "\n" + " /**\n" + - " * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * returns true if this enum is part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + " * \n" + " */\n" + - " public Boolean isNotDeclaredValue() {\n" + - " return (!Status.declaredValues.contains(this));\n" + + " public Boolean isDeclaredValue() {\n" + + " return Status.declaredValues.contains(this);\n" + " }\n" + "\n" + "}" + @@ -153,11 +153,11 @@ public void testLowercaseValues() throws IOException { " }\n" + "\n" + " /**\n" + - " * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + + " * returns true if this enum is part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + " * \n" + " */\n" + - " public Boolean isNotDeclaredValue() {\n" + - " return (!Status.declaredValues.contains(this));\n" + + " public Boolean isDeclaredValue() {\n" + + " return Status.declaredValues.contains(this);\n" + " }\n" + "\n" + "}" + diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java index 9e4c055..dff19a2 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/Status.java @@ -34,10 +34,10 @@ public String toString() { } /** - * returns true if this enum is NOT part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. + * returns true if this enum is part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. * */ - public Boolean isNotDeclaredValue() { - return (!Status.declaredValues.contains(this)); + public Boolean isDeclaredValue() { + return Status.declaredValues.contains(this); } } \ No newline at end of file diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java index 7ac3288..36800f8 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java @@ -33,12 +33,12 @@ public void iterateDeclaredValues() { } @Test - public void isNotDeclaredValue() { - assertFalse(Status.OPEN.isNotDeclaredValue()); - assertFalse(Status.CLOSED.isNotDeclaredValue()); - assertFalse(Status.fromString("OPEN").isNotDeclaredValue()); - assertFalse(Status.fromString("CLOSED").isNotDeclaredValue()); - assertTrue(Status.fromString("NOT_DECLARED_VALUE").isNotDeclaredValue()); - assertTrue(Status.fromString("OTHER").isNotDeclaredValue()); + public void isDeclaredValue() { + assertTrue(Status.OPEN.isDeclaredValue()); + assertTrue(Status.CLOSED.isDeclaredValue()); + assertTrue(Status.fromString("OPEN").isDeclaredValue()); + assertTrue(Status.fromString("CLOSED").isDeclaredValue()); + assertFalse(Status.fromString("NOT_DECLARED_VALUE").isDeclaredValue()); + assertFalse(Status.fromString("OTHER").isDeclaredValue()); } } From 200e59b5c4fa88b81514660fc5b4a4d77bd9281d Mon Sep 17 00:00:00 2001 From: Francesco Rigotti Date: Tue, 9 Jan 2024 14:56:43 +0100 Subject: [PATCH 10/12] isDeclaredValue returns boolean --- README.md | 2 +- .../org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java | 2 +- .../swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java | 4 ++-- .../org/swisspush/jsonschema2pojo/openenum/StatusTest.java | 2 -- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index decf6f7..1a48d78 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ public class Status { * returns true if this enum is part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of. * */ - public Boolean isDeclaredValue() { + public boolean isDeclaredValue() { return Status.declaredValues.contains(this); } diff --git a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java index 4ed1196..b5556e7 100644 --- a/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java +++ b/src/main/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRule.java @@ -202,7 +202,7 @@ private void addConstantDeclaredValues(JDefinedClass enumClass, List } private void addMethodIsDeclaredValue(JDefinedClass _enum) { - JMethod method = _enum.method(JMod.PUBLIC, Boolean.class, "isDeclaredValue"); + JMethod method = _enum.method(JMod.PUBLIC, boolean.class, "isDeclaredValue"); JExpression toReturn = _enum.staticRef("declaredValues").invoke("contains").arg(JExpr._this()); method.body()._return(toReturn); diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java index c695944..5d85820 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/OpenEnumRuleTest.java @@ -83,7 +83,7 @@ public void testSimpleEnum() throws IOException { " * returns true if this enum is part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + " * \n" + " */\n" + - " public Boolean isDeclaredValue() {\n" + + " public boolean isDeclaredValue() {\n" + " return Status.declaredValues.contains(this);\n" + " }\n" + "\n" + @@ -156,7 +156,7 @@ public void testLowercaseValues() throws IOException { " * returns true if this enum is part of the declared values. Use it in your application to detect when values coming from outside of the app are not yet part of the declared values (i.e.: there is a new version of the enum that your application is not yet aware of.\n" + " * \n" + " */\n" + - " public Boolean isDeclaredValue() {\n" + + " public boolean isDeclaredValue() {\n" + " return Status.declaredValues.contains(this);\n" + " }\n" + "\n" + diff --git a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java index 36800f8..834c463 100644 --- a/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java +++ b/src/test/java/org/swisspush/jsonschema2pojo/openenum/StatusTest.java @@ -11,8 +11,6 @@ public void testOpenEnumPattern() { assertNotSame(Status.CLOSED, Status.OPEN); assertNotSame(Status.CLOSED, Status.fromString("OTHER")); assertEquals(Status.OPEN.toString(), "OPEN"); - Status s = Status.fromString(null); - System.out.println(s); } @Test From 6c757eadd1f0a61ea827fa2290bffc4a2498f67c Mon Sep 17 00:00:00 2001 From: "Ball, Nware" Date: Thu, 18 Jan 2024 11:57:23 +0700 Subject: [PATCH 11/12] fix deployment --- .github/workflows/maven.yml | 47 +++++++++++++++++++++------- .travis.yml | 22 ------------- maybe-release.sh | 62 ++++++++++++++++--------------------- pom.xml | 8 +++++ settings.xml | 48 ---------------------------- 5 files changed, 70 insertions(+), 117 deletions(-) delete mode 100644 .travis.yml delete mode 100644 settings.xml diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 4760699..9695b93 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -1,6 +1,10 @@ name: Java CI -on: [push] +on: + workflow_dispatch: + push: + pull_request: + branches: [ "master", "develop" ] jobs: build: @@ -8,16 +12,35 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 + - uses: actions/checkout@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 with: - java-version: 1.8 - - name: Build with Maven - run: mvn -B package --file pom.xml - - name: Deploy to Github Package Registry + java-version: '17' + distribution: 'temurin' + server-id: sonatype-nexus-staging # Value of the distributionManagement/repository/id field of the pom.xml + server-username: CI_DEPLOY_USERNAME # env variable for username in deploy + server-password: CI_DEPLOY_PASSWORD # env variable for token in deploy + gpg-private-key: ${{ secrets.CI_GPG_PRIVATE_KEY }} # Value of the GPG private key to import + gpg-passphrase: CI_GPG_PASSPHRASE # env variable for GPG private key passphrase + + + - name: Install, unit test, integration test + run: mvn install -Dmaven.javadoc.skip=true -B -V + + - name: Release to maven central + if: github.ref_name == 'master' && github.event_name != 'pull_request' && github.repository == 'swisspost/jsonschema2pojo-openenum' + run: | + curl -s get.sdkman.io | bash + source "$HOME/.sdkman/bin/sdkman-init.sh" + sdk install groovy 3.0.8 + + chmod +x ./maybe-release.sh + ./maybe-release.sh env: - GITHUB_USERNAME: x-access-token - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: - mvn --settings settings.xml deploy + CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }} + CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }} + CI_GPG_PASSPHRASE: ${{ secrets.CI_GPG_PASSPHRASE }} + + - name: After release + run: bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fc3351e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -language: java -dist: trusty -jdk: - - oraclejdk8 -before_install: - - chmod +x maybe-release.sh - # install groovy - - curl -s get.sdkman.io | bash - - source "$HOME/.sdkman/bin/sdkman-init.sh" - - sdk install groovy 3.0.8 - - echo $(groovy --version) -install: - - mvn install -Dmaven.javadoc.skip=true -B -V -before_script: - - git config --global user.email "swisspush@post.ch" - - git config --global user.name "Travis-CI" -script: - - git config credential.helper "store --file=.git/credentials" - - echo "https://${GH_TOKEN}:@github.com" > .git/credentials - - ./maybe-release.sh -after_success: - - bash <(curl -s https://codecov.io/bash) diff --git a/maybe-release.sh b/maybe-release.sh index 276dd9e..601421e 100644 --- a/maybe-release.sh +++ b/maybe-release.sh @@ -1,42 +1,34 @@ #!/bin/bash set -ev -if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_REPO_SLUG" == "swisspush/jsonschema2pojo-openenum" ] +git fetch +git reset --hard +groovy staging.groovy drop +rc=$? +if [ $rc -ne 0 ] then - git reset --hard - git clean -fd - git checkout master - echo 'Master checked out' - groovy staging.groovy drop + echo 'problem when trying to drop, ignored' +fi +echo 'starting a new nexus repository ...' +OUTPUT=$(groovy staging.groovy start) +echo "repository Id: $OUTPUT" +mvn -B -Prelease jgitflow:release-start jgitflow:release-finish -DrepositoryId=${OUTPUT} +rc=$? +if [ $rc -eq 0 ] +then + groovy staging.groovy close ${OUTPUT} + groovy staging.groovy promote ${OUTPUT} rc=$? if [ $rc -ne 0 ] then - echo 'problem when trying to drop, ignored' - fi - echo 'starting a new nexus repository ...' - OUTPUT=$(groovy staging.groovy start) - echo "repository Id: $OUTPUT" - mvn -B -Prelease jgitflow:release-start jgitflow:release-finish --settings settings.xml -DrepositoryId=${OUTPUT} - rc=$? - if [ $rc -eq 0 ] - then - groovy staging.groovy close ${OUTPUT} - groovy staging.groovy promote ${OUTPUT} - rc=$? - if [ $rc -ne 0 ] - then - echo 'Release failed, cannot promote stage' - exit rc - fi - echo 'Release done, will push' - git tag - git push --tags - git checkout develop - git push origin develop - exit 0 + echo 'Release failed, cannot promote stage' + exit $rc fi - echo 'Release failed' - exit rc -else - echo 'Release skipped' - exit 0 -fi \ No newline at end of file + echo 'Release done, will push' + git tag + git push --tags + git checkout develop + git push origin develop + exit 0 +fi +echo 'Release failed' +exit $rc diff --git a/pom.xml b/pom.xml index a7fac70..becd93f 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,10 @@ ${session.executionRootDirectory} 230584D7 + + --pinentry-mode + loopback + @@ -224,6 +228,10 @@ ${session.executionRootDirectory} 230584D7 + + --pinentry-mode + loopback + diff --git a/settings.xml b/settings.xml deleted file mode 100644 index 1bb3076..0000000 --- a/settings.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - github - - - - - github - - - central - https://repo1.maven.org/maven2 - - true - - - true - - - - github - GitHub OWNER Apache Maven Packages - https://maven.pkg.github.com/swisspush - - - - - - - - sonatype-nexus-staging - ${env.CI_DEPLOY_USERNAME} - ${env.CI_DEPLOY_PASSWORD} - - - gpg.passphrase - ${env.CI_PGP_PASSWORD} - - - github - ${env.GITHUB_USERNAME} - ${env.GITHUB_TOKEN} - - - From 640abe02cee788cb4a53cb5cba267e0254390a98 Mon Sep 17 00:00:00 2001 From: "Ball, Nware" Date: Thu, 18 Jan 2024 13:08:44 +0700 Subject: [PATCH 12/12] run with java 8 same as travis script --- .github/workflows/maven.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 9695b93..f557afb 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -13,10 +13,10 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Set up JDK 17 + - name: Set up JDK 8 uses: actions/setup-java@v3 with: - java-version: '17' + java-version: '8' distribution: 'temurin' server-id: sonatype-nexus-staging # Value of the distributionManagement/repository/id field of the pom.xml server-username: CI_DEPLOY_USERNAME # env variable for username in deploy