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.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertTrue; 024 025import java.io.IOException; 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 assertArrayEquals("Local stack trace got corrupted", generic.getStackTrace(), e.getCause().getStackTrace()); 079 080 e.printStackTrace(); // should have ForeignException and source node in it. 081 assertTrue(e.getCause().getCause() == null); 082 083 // verify that original error message is present in Foreign exception message 084 assertTrue(e.getCause().getMessage().contains(errorMsg)); 085 } 086 087}