Skip to content

Commit

Permalink
Add the chance to autoconfigure enum fields
Browse files Browse the repository at this point in the history
Signed-off-by: Andrés Alcarraz <[email protected]>
  • Loading branch information
alcarraz authored and ar committed Jun 18, 2024
1 parent 745b257 commit c582ef6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 2 additions & 0 deletions jpos/src/main/java/org/jpos/q2/QFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Enum>) c, v));
else if (c.isArray()) {
Class<?> ct = c.getComponentType();
if (ct.isAssignableFrom(String.class))
Expand Down
27 changes: 20 additions & 7 deletions jpos/src/test/java/org/jpos/core/ConfigAnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,24 @@ 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());
assertEquals(10000L, bean.getMylong());
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;
Expand All @@ -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;
}
Expand All @@ -129,6 +136,10 @@ public double getMydouble() {

public boolean isMyboolean() {return myboolean; }

public MyEnum getMyenum() {
return myenum;
}

public String[] getMyarray() {
return myarray;
}
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions jpos/src/test/java/org/jpos/q2/QFactory2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
}
}

0 comments on commit c582ef6

Please sign in to comment.