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.codehaus.plexus.util.IOUtil; 036 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040/** 041 * 042 */ 043public final class TestUtil 044{ 045 // CHECKSTYLE_OFF: ConstantName 046 private static final Logger logger = LoggerFactory.getLogger( TestUtil.class ); 047 // CHECKSTYLE_ON: ConstantName 048 049 private static final Map<String, File> BASES = new HashMap<String, File>(); 050 051 private TestUtil() 052 { 053 } 054 055 public static File getResource( final String path ) 056 throws URISyntaxException, IOException 057 { 058 URL resource = Thread.currentThread().getContextClassLoader().getResource( path ); 059 if ( resource == null ) 060 { 061 throw new IllegalStateException( "Cannot find classpath resource: " + path ); 062 } 063 064 if ( resource.getProtocol().startsWith( "jar" ) ) 065 { 066 // File f = new File( path ); 067 // f = File.createTempFile( f.getName() + ".", ".tmp" ); 068 069 String url = resource.toExternalForm(); 070 int startIdx = url.lastIndexOf( ':' ) + 1; 071 int endIdx = url.indexOf( "!" ); 072 url = url.substring( startIdx, endIdx ); 073 074 File base = BASES.get( url ); 075 if ( base == null ) 076 { 077 File urlFile = new File( url ); 078 079 base = new File( "target/tck-resources/" + urlFile.getName() ); 080 base.getParentFile().mkdirs(); 081 082 logger.info( "unpacking test resources in jar: " + url ); 083 JarFile jf = null; 084 try 085 { 086 jf = new JarFile( urlFile ); 087 088 InputStream in = null; 089 OutputStream out = null; 090 091 for ( Enumeration<JarEntry> en = jf.entries(); en.hasMoreElements(); ) 092 { 093 JarEntry je = en.nextElement(); 094 File target = new File( base, je.getName() ).getAbsoluteFile(); 095 if ( je.isDirectory() ) 096 { 097 target.mkdirs(); 098 } 099 else 100 { 101 target.getParentFile().mkdirs(); 102 103 try 104 { 105 in = jf.getInputStream( je ); 106 out = new FileOutputStream( target ); 107 108 IOUtil.copy( in, out ); 109 110 out.close(); 111 out = null; 112 113 in.close(); 114 in = null; 115 } 116 finally 117 { 118 IOUtil.close( in ); 119 IOUtil.close( out ); 120 } 121 } 122 } 123 124 BASES.put( url, base ); 125 } 126 finally 127 { 128 if ( jf != null ) 129 { 130 try 131 { 132 jf.close(); 133 } 134 catch ( Exception e ) 135 { 136 // ignore 137 } 138 } 139 } 140 } 141 142 return new File( base, path ); 143 } 144 else 145 { 146 return new File( resource.toURI().normalize() ); 147 } 148 } 149 150}