Coverage Report - org.apache.giraph.utils.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
81%
27/33
70%
7/10
1.8
FileUtils$1
N/A
N/A
1.8
FileUtils$DeletingVisitor
100%
5/5
100%
2/2
1.8
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 package org.apache.giraph.utils;
 20  
 
 21  
 import com.google.common.base.Charsets;
 22  
 import com.google.common.io.Closeables;
 23  
 import com.google.common.io.Files;
 24  
 import org.apache.hadoop.conf.Configuration;
 25  
 import org.apache.hadoop.fs.FileSystem;
 26  
 import org.apache.hadoop.fs.Path;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileFilter;
 30  
 import java.io.IOException;
 31  
 import java.io.Writer;
 32  
 
 33  
 /**
 34  
  * Helper class for filesystem operations during testing
 35  
  */
 36  
 public class FileUtils {
 37  
 
 38  
   /**
 39  
    * Utility class should not be instantiatable
 40  
    */
 41  0
   private FileUtils() {
 42  0
   }
 43  
 
 44  
   /**
 45  
    * Create a temporary folder that will be removed after the test.
 46  
    *
 47  
    * @param vertexClass Used for generating the folder name.
 48  
    * @return File object for the directory.
 49  
    */
 50  
   public static File createTestDir(Class<?> vertexClass)
 51  
     throws IOException {
 52  3
     String systemTmpDir = System.getProperty("java.io.tmpdir");
 53  3
     long simpleRandomLong = (long) (Long.MAX_VALUE * Math.random());
 54  3
     File testTempDir = new File(systemTmpDir, "giraph-" +
 55  
         vertexClass.getSimpleName() + '-' + simpleRandomLong);
 56  3
     if (!testTempDir.mkdir()) {
 57  0
       throw new IOException("Could not create " + testTempDir);
 58  
     }
 59  3
     testTempDir.deleteOnExit();
 60  3
     return testTempDir;
 61  
   }
 62  
 
 63  
   /**
 64  
    * Make a temporary file.
 65  
    *
 66  
    * @param parent Parent directory.
 67  
    * @param name File name.
 68  
    * @return File object to temporary file.
 69  
    * @throws IOException
 70  
    */
 71  
   public static File createTempFile(File parent, String name)
 72  
     throws IOException {
 73  3
     return createTestTempFileOrDir(parent, name, false);
 74  
   }
 75  
 
 76  
   /**
 77  
    * Make a temporary directory.
 78  
    *
 79  
    * @param parent Parent directory.
 80  
    * @param name Directory name.
 81  
    * @return File object to temporary file.
 82  
    * @throws IOException
 83  
    */
 84  
   public static File createTempDir(File parent, String name)
 85  
     throws IOException {
 86  12
     File dir = createTestTempFileOrDir(parent, name, true);
 87  12
     dir.delete();
 88  12
     return dir;
 89  
   }
 90  
 
 91  
   /**
 92  
    * Create a test temp file or directory.
 93  
    *
 94  
    * @param parent Parent directory
 95  
    * @param name Name of file
 96  
    * @param dir Is directory?
 97  
    * @return File object
 98  
    * @throws IOException
 99  
    */
 100  
   public static File createTestTempFileOrDir(File parent, String name,
 101  
     boolean dir) throws IOException {
 102  15
     File f = new File(parent, name);
 103  15
     f.deleteOnExit();
 104  15
     if (dir && !f.mkdirs()) {
 105  0
       throw new IOException("Could not make directory " + f);
 106  
     }
 107  15
     return f;
 108  
   }
 109  
 
 110  
   /**
 111  
    * Write lines to a file.
 112  
    *
 113  
    * @param file File to write lines to
 114  
    * @param lines Strings written to the file
 115  
    * @throws IOException
 116  
    */
 117  
   public static void writeLines(File file, String... lines)
 118  
     throws IOException {
 119  3
     Writer writer = Files.newWriter(file, Charsets.UTF_8);
 120  
     try {
 121  33
       for (String line : lines) {
 122  30
         writer.write(line);
 123  30
         writer.write('\n');
 124  
       }
 125  
     } finally {
 126  3
       Closeables.closeQuietly(writer);
 127  3
     }
 128  3
   }
 129  
 
 130  
   /**
 131  
    * Recursively delete a directory
 132  
    *
 133  
    * @param dir Directory to delete
 134  
    */
 135  
   public static void delete(File dir) {
 136  3
     if (dir != null) {
 137  3
       new DeletingVisitor().accept(dir);
 138  
     }
 139  3
   }
 140  
 
 141  
   /**
 142  
    * Deletes files.
 143  
    */
 144  6
   private static class DeletingVisitor implements FileFilter {
 145  
     @Override
 146  
     public boolean accept(File f) {
 147  33
       if (!f.isFile()) {
 148  12
         f.listFiles(this);
 149  
       }
 150  33
       f.delete();
 151  33
       return false;
 152  
     }
 153  
   }
 154  
 
 155  
   /**
 156  
    * Helper method to remove a path if it exists.
 157  
    *
 158  
    * @param conf Configuration to load FileSystem from
 159  
    * @param path Path to remove
 160  
    * @throws IOException
 161  
    */
 162  
   public static void deletePath(Configuration conf, String path)
 163  
     throws IOException {
 164  0
     deletePath(conf, new Path(path));
 165  0
   }
 166  
 
 167  
   /**
 168  
    * Helper method to remove a path if it exists.
 169  
    *
 170  
    * @param conf Configuration to load FileSystem from
 171  
    * @param path Path to remove
 172  
    * @throws IOException
 173  
    */
 174  
   public static void deletePath(Configuration conf, Path path)
 175  
     throws IOException {
 176  120
     FileSystem fs = FileSystem.get(conf);
 177  120
     fs.delete(path, true);
 178  120
   }
 179  
 }