Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
abuttaro committed Feb 6, 2018
1 parent 4628ba4 commit 7a86251
Show file tree
Hide file tree
Showing 30 changed files with 609 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private <T> void writeValue(MappedField field, Object instance, Object value) {
field.writeValue(instance, value);
}

private <T> Object collectValues(MappedField<T> field, ParameterProvider parameterProvider, Errors errors) {
<T> Object collectValues(MappedField<T> field, ParameterProvider parameterProvider, Errors errors) {
Object result = null;
String firstParameterNameForErrorDetail = null;
Collection<Object> collection = null;
Expand Down Expand Up @@ -101,7 +101,7 @@ private <T> Object collectValues(MappedField<T> field, ParameterProvider paramet
}

@SuppressWarnings({"unchecked", "rawtypes"})
private <T> Object convert(String parameterName, MappedField<T> field, Errors errors, String value) {
<T> Object convert(String parameterName, MappedField<T> field, Errors errors, String value) {
ParameterConverter converter = parameterConverterFactory.getConverter(field.getType());
if (converter != null) {
return converter.convert(parameterName, value, errors);
Expand All @@ -110,7 +110,7 @@ private <T> Object convert(String parameterName, MappedField<T> field, Errors er
}

@SuppressWarnings({"unchecked", "rawtypes"})
private <T> Collection<Object> getApplicableCollectionInstance(MappedField<T> field, Collection<Object> collection) {
<T> Collection<Object> getApplicableCollectionInstance(MappedField<T> field, Collection<Object> collection) {
if (collection != null) {
return collection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public MappedField<T> build() {
readable = ReflectMappedMethod.of(getter, setter);
} else if (getter != null) {
readable = ReflectReadableMappedMethod.of(getter);
} else if (writable == null) {
} else {
// if writable is explicitly configured (not null) then
// we will use setter or used default MapField readable
if (setter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public interface WritableField<TARGET, VALUE> {
*/
TARGET createDeclaringInstance();

VALUE createInstance();
/**
* Create an instance of a container object of the field.
* @return
*/
default VALUE createInstance() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private Builder me() {
return this;
}

public Builder monAutoGeneratedValuePermitted(boolean nonAutoGeneratedValuePermitted) {
public Builder nonAutoGeneratedValuePermitted(boolean nonAutoGeneratedValuePermitted) {
this.nonAutoGeneratedValuePermitted = nonAutoGeneratedValuePermitted;
return me();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.github.restup.mapping.fields.composition;

import java.lang.reflect.Field;
import java.util.Objects;
import com.github.restup.errors.ErrorBuilder;
import com.github.restup.mapping.fields.ReadWriteField;
import com.github.restup.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.Objects;

public class ReflectMappedField<TARGET, VALUE> implements ReadWriteField<TARGET, VALUE> {

Expand All @@ -24,9 +24,7 @@ public class ReflectMappedField<TARGET, VALUE> implements ReadWriteField<TARGET,
public VALUE readValue(Object o) {
try {
return o == null ? null : (VALUE) field.get(o);
} catch (IllegalAccessException e) {
throw ErrorBuilder.buildException(e);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException | IllegalArgumentException e) {
throw ErrorBuilder.buildException(e);
}
}
Expand All @@ -36,9 +34,7 @@ public void writeValue(TARGET obj, VALUE value) {
if (obj != null) {
try {
field.set(obj, value);
} catch (IllegalAccessException e) {
ErrorBuilder.throwError(e);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException | IllegalArgumentException e) {
ErrorBuilder.throwError(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public VALUE readValue(Object o) {
throw ErrorBuilder.buildException(e);
}
}

public Method getGetter() {
return getter;
}

@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ReflectReadableMappedMethod<T> implements ReadableField<T>, Declare
this.getter = getter;
}

public static ReflectReadableMappedMethod<?> of(Method getter) {
public static ReflectReadableMappedMethod<Object> of(Method getter) {
return new ReflectReadableMappedMethod<>(getter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public TARGET createDeclaringInstance() {
return createDeclaringInstance(setter);
}

@Override
public VALUE createInstance() {
return null;
}

@SuppressWarnings("unchecked")
public TARGET createDeclaringInstance(Method m) {
return (TARGET) ReflectionUtils.newInstance(m.getDeclaringClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ final static class Builder {
private boolean validateReferences;
private String resourceName;
private Class<?> resourceClass;

private Builder() {
includable = true;
validateReferences = true;
}

private Builder me() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public Object createDeclaringInstance() {
return ReflectionUtils.newInstance(type);
}

@Override
public Object createInstance() {
return null;
}

/**
* @return {@link #toString()}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
package com.github.restup.repository.collections;

import java.lang.reflect.Type;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import com.github.restup.registry.Resource;
import com.github.restup.repository.RepositoryFactory;
import com.google.common.collect.ImmutableMap;

/**
* {@link RepositoryFactory} for creating {@link MapBackedRepository} instances
*/
public class MapBackedRepositoryFactory implements RepositoryFactory {

private final Map<Type, Supplier<? extends IdentityStrategy<?>>> map;

public MapBackedRepositoryFactory(Map<Type, Supplier<? extends IdentityStrategy<?>>> map) {
this.map = ImmutableMap.copyOf(map);
}

public MapBackedRepositoryFactory() {
this(defaultIdentityStrategies());
}

private static Map<Type, Supplier<? extends IdentityStrategy<?>>> defaultIdentityStrategies() {
Map<Type, Supplier<? extends IdentityStrategy<?>>> map = new HashMap<>();
map.put(String.class, new Supplier<StringIdentityStrategy>() {
@Override
public StringIdentityStrategy get() {
return new StringIdentityStrategy();
}
});
map.put(Long.class, new Supplier<LongIdentityStrategy>() {
@Override
public LongIdentityStrategy get() {
return new LongIdentityStrategy();
}
});
map.put(Integer.class, new Supplier<IntegerIdentityStrategy>() {
@Override
public IntegerIdentityStrategy get() {
return new IntegerIdentityStrategy();
}
});
return map;
}

@SuppressWarnings({"rawtypes", "unchecked"})
public Object getRepository(Resource resource) {
Expand All @@ -17,16 +53,11 @@ public Object getRepository(Resource resource) {
}

@SuppressWarnings("rawtypes")
private IdentityStrategy getStrategy(Type type) {
if (type == String.class) {
return new StringIdentityStrategy();
}
if (type == Long.class) {
return new LongIdentityStrategy();
}
if (type == Integer.class) {
return new IntegerIdentityStrategy();
IdentityStrategy getStrategy(Type type) {
Supplier<? extends IdentityStrategy> strategy = map.get(type);
if ( strategy == null ) {
throw new IllegalArgumentException("Unable to provide a strategy for " + type);
}
throw new IllegalArgumentException("Unable to provide a strategy for " + type);
return strategy.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private PropertyDescriptor find(String fieldName) {
if (p == null) {
p = map.get("_" + fieldName);
if (p != null) {
p.name = fieldName;
p.setName(fieldName);
}
}
return p;
Expand Down Expand Up @@ -403,7 +403,7 @@ public Collection<PropertyDescriptor> getPropertyDescriptors() {
public PropertyDescriptor getPropertyDescriptor(String name) {

for (PropertyDescriptor pd : (Collection<PropertyDescriptor>) getPropertyDescriptors()) {
if (pd.getName().equals("name")) {
if (pd.getName().equals(name)) {
return pd;
}
}
Expand All @@ -414,7 +414,7 @@ public PropertyDescriptor getPropertyDescriptor(String name) {
public final static class PropertyDescriptor {

private String name;
private Field field;
private final Field field;
private Method getter;
private Method setter;
private List<Method> getterOverrides;
Expand All @@ -426,8 +426,9 @@ private PropertyDescriptor(Field f) {
field = f;
}

public PropertyDescriptor(String fieldName) {
private PropertyDescriptor(String fieldName) {
this.name = fieldName;
this.field = null;
}

public void addGetterOverrides(Method m) {
Expand Down Expand Up @@ -475,6 +476,10 @@ public Method getSetter() {
private void setSetter(Method setter) {
this.setter = setter;
}

private void setName(String name) {
this.name = name;
}

@Override
public int hashCode() {
Expand Down
7 changes: 0 additions & 7 deletions up-core/src/main/java/com/github/restup/util/UpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import com.github.restup.path.ResourcePath;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

public class UpUtils {
Expand All @@ -28,11 +26,6 @@ public static <K, V> Map<K, V> unmodifiableMap(Map<K, V> map) {
return map == null ? Collections.EMPTY_MAP : Collections.unmodifiableMap(map);
}

@SuppressWarnings("unchecked")
public static <T> Set<T> unmodifiableSet(Set<T> map) {
return map == null ? Collections.EMPTY_SET : Collections.unmodifiableSet(map);
}

public static <T> void removeAll(List<T> target, List<T> source) {
if (source != null) {
target.removeAll(source);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.restup.bind;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -49,6 +53,7 @@ public void before() {

@After
public void after() {
verifySettings();
verifyNoMoreInteractions(ctx, errors, mappedClassRegistry, settings, parameterConverterFactory);
}

Expand All @@ -59,7 +64,6 @@ private void verifySettings() {

private void verifyParameterProvider() {
verify(ctx).getParameterProvider();
verifySettings();
}

@Test
Expand Down Expand Up @@ -121,5 +125,38 @@ public void testEmptyMappedClassAttributes() {
assertNotNull(p);
verifyNullMappedClassAttributes(Person.class);
}

@SuppressWarnings("unchecked")
@Test
public void testGetApplicableCollectionInstance() {
List<Object> list = new ArrayList<>();
MappedField<Object> field = mock(MappedField.class);
Collection<Object> result = factory.getApplicableCollectionInstance((MappedField<?>)field, list);
assertEquals(list, result);

}

@SuppressWarnings("unchecked")
@Test
public void testGetApplicableCollectionInstanceWithNullCollectionArg() {
MappedField<Object> field = mock(MappedField.class);

Object set = new HashSet<>();
when(field.isCollection()).thenReturn(true);
when(field.newInstance()).thenReturn(set);
Collection<Object> result = factory.getApplicableCollectionInstance((MappedField<?>)field, null);
assertEquals(set, result);

}

@SuppressWarnings("unchecked")
@Test
public void testConvert() {
MappedField<Object> field = mock(MappedField.class);
Object result = factory.convert("foo", field, errors, "bar");
assertEquals("bar", result);

verify(parameterConverterFactory).getConverter(null);
}

}
Loading

0 comments on commit 7a86251

Please sign in to comment.