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 static org.junit.jupiter.api.Assertions.assertNotNull; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.ByteArrayInputStream; 024import java.io.ByteArrayOutputStream; 025import java.io.File; 026import java.io.FileOutputStream; 027import java.io.FileWriter; 028import java.io.IOException; 029import java.io.OutputStream; 030import java.io.Writer; 031import java.text.MessageFormat; 032import java.util.Properties; 033import java.util.jar.JarInputStream; 034import java.util.jar.JarOutputStream; 035import java.util.jar.Manifest; 036import org.apache.hadoop.hbase.testclassification.SmallTests; 037import org.junit.jupiter.api.Tag; 038import org.junit.jupiter.api.Test; 039import org.slf4j.LoggerFactory; 040 041/** 042 * This file was forked from hadoop/common/branches/branch-2@1350012. 043 */ 044@Tag(SmallTests.TAG) 045public class TestJarFinder { 046 047 @Test 048 public void testJar() throws Exception { 049 050 // picking a class that is for sure in a JAR in the classpath 051 String jar = JarFinder.getJar(LoggerFactory.class); 052 assertTrue(new File(jar).exists()); 053 } 054 055 private static void delete(File file) throws IOException { 056 if (file.getAbsolutePath().length() < 5) { 057 throw new IllegalArgumentException( 058 MessageFormat.format("Path [{0}] is too short, not deleting", file.getAbsolutePath())); 059 } 060 if (file.exists()) { 061 if (file.isDirectory()) { 062 File[] children = file.listFiles(); 063 if (children != null) { 064 for (File child : children) { 065 delete(child); 066 } 067 } 068 } 069 if (!file.delete()) { 070 throw new RuntimeException( 071 MessageFormat.format("Could not delete path [{0}]", file.getAbsolutePath())); 072 } 073 } 074 } 075 076 @Test 077 public void testExpandedClasspath() throws Exception { 078 // picking a class that is for sure in a directory in the classpath 079 // in this case the JAR is created on the fly 080 String jar = JarFinder.getJar(TestJarFinder.class); 081 assertTrue(new File(jar).exists()); 082 } 083 084 @Test 085 public void testExistingManifest() throws Exception { 086 File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), 087 TestJarFinder.class.getName() + "-testExistingManifest"); 088 delete(dir); 089 dir.mkdirs(); 090 091 File metaInfDir = new File(dir, "META-INF"); 092 metaInfDir.mkdirs(); 093 File manifestFile = new File(metaInfDir, "MANIFEST.MF"); 094 Manifest manifest = new Manifest(); 095 OutputStream os = new FileOutputStream(manifestFile); 096 manifest.write(os); 097 os.close(); 098 099 File propsFile = new File(dir, "props.properties"); 100 Writer writer = new FileWriter(propsFile); 101 new Properties().store(writer, ""); 102 writer.close(); 103 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 104 JarOutputStream zos = new JarOutputStream(baos); 105 JarFinder.jarDir(dir, "", zos); 106 JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); 107 assertNotNull(jis.getManifest()); 108 jis.close(); 109 } 110 111 @Test 112 public void testNoManifest() throws Exception { 113 File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), 114 TestJarFinder.class.getName() + "-testNoManifest"); 115 delete(dir); 116 dir.mkdirs(); 117 File propsFile = new File(dir, "props.properties"); 118 Writer writer = new FileWriter(propsFile); 119 new Properties().store(writer, ""); 120 writer.close(); 121 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 122 JarOutputStream zos = new JarOutputStream(baos); 123 JarFinder.jarDir(dir, "", zos); 124 JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); 125 assertNotNull(jis.getManifest()); 126 jis.close(); 127 } 128}