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