001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.errorhandling;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.util.Objects;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.MasterTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033/**
034 * Test that we correctly serialize exceptions from a remote source
035 */
036@Category({ MasterTests.class, SmallTests.class })
037public class TestForeignExceptionSerialization {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestForeignExceptionSerialization.class);
042
043  private static final String srcName = "someNode";
044
045  /**
046   * Verify that we get back similar stack trace information before an after serialization.
047   */
048  @Test
049  public void testSimpleException() throws IOException {
050    String data = "some bytes";
051    ForeignException in = new ForeignException("SRC", new IllegalArgumentException(data));
052    // check that we get the data back out
053    ForeignException e = ForeignException.deserialize(ForeignException.serialize(srcName, in));
054    assertNotNull(e);
055
056    // now check that we get the right stack trace
057    StackTraceElement elem = new StackTraceElement(this.getClass().toString(), "method", "file", 1);
058    in.setStackTrace(new StackTraceElement[] { elem });
059    e = ForeignException.deserialize(ForeignException.serialize(srcName, in));
060
061    assertNotNull(e);
062    assertEquals("Stack trace got corrupted", elem, e.getCause().getStackTrace()[0]);
063    assertEquals("Got an unexpectedly long stack trace", 1, e.getCause().getStackTrace().length);
064  }
065
066  /**
067   * Compare that a generic exception's stack trace has the same stack trace elements after
068   * serialization and deserialization
069   */
070  @Test
071  public void testRemoteFromLocal() throws IOException {
072    String errorMsg = "some message";
073    Exception generic = new Exception(errorMsg);
074    generic.printStackTrace();
075    assertTrue(generic.getMessage().contains(errorMsg));
076
077    ForeignException e = ForeignException.deserialize(ForeignException.serialize(srcName, generic));
078
079    // Workaround for java 11 - replaced assertArrayEquals with individual elements comparison
080    // using custom comparison helper method
081    assertEquals("Stacktrace lengths don't match", generic.getStackTrace().length,
082      e.getCause().getStackTrace().length);
083    for (int i = 0; i < generic.getStackTrace().length; i++) {
084      assertTrue("Local stack trace got corrupted at " + i + "th index",
085        compareStackTraceElement(generic.getStackTrace()[i], e.getCause().getStackTrace()[i]));
086    }
087
088    e.printStackTrace(); // should have ForeignException and source node in it.
089    assertTrue(e.getCause().getCause() == null);
090
091    // verify that original error message is present in Foreign exception message
092    assertTrue(e.getCause().getMessage().contains(errorMsg));
093  }
094
095  // Helper method to compare two stackTraceElements
096  private boolean compareStackTraceElement(StackTraceElement obj1, StackTraceElement obj2) {
097    return obj1.getClassName().equals(obj2.getClassName())
098      && obj1.getLineNumber() == obj2.getLineNumber()
099      && Objects.equals(obj1.getMethodName(), obj2.getMethodName())
100      && Objects.equals(obj1.getFileName(), obj2.getFileName());
101  }
102}