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.SmallTests;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  /**
35   * Test TestDynamicClassLoader
36   */
37  @Category(SmallTests.class)
38  public class TestDynamicClassLoader {
39    private static final Log LOG = LogFactory.getLog(TestDynamicClassLoader.class);
40  
41    private static final HBaseCommonTestingUtility TEST_UTIL = new HBaseCommonTestingUtility();
42    private static final Configuration conf = TEST_UTIL.getConfiguration();
43  
44    static {
45      conf.set("hbase.dynamic.jars.dir", TEST_UTIL.getDataTestDir().toString());
46    }
47  
48    @Test
49    public void testLoadClassFromLocalPath() throws Exception {
50      ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
51      DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
52  
53      String className = "TestLoadClassFromLocalPath";
54      deleteClass(className);
55      try {
56        classLoader.loadClass(className);
57        fail("Should not be able to load class " + className);
58      } catch (ClassNotFoundException cnfe) {
59        // expected, move on
60      }
61  
62      try {
63        String folder = TEST_UTIL.getDataTestDir().toString();
64        ClassLoaderTestHelper.buildJar(
65          folder, className, null, ClassLoaderTestHelper.localDirPath(conf));
66        classLoader.loadClass(className);
67      } catch (ClassNotFoundException cnfe) {
68        LOG.error("Should be able to load class " + className, cnfe);
69        fail(cnfe.getMessage());
70      }
71    }
72  
73    @Test
74    public void testLoadClassFromAnotherPath() throws Exception {
75      ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
76      DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
77  
78      String className = "TestLoadClassFromAnotherPath";
79      deleteClass(className);
80      try {
81        classLoader.loadClass(className);
82        fail("Should not be able to load class " + className);
83      } catch (ClassNotFoundException cnfe) {
84        // expected, move on
85      }
86  
87      try {
88        String folder = TEST_UTIL.getDataTestDir().toString();
89        ClassLoaderTestHelper.buildJar(folder, className, null);
90        classLoader.loadClass(className);
91      } catch (ClassNotFoundException cnfe) {
92        LOG.error("Should be able to load class " + className, cnfe);
93        fail(cnfe.getMessage());
94      }
95    }
96  
97    private void deleteClass(String className) throws Exception {
98      String jarFileName = className + ".jar";
99      File file = new File(TEST_UTIL.getDataTestDir().toString(), jarFileName);
100     file.delete();
101     assertFalse("Should be deleted: " + file.getPath(), file.exists());
102 
103     file = new File(conf.get("hbase.dynamic.jars.dir"), jarFileName);
104     file.delete();
105     assertFalse("Should be deleted: " + file.getPath(), file.exists());
106 
107     file = new File(ClassLoaderTestHelper.localDirPath(conf), jarFileName);
108     file.delete();
109     assertFalse("Should be deleted: " + file.getPath(), file.exists());
110   }
111 }