From de7b440edf0f908139519f4174a662b3f11915e5 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 11 Apr 2017 21:06:58 +0200 Subject: [PATCH] DefaultEnumParameterMapMapper + BeanValidation in JUnit Tests zur Vorbereitung fuer MemberShareVO --- cust-services/pom.xml | 7 ++- framework/pom.xml | 55 +++++++++++-------- .../java/de/hsadmin/module/ValueObject.java | 3 +- .../hsadmin/module/impl/AbstractProperty.java | 22 +++++++- .../de/hsadmin/module/impl/AbstractVO.java | 19 ++++++- .../de/hsadmin/module/property/Property.java | 3 +- .../DefaultBooleanParameterMapMapper.java | 2 +- .../DefaultDateParameterMapMapper.java | 2 +- .../DefaultEnumParameterMapMapper.java | 38 +++++++++++++ .../DefaultIntegerParameterMapMapper.java | 4 +- .../DefaultListParameterMapMapper.java | 3 +- .../DefaultStringParameterMapMapper.java | 3 +- .../DefaultStringSetParameterMapMapper.java | 2 +- .../property/mapping/ParameterMapMapper.java | 2 +- .../DefaultEnumParameterMapMapperTest.java | 32 +++++++++++ pom.xml | 8 +++ 16 files changed, 168 insertions(+), 37 deletions(-) create mode 100644 framework/src/main/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapper.java create mode 100644 framework/src/test/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapperTest.java diff --git a/cust-services/pom.xml b/cust-services/pom.xml index 357f99a..297f2f8 100644 --- a/cust-services/pom.xml +++ b/cust-services/pom.xml @@ -33,7 +33,12 @@ org.apache.geronimo.specs geronimo-validation_1.0_spec 1.1 - provided + + + org.apache.bval + org.apache.bval.bundle + 0.5 + test org.apache.xmlrpc diff --git a/framework/pom.xml b/framework/pom.xml index fc15927..bbe0b37 100644 --- a/framework/pom.xml +++ b/framework/pom.xml @@ -29,32 +29,43 @@ - org.apache.geronimo.specs - geronimo-jpa_2.0_spec - 1.1 - provided - - - org.apache.geronimo.specs - geronimo-ejb_3.0_spec - 1.0.1 - provided - - - org.apache.geronimo.specs - geronimo-validation_1.0_spec - 1.1 - provided - - - org.apache.xmlrpc - xmlrpc-server - 3.1.3 - + org.apache.geronimo.specs + geronimo-jpa_2.0_spec + 1.1 + provided + + + org.apache.geronimo.specs + geronimo-ejb_3.0_spec + 1.0.1 + provided + + + org.apache.geronimo.specs + geronimo-validation_1.0_spec + 1.1 + + + org.apache.bval + org.apache.bval.bundle + 0.5 + test + + + org.apache.xmlrpc + xmlrpc-server + 3.1.3 + log4j log4j 1.2.17 + + joda-time + joda-time + 2.9.9 + + diff --git a/framework/src/main/java/de/hsadmin/module/ValueObject.java b/framework/src/main/java/de/hsadmin/module/ValueObject.java index 403b49f..ca6bb7a 100644 --- a/framework/src/main/java/de/hsadmin/module/ValueObject.java +++ b/framework/src/main/java/de/hsadmin/module/ValueObject.java @@ -12,6 +12,8 @@ public interface ValueObject { public Property get(String propertyName) throws UserException; + public Class getType(String name) throws TechnicalException; + public boolean hasProperty(String propertyName) throws UserException; public void copyPropertiesToPersistentObject(Object persistentObject) @@ -19,5 +21,4 @@ public interface ValueObject { public void copyPropertiesFromPersistentObject(Object persistentObject) throws UserException, TechnicalException; - } diff --git a/framework/src/main/java/de/hsadmin/module/impl/AbstractProperty.java b/framework/src/main/java/de/hsadmin/module/impl/AbstractProperty.java index a8e0ab2..d8d582a 100644 --- a/framework/src/main/java/de/hsadmin/module/impl/AbstractProperty.java +++ b/framework/src/main/java/de/hsadmin/module/impl/AbstractProperty.java @@ -87,13 +87,12 @@ public abstract class AbstractProperty implements Property { return (Class>) getClass(); } - @SuppressWarnings("unchecked") @Override public T getValue() throws TechnicalException { if (undefinedValue) { throw new TechnicalException("undefined value"); } - return (T) ReflectionUtil.invokeGetter(owningVO, getName()); + return getValueImpl(); } @Override @@ -140,7 +139,24 @@ public abstract class AbstractProperty implements Property { @Override public void copyValueFromParameterMap(Map rpcParameter) throws TechnicalException, UserException { - setValue(getParameterMapMapper().readValueFromParameterMap(rpcParameter, getName())); + setValue(getParameterMapMapper().readValueFromParameterMap(rpcParameter, getName(), owningVO.getType(getName()))); } + @Override + public void initValueFromVO() throws TechnicalException, UserException { + setValue(getValueImpl()); + } + + protected Class getFieldType() throws TechnicalException { + try { + return owningVO.getClass().getDeclaredField(getName()).getType(); + } catch (NoSuchFieldException | SecurityException e) { + throw new TechnicalException(e); + } + } + + @SuppressWarnings("unchecked") + private T getValueImpl() throws TechnicalException { + return (T) ReflectionUtil.invokeGetter(owningVO, getName()); + } } diff --git a/framework/src/main/java/de/hsadmin/module/impl/AbstractVO.java b/framework/src/main/java/de/hsadmin/module/impl/AbstractVO.java index 6326160..aaa0476 100644 --- a/framework/src/main/java/de/hsadmin/module/impl/AbstractVO.java +++ b/framework/src/main/java/de/hsadmin/module/impl/AbstractVO.java @@ -38,6 +38,15 @@ public abstract class AbstractVO implements ValueObject { } } } + + public void initPropertyValues() throws UserException, TechnicalException { + final Field[] fields = getClass().getDeclaredFields(); + for (Field f : fields) { + if (isPropertyField(f)) { + get(f.getName()).initValueFromVO(); + } + } + } @Override public List> properties() throws UserException, TechnicalException { @@ -61,6 +70,15 @@ public abstract class AbstractVO implements ValueObject { return propertiesMap.get(propertyName); } + @Override + public Class getType(String propertyName) throws TechnicalException { + try { + return getClass().getDeclaredField(propertyName).getType(); + } catch (NoSuchFieldException | SecurityException e) { + throw new TechnicalException(e); + } + } + @Override public boolean hasProperty(final String propertyName) throws UserException { assert propertyName != null && propertyName.length() > 0; @@ -164,5 +182,4 @@ public abstract class AbstractVO implements ValueObject { } return rwPolicy; } - } diff --git a/framework/src/main/java/de/hsadmin/module/property/Property.java b/framework/src/main/java/de/hsadmin/module/property/Property.java index 6e28d6a..393bd26 100644 --- a/framework/src/main/java/de/hsadmin/module/property/Property.java +++ b/framework/src/main/java/de/hsadmin/module/property/Property.java @@ -34,7 +34,8 @@ public interface Property { public void copyValueToParameterMap(final Map rpcParameter) throws TechnicalException, UserException; public void copyValueFromParameterMap(final Map rpcParameter) throws TechnicalException, UserException; - + + public void initValueFromVO() throws TechnicalException, UserException; } diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultBooleanParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultBooleanParameterMapMapper.java index 741d6b0..47b2309 100644 --- a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultBooleanParameterMapMapper.java +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultBooleanParameterMapMapper.java @@ -19,7 +19,7 @@ public class DefaultBooleanParameterMapMapper implements @Override public Boolean readValueFromParameterMap(final Map rpcParameter, - final String propertyName) throws TechnicalException, UserException { + final String propertyName, final Class propertyClass) throws TechnicalException, UserException { final Object value = rpcParameter.get(propertyName); if (value instanceof Boolean) { return (Boolean) value; diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultDateParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultDateParameterMapMapper.java index 7c77c98..537fb47 100644 --- a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultDateParameterMapMapper.java +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultDateParameterMapMapper.java @@ -22,7 +22,7 @@ public class DefaultDateParameterMapMapper implements ParameterMapMapper { @Override public Date readValueFromParameterMap(final Map rpcParameter, - final String propertyName) throws TechnicalException, UserException { + final String propertyName, final Class propertyClass) throws TechnicalException, UserException { final Object value = rpcParameter.get(propertyName); if (value instanceof Date) { return (Date) value; diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapper.java new file mode 100644 index 0000000..0f96e3c --- /dev/null +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapper.java @@ -0,0 +1,38 @@ +package de.hsadmin.module.property.mapping; + +import java.util.Map; + +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.common.error.UserError; +import de.hsadmin.common.error.UserException; + +public class DefaultEnumParameterMapMapper implements ParameterMapMapper> { + + @Override + public void writeValueToParameterMap(final Map rpcParameter, + final String propertyName, Enum value) + throws TechnicalException, UserException { + if (value != null) { + rpcParameter.put(propertyName, value.toString()); + } + } + + @Override + @SuppressWarnings("unchecked") + public Enum readValueFromParameterMap(Map rpcParameter, + final String propertyName, final Class propertyClass) + throws TechnicalException, UserException { + final Object value = rpcParameter.get(propertyName); + if (value instanceof Enum) { + return (Enum) value; + } + if (value instanceof String) { + try { + return Enum.valueOf(propertyClass.asSubclass(Enum.class), (String) value); + } catch (NumberFormatException e) { + throw new UserException(new UserError(UserError.MSG_INT_VALUE_EXPECTED, propertyName, (String) value)); + } + } + return null; + } +} \ No newline at end of file diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultIntegerParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultIntegerParameterMapMapper.java index b19b343..b16a163 100644 --- a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultIntegerParameterMapMapper.java +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultIntegerParameterMapMapper.java @@ -10,7 +10,7 @@ public class DefaultIntegerParameterMapMapper implements ParameterMapMapper rpcParameter, - final String propertyName, Integer value) + final String propertyName, final Integer value) throws TechnicalException, UserException { if (value != null) { rpcParameter.put(propertyName, value.toString()); @@ -19,7 +19,7 @@ public class DefaultIntegerParameterMapMapper implements ParameterMapMapper rpcParameter, - final String propertyName) + final String propertyName, final Class propertyClass) throws TechnicalException, UserException { final Object value = rpcParameter.get(propertyName); if (value instanceof Integer) { diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultListParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultListParameterMapMapper.java index 2148e82..6c5b2c5 100644 --- a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultListParameterMapMapper.java +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultListParameterMapMapper.java @@ -37,7 +37,8 @@ public class DefaultListParameterMapMapper implements Pa @SuppressWarnings("unchecked") @Override - public List readValueFromParameterMap(Map rpcParameter, String propertyName) + public List readValueFromParameterMap(final Map rpcParameter, + final String propertyName, final Class propertyClass) throws TechnicalException, UserException { final List value = new ArrayList<>(); final Object list = rpcParameter.get(propertyName); diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringParameterMapMapper.java index 27943e0..5ad2474 100644 --- a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringParameterMapMapper.java +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringParameterMapMapper.java @@ -17,7 +17,8 @@ public class DefaultStringParameterMapMapper implements ParameterMapMapper rpcParameter, - final String propertyName) throws TechnicalException, UserException { + final String propertyName, final Class propertyClass) + throws TechnicalException, UserException { final Object value = rpcParameter.get(propertyName); if (value instanceof String) { return (String) value; diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringSetParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringSetParameterMapMapper.java index b581926..fb6c1b6 100644 --- a/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringSetParameterMapMapper.java +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/DefaultStringSetParameterMapMapper.java @@ -22,7 +22,7 @@ public class DefaultStringSetParameterMapMapper implements @Override public StringSet readValueFromParameterMap( - final Map rpcParameter, final String propertyName) + final Map rpcParameter, final String propertyName, final Class propertyClass) throws TechnicalException, UserException { final Object rpcParam = rpcParameter.get(propertyName); final StringSet value = new StringSet(); diff --git a/framework/src/main/java/de/hsadmin/module/property/mapping/ParameterMapMapper.java b/framework/src/main/java/de/hsadmin/module/property/mapping/ParameterMapMapper.java index 36996cb..6847793 100644 --- a/framework/src/main/java/de/hsadmin/module/property/mapping/ParameterMapMapper.java +++ b/framework/src/main/java/de/hsadmin/module/property/mapping/ParameterMapMapper.java @@ -9,6 +9,6 @@ public interface ParameterMapMapper { void writeValueToParameterMap(Map rpcParameter, String propertyName, T value) throws TechnicalException, UserException; - T readValueFromParameterMap(Map rpcParameter, String propertyName) throws TechnicalException, UserException; + T readValueFromParameterMap(Map rpcParameter, String propertyName, Class propertyClass) throws TechnicalException, UserException; } diff --git a/framework/src/test/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapperTest.java b/framework/src/test/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapperTest.java new file mode 100644 index 0000000..3e6c671 --- /dev/null +++ b/framework/src/test/java/de/hsadmin/module/property/mapping/DefaultEnumParameterMapMapperTest.java @@ -0,0 +1,32 @@ +package de.hsadmin.module.property.mapping; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.common.error.UserException; + +public class DefaultEnumParameterMapMapperTest { + + private static enum SomeEnum { A, B }; + + private static final Map rpcParameters = new HashMap(); + static { + rpcParameters.put("testA", "A"); + } + + @Test + public void readValueFromParameterMap() throws TechnicalException, UserException{ + assertEquals(SomeEnum.A, new DefaultEnumParameterMapMapper().readValueFromParameterMap(rpcParameters, "testA", SomeEnum.class)); + } + + @Test + public void writeValueToParameterMap() throws TechnicalException, UserException{ + new DefaultEnumParameterMapMapper().writeValueToParameterMap(rpcParameters, "testB", SomeEnum.B); + assertEquals("B", rpcParameters.get("testB")); + } +} diff --git a/pom.xml b/pom.xml index b4997f7..d1cbf33 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,8 @@ 1.7 1.7 + true + true @@ -41,5 +43,11 @@ 4.12 test + + pl.pragmatists + JUnitParams + 1.0.4 + test +