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