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.util; 019 020import static org.junit.jupiter.api.Assertions.assertFalse; 021import static org.junit.jupiter.api.Assertions.fail; 022 023import java.io.File; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.HBaseCommonTestingUtil; 026import org.apache.hadoop.hbase.testclassification.MiscTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.junit.jupiter.api.BeforeEach; 029import org.junit.jupiter.api.Tag; 030import org.junit.jupiter.api.Test; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Test TestDynamicClassLoader 036 */ 037@Tag(MiscTests.TAG) 038@Tag(SmallTests.TAG) 039public class TestDynamicClassLoader { 040 041 private static final Logger LOG = LoggerFactory.getLogger(TestDynamicClassLoader.class); 042 043 private static final HBaseCommonTestingUtil TEST_UTIL = new HBaseCommonTestingUtil(); 044 private Configuration conf; 045 046 static { 047 TEST_UTIL.getConfiguration().set("hbase.dynamic.jars.dir", 048 TEST_UTIL.getDataTestDir().toString()); 049 } 050 051 @BeforeEach 052 public void initializeConfiguration() { 053 conf = new Configuration(TEST_UTIL.getConfiguration()); 054 } 055 056 @Test 057 public void testLoadClassFromLocalPath() throws Exception { 058 ClassLoader parent = TestDynamicClassLoader.class.getClassLoader(); 059 DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent); 060 061 String className = "TestLoadClassFromLocalPath"; 062 deleteClass(className); 063 try { 064 classLoader.loadClass(className); 065 fail("Should not be able to load class " + className); 066 } catch (ClassNotFoundException cnfe) { 067 // expected, move on 068 } 069 070 try { 071 String folder = TEST_UTIL.getDataTestDir().toString(); 072 ClassLoaderTestHelper.buildJar(folder, className, null, 073 ClassLoaderTestHelper.localDirPath(conf)); 074 classLoader.loadClass(className); 075 } catch (ClassNotFoundException cnfe) { 076 LOG.error("Should be able to load class " + className, cnfe); 077 fail(cnfe.getMessage()); 078 } 079 } 080 081 @Test 082 public void testLoadClassFromAnotherPath() throws Exception { 083 ClassLoader parent = TestDynamicClassLoader.class.getClassLoader(); 084 DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent); 085 086 String className = "TestLoadClassFromAnotherPath"; 087 deleteClass(className); 088 try { 089 classLoader.loadClass(className); 090 fail("Should not be able to load class " + className); 091 } catch (ClassNotFoundException cnfe) { 092 // expected, move on 093 } 094 095 try { 096 String folder = TEST_UTIL.getDataTestDir().toString(); 097 ClassLoaderTestHelper.buildJar(folder, className, null); 098 classLoader.loadClass(className); 099 } catch (ClassNotFoundException cnfe) { 100 LOG.error("Should be able to load class " + className, cnfe); 101 fail(cnfe.getMessage()); 102 } 103 } 104 105 @Test 106 public void testLoadClassFromLocalPathWithDynamicDirOff() throws Exception { 107 conf.setBoolean("hbase.use.dynamic.jars", false); 108 ClassLoader parent = TestDynamicClassLoader.class.getClassLoader(); 109 DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent); 110 111 String className = "TestLoadClassFromLocalPath"; 112 deleteClass(className); 113 114 try { 115 String folder = TEST_UTIL.getDataTestDir().toString(); 116 ClassLoaderTestHelper.buildJar(folder, className, null, 117 ClassLoaderTestHelper.localDirPath(conf)); 118 classLoader.loadClass(className); 119 fail("Should not be able to load class " + className); 120 } catch (ClassNotFoundException cnfe) { 121 // expected, move on 122 } 123 } 124 125 private void deleteClass(String className) throws Exception { 126 String jarFileName = className + ".jar"; 127 File file = new File(TEST_UTIL.getDataTestDir().toString(), jarFileName); 128 file.delete(); 129 assertFalse(file.exists(), "Should be deleted: " + file.getPath()); 130 131 file = new File(conf.get("hbase.dynamic.jars.dir"), jarFileName); 132 file.delete(); 133 assertFalse(file.exists(), "Should be deleted: " + file.getPath()); 134 135 file = new File(ClassLoaderTestHelper.localDirPath(conf), jarFileName); 136 file.delete(); 137 assertFalse(file.exists(), "Should be deleted: " + file.getPath()); 138 } 139}