Skip to content

Commit

Permalink
#48 Cleaning up tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
weicheng113 committed Oct 7, 2014
1 parent 32e8366 commit 50277d0
Show file tree
Hide file tree
Showing 54 changed files with 328 additions and 1,124 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.robobinding.codegen.processor;

import org.apache.commons.lang3.StringUtils;
import org.robobinding.function.Bug;
import org.robobinding.Bug;
import org.robobinding.internal.java_beans.Introspector;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.robobinding.property;

import static org.robobinding.property.MockPropertyAccessorBuilder.aPropertyAccessor;
package org.robobinding.codegen.processor;

import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.robobinding.codegen.DependencyValidation;

import com.google.common.collect.Sets;

Expand All @@ -30,17 +27,17 @@ public void setUp() {

@Test
public void givenValidDependentProperties_thenSuccessful() {
validation.validate(aPropertyAccessor().withPropertyName(PROPERTY_NAME).build(), Sets.newHashSet("property1", "property2"));
validation.validate(PROPERTY_NAME, Sets.newHashSet("property1", "property2"));
}

@Test(expected = RuntimeException.class)
public void givenDependingOnSelf_thenThrowException() {
validation.validate(aPropertyAccessor().withPropertyName(PROPERTY_NAME).build(), Sets.newHashSet("property1", PROPERTY_NAME, "property2"));
validation.validate(PROPERTY_NAME, Sets.newHashSet("property1", PROPERTY_NAME, "property2"));
}

@Test(expected = RuntimeException.class)
public void whenCreateWithSomeNonExistingDependentProperties_thenThrowException() {
validation.validate(aPropertyAccessor().withPropertyName(PROPERTY_NAME).build(), Sets.newHashSet("property1", "nonExistingProperty", "property2"));
validation.validate(PROPERTY_NAME, Sets.newHashSet("property1", "nonExistingProperty", "property2"));
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package org.robobinding.itempresentationmodel;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.robobinding.property.PropertyAccessor;
import org.robobinding.property.PropertyDescriptor;
import org.robobinding.internal.java_beans.PropertyDescriptor;
import org.robobinding.property.PropertyUtils;

import android.database.AbstractCursor;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
*
Expand Down Expand Up @@ -84,16 +85,23 @@ public boolean isNull(int column) {

private Object getColumnValue(int column) {
Preconditions.checkArgument(column < getNumColumns(), "column '" + column + "' is out of bound");
PropertyAccessor propertyAccesor = mapColumnToPropertyAccessor(column);
return propertyAccesor.getValue();
PropertyDescriptor descriptor = mapColumnToPropertyDescriptor(column);
try {
return descriptor.getReadMethod().invoke(getBean(), new Object[0]);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}

private PropertyAccessor mapColumnToPropertyAccessor(int column) {
private PropertyDescriptor mapColumnToPropertyDescriptor(int column) {
String propertyName = mapColumnToPropertyName(column);
PropertyDescriptor descriptor = propertyDescriptorMap.get(propertyName);

PropertyAccessor propertyAccesor = new PropertyAccessor(getBean(), descriptor);
return propertyAccesor;
return descriptor;
}

private String mapColumnToPropertyName(int column) {
Expand All @@ -117,7 +125,14 @@ public T getObjectAtPosition(int position) {
public static <T> BeanCursor<T> create(Collection<T> beans, Class<T> beanClass) {
Preconditions.checkNotNull(beans, "beans can not be null");
Preconditions.checkNotNull(beanClass, "beanClass can not be null");
return new BeanCursor<T>(Lists.newArrayList(beans), PropertyUtils.getPropertyDescriptorMap(beanClass));

return new BeanCursor<T>(Lists.newArrayList(beans), getPropertyDescriptorMap(beanClass));
}

private static Map<String, PropertyDescriptor> getPropertyDescriptorMap(Class<?> beanClass) {
Map<String, PropertyDescriptor> propertyDescriptorMap = Maps.newHashMap();
for(PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(beanClass)) {
propertyDescriptorMap.put(descriptor.getName(), descriptor);
}
return propertyDescriptorMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.Before;
import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.adapterview.AbstractAdapterViewAttributeTest;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.Before;
import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.abslistview.CheckedItemPositionsAttribute.MapCheckedItemPositionsAttribute;
import org.robolectric.annotation.Config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.Before;
import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.abslistview.CheckedItemPositionsAttribute.SetCheckedItemPositionsAttribute;
import org.robolectric.annotation.Config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.Before;
import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.abslistview.CheckedItemPositionsAttribute.SparseBooleanArrayCheckedItemPositionsAttribute;
import org.robolectric.annotation.Config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.mockito.MockitoAnnotations;
import org.robobinding.BindableView;
import org.robobinding.itempresentationmodel.ItemPresentationModel;
import org.robobinding.itempresentationmodel.RefreshableItemPresentationModel;
import org.robobinding.property.DataSetValueModel;
import org.robobinding.property.PropertyChangeListener;
import org.robobinding.property.PropertyChangeListeners;
Expand Down Expand Up @@ -168,10 +169,9 @@ public Object getItem(int position) {
return null;
}

@SuppressWarnings("unchecked")
@Override
public ItemPresentationModel<Object> newRefreshableItemPresentationModel() {
return mock(ItemPresentationModel.class);
public RefreshableItemPresentationModel newRefreshableItemPresentationModel() {
return mock(RefreshableItemPresentationModel.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.junit.Ignore;
import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.util.RandomValues;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.util.RandomValues;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robolectric.annotation.Config;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.edittext.TwoWayTextAttribute.TwoWayCharSequenceTextAttribute;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.edittext.TwoWayTextAttribute.TwoWayStringTextAttribute;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.junit.Before;
import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.util.RandomValues;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.ratingbar.RatingAttribute.FloatRatingAttribute;
import org.robolectric.annotation.Config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.junit.Before;
import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.util.RandomValues;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robobinding.widget.ratingbar.RatingAttribute.IntegerRatingAttribute;
import org.robolectric.annotation.Config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import org.junit.Test;
import org.robobinding.property.ValueModel;
import org.robobinding.property.ValueModelUtils;
import org.robobinding.util.RandomValues;
import org.robobinding.viewattribute.property.ValueModelUtils;
import org.robolectric.annotation.Config;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.robobinding.function;
package org.robobinding;

/**
* @since 1.0
Expand Down
24 changes: 11 additions & 13 deletions framework/src/main/java/org/robobinding/function/LazyFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.Set;

import org.robobinding.Bug;
import org.robobinding.util.MethodUtils;

import com.google.android.collect.Maps;
Expand Down Expand Up @@ -42,24 +43,21 @@ public Function find(String functionName, Class<?>... parameterTypes) {
}

MethodDescriptor methodDescriptor = new MethodDescriptor(method.getName(), method.getParameterTypes());
if(!functions.containsKey(methodDescriptor)) {
throw new RuntimeException("No such method '"+new MethodDescription(beanClass, method)+"'");
}

Function function = functions.get(method);
Function function = functions.get(methodDescriptor);

if (function == null) {
functions.put(methodDescriptor, createFunction(method, methodDescriptor));
function = functions.get(method);
function = supply.tryToCreateFunction(methodDescriptor);
if(function == null) {
throw new Bug(MessageFormat.format("The method '{0}' is not generated", new MethodDescription(beanClass, method)));
}

functions.put(methodDescriptor, function);
}

return function;
}

private Function createFunction(Method method, MethodDescriptor methodDescriptor) {
Function function = supply.tryToCreateFunction(methodDescriptor);
if(function == null) {
MethodDescription description = new MethodDescription(beanClass, method);
throw new Bug(MessageFormat.format("The method '{0}' is not generated", description));
} else {
return function;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hashCode(name, parameterTypes);
int sum = Objects.hashCode(name);
for(Class<?> parameterType : parameterTypes) {
sum += Objects.hashCode(parameterType);
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.robobinding.property;

import org.robobinding.function.Bug;
import org.robobinding.Bug;

/**
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public <T> ValueModel<T> getReadOnlyProperty(String propertyName) {
}

private PropertyValueModel getProperty(String propertyName, boolean isReadWriteProperty) {
if(properties.containsKey(propertyName)) {
if(!properties.containsKey(propertyName)) {
throw new RuntimeException("No such property '"+describeProperty(propertyName)+"'");
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public Class<?> getPropertyType(String propertyName) {
@SuppressWarnings("unchecked")
@Override
public <T> DataSetValueModel<T> getDataSetProperty(String propertyName) {
if(dataSetProperties.containsKey(propertyName)) {
if(!dataSetProperties.containsKey(propertyName)) {
throw new RuntimeException("No such dataSet property '"+describeProperty(propertyName)+"'");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.text.MessageFormat;

import org.robobinding.function.Bug;
import org.robobinding.Bug;

/**
* @since 1.0
Expand Down

This file was deleted.

Loading

0 comments on commit 50277d0

Please sign in to comment.