diff --git a/jpos/src/main/java/org/jpos/q2/QFactory.java b/jpos/src/main/java/org/jpos/q2/QFactory.java index c031cbc48a..328f332b07 100644 --- a/jpos/src/main/java/org/jpos/q2/QFactory.java +++ b/jpos/src/main/java/org/jpos/q2/QFactory.java @@ -465,6 +465,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 2c762672c9..3be99a373e 100644 --- a/jpos/src/test/java/org/jpos/q2/QFactory2Test.java +++ b/jpos/src/test/java/org/jpos/q2/QFactory2Test.java @@ -32,7 +32,10 @@ import org.jdom2.Attribute; import org.jdom2.Element; +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; @@ -308,4 +311,20 @@ public void testStartQBeanThrowsNullPointerException() throws Throwable { 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 { + } }