View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Test TestCoprocessorClassLoader. More tests are in TestClassLoading
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(); // So that clean up can be triggered
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     * Test to make sure the lib jar file extracted from a coprocessor jar have
80     * the right name.  Otherwise, some existing jar could be override if there are
81     * naming conflicts.
82     */
83    private void checkingLibJarName(String jarName, String libPrefix) throws Exception {
84      File tmpFolder = new File(ClassLoaderTestHelper.localDirPath(conf), "tmp");
85      if (tmpFolder.exists()) { // Clean up the tmp folder
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         // Cool, found it;
104         return;
105       }
106     }
107     fail("Could not find the expected lib jar file");
108   }
109 }