View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.mapreduce.hadoopbackport;
20  
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.hbase.SmallTests;
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.junit.experimental.categories.Category;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.File;
30  import java.io.FileOutputStream;
31  import java.io.FileWriter;
32  import java.io.IOException;
33  import java.io.OutputStream;
34  import java.io.Writer;
35  import java.text.MessageFormat;
36  import java.util.Properties;
37  import java.util.jar.JarInputStream;
38  import java.util.jar.JarOutputStream;
39  import java.util.jar.Manifest;
40  
41  /**
42   * This file was forked from hadoop/common/branches/branch-2@1350012.
43   */
44  @Category(SmallTests.class)
45  public class TestJarFinder {
46  
47    @Test
48    public void testJar() throws Exception {
49  
50      //picking a class that is for sure in a JAR in the classpath
51      String jar = JarFinder.getJar(LogFactory.class);
52      Assert.assertTrue(new File(jar).exists());
53    }
54  
55    private static void delete(File file) throws IOException {
56      if (file.getAbsolutePath().length() < 5) {
57        throw new IllegalArgumentException(
58          MessageFormat.format("Path [{0}] is too short, not deleting",
59                               file.getAbsolutePath()));
60      }
61      if (file.exists()) {
62        if (file.isDirectory()) {
63          File[] children = file.listFiles();
64          if (children != null) {
65            for (File child : children) {
66              delete(child);
67            }
68          }
69        }
70        if (!file.delete()) {
71          throw new RuntimeException(
72            MessageFormat.format("Could not delete path [{0}]",
73                                 file.getAbsolutePath()));
74        }
75      }
76    }
77  
78    @Test
79    public void testExpandedClasspath() throws Exception {
80      //picking a class that is for sure in a directory in the classpath
81      //in this case the JAR is created on the fly
82      String jar = JarFinder.getJar(TestJarFinder.class);
83      Assert.assertTrue(new File(jar).exists());
84    }
85  
86    @Test
87    public void testExistingManifest() throws Exception {
88      File dir = new File(System.getProperty("test.build.dir", "target/test-dir"),
89                          TestJarFinder.class.getName() + "-testExistingManifest");
90      delete(dir);
91      dir.mkdirs();
92  
93      File metaInfDir = new File(dir, "META-INF");
94      metaInfDir.mkdirs();
95      File manifestFile = new File(metaInfDir, "MANIFEST.MF");
96      Manifest manifest = new Manifest();
97      OutputStream os = new FileOutputStream(manifestFile);
98      manifest.write(os);
99      os.close();
100 
101     File propsFile = new File(dir, "props.properties");
102     Writer writer = new FileWriter(propsFile);
103     new Properties().store(writer, "");
104     writer.close();
105     ByteArrayOutputStream baos = new ByteArrayOutputStream();
106     JarOutputStream zos = new JarOutputStream(baos);
107     JarFinder.jarDir(dir, "", zos);
108     JarInputStream jis =
109       new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
110     Assert.assertNotNull(jis.getManifest());
111     jis.close();
112   }
113 
114   @Test
115   public void testNoManifest() throws Exception {
116     File dir = new File(System.getProperty("test.build.dir", "target/test-dir"),
117                         TestJarFinder.class.getName() + "-testNoManifest");
118     delete(dir);
119     dir.mkdirs();
120     File propsFile = new File(dir, "props.properties");
121     Writer writer = new FileWriter(propsFile);
122     new Properties().store(writer, "");
123     writer.close();
124     ByteArrayOutputStream baos = new ByteArrayOutputStream();
125     JarOutputStream zos = new JarOutputStream(baos);
126     JarFinder.jarDir(dir, "", zos);
127     JarInputStream jis =
128       new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
129     Assert.assertNotNull(jis.getManifest());
130     jis.close();
131   }
132 }