Coverage Report - org.apache.maven.wagon.tck.http.util.TestUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
TestUtil
0%
0/41
0%
0/12
0
 
 1  
 package org.apache.maven.wagon.tck.http.util;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.codehaus.plexus.util.IOUtil;
 23  
 
 24  
 import java.io.File;
 25  
 import java.io.FileOutputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.OutputStream;
 29  
 import java.net.URISyntaxException;
 30  
 import java.net.URL;
 31  
 import java.util.Enumeration;
 32  
 import java.util.HashMap;
 33  
 import java.util.Map;
 34  
 import java.util.jar.JarEntry;
 35  
 import java.util.jar.JarFile;
 36  
 
 37  
 public final class TestUtil
 38  
 {
 39  
 
 40  0
     private static final Map<String, File> bases = new HashMap<String, File>();
 41  
 
 42  
     private TestUtil()
 43  0
     {
 44  0
     }
 45  
 
 46  
     public static File getResource( final String path )
 47  
         throws URISyntaxException, IOException
 48  
     {
 49  0
         URL resource = Thread.currentThread().getContextClassLoader().getResource( path );
 50  0
         if ( resource == null )
 51  
         {
 52  0
             throw new IllegalStateException( "Cannot find classpath resource: " + path );
 53  
         }
 54  
 
 55  0
         if ( resource.getProtocol().startsWith( "jar" ) )
 56  
         {
 57  
             // File f = new File( path );
 58  
             // f = File.createTempFile( f.getName() + ".", ".tmp" );
 59  
 
 60  0
             String url = resource.toExternalForm();
 61  0
             int startIdx = url.lastIndexOf( ':' ) + 1;
 62  0
             int endIdx = url.indexOf( "!" );
 63  0
             url = url.substring( startIdx, endIdx );
 64  
 
 65  0
             File base = bases.get( url );
 66  0
             if ( base == null )
 67  
             {
 68  0
                 File urlFile = new File( url );
 69  
 
 70  0
                 base = new File( "target/tck-resources/" + urlFile.getName() );
 71  0
                 base.getParentFile().mkdirs();
 72  
 
 73  0
                 System.out.println( "unpacking test resources in jar: " + url );
 74  0
                 JarFile jf = null;
 75  
                 try
 76  
                 {
 77  0
                     jf = new JarFile( urlFile );
 78  
 
 79  0
                     InputStream in = null;
 80  0
                     OutputStream out = null;
 81  
 
 82  0
                     for ( Enumeration<JarEntry> en = jf.entries(); en.hasMoreElements(); )
 83  
                     {
 84  0
                         JarEntry je = en.nextElement();
 85  0
                         File target = new File( base, je.getName() ).getAbsoluteFile();
 86  0
                         if ( je.isDirectory() )
 87  
                         {
 88  0
                             target.mkdirs();
 89  
                         }
 90  
                         else
 91  
                         {
 92  0
                             target.getParentFile().mkdirs();
 93  
 
 94  
                             try
 95  
                             {
 96  0
                                 in = jf.getInputStream( je );
 97  0
                                 out = new FileOutputStream( target );
 98  
 
 99  0
                                 IOUtil.copy( in, out );
 100  
                             }
 101  
                             finally
 102  
                             {
 103  0
                                 IOUtil.close( in );
 104  0
                                 IOUtil.close( out );
 105  0
                             }
 106  
                         }
 107  0
                     }
 108  
 
 109  0
                     bases.put( url, base );
 110  
                 }
 111  
                 finally
 112  
                 {
 113  0
                     if ( jf != null )
 114  
                     {
 115  
                         try
 116  
                         {
 117  0
                             jf.close();
 118  
                         }
 119  0
                         catch ( Exception e )
 120  
                         {
 121  0
                         }
 122  
                     }
 123  
                 }
 124  
             }
 125  
 
 126  0
             return new File( base, path );
 127  
         }
 128  
         else
 129  
         {
 130  0
             return new File( resource.toURI().normalize() );
 131  
         }
 132  
     }
 133  
 
 134  
 }