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.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
33 import org.apache.hadoop.hbase.HBaseConfiguration;
34 import org.apache.hadoop.hbase.SmallTests;
35 import org.apache.hadoop.io.IOUtils;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38
39
40
41
42 @Category(SmallTests.class)
43 public class TestCoprocessorClassLoader {
44 private static final Configuration conf = HBaseConfiguration.create();
45
46 private static final HBaseCommonTestingUtility TEST_UTIL = new HBaseCommonTestingUtility();
47
48 @Test
49 public void testCleanupOldJars() throws Exception {
50 String className = "TestCleanupOldJars";
51 String folder = TEST_UTIL.getDataTestDir().toString();
52 File jarFile = ClassLoaderTestHelper.buildJar(
53 folder, className, null, ClassLoaderTestHelper.localDirPath(conf));
54 File tmpJarFile = new File(jarFile.getParent(), "/tmp/" + className + ".test.jar");
55 if (tmpJarFile.exists()) tmpJarFile.delete();
56 assertFalse("tmp jar file should not exist", tmpJarFile.exists());
57 IOUtils.copyBytes(new FileInputStream(jarFile),
58 new FileOutputStream(tmpJarFile), conf, true);
59 assertTrue("tmp jar file should be created", tmpJarFile.exists());
60 Path path = new Path(jarFile.getAbsolutePath());
61 ClassLoader parent = TestCoprocessorClassLoader.class.getClassLoader();
62 CoprocessorClassLoader.parentDirLockSet.clear();
63 ClassLoader classLoader = CoprocessorClassLoader.getClassLoader(path, parent, "111", conf);
64 assertNotNull("Classloader should be created", classLoader);
65 assertFalse("tmp jar file should be removed", tmpJarFile.exists());
66 }
67
68 @Test
69 public void testLibJarName() throws Exception {
70 checkingLibJarName("TestLibJarName.jar", "/lib/");
71 }
72
73 @Test
74 public void testRelativeLibJarName() throws Exception {
75 checkingLibJarName("TestRelativeLibJarName.jar", "lib/");
76 }
77
78
79
80
81
82
83 private void checkingLibJarName(String jarName, String libPrefix) throws Exception {
84 File tmpFolder = new File(ClassLoaderTestHelper.localDirPath(conf), "tmp");
85 if (tmpFolder.exists()) {
86 for (File f: tmpFolder.listFiles()) {
87 f.delete();
88 }
89 }
90 String className = "CheckingLibJarName";
91 String folder = TEST_UTIL.getDataTestDir().toString();
92 File innerJarFile = ClassLoaderTestHelper.buildJar(
93 folder, className, null, ClassLoaderTestHelper.localDirPath(conf));
94 File targetJarFile = new File(innerJarFile.getParent(), jarName);
95 ClassLoaderTestHelper.addJarFilesToJar(targetJarFile, libPrefix, innerJarFile);
96 Path path = new Path(targetJarFile.getAbsolutePath());
97 ClassLoader parent = TestCoprocessorClassLoader.class.getClassLoader();
98 ClassLoader classLoader = CoprocessorClassLoader.getClassLoader(path, parent, "112", conf);
99 assertNotNull("Classloader should be created", classLoader);
100 String fileToLookFor = "." + className + ".jar";
101 for (String f: tmpFolder.list()) {
102 if (f.endsWith(fileToLookFor) && f.contains(jarName)) {
103
104 return;
105 }
106 }
107 fail("Could not find the expected lib jar file");
108 }
109 }