1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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 }