001package org.apache.maven.wagon.tck.http.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.FileOutputStream; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.net.URISyntaxException; 028import java.net.URL; 029import java.util.Enumeration; 030import java.util.HashMap; 031import java.util.Map; 032import java.util.jar.JarEntry; 033import java.util.jar.JarFile; 034 035import org.apache.log4j.Logger; 036import org.codehaus.plexus.util.IOUtil; 037 038/** 039 * 040 */ 041public final class TestUtil 042{ 043 private static final Logger LOGGER = Logger.getLogger( TestUtil.class ); 044 045 private static final Map<String, File> BASES = new HashMap<String, File>(); 046 047 private TestUtil() 048 { 049 } 050 051 public static File getResource( final String path ) 052 throws URISyntaxException, IOException 053 { 054 URL resource = Thread.currentThread().getContextClassLoader().getResource( path ); 055 if ( resource == null ) 056 { 057 throw new IllegalStateException( "Cannot find classpath resource: " + path ); 058 } 059 060 if ( resource.getProtocol().startsWith( "jar" ) ) 061 { 062 // File f = new File( path ); 063 // f = File.createTempFile( f.getName() + ".", ".tmp" ); 064 065 String url = resource.toExternalForm(); 066 int startIdx = url.lastIndexOf( ':' ) + 1; 067 int endIdx = url.indexOf( "!" ); 068 url = url.substring( startIdx, endIdx ); 069 070 File base = BASES.get( url ); 071 if ( base == null ) 072 { 073 File urlFile = new File( url ); 074 075 base = new File( "target/tck-resources/" + urlFile.getName() ); 076 base.getParentFile().mkdirs(); 077 078 LOGGER.info( "unpacking test resources in jar: " + url ); 079 JarFile jf = null; 080 try 081 { 082 jf = new JarFile( urlFile ); 083 084 InputStream in = null; 085 OutputStream out = null; 086 087 for ( Enumeration<JarEntry> en = jf.entries(); en.hasMoreElements(); ) 088 { 089 JarEntry je = en.nextElement(); 090 File target = new File( base, je.getName() ).getAbsoluteFile(); 091 if ( je.isDirectory() ) 092 { 093 target.mkdirs(); 094 } 095 else 096 { 097 target.getParentFile().mkdirs(); 098 099 try 100 { 101 in = jf.getInputStream( je ); 102 out = new FileOutputStream( target ); 103 104 IOUtil.copy( in, out ); 105 } 106 finally 107 { 108 IOUtil.close( in ); 109 IOUtil.close( out ); 110 } 111 } 112 } 113 114 BASES.put( url, base ); 115 } 116 finally 117 { 118 if ( jf != null ) 119 { 120 try 121 { 122 jf.close(); 123 } 124 catch ( Exception e ) 125 { 126 // ignore 127 } 128 } 129 } 130 } 131 132 return new File( base, path ); 133 } 134 else 135 { 136 return new File( resource.toURI().normalize() ); 137 } 138 } 139 140}