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