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.ipc;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNull;
022import static org.junit.jupiter.api.Assertions.assertSame;
023
024import org.apache.hadoop.hbase.testclassification.ClientTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.jupiter.api.Tag;
027import org.junit.jupiter.api.Test;
028
029@Tag(ClientTests.TAG)
030@Tag(SmallTests.TAG)
031public class TestShadedPrefixUtil {
032
033  private static final String EXCEPTION_CLASS =
034    "org.apache.hadoop.hbase.exceptions.RegionMovedException";
035
036  @Test
037  public void testApplyShadingNotShaded() {
038    ShadedPrefixUtil util = ShadedPrefixUtil.getInstance();
039    assertNull(util.getShadedBase());
040    assertEquals(EXCEPTION_CLASS, util.applyShading(EXCEPTION_CLASS));
041    assertNull(util.applyShading(null));
042  }
043
044  @Test
045  public void testApplyShadingPrependPrefix() {
046    ShadedPrefixUtil util =
047      ShadedPrefixUtil.newInstance("a.b.c." + ShadedPrefixUtil.class.getPackage().getName());
048    assertEquals("a.b.c.org.apache.hadoop.hbase", util.getShadedBase());
049    assertEquals("a.b.c." + EXCEPTION_CLASS, util.applyShading(EXCEPTION_CLASS));
050    assertNull(util.applyShading(null));
051  }
052
053  @Test
054  public void testApplyShadingFullReplacement() {
055    ShadedPrefixUtil util = ShadedPrefixUtil.newInstance("shaded.hbase1.ipc");
056    assertEquals("shaded.hbase1", util.getShadedBase());
057    assertEquals("shaded.hbase1.exceptions.RegionMovedException",
058      util.applyShading(EXCEPTION_CLASS));
059    assertNull(util.applyShading(null));
060  }
061
062  @Test
063  public void testApplyShadingNonHBaseClass() {
064    ShadedPrefixUtil util = ShadedPrefixUtil.newInstance("shaded.hbase1.ipc");
065    assertEquals("com.example.SomeClass", util.applyShading("com.example.SomeClass"));
066  }
067
068  @Test
069  public void testApplyShadingNullPackage() {
070    ShadedPrefixUtil util = ShadedPrefixUtil.newInstance(null);
071    assertNull(util.getShadedBase());
072    assertEquals(EXCEPTION_CLASS, util.applyShading(EXCEPTION_CLASS));
073  }
074
075  @Test
076  public void testApplyShadingUnrecognizablePackage() {
077    ShadedPrefixUtil util = ShadedPrefixUtil.newInstance("some.unrelated.package");
078    assertNull(util.getShadedBase());
079    assertEquals(EXCEPTION_CLASS, util.applyShading(EXCEPTION_CLASS));
080  }
081
082  @Test
083  public void testResolveShadingNotShaded() {
084    ShadedPrefixUtil util = ShadedPrefixUtil.getInstance();
085    // No shading: returned as-is without class loading
086    assertEquals(EXCEPTION_CLASS, util.resolveShading(EXCEPTION_CLASS));
087    assertNull(util.resolveShading(null));
088  }
089
090  @Test
091  public void testResolveShadingFallsBackWhenShadedClassMissing() {
092    // In the test classpath no "shaded.hbase1.*" classes exist, so resolveShading must
093    // fall back to the original name rather than the transformed one.
094    ShadedPrefixUtil util = ShadedPrefixUtil.newInstance("shaded.hbase1.ipc");
095    assertEquals(EXCEPTION_CLASS, util.resolveShading(EXCEPTION_CLASS));
096  }
097
098  @Test
099  public void testResolveShadingCachesResult() {
100    ShadedPrefixUtil util = ShadedPrefixUtil.newInstance("shaded.hbase1.ipc");
101    String first = util.resolveShading(EXCEPTION_CLASS);
102    String second = util.resolveShading(EXCEPTION_CLASS);
103    // Identical object reference confirms the cache returned the memoized result
104    assertSame(first, second);
105  }
106}