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.testclassification.SmallTests;
31  import org.junit.Test;
32  import org.junit.Before;
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 HBaseCommonTestingUtility TEST_UTIL = new HBaseCommonTestingUtility();
43    private Configuration conf;
44  
45    static {
46      TEST_UTIL.getConfiguration().set(
47          "hbase.dynamic.jars.dir", TEST_UTIL.getDataTestDir().toString());
48    }
49  
50    @Before
51    public void initializeConfiguration() {
52      conf = new Configuration(TEST_UTIL.getConfiguration());
53    }
54  
55    @Test
56    public void testLoadClassFromLocalPath() throws Exception {
57      ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
58      DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
59  
60      String className = "TestLoadClassFromLocalPath";
61      deleteClass(className);
62      try {
63        classLoader.loadClass(className);
64        fail("Should not be able to load class " + className);
65      } catch (ClassNotFoundException cnfe) {
66        // expected, move on
67      }
68  
69      try {
70        String folder = TEST_UTIL.getDataTestDir().toString();
71        ClassLoaderTestHelper.buildJar(
72          folder, className, null, ClassLoaderTestHelper.localDirPath(conf));
73        classLoader.loadClass(className);
74      } catch (ClassNotFoundException cnfe) {
75        LOG.error("Should be able to load class " + className, cnfe);
76        fail(cnfe.getMessage());
77      }
78    }
79  
80    @Test
81    public void testLoadClassFromAnotherPath() throws Exception {
82      ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
83      DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
84  
85      String className = "TestLoadClassFromAnotherPath";
86      deleteClass(className);
87      try {
88        classLoader.loadClass(className);
89        fail("Should not be able to load class " + className);
90      } catch (ClassNotFoundException cnfe) {
91        // expected, move on
92      }
93  
94      try {
95        String folder = TEST_UTIL.getDataTestDir().toString();
96        ClassLoaderTestHelper.buildJar(folder, className, null);
97        classLoader.loadClass(className);
98      } catch (ClassNotFoundException cnfe) {
99        LOG.error("Should be able to load class " + className, cnfe);
100       fail(cnfe.getMessage());
101     }
102   }
103 
104   @Test
105   public void testLoadClassFromLocalPathWithDynamicDirOff() throws Exception {
106     conf.setBoolean("hbase.use.dynamic.jars", false);
107     ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
108     DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
109 
110     String className = "TestLoadClassFromLocalPath";
111     deleteClass(className);
112 
113     try {
114       String folder = TEST_UTIL.getDataTestDir().toString();
115       ClassLoaderTestHelper.buildJar(
116           folder, className, null, ClassLoaderTestHelper.localDirPath(conf));
117       classLoader.loadClass(className);
118       fail("Should not be able to load class " + className);
119     } catch (ClassNotFoundException cnfe) {
120       // expected, move on
121     }
122   }
123 
124   private void deleteClass(String className) throws Exception {
125     String jarFileName = className + ".jar";
126     File file = new File(TEST_UTIL.getDataTestDir().toString(), jarFileName);
127     file.delete();
128     assertFalse("Should be deleted: " + file.getPath(), file.exists());
129 
130     file = new File(conf.get("hbase.dynamic.jars.dir"), jarFileName);
131     file.delete();
132     assertFalse("Should be deleted: " + file.getPath(), file.exists());
133 
134     file = new File(ClassLoaderTestHelper.localDirPath(conf), jarFileName);
135     file.delete();
136     assertFalse("Should be deleted: " + file.getPath(), file.exists());
137   }
138 }