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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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