1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapreduce.hadoopbackport;
20
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.hadoop.hbase.testclassification.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
43
44 @Category(SmallTests.class)
45 public class TestJarFinder {
46
47 @Test
48 public void testJar() throws Exception {
49
50
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
81
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 }