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.HBaseConfiguration;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
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 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
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(folder, className, null, localDirPath());
67 classLoader.loadClass(className);
68 } catch (ClassNotFoundException cnfe) {
69 LOG.error("Should be able to load class " + className, cnfe);
70 fail(cnfe.getMessage());
71 }
72 }
73
74 @Test
75 public void testLoadClassFromAnotherPath() throws Exception {
76 ClassLoader parent = TestDynamicClassLoader.class.getClassLoader();
77 DynamicClassLoader classLoader = new DynamicClassLoader(conf, parent);
78
79 String className = "TestLoadClassFromAnotherPath";
80 deleteClass(className);
81 try {
82 classLoader.loadClass(className);
83 fail("Should not be able to load class " + className);
84 } catch (ClassNotFoundException cnfe) {
85
86 }
87
88 try {
89 String folder = TEST_UTIL.getDataTestDir().toString();
90 ClassLoaderTestHelper.buildJar(folder, className, null);
91 classLoader.loadClass(className);
92 } catch (ClassNotFoundException cnfe) {
93 LOG.error("Should be able to load class " + className, cnfe);
94 fail(cnfe.getMessage());
95 }
96 }
97
98 private String localDirPath() {
99 return conf.get("hbase.local.dir")
100 + File.separator + "jars" + File.separator;
101 }
102
103 private void deleteClass(String className) throws Exception {
104 String jarFileName = className + ".jar";
105 File file = new File(TEST_UTIL.getDataTestDir().toString(), jarFileName);
106 file.delete();
107 assertFalse("Should be deleted: " + file.getPath(), file.exists());
108
109 file = new File(conf.get("hbase.dynamic.jars.dir"), jarFileName);
110 file.delete();
111 assertFalse("Should be deleted: " + file.getPath(), file.exists());
112
113 file = new File(localDirPath(), jarFileName);
114 file.delete();
115 assertFalse("Should be deleted: " + file.getPath(), file.exists());
116 }
117
118 @org.junit.Rule
119 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
120 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
121 }