Coverage Report - org.apache.maven.wagon.tck.http.util.TestUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
TestUtil
0 %
0/42
0 %
0/12
5,5
 
 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 java.io.File;
 23  
 import java.io.FileOutputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.io.OutputStream;
 27  
 import java.net.URISyntaxException;
 28  
 import java.net.URL;
 29  
 import java.util.Enumeration;
 30  
 import java.util.HashMap;
 31  
 import java.util.Map;
 32  
 import java.util.jar.JarEntry;
 33  
 import java.util.jar.JarFile;
 34  
 
 35  
 import org.apache.log4j.Logger;
 36  
 import org.codehaus.plexus.util.IOUtil;
 37  
 
 38  
 public final class TestUtil
 39  
 {
 40  0
     private static Logger logger = Logger.getLogger( TestUtil.class );
 41  
 
 42  0
     private static final Map<String, File> bases = new HashMap<String, File>();
 43  
 
 44  
     private TestUtil()
 45  0
     {
 46  0
     }
 47  
 
 48  
     public static File getResource( final String path )
 49  
         throws URISyntaxException, IOException
 50  
     {
 51  0
         URL resource = Thread.currentThread().getContextClassLoader().getResource( path );
 52  0
         if ( resource == null )
 53  
         {
 54  0
             throw new IllegalStateException( "Cannot find classpath resource: " + path );
 55  
         }
 56  
 
 57  0
         if ( resource.getProtocol().startsWith( "jar" ) )
 58  
         {
 59  
             // File f = new File( path );
 60  
             // f = File.createTempFile( f.getName() + ".", ".tmp" );
 61  
 
 62  0
             String url = resource.toExternalForm();
 63  0
             int startIdx = url.lastIndexOf( ':' ) + 1;
 64  0
             int endIdx = url.indexOf( "!" );
 65  0
             url = url.substring( startIdx, endIdx );
 66  
 
 67  0
             File base = bases.get( url );
 68  0
             if ( base == null )
 69  
             {
 70  0
                 File urlFile = new File( url );
 71  
 
 72  0
                 base = new File( "target/tck-resources/" + urlFile.getName() );
 73  0
                 base.getParentFile().mkdirs();
 74  
 
 75  0
                 logger.info( "unpacking test resources in jar: " + url );
 76  0
                 JarFile jf = null;
 77  
                 try
 78  
                 {
 79  0
                     jf = new JarFile( urlFile );
 80  
 
 81  0
                     InputStream in = null;
 82  0
                     OutputStream out = null;
 83  
 
 84  0
                     for ( Enumeration<JarEntry> en = jf.entries(); en.hasMoreElements(); )
 85  
                     {
 86  0
                         JarEntry je = en.nextElement();
 87  0
                         File target = new File( base, je.getName() ).getAbsoluteFile();
 88  0
                         if ( je.isDirectory() )
 89  
                         {
 90  0
                             target.mkdirs();
 91  
                         }
 92  
                         else
 93  
                         {
 94  0
                             target.getParentFile().mkdirs();
 95  
 
 96  
                             try
 97  
                             {
 98  0
                                 in = jf.getInputStream( je );
 99  0
                                 out = new FileOutputStream( target );
 100  
 
 101  0
                                 IOUtil.copy( in, out );
 102  
                             }
 103  
                             finally
 104  
                             {
 105  0
                                 IOUtil.close( in );
 106  0
                                 IOUtil.close( out );
 107  0
                             }
 108  
                         }
 109  0
                     }
 110  
 
 111  0
                     bases.put( url, base );
 112  
                 }
 113  
                 finally
 114  
                 {
 115  0
                     if ( jf != null )
 116  
                     {
 117  
                         try
 118  
                         {
 119  0
                             jf.close();
 120  
                         }
 121  0
                         catch ( Exception e )
 122  
                         {
 123  0
                         }
 124  
                     }
 125  
                 }
 126  
             }
 127  
 
 128  0
             return new File( base, path );
 129  
         }
 130  
         else
 131  
         {
 132  0
             return new File( resource.toURI().normalize() );
 133  
         }
 134  
     }
 135  
 
 136  
 }