com.hotels.plunger.asserts.IsSerializable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nexusreleasetest Show documentation
Show all versions of nexusreleasetest Show documentation
Framework to simplify testing Cascading applications
The newest version!
package com.hotels.plunger.asserts;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
/**
* Is the value serializable?
*/
public class IsSerializable extends BaseMatcher {
private String errorMessage;
private Object value;
@Override
public boolean matches(Object value) {
this.value = value;
errorMessage = getSerializationError(value);
return errorMessage == null;
}
@Override
public void describeTo(Description description) {
description.appendText(value.getClass().getName()).appendText(" to be serializable");
}
@Override
public void describeMismatch(Object item, Description description) {
description.appendText("failed to serialize ").appendText(errorMessage);
}
/**
* Matches if value is serializable.
*/
@Factory
public static Matcher serializable() {
return new IsSerializable();
}
/**
* Matches if value is serializable. With type inference.
*/
@Factory
public static Matcher serializable(Class type) {
return serializable();
}
static String getSerializationError(Object value) {
ObjectOutputStream outputStream = null;
try {
outputStream = new ObjectOutputStream(NULL_OUTPUT_STREAM);
outputStream.writeObject(value);
return null;
} catch (IOException e) {
return e.getMessage();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ignored) {
}
}
}
}
static final OutputStream NULL_OUTPUT_STREAM = new OutputStream() {
/** Discards the specified byte. */
@Override
public void write(int b) {
}
/** Discards the specified byte array. */
@Override
public void write(byte[] b) {
if (b == null) {
throw new NullPointerException();
}
}
/** Discards the specified byte array. */
@Override
public void write(byte[] b, int off, int len) {
if (b == null) {
throw new NullPointerException();
}
}
@Override
public String toString() {
return "NULL_OUTPUT_STREAM";
}
};
}