DefaultEnumParameterMapMapper + BeanValidation in JUnit Tests zur Vorbereitung fuer MemberShareVO
This commit is contained in:
parent
69f3721e98
commit
de7b440edf
@ -33,7 +33,12 @@
|
|||||||
<groupId>org.apache.geronimo.specs</groupId>
|
<groupId>org.apache.geronimo.specs</groupId>
|
||||||
<artifactId>geronimo-validation_1.0_spec</artifactId>
|
<artifactId>geronimo-validation_1.0_spec</artifactId>
|
||||||
<version>1.1</version>
|
<version>1.1</version>
|
||||||
<scope>provided</scope>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.bval</groupId>
|
||||||
|
<artifactId>org.apache.bval.bundle</artifactId>
|
||||||
|
<version>0.5</version>
|
||||||
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.xmlrpc</groupId>
|
<groupId>org.apache.xmlrpc</groupId>
|
||||||
|
@ -29,32 +29,43 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.geronimo.specs</groupId>
|
<groupId>org.apache.geronimo.specs</groupId>
|
||||||
<artifactId>geronimo-jpa_2.0_spec</artifactId>
|
<artifactId>geronimo-jpa_2.0_spec</artifactId>
|
||||||
<version>1.1</version>
|
<version>1.1</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.geronimo.specs</groupId>
|
<groupId>org.apache.geronimo.specs</groupId>
|
||||||
<artifactId>geronimo-ejb_3.0_spec</artifactId>
|
<artifactId>geronimo-ejb_3.0_spec</artifactId>
|
||||||
<version>1.0.1</version>
|
<version>1.0.1</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.geronimo.specs</groupId>
|
<groupId>org.apache.geronimo.specs</groupId>
|
||||||
<artifactId>geronimo-validation_1.0_spec</artifactId>
|
<artifactId>geronimo-validation_1.0_spec</artifactId>
|
||||||
<version>1.1</version>
|
<version>1.1</version>
|
||||||
<scope>provided</scope>
|
</dependency>
|
||||||
</dependency>
|
<dependency>
|
||||||
<dependency>
|
<groupId>org.apache.bval</groupId>
|
||||||
<groupId>org.apache.xmlrpc</groupId>
|
<artifactId>org.apache.bval.bundle</artifactId>
|
||||||
<artifactId>xmlrpc-server</artifactId>
|
<version>0.5</version>
|
||||||
<version>3.1.3</version>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.xmlrpc</groupId>
|
||||||
|
<artifactId>xmlrpc-server</artifactId>
|
||||||
|
<version>3.1.3</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>1.2.17</version>
|
<version>1.2.17</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>joda-time</groupId>
|
||||||
|
<artifactId>joda-time</artifactId>
|
||||||
|
<version>2.9.9</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@ public interface ValueObject {
|
|||||||
|
|
||||||
public Property<?> get(String propertyName) throws UserException;
|
public Property<?> get(String propertyName) throws UserException;
|
||||||
|
|
||||||
|
public Class<?> getType(String name) throws TechnicalException;
|
||||||
|
|
||||||
public boolean hasProperty(String propertyName) throws UserException;
|
public boolean hasProperty(String propertyName) throws UserException;
|
||||||
|
|
||||||
public void copyPropertiesToPersistentObject(Object persistentObject)
|
public void copyPropertiesToPersistentObject(Object persistentObject)
|
||||||
@ -19,5 +21,4 @@ public interface ValueObject {
|
|||||||
|
|
||||||
public void copyPropertiesFromPersistentObject(Object persistentObject)
|
public void copyPropertiesFromPersistentObject(Object persistentObject)
|
||||||
throws UserException, TechnicalException;
|
throws UserException, TechnicalException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -87,13 +87,12 @@ public abstract class AbstractProperty<T> implements Property<T> {
|
|||||||
return (Class<? extends Property<T>>) getClass();
|
return (Class<? extends Property<T>>) getClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public T getValue() throws TechnicalException {
|
public T getValue() throws TechnicalException {
|
||||||
if (undefinedValue) {
|
if (undefinedValue) {
|
||||||
throw new TechnicalException("undefined value");
|
throw new TechnicalException("undefined value");
|
||||||
}
|
}
|
||||||
return (T) ReflectionUtil.invokeGetter(owningVO, getName());
|
return getValueImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -140,7 +139,24 @@ public abstract class AbstractProperty<T> implements Property<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void copyValueFromParameterMap(Map<String, Object> rpcParameter) throws TechnicalException, UserException {
|
public void copyValueFromParameterMap(Map<String, Object> 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,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
|
@Override
|
||||||
public List<Property<?>> properties() throws UserException, TechnicalException {
|
public List<Property<?>> properties() throws UserException, TechnicalException {
|
||||||
final List<Property<?>> result = new ArrayList<>();
|
final List<Property<?>> result = new ArrayList<>();
|
||||||
@ -61,6 +70,15 @@ public abstract class AbstractVO implements ValueObject {
|
|||||||
return propertiesMap.get(propertyName);
|
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
|
@Override
|
||||||
public boolean hasProperty(final String propertyName) throws UserException {
|
public boolean hasProperty(final String propertyName) throws UserException {
|
||||||
assert propertyName != null && propertyName.length() > 0;
|
assert propertyName != null && propertyName.length() > 0;
|
||||||
@ -164,5 +182,4 @@ public abstract class AbstractVO implements ValueObject {
|
|||||||
}
|
}
|
||||||
return rwPolicy;
|
return rwPolicy;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ public interface Property<T> {
|
|||||||
|
|
||||||
public void copyValueFromParameterMap(final Map<String, Object> rpcParameter) throws TechnicalException, UserException;
|
public void copyValueFromParameterMap(final Map<String, Object> rpcParameter) throws TechnicalException, UserException;
|
||||||
|
|
||||||
|
public void initValueFromVO() throws TechnicalException, UserException;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public class DefaultBooleanParameterMapMapper implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
public Boolean readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
||||||
final String propertyName) throws TechnicalException, UserException {
|
final String propertyName, final Class<?> propertyClass) throws TechnicalException, UserException {
|
||||||
final Object value = rpcParameter.get(propertyName);
|
final Object value = rpcParameter.get(propertyName);
|
||||||
if (value instanceof Boolean) {
|
if (value instanceof Boolean) {
|
||||||
return (Boolean) value;
|
return (Boolean) value;
|
||||||
|
@ -22,7 +22,7 @@ public class DefaultDateParameterMapMapper implements ParameterMapMapper<Date> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Date readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
public Date readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
||||||
final String propertyName) throws TechnicalException, UserException {
|
final String propertyName, final Class<?> propertyClass) throws TechnicalException, UserException {
|
||||||
final Object value = rpcParameter.get(propertyName);
|
final Object value = rpcParameter.get(propertyName);
|
||||||
if (value instanceof Date) {
|
if (value instanceof Date) {
|
||||||
return (Date) value;
|
return (Date) value;
|
||||||
|
@ -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<Enum<?>> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeValueToParameterMap(final Map<String, Object> rpcParameter,
|
||||||
|
final String propertyName, Enum<?> value)
|
||||||
|
throws TechnicalException, UserException {
|
||||||
|
if (value != null) {
|
||||||
|
rpcParameter.put(propertyName, value.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public Enum<?> readValueFromParameterMap(Map<String, Object> 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;
|
||||||
|
}
|
||||||
|
}
|
@ -10,7 +10,7 @@ public class DefaultIntegerParameterMapMapper implements ParameterMapMapper<Inte
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeValueToParameterMap(final Map<String, Object> rpcParameter,
|
public void writeValueToParameterMap(final Map<String, Object> rpcParameter,
|
||||||
final String propertyName, Integer value)
|
final String propertyName, final Integer value)
|
||||||
throws TechnicalException, UserException {
|
throws TechnicalException, UserException {
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
rpcParameter.put(propertyName, value.toString());
|
rpcParameter.put(propertyName, value.toString());
|
||||||
@ -19,7 +19,7 @@ public class DefaultIntegerParameterMapMapper implements ParameterMapMapper<Inte
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
public Integer readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
||||||
final String propertyName)
|
final String propertyName, final Class<?> propertyClass)
|
||||||
throws TechnicalException, UserException {
|
throws TechnicalException, UserException {
|
||||||
final Object value = rpcParameter.get(propertyName);
|
final Object value = rpcParameter.get(propertyName);
|
||||||
if (value instanceof Integer) {
|
if (value instanceof Integer) {
|
||||||
|
@ -37,7 +37,8 @@ public class DefaultListParameterMapMapper<VO extends ValueObject> implements Pa
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public List<VO> readValueFromParameterMap(Map<String, Object> rpcParameter, String propertyName)
|
public List<VO> readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
||||||
|
final String propertyName, final Class<?> propertyClass)
|
||||||
throws TechnicalException, UserException {
|
throws TechnicalException, UserException {
|
||||||
final List<VO> value = new ArrayList<>();
|
final List<VO> value = new ArrayList<>();
|
||||||
final Object list = rpcParameter.get(propertyName);
|
final Object list = rpcParameter.get(propertyName);
|
||||||
|
@ -17,7 +17,8 @@ public class DefaultStringParameterMapMapper implements ParameterMapMapper<Strin
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
public String readValueFromParameterMap(final Map<String, Object> rpcParameter,
|
||||||
final String propertyName) throws TechnicalException, UserException {
|
final String propertyName, final Class<?> propertyClass)
|
||||||
|
throws TechnicalException, UserException {
|
||||||
final Object value = rpcParameter.get(propertyName);
|
final Object value = rpcParameter.get(propertyName);
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
return (String) value;
|
return (String) value;
|
||||||
|
@ -22,7 +22,7 @@ public class DefaultStringSetParameterMapMapper implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StringSet readValueFromParameterMap(
|
public StringSet readValueFromParameterMap(
|
||||||
final Map<String, Object> rpcParameter, final String propertyName)
|
final Map<String, Object> rpcParameter, final String propertyName, final Class<?> propertyClass)
|
||||||
throws TechnicalException, UserException {
|
throws TechnicalException, UserException {
|
||||||
final Object rpcParam = rpcParameter.get(propertyName);
|
final Object rpcParam = rpcParameter.get(propertyName);
|
||||||
final StringSet value = new StringSet();
|
final StringSet value = new StringSet();
|
||||||
|
@ -9,6 +9,6 @@ public interface ParameterMapMapper<T> {
|
|||||||
|
|
||||||
void writeValueToParameterMap(Map<String, Object> rpcParameter, String propertyName, T value) throws TechnicalException, UserException;
|
void writeValueToParameterMap(Map<String, Object> rpcParameter, String propertyName, T value) throws TechnicalException, UserException;
|
||||||
|
|
||||||
T readValueFromParameterMap(Map<String, Object> rpcParameter, String propertyName) throws TechnicalException, UserException;
|
T readValueFromParameterMap(Map<String, Object> rpcParameter, String propertyName, Class<?> propertyClass) throws TechnicalException, UserException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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<String, Object> rpcParameters = new HashMap<String, Object>();
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
}
|
8
pom.xml
8
pom.xml
@ -28,6 +28,8 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<source>1.7</source>
|
<source>1.7</source>
|
||||||
<target>1.7</target>
|
<target>1.7</target>
|
||||||
|
<downloadSources>true</downloadSources>
|
||||||
|
<downloadJavadocs>true</downloadJavadocs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
@ -41,5 +43,11 @@
|
|||||||
<version>4.12</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>pl.pragmatists</groupId>
|
||||||
|
<artifactId>JUnitParams</artifactId>
|
||||||
|
<version>1.0.4</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
Loading…
Reference in New Issue
Block a user