View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.errorhandling;
19  
20  import static org.junit.Assert.assertArrayEquals;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.junit.Test;
27  import org.junit.experimental.categories.Category;
28  
29  import com.google.protobuf.InvalidProtocolBufferException;
30  
31  /**
32   * Test that we correctly serialize exceptions from a remote source
33   */
34  @Category(SmallTests.class)
35  public class TestForeignExceptionSerialization {
36    private static final String srcName = "someNode";
37  
38    /**
39     * Verify that we get back similar stack trace information before an after serialization.
40     * @throws InvalidProtocolBufferException
41     */
42    @Test
43    public void testSimpleException() throws InvalidProtocolBufferException {
44      String data = "some bytes";
45      ForeignException in = new ForeignException("SRC", new IllegalArgumentException(data));
46      // check that we get the data back out
47      ForeignException e = ForeignException.deserialize(ForeignException.serialize(srcName, in));
48      assertNotNull(e);
49  
50      // now check that we get the right stack trace
51      StackTraceElement elem = new StackTraceElement(this.getClass().toString(), "method", "file", 1);
52      in.setStackTrace(new StackTraceElement[] { elem });
53      e = ForeignException.deserialize(ForeignException.serialize(srcName, in));
54  
55      assertNotNull(e);
56      assertEquals("Stack trace got corrupted", elem, e.getCause().getStackTrace()[0]);
57      assertEquals("Got an unexpectedly long stack trace", 1, e.getCause().getStackTrace().length);
58    }
59  
60    /**
61     * Compare that a generic exception's stack trace has the same stack trace elements after
62     * serialization and deserialization
63     * @throws InvalidProtocolBufferException
64     */
65    @Test
66    public void testRemoteFromLocal() throws InvalidProtocolBufferException {
67      String errorMsg = "some message";
68      Exception generic = new Exception(errorMsg);
69      generic.printStackTrace();
70      assertTrue(generic.getMessage().contains(errorMsg));
71  
72      ForeignException e = ForeignException.deserialize(ForeignException.serialize(srcName, generic));
73      assertArrayEquals("Local stack trace got corrupted", generic.getStackTrace(), e.getCause().getStackTrace());
74  
75      e.printStackTrace(); // should have ForeignException and source node in it.
76      assertTrue(e.getCause().getCause() == null);
77  
78      // verify that original error message is present in Foreign exception message
79      assertTrue(e.getCause().getMessage().contains(errorMsg));
80    }
81  
82  }