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.testclassification.SmallTests;
34  import org.apache.hadoop.io.IOUtils;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  /**
39   * Test TestCoprocessorClassLoader. More tests are in TestClassLoading
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(); // prepare data test dir and hbase local dir
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(); // So that clean up can be triggered
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     * Test to make sure the lib jar file extracted from a coprocessor jar have
82     * the right name.  Otherwise, some existing jar could be override if there are
83     * naming conflicts.
84     */
85    private void checkingLibJarName(String jarName, String libPrefix) throws Exception {
86      File tmpFolder = new File(ClassLoaderTestHelper.localDirPath(conf), "tmp");
87      if (tmpFolder.exists()) { // Clean up the tmp folder
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         // Cool, found it;
106         return;
107       }
108     }
109     fail("Could not find the expected lib jar file");
110   }
111 }