View Javadoc
1   package org.apache.maven.shared.dependency.analyzer;
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.OutputStream;
26  import java.util.jar.JarOutputStream;
27  import java.util.zip.ZipEntry;
28  
29  import org.codehaus.plexus.util.FileUtils;
30  import org.codehaus.plexus.util.IOUtil;
31  import org.jmock.MockObjectTestCase;
32  
33  /**
34   * 
35   * 
36   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
37   * @version $Id: AbstractFileTest.java 1400598 2012-10-21 08:50:43Z hboutemy $
38   */
39  public abstract class AbstractFileTest extends MockObjectTestCase
40  {
41      // protected methods ------------------------------------------------------
42  
43      protected File createJar() throws IOException
44      {
45          File file = File.createTempFile( "test", ".jar" );
46          file.deleteOnExit();
47  
48          return file;
49      }
50  
51      protected File createDir() throws IOException
52      {
53          File file = File.createTempFile( "test", null );
54          file.delete();
55  
56          if ( !file.mkdir() )
57              throw new IOException( "Cannot create temporary directory: " + file );
58  
59          return file;
60      }
61  
62      protected File createFile( File parent, String child, String data ) throws IOException
63      {
64          File file = new File( parent, child );
65  
66          OutputStream out = new FileOutputStream( file );
67          IOUtil.copy( data, out );
68          out.close();
69  
70          return file;
71      }
72  
73      protected File mkdirs( File parent, String child ) throws IOException
74      {
75          File dir = new File( parent, child );
76  
77          FileUtils.forceMkdir( dir );
78  
79          return dir;
80      }
81  
82      protected void writeEntry( JarOutputStream out, String path, String data ) throws IOException
83      {
84          out.putNextEntry( new ZipEntry( path ) );
85  
86          byte[] bytes = data.getBytes( "UTF-8" );
87  
88          out.write( bytes, 0, bytes.length );
89      }
90  }