View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.fail;
23  
24  import java.io.File;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
30  import org.apache.hadoop.hbase.HBaseConfiguration;
31  import org.apache.hadoop.hbase.SmallTests;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  /**
36   * Test TestDynamicClassLoader
37   */
38  @Category(SmallTests.class)
39  public class TestDynamicClassLoader {
40    private static final Log LOG = LogFactory.getLog(TestDynamicClassLoader.class);
41  
42    private static final Configuration conf = HBaseConfiguration.create();
43  
44    private static final HBaseCommonTestingUtility TEST_UTIL = new HBaseCommonTestingUtility();
45  
46    static {
47      conf.set("hbase.dynamic.jars.dir", TEST_UTIL.getDataTestDir().toString());
48    }
49  
50    @Test
51    public void testLoadClassFromLocalPath() throws Exception {
52      ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
53      DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
54  
55      String className = "TestLoadClassFromLocalPath";
56      deleteClass(className);
57      try {
58        classLoader.loadClass(className);
59        fail("Should not be able to load class " + className);
60      } catch (ClassNotFoundException cnfe) {
61        // expected, move on
62      }
63  
64      try {
65        String folder = TEST_UTIL.getDataTestDir().toString();
66        ClassLoaderTestHelper.buildJar(
67          folder, className, null, ClassLoaderTestHelper.localDirPath(conf));
68        classLoader.loadClass(className);
69      } catch (ClassNotFoundException cnfe) {
70        LOG.error("Should be able to load class " + className, cnfe);
71        fail(cnfe.getMessage());
72      }
73    }
74  
75    @Test
76    public void testLoadClassFromAnotherPath() throws Exception {
77      ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
78      DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
79  
80      String className = "TestLoadClassFromAnotherPath";
81      deleteClass(className);
82      try {
83        classLoader.loadClass(className);
84        fail("Should not be able to load class " + className);
85      } catch (ClassNotFoundException cnfe) {
86        // expected, move on
87      }
88  
89      try {
90        String folder = TEST_UTIL.getDataTestDir().toString();
91        ClassLoaderTestHelper.buildJar(folder, className, null);
92        classLoader.loadClass(className);
93      } catch (ClassNotFoundException cnfe) {
94        LOG.error("Should be able to load class " + className, cnfe);
95        fail(cnfe.getMessage());
96      }
97    }
98  
99    private void deleteClass(String className) throws Exception {
100     String jarFileName = className + ".jar";
101     File file = new File(TEST_UTIL.getDataTestDir().toString(), jarFileName);
102     file.delete();
103     assertFalse("Should be deleted: " + file.getPath(), file.exists());
104 
105     file = new File(conf.get("hbase.dynamic.jars.dir"), jarFileName);
106     file.delete();
107     assertFalse("Should be deleted: " + file.getPath(), file.exists());
108 
109     file = new File(ClassLoaderTestHelper.localDirPath(conf), jarFileName);
110     file.delete();
111     assertFalse("Should be deleted: " + file.getPath(), file.exists());
112   }
113 }