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.testclassification.SmallTests;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34
35
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
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
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 }