001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.mapreduce; 019 020import java.io.ByteArrayInputStream; 021import java.io.ByteArrayOutputStream; 022import java.io.File; 023import java.io.FileOutputStream; 024import java.io.FileWriter; 025import java.io.IOException; 026import java.io.OutputStream; 027import java.io.Writer; 028import java.text.MessageFormat; 029import java.util.Properties; 030import java.util.jar.JarInputStream; 031import java.util.jar.JarOutputStream; 032import java.util.jar.Manifest; 033import org.apache.hadoop.hbase.HBaseClassTestRule; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.junit.Assert; 036import org.junit.ClassRule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039import org.slf4j.LoggerFactory; 040 041/** 042 * This file was forked from hadoop/common/branches/branch-2@1350012. 043 */ 044@Category(SmallTests.class) 045public class TestJarFinder { 046 047 @ClassRule 048 public static final HBaseClassTestRule CLASS_RULE = 049 HBaseClassTestRule.forClass(TestJarFinder.class); 050 051 @Test 052 public void testJar() throws Exception { 053 054 // picking a class that is for sure in a JAR in the classpath 055 String jar = JarFinder.getJar(LoggerFactory.class); 056 Assert.assertTrue(new File(jar).exists()); 057 } 058 059 private static void delete(File file) throws IOException { 060 if (file.getAbsolutePath().length() < 5) { 061 throw new IllegalArgumentException( 062 MessageFormat.format("Path [{0}] is too short, not deleting", file.getAbsolutePath())); 063 } 064 if (file.exists()) { 065 if (file.isDirectory()) { 066 File[] children = file.listFiles(); 067 if (children != null) { 068 for (File child : children) { 069 delete(child); 070 } 071 } 072 } 073 if (!file.delete()) { 074 throw new RuntimeException( 075 MessageFormat.format("Could not delete path [{0}]", file.getAbsolutePath())); 076 } 077 } 078 } 079 080 @Test 081 public void testExpandedClasspath() throws Exception { 082 // picking a class that is for sure in a directory in the classpath 083 // in this case the JAR is created on the fly 084 String jar = JarFinder.getJar(TestJarFinder.class); 085 Assert.assertTrue(new File(jar).exists()); 086 } 087 088 @Test 089 public void testExistingManifest() throws Exception { 090 File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), 091 TestJarFinder.class.getName() + "-testExistingManifest"); 092 delete(dir); 093 dir.mkdirs(); 094 095 File metaInfDir = new File(dir, "META-INF"); 096 metaInfDir.mkdirs(); 097 File manifestFile = new File(metaInfDir, "MANIFEST.MF"); 098 Manifest manifest = new Manifest(); 099 OutputStream os = new FileOutputStream(manifestFile); 100 manifest.write(os); 101 os.close(); 102 103 File propsFile = new File(dir, "props.properties"); 104 Writer writer = new FileWriter(propsFile); 105 new Properties().store(writer, ""); 106 writer.close(); 107 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 108 JarOutputStream zos = new JarOutputStream(baos); 109 JarFinder.jarDir(dir, "", zos); 110 JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); 111 Assert.assertNotNull(jis.getManifest()); 112 jis.close(); 113 } 114 115 @Test 116 public void testNoManifest() throws Exception { 117 File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), 118 TestJarFinder.class.getName() + "-testNoManifest"); 119 delete(dir); 120 dir.mkdirs(); 121 File propsFile = new File(dir, "props.properties"); 122 Writer writer = new FileWriter(propsFile); 123 new Properties().store(writer, ""); 124 writer.close(); 125 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 126 JarOutputStream zos = new JarOutputStream(baos); 127 JarFinder.jarDir(dir, "", zos); 128 JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); 129 Assert.assertNotNull(jis.getManifest()); 130 jis.close(); 131 } 132}