moved the binding of the underlying exception from ExceptionContextProvider to ExceptionContext, as diccussed on the dev list

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1165034 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-09-04 14:38:40 +00:00
parent f4abfdc106
commit d696293435
8 changed files with 46 additions and 43 deletions

View File

@ -35,12 +35,13 @@ public class MathArithmeticException extends ArithmeticException
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
private final ExceptionContext context;
/**
* Default constructor.
*/
public MathArithmeticException() {
context = new ExceptionContext(this);
context.addMessage(LocalizedFormats.ARITHMETIC_EXCEPTION);
}
@ -53,6 +54,7 @@ public class MathArithmeticException extends ArithmeticException
*/
public MathArithmeticException(Localizable pattern,
Object ... args) {
context = new ExceptionContext(this);
context.addMessage(pattern, args);
}
@ -61,11 +63,6 @@ public class MathArithmeticException extends ArithmeticException
return context;
}
/** {@inheritDoc} */
public Throwable getException() {
return this;
}
/** {@inheritDoc} */
@Override
public String getMessage() {

View File

@ -34,7 +34,7 @@ public class MathIllegalArgumentException extends IllegalArgumentException
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
private final ExceptionContext context;
/**
* @param pattern Message pattern explaining the cause of the error.
@ -42,6 +42,7 @@ public class MathIllegalArgumentException extends IllegalArgumentException
*/
public MathIllegalArgumentException(Localizable pattern,
Object ... args) {
context = new ExceptionContext(this);
context.addMessage(pattern, args);
}
@ -50,11 +51,6 @@ public class MathIllegalArgumentException extends IllegalArgumentException
return context;
}
/** {@inheritDoc} */
public Throwable getException() {
return this;
}
/** {@inheritDoc} */
@Override
public String getMessage() {

View File

@ -33,7 +33,7 @@ public class MathIllegalStateException extends IllegalStateException
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
private final ExceptionContext context;
/**
* Simple constructor.
@ -43,6 +43,7 @@ public class MathIllegalStateException extends IllegalStateException
*/
public MathIllegalStateException(Localizable pattern,
Object ... args) {
context = new ExceptionContext(this);
context.addMessage(pattern, args);
}
@ -57,6 +58,7 @@ public class MathIllegalStateException extends IllegalStateException
Localizable pattern,
Object ... args) {
super(cause);
context = new ExceptionContext(this);
context.addMessage(pattern, args);
}
@ -72,11 +74,6 @@ public class MathIllegalStateException extends IllegalStateException
return context;
}
/** {@inheritDoc} */
public Throwable getException() {
return this;
}
/** {@inheritDoc} */
@Override
public String getMessage() {

View File

@ -35,7 +35,7 @@ public class MathUnsupportedOperationException extends UnsupportedOperationExcep
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
private final ExceptionContext context;
/**
* Default constructor.
@ -50,6 +50,7 @@ public class MathUnsupportedOperationException extends UnsupportedOperationExcep
*/
public MathUnsupportedOperationException(Localizable pattern,
Object ... args) {
context = new ExceptionContext(this);
context.addMessage(pattern, args);
}
@ -58,11 +59,6 @@ public class MathUnsupportedOperationException extends UnsupportedOperationExcep
return context;
}
/** {@inheritDoc} */
public Throwable getException() {
return this;
}
/** {@inheritDoc} */
@Override
public String getMessage() {

View File

@ -37,12 +37,13 @@ public class MathUserException extends RuntimeException
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context = new ExceptionContext();
private final ExceptionContext context;
/**
* Build an exception with a default message.
*/
public MathUserException() {
context = new ExceptionContext(this);
context.addMessage(LocalizedFormats.USER_EXCEPTION);
}
@ -52,6 +53,7 @@ public class MathUserException extends RuntimeException
*/
public MathUserException(final Throwable cause) {
super(cause);
context = new ExceptionContext(this);
context.addMessage(LocalizedFormats.USER_EXCEPTION);
}
@ -63,6 +65,7 @@ public class MathUserException extends RuntimeException
*/
public MathUserException(final Localizable pattern,
final Object ... arguments) {
context = new ExceptionContext(this);
context.addMessage(pattern, arguments);
}
@ -77,6 +80,7 @@ public class MathUserException extends RuntimeException
final Localizable pattern,
final Object ... arguments) {
super(cause);
context = new ExceptionContext(this);
context.addMessage(pattern, arguments);
}
@ -85,11 +89,6 @@ public class MathUserException extends RuntimeException
return context;
}
/** {@inheritDoc} */
public Throwable getException() {
return this;
}
/** {@inheritDoc} */
@Override
public String getMessage() {

View File

@ -39,20 +39,41 @@ import java.util.Locale;
public class ExceptionContext implements Serializable {
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/**
* The throwable to which this context refers to.
*/
private Throwable throwable;
/**
* Various informations that enrich the informative message.
*/
private List<Localizable> msgPatterns = new ArrayList<Localizable>();
private List<Localizable> msgPatterns;
/**
* Various informations that enrich the informative message.
* The arguments will replace the corresponding place-holders in
* {@link #msgPatterns}.
*/
private List<Object[]> msgArguments = new ArrayList<Object[]>();
private List<Object[]> msgArguments;
/**
* Arbitrary context information.
*/
private Map<String, Object> context = new HashMap<String, Object>();
private Map<String, Object> context;
/** Simple constructor.
* @param throwable the exception this context refers too
*/
public ExceptionContext(final Throwable throwable) {
this.throwable = throwable;
msgPatterns = new ArrayList<Localizable>();
msgArguments = new ArrayList<Object[]>();
context = new HashMap<String, Object>();
}
/** Get a reference to the exception to which the context relates.
* @return a reference to the exception to which the context relates
*/
public Throwable getThrowable() {
return throwable;
}
/**
* Adds a message.
@ -173,6 +194,7 @@ public class ExceptionContext implements Serializable {
*/
private void writeObject(ObjectOutputStream out)
throws IOException {
out.writeObject(throwable);
serializeMessages(out);
serializeContext(out);
}
@ -186,6 +208,7 @@ public class ExceptionContext implements Serializable {
private void readObject(ObjectInputStream in)
throws IOException,
ClassNotFoundException {
throwable = (Throwable) in.readObject();
deSerializeMessages(in);
deSerializeContext(in);
}

View File

@ -31,9 +31,4 @@ public interface ExceptionContextProvider {
*/
ExceptionContext getContext();
/** Get a reference to the exception to which the context relates.
* @return a reference to the exception to which the context relates
*/
Throwable getException();
}

View File

@ -35,7 +35,7 @@ import org.junit.Test;
public class ExceptionContextTest {
@Test
public void testMessageChain() {
final ExceptionContext c = new ExceptionContext();
final ExceptionContext c = new ExceptionContext(new Exception("oops"));
final String sep = " | "; // Non-default separator.
final String m1 = "column index (0)";
c.addMessage(LocalizedFormats.COLUMN_INDEX, 0);
@ -50,14 +50,14 @@ public class ExceptionContextTest {
@Test
public void testNoArgAddMessage() {
final ExceptionContext c = new ExceptionContext();
final ExceptionContext c = new ExceptionContext(new Exception("hello"));
c.addMessage(LocalizedFormats.SIMPLE_MESSAGE);
Assert.assertEquals(c.getMessage(), "{0}");
}
@Test
public void testContext() {
final ExceptionContext c = new ExceptionContext();
final ExceptionContext c = new ExceptionContext(new Exception("bye"));
final String[] keys = {"Key 1", "Key 2"};
final Object[] values = {"Value 1", Integer.valueOf(2)};
@ -82,7 +82,7 @@ public class ExceptionContextTest {
public void testSerialize()
throws IOException,
ClassNotFoundException {
final ExceptionContext cOut = new ExceptionContext();
final ExceptionContext cOut = new ExceptionContext(new Exception("Apache"));
cOut.addMessage(LocalizedFormats.COLUMN_INDEX, 0);
cOut.setValue("Key 1", Integer.valueOf(0));
@ -102,7 +102,7 @@ public class ExceptionContextTest {
@Test
public void testSerializeUnserializable() {
final ExceptionContext cOut = new ExceptionContext();
final ExceptionContext cOut = new ExceptionContext(new Exception("Apache Commons Math"));
cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, "OK");
cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, new Unserializable());
String key = "Key 1";