View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test for {@link ExceptionContext}.
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 = " | "; // Non-default separator.
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          // Check that all keys are present.
69          Assert.assertTrue(c.getKeys().containsAll(Arrays.asList(keys)));
70  
71          // Check that all values are correctly stored.
72          for (int i = 0; i < keys.length; i++) {
73              Assert.assertEquals(values[i], c.getValue(keys[i]));
74          }
75  
76          // Check behaviour on missing key.
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      * Class used by {@link #testSerializeUnserializable()}.
124      */
125     private static class Unserializable {
126         Unserializable() {}
127     }
128 }