1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.exception.util;
18
19 import java.util.Locale;
20 import java.util.Arrays;
21 import java.io.IOException;
22 import java.io.ObjectOutputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.ByteArrayInputStream;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29
30
31
32
33
34 public class ExceptionContextTest {
35 @Test
36 public void testMessageChain() {
37 final ExceptionContext c = new ExceptionContext(new Exception("oops"));
38 final String sep = " | ";
39 final String m1 = "column index (0)";
40 c.addMessage(LocalizedFormats.COLUMN_INDEX, 0);
41 final String m2 = "got 1x2 but expected 3x4";
42 c.addMessage(LocalizedFormats.DIMENSIONS_MISMATCH_2x2, 1, 2, 3, 4);
43 final String m3 = "It didn't work out";
44 c.addMessage(LocalizedFormats.SIMPLE_MESSAGE, m3);
45
46 Assert.assertEquals(c.getMessage(Locale.US, sep),
47 m1 + sep + m2 + sep + m3);
48 }
49
50 @Test
51 public void testNoArgAddMessage() {
52 final ExceptionContext c = new ExceptionContext(new Exception("hello"));
53 c.addMessage(LocalizedFormats.SIMPLE_MESSAGE);
54 Assert.assertEquals(c.getMessage(), "{0}");
55 }
56
57 @Test
58 public void testContext() {
59 final ExceptionContext c = new ExceptionContext(new Exception("bye"));
60
61 final String[] keys = {"Key 1", "Key 2"};
62 final Object[] values = {"Value 1", Integer.valueOf(2)};
63
64 for (int i = 0; i < keys.length; i++) {
65 c.setValue(keys[i], values[i]);
66 }
67
68
69 Assert.assertTrue(c.getKeys().containsAll(Arrays.asList(keys)));
70
71
72 for (int i = 0; i < keys.length; i++) {
73 Assert.assertEquals(values[i], c.getValue(keys[i]));
74 }
75
76
77 Assert.assertNull(c.getValue("xyz"));
78 }
79
80 @Test
81 public void testSerialize()
82 throws IOException,
83 ClassNotFoundException {
84 final ExceptionContext cOut = new ExceptionContext(new Exception("Apache"));
85 cOut.addMessage(LocalizedFormats.COLUMN_INDEX, 0);
86 cOut.setValue("Key 1", Integer.valueOf(0));
87
88 ByteArrayOutputStream bos = new ByteArrayOutputStream();
89 ObjectOutputStream oos = new ObjectOutputStream(bos);
90 oos.writeObject(cOut);
91
92 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
93 ObjectInputStream ois = new ObjectInputStream(bis);
94 ExceptionContext cIn = (ExceptionContext) ois.readObject();
95
96 Assert.assertEquals(cOut.getMessage(), cIn.getMessage());
97 for (String key : cIn.getKeys()) {
98 Assert.assertEquals(cOut.getValue(key), cIn.getValue(key));
99 }
100 }
101
102 @Test
103 public void testSerializeUnserializable() throws Exception {
104 final ExceptionContext cOut = new ExceptionContext(new Exception("Apache Commons Math"));
105 cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, "OK");
106 cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, new Unserializable());
107 String key = "Key 1";
108 cOut.setValue(key, new Unserializable());
109
110 ByteArrayOutputStream bos = new ByteArrayOutputStream();
111 ObjectOutputStream oos = new ObjectOutputStream(bos);
112 oos.writeObject(cOut);
113
114 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
115 ObjectInputStream ois = new ObjectInputStream(bis);
116 ExceptionContext cIn = (ExceptionContext) ois.readObject();
117
118 String nsObjStr = (String) cIn.getValue(key);
119 Assert.assertTrue(nsObjStr.matches(".*could not be serialized.*"));
120 }
121
122
123
124
125 private static class Unserializable {
126 Unserializable() {}
127 }
128 }