Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the chance to autoconfigure enum fields #600

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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<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
19 changes: 19 additions & 0 deletions jpos/src/test/java/org/jpos/q2/QFactory2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
}
}
Loading