From c582ef6950142b22ac59de30f6e02dcff6df8b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alcarraz?= Date: Tue, 18 Jun 2024 11:36:33 -0500 Subject: [PATCH] Add the chance to autoconfigure enum fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrés Alcarraz --- jpos/src/main/java/org/jpos/q2/QFactory.java | 2 ++ .../org/jpos/core/ConfigAnnotationTest.java | 27 ++++++++++++++----- .../test/java/org/jpos/q2/QFactory2Test.java | 20 ++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/jpos/src/main/java/org/jpos/q2/QFactory.java b/jpos/src/main/java/org/jpos/q2/QFactory.java index 5de6dc327c..3928c38e95 100644 --- a/jpos/src/main/java/org/jpos/q2/QFactory.java +++ b/jpos/src/main/java/org/jpos/q2/QFactory.java @@ -467,6 +467,8 @@ else if (c.isAssignableFrom(double.class) || c.isAssignableFrom(Double.class)) field.set(obj, cfg.getDouble(config.value())); else if (c.isAssignableFrom(boolean.class) || c.isAssignableFrom(Boolean.class)) field.set(obj, cfg.getBoolean(config.value())); + else if (c.isEnum()) + field.set(obj, Enum.valueOf((Class) c, v)); else if (c.isArray()) { Class ct = c.getComponentType(); if (ct.isAssignableFrom(String.class)) diff --git a/jpos/src/test/java/org/jpos/core/ConfigAnnotationTest.java b/jpos/src/test/java/org/jpos/core/ConfigAnnotationTest.java index 1306071f8c..3713b8902f 100644 --- a/jpos/src/test/java/org/jpos/core/ConfigAnnotationTest.java +++ b/jpos/src/test/java/org/jpos/core/ConfigAnnotationTest.java @@ -69,6 +69,7 @@ public void testChildConfig() throws ConfigurationException, IllegalAccessExcept cfg.put("myints", new String[] {"1", "2"}); cfg.put("mylongs", new String[] {"1", "2", "3"}); cfg.put("mydoubles", new String[] {"1.1", "2.2", "3.3"}); + cfg.put("myenum", MyEnum.ONE.name()); QFactory.autoconfigure(bean, cfg); assertEquals("My String", bean.getMystring()); assertEquals(1000, bean.getMyint()); @@ -76,13 +77,16 @@ public void testChildConfig() throws ConfigurationException, IllegalAccessExcept assertThat("mydouble should have the configured value", bean.getMydouble(), is(1000.1)); assertEquals("My Child String", bean.getChildString()); assertTrue(bean.isMyboolean()); + assertThat("myenum should be ONE", bean.getMyenum(), is(MyEnum.ONE)); assertThat("myarray should have the configured values", bean.getMyarray(), is(new String[]{"one", "two"})); assertThat("myints should have the configured values", bean.getMyints(), is(new int[]{1, 2})); assertThat("mylongs should have the configured values", bean.getMylongs(), is(new long[]{1, 2, 3})); assertThat("mydoubles should have the configured values", bean.getMydoubles(), is(new double[]{1.1, 2.2, 3.3})); } - + enum MyEnum { + ONE, TWO, THREE + } public static class MyAutoConfigurable { @Config("mystring") private String mystring; @@ -108,9 +112,12 @@ public static class MyAutoConfigurable { @Config("mylongs") private long[] mylongs; + @Config("myenum") + private MyEnum myenum; + @Config("mydoubles") private double[] mydoubles; - + public String getMystring() { return mystring; } @@ -129,6 +136,10 @@ public double getMydouble() { public boolean isMyboolean() {return myboolean; } + public MyEnum getMyenum() { + return myenum; + } + public String[] getMyarray() { return myarray; } @@ -148,12 +159,14 @@ public double[] getMydoubles() { @Override public String toString() { return "MyAutoConfigurable{" + - "mystring='" + mystring + '\'' + - ", myint=" + myint + - ", mylong=" + mylong + - ", myarray=[" + String.join(", ", myarray)+"]" + - '}'; + "mystring='" + mystring + '\'' + + ", myint=" + myint + + ", mylong=" + mylong + + ", myenum=" + myenum + + ", myarray=[" + String.join(", ", myarray) + "]" + + '}'; } + } public static class MyChildAutoConfigurable extends MyAutoConfigurable { diff --git a/jpos/src/test/java/org/jpos/q2/QFactory2Test.java b/jpos/src/test/java/org/jpos/q2/QFactory2Test.java index a07e5c45ae..fd33edcdd1 100644 --- a/jpos/src/test/java/org/jpos/q2/QFactory2Test.java +++ b/jpos/src/test/java/org/jpos/q2/QFactory2Test.java @@ -36,6 +36,10 @@ import org.jdom2.Attribute; import org.jdom2.Element; import org.jdom2.Text; +import org.jpos.core.Configurable; +import org.jpos.core.Configuration; +import org.jpos.core.ConfigurationException; +import org.jpos.core.annotation.Config; import org.jpos.iso.ISOFieldValidator; import org.jpos.iso.IVA_ALPHANUM; import org.jpos.q2.iso.ChannelAdaptor; @@ -499,4 +503,20 @@ void testInstantiateWithVerbatim() throws ReflectionException, InstanceNotFoundE q2.stop(); } + enum QFactoryTestEnum { + TEST1, TEST2, TEST3 + } + + class EnumConfigurable implements Configurable { + @Config("enum") + QFactoryTestEnum testEnum; + @Override + public void setConfiguration(Configuration cfg) throws ConfigurationException { + + } + } + + @Test + public void testAutoconfigureEnum() throws Throwable { + } }