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

Fix value rendering in error last cause for primitive types #2992

Merged
merged 1 commit into from
May 30, 2015
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
28 changes: 26 additions & 2 deletions src/main/java/rx/exceptions/OnErrorThrowable.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package rx.exceptions;

import java.util.HashSet;
import java.util.Set;

import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;

Expand Down Expand Up @@ -109,6 +112,27 @@ public static Throwable addValueAsLastCause(Throwable e, Object value) {
public static class OnNextValue extends RuntimeException {

private static final long serialVersionUID = -3454462756050397899L;

// Lazy loaded singleton
private static final class Primitives {

static final Set<Class<?>> INSTANCE = create();

private static Set<Class<?>> create() {
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(Boolean.class);
set.add(Character.class);
set.add(Byte.class);
set.add(Short.class);
set.add(Integer.class);
set.add(Long.class);
set.add(Float.class);
set.add(Double.class);
// Void is another primitive but cannot be instantiated
// and is caught by the null check in renderValue
return set;
}
}

private final Object value;

Expand Down Expand Up @@ -148,11 +172,11 @@ public Object getValue() {
* @return a string version of the object if primitive or managed through error plugin,
* otherwise the classname of the object
*/
private static String renderValue(Object value){
static String renderValue(Object value){
if (value == null) {
return "null";
}
if (value.getClass().isPrimitive()) {
if (Primitives.INSTANCE.contains(value.getClass())) {
return value.toString();
}
if (value instanceof String) {
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/rx/exceptions/OnNextValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package rx.exceptions;

import org.junit.Assert;
import org.junit.Test;

import rx.Observable;
import rx.Observer;
import rx.exceptions.OnErrorThrowable.OnNextValue;
import rx.functions.Func1;

import java.io.PrintWriter;
import java.io.StringWriter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -118,4 +122,49 @@ public BadToString call(BadToString badToString) {
}).subscribe(observer);

}

@Test
public void testRenderInteger() {
assertEquals("123", OnNextValue.renderValue(123));
}

@Test
public void testRenderByte() {
assertEquals("10", OnNextValue.renderValue((byte) 10));
}

@Test
public void testRenderBoolean() {
assertEquals("true", OnNextValue.renderValue(true));
}

@Test
public void testRenderShort() {
assertEquals("10", OnNextValue.renderValue((short) 10));
}

@Test
public void testRenderLong() {
assertEquals("10", OnNextValue.renderValue(10L));
}

@Test
public void testRenderCharacter() {
assertEquals("10", OnNextValue.renderValue(10L));
}

@Test
public void testRenderFloat() {
assertEquals("10.0", OnNextValue.renderValue(10.0f));
}

@Test
public void testRenderDouble() {
assertEquals("10.0", OnNextValue.renderValue(10.0));
}

@Test
public void testRenderVoid() {
assertEquals("null", OnNextValue.renderValue((Void) null));
}
}